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.jbi.interceptor;
019:
020: import java.util.List;
021: import java.util.ResourceBundle;
022: import java.util.logging.Logger;
023:
024: import javax.xml.stream.XMLStreamException;
025: import javax.xml.stream.XMLStreamWriter;
026:
027: import org.apache.cxf.binding.jbi.JBIConstants;
028: import org.apache.cxf.common.logging.LogUtils;
029: import org.apache.cxf.databinding.DataWriter;
030: import org.apache.cxf.interceptor.AbstractOutDatabindingInterceptor;
031: import org.apache.cxf.interceptor.Fault;
032: import org.apache.cxf.message.Message;
033: import org.apache.cxf.phase.Phase;
034: import org.apache.cxf.service.Service;
035: import org.apache.cxf.service.model.BindingOperationInfo;
036: import org.apache.cxf.service.model.MessagePartInfo;
037:
038: public class JBIWrapperOutInterceptor extends
039: AbstractOutDatabindingInterceptor {
040:
041: private static final Logger LOG = LogUtils
042: .getL7dLogger(JBIWrapperOutInterceptor.class);
043:
044: private static final ResourceBundle BUNDLE = LOG
045: .getResourceBundle();
046:
047: public JBIWrapperOutInterceptor() {
048: super (Phase.MARSHAL);
049: }
050:
051: public void handleMessage(Message message) throws Fault {
052: BindingOperationInfo bop = message.getExchange().get(
053: BindingOperationInfo.class);
054: XMLStreamWriter xmlWriter = getXMLStreamWriter(message);
055: Service service = message.getExchange().get(Service.class);
056:
057: DataWriter<XMLStreamWriter> dataWriter = getDataWriter(message,
058: service, XMLStreamWriter.class);
059:
060: try {
061: xmlWriter.setPrefix("jbi", JBIConstants.NS_JBI_WRAPPER);
062: xmlWriter.writeStartElement(JBIConstants.NS_JBI_WRAPPER,
063: JBIConstants.JBI_WRAPPER_MESSAGE.getLocalPart());
064: xmlWriter
065: .writeNamespace("jbi", JBIConstants.NS_JBI_WRAPPER);
066:
067: List<MessagePartInfo> parts = null;
068: if (!isRequestor(message)) {
069: parts = bop.getOutput().getMessageParts();
070: } else {
071: parts = bop.getInput().getMessageParts();
072: }
073: List<?> objs = (List<?>) message.getContent(List.class);
074: if (objs.size() < parts.size()) {
075: throw new Fault(new org.apache.cxf.common.i18n.Message(
076: "NOT_EQUAL_ARG_NUM", BUNDLE));
077: }
078: for (int idx = 0; idx < parts.size(); idx++) {
079: MessagePartInfo part = parts.get(idx);
080: Object obj = objs.get(idx);
081: if (!part.isElement()) {
082: if (part.getTypeClass() == String.class) {
083: xmlWriter.writeStartElement(
084: JBIConstants.NS_JBI_WRAPPER,
085: JBIConstants.JBI_WRAPPER_PART
086: .getLocalPart());
087: xmlWriter.writeCharacters(obj.toString());
088: xmlWriter.writeEndElement();
089: } else {
090: part = new MessagePartInfo(part.getName(), part
091: .getMessageInfo());
092: part.setElement(false);
093: part
094: .setConcreteName(JBIConstants.JBI_WRAPPER_PART);
095: dataWriter.write(obj, part, xmlWriter);
096: }
097: } else {
098: xmlWriter.writeStartElement(
099: JBIConstants.NS_JBI_WRAPPER,
100: JBIConstants.JBI_WRAPPER_PART
101: .getLocalPart());
102: dataWriter.write(obj, part, xmlWriter);
103: xmlWriter.writeEndElement();
104: }
105: }
106: xmlWriter.writeEndElement();
107:
108: } catch (XMLStreamException e) {
109: throw new Fault(new org.apache.cxf.common.i18n.Message(
110: "STAX_WRITE_EXC", BUNDLE), e);
111: }
112: }
113:
114: }
|