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.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.common.i18n.BundleUtils;
027: import org.apache.cxf.message.Message;
028: import org.apache.cxf.phase.Phase;
029: import org.apache.cxf.service.model.BindingOperationInfo;
030: import org.apache.cxf.service.model.MessageInfo;
031: import org.apache.cxf.service.model.MessagePartInfo;
032:
033: public class WrappedOutInterceptor extends
034: AbstractOutDatabindingInterceptor {
035: private static final ResourceBundle BUNDLE = BundleUtils
036: .getBundle(WrappedOutInterceptor.class);
037:
038: private WrappedOutEndingInterceptor ending = new WrappedOutEndingInterceptor();
039:
040: public WrappedOutInterceptor() {
041: this (Phase.MARSHAL);
042: }
043:
044: public WrappedOutInterceptor(String phase) {
045: super (phase);
046: addBefore(BareOutInterceptor.class.getName());
047: }
048:
049: public void handleMessage(Message message) {
050: BindingOperationInfo bop = message.getExchange().get(
051: BindingOperationInfo.class);
052:
053: if (bop != null && bop.isUnwrapped()) {
054: XMLStreamWriter xmlWriter = message
055: .getContent(XMLStreamWriter.class);
056:
057: MessageInfo messageInfo;
058: if (isRequestor(message)) {
059: messageInfo = bop.getWrappedOperation()
060: .getOperationInfo().getInput();
061: } else {
062: messageInfo = bop.getWrappedOperation()
063: .getOperationInfo().getOutput();
064: }
065:
066: MessagePartInfo part = messageInfo.getMessageParts().get(0);
067: QName name = part.getConcreteName();
068:
069: try {
070:
071: int x = 1;
072: while (xmlWriter.getNamespaceContext().getNamespaceURI(
073: "ns" + x) != null) {
074: x++;
075: }
076: xmlWriter.setPrefix("ns" + x, name.getNamespaceURI());
077: xmlWriter.writeStartElement("ns" + x, name
078: .getLocalPart(), name.getNamespaceURI());
079: xmlWriter.writeNamespace("ns" + x, name
080: .getNamespaceURI());
081: } catch (XMLStreamException e) {
082: throw new Fault(new org.apache.cxf.common.i18n.Message(
083: "STAX_WRITE_EXC", BUNDLE), e);
084: }
085:
086: // Add a final interceptor to write end element
087: message.getInterceptorChain().add(ending);
088: }
089: }
090:
091: public class WrappedOutEndingInterceptor extends
092: AbstractOutDatabindingInterceptor {
093: public WrappedOutEndingInterceptor() {
094: super (Phase.MARSHAL_ENDING);
095: }
096:
097: public void handleMessage(Message message) throws Fault {
098: try {
099: XMLStreamWriter xtw = message
100: .getContent(XMLStreamWriter.class);
101: if (xtw != null) {
102: xtw.writeEndElement();
103: }
104: } catch (XMLStreamException e) {
105: throw new Fault(new org.apache.cxf.common.i18n.Message(
106: "STAX_WRITE_EXC", BUNDLE), e);
107: }
108: }
109:
110: }
111: }
|