001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.binding.soap.interceptor;
019:
020: import java.util.List;
021: import java.util.logging.Logger;
022:
023: import javax.xml.stream.XMLStreamException;
024: import javax.xml.stream.XMLStreamWriter;
025:
026: import org.apache.cxf.common.logging.LogUtils;
027: import org.apache.cxf.helpers.NSStack;
028: import org.apache.cxf.interceptor.AbstractOutDatabindingInterceptor;
029: import org.apache.cxf.interceptor.Fault;
030: import org.apache.cxf.message.Message;
031: import org.apache.cxf.message.MessageContentsList;
032: import org.apache.cxf.phase.Phase;
033: import org.apache.cxf.service.model.BindingOperationInfo;
034: import org.apache.cxf.service.model.MessagePartInfo;
035: import org.apache.cxf.staxutils.StaxUtils;
036:
037: public class RPCOutInterceptor extends
038: AbstractOutDatabindingInterceptor {
039: private static final Logger LOG = LogUtils
040: .getL7dLogger(RPCOutInterceptor.class);
041:
042: public RPCOutInterceptor() {
043: super (Phase.MARSHAL);
044: }
045:
046: public void handleMessage(Message message) {
047: try {
048: NSStack nsStack = new NSStack();
049: nsStack.push();
050:
051: BindingOperationInfo operation = (BindingOperationInfo) message
052: .getExchange().get(
053: BindingOperationInfo.class.getName());
054:
055: assert operation.getName() != null;
056:
057: XMLStreamWriter xmlWriter = getXMLStreamWriter(message);
058:
059: addOperationNode(nsStack, message, xmlWriter);
060:
061: List<MessagePartInfo> parts = null;
062:
063: if (!isRequestor(message)) {
064: parts = operation.getOutput().getMessageParts();
065: } else {
066: parts = operation.getInput().getMessageParts();
067: }
068:
069: MessageContentsList objs = MessageContentsList
070: .getContentsList(message);
071: if (objs == null) {
072: return;
073: }
074:
075: for (MessagePartInfo part : parts) {
076: if (objs.hasValue(part)) {
077: Object o = objs.get(part);
078: if (o == null) {
079: //WSI-BP R2211 - RPC/Lit parts are not allowed to be xsi:nil
080: throw new Fault(
081: new org.apache.cxf.common.i18n.Message(
082: "BP_2211_RPCLIT_CANNOT_BE_NULL",
083: LOG, part.getConcreteName()));
084: }
085: }
086: }
087: writeParts(message, message.getExchange(), operation, objs,
088: parts);
089:
090: // Finishing the writing.
091: xmlWriter.writeEndElement();
092: } catch (XMLStreamException e) {
093: throw new Fault(e);
094: }
095: }
096:
097: protected String addOperationNode(NSStack nsStack, Message message,
098: XMLStreamWriter xmlWriter) throws XMLStreamException {
099: String responseSuffix = !isRequestor(message) ? "Response" : "";
100: BindingOperationInfo boi = message.getExchange().get(
101: BindingOperationInfo.class);
102: String ns = boi.getName().getNamespaceURI();
103: nsStack.add(ns);
104: String prefix = nsStack.getPrefix(ns);
105: StaxUtils.writeStartElement(xmlWriter, prefix, boi.getName()
106: .getLocalPart()
107: + responseSuffix, ns);
108: return ns;
109: }
110:
111: protected XMLStreamWriter getXMLStreamWriter(Message message) {
112: return message.getContent(XMLStreamWriter.class);
113: }
114:
115: }
|