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.xml.interceptor;
019:
020: import java.util.ResourceBundle;
021:
022: import javax.xml.namespace.QName;
023: import javax.xml.stream.XMLStreamException;
024: import javax.xml.stream.XMLStreamWriter;
025:
026: import org.apache.cxf.bindings.xformat.XMLBindingMessageFormat;
027: import org.apache.cxf.common.i18n.BundleUtils;
028: import org.apache.cxf.interceptor.AbstractOutDatabindingInterceptor;
029: import org.apache.cxf.interceptor.BareOutInterceptor;
030: import org.apache.cxf.interceptor.Fault;
031: import org.apache.cxf.interceptor.WrappedOutInterceptor;
032: import org.apache.cxf.message.Message;
033: import org.apache.cxf.phase.Phase;
034: import org.apache.cxf.service.model.BindingMessageInfo;
035: import org.apache.cxf.service.model.BindingOperationInfo;
036: import org.apache.cxf.service.model.MessageInfo;
037: import org.apache.cxf.staxutils.StaxUtils;
038:
039: public class XMLMessageOutInterceptor extends
040: AbstractOutDatabindingInterceptor {
041:
042: private static final ResourceBundle BUNDLE = BundleUtils
043: .getBundle(XMLMessageOutInterceptor.class);
044:
045: public XMLMessageOutInterceptor() {
046: this (Phase.MARSHAL);
047: }
048:
049: public XMLMessageOutInterceptor(String phase) {
050: super (phase);
051: addAfter(WrappedOutInterceptor.class.getName());
052: }
053:
054: public void handleMessage(Message message) throws Fault {
055: BindingOperationInfo boi = message.getExchange().get(
056: BindingOperationInfo.class);
057: MessageInfo mi;
058: BindingMessageInfo bmi;
059: if (isRequestor(message)) {
060: mi = boi.getOperationInfo().getInput();
061: bmi = boi.getInput();
062: } else {
063: mi = boi.getOperationInfo().getOutput();
064: bmi = boi.getOutput();
065: }
066: XMLBindingMessageFormat xmf = bmi
067: .getExtensor(XMLBindingMessageFormat.class);
068: QName rootInModel = null;
069: if (xmf != null) {
070: rootInModel = xmf.getRootNode();
071: }
072: if (mi.getMessageParts().size() == 1) {
073: // bare-one-param & wrap
074: new BareOutInterceptor().handleMessage(message);
075: } else {
076: if (rootInModel == null) {
077: rootInModel = boi.getName();
078: }
079: if (mi.getMessageParts().size() == 0 && !boi.isUnwrapped()) {
080: // write empty operation qname
081: writeMessage(message, rootInModel, false);
082: } else {
083: // multi param, bare mode, needs write root node
084: writeMessage(message, rootInModel, true);
085: }
086: }
087: // in the end we do flush ;)
088: XMLStreamWriter writer = message
089: .getContent(XMLStreamWriter.class);
090: try {
091: writer.flush();
092: } catch (XMLStreamException e) {
093: throw new Fault(new org.apache.cxf.common.i18n.Message(
094: "STAX_WRITE_EXC", BUNDLE, e));
095: }
096: }
097:
098: private void writeMessage(Message message, QName name,
099: boolean executeBare) {
100: XMLStreamWriter xmlWriter = message
101: .getContent(XMLStreamWriter.class);
102: try {
103: StaxUtils.writeStartElement(xmlWriter, name.getPrefix(),
104: name.getLocalPart(), name.getNamespaceURI());
105: if (executeBare) {
106: new BareOutInterceptor().handleMessage(message);
107: }
108: xmlWriter.writeEndElement();
109: } catch (XMLStreamException e) {
110: throw new Fault(new org.apache.cxf.common.i18n.Message(
111: "STAX_WRITE_EXC", BUNDLE, e));
112: }
113:
114: }
115: }
|