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.transport.jbi;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.IOException;
023: import java.util.logging.Level;
024: import java.util.logging.Logger;
025:
026: import javax.jbi.messaging.DeliveryChannel;
027: import javax.jbi.messaging.Fault;
028: import javax.jbi.messaging.MessageExchange;
029: import javax.jbi.messaging.NormalizedMessage;
030: import javax.xml.parsers.DocumentBuilder;
031: import javax.xml.parsers.DocumentBuilderFactory;
032: import javax.xml.transform.dom.DOMSource;
033:
034: import org.w3c.dom.Document;
035:
036: import org.apache.cxf.common.logging.LogUtils;
037: import org.apache.cxf.io.CachedOutputStream;
038: import org.apache.cxf.message.Message;
039:
040: public class JBIDestinationOutputStream extends CachedOutputStream {
041:
042: private static final Logger LOG = LogUtils
043: .getL7dLogger(JBIDestinationOutputStream.class);
044: private Message inMessage;
045: private DeliveryChannel channel;
046:
047: public JBIDestinationOutputStream(Message m, DeliveryChannel dc) {
048: super ();
049: inMessage = m;
050: channel = dc;
051: }
052:
053: @Override
054: protected void doFlush() throws IOException {
055: // so far do nothing
056: }
057:
058: @Override
059: protected void doClose() throws IOException {
060: commitOutputMessage();
061: }
062:
063: @Override
064: protected void onWrite() throws IOException {
065: // so far do nothing
066: }
067:
068: private void commitOutputMessage() throws IOException {
069: try {
070: if (inMessage.getExchange().isOneWay()) {
071: return;
072: } else {
073:
074: ByteArrayOutputStream baos = (ByteArrayOutputStream) getOut();
075: ByteArrayInputStream bais = new ByteArrayInputStream(
076: baos.toByteArray());
077: LOG.finest(new org.apache.cxf.common.i18n.Message(
078: "BUILDING.DOCUMENT", LOG).toString());
079: DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
080: .newInstance();
081: docBuilderFactory.setNamespaceAware(true);
082: DocumentBuilder builder = docBuilderFactory
083: .newDocumentBuilder();
084: Document doc = builder.parse(bais);
085:
086: MessageExchange xchng = inMessage
087: .get(MessageExchange.class);
088: LOG.fine(new org.apache.cxf.common.i18n.Message(
089: "CREATE.NORMALIZED.MESSAGE", LOG).toString());
090: if (inMessage.getExchange().getOutFaultMessage() != null) {
091: org.apache.cxf.interceptor.Fault f = (org.apache.cxf.interceptor.Fault) inMessage
092: .getContent(Exception.class);
093: if (f.hasDetails()) {
094: Fault fault = xchng.createFault();
095: fault.setContent(new DOMSource(doc));
096: xchng.setFault(fault);
097: } else {
098: xchng.setError(f);
099: }
100: } else {
101: NormalizedMessage msg = xchng.createMessage();
102: msg.setContent(new DOMSource(doc));
103: xchng.setMessage(msg, "out");
104:
105: }
106: LOG.fine(new org.apache.cxf.common.i18n.Message(
107: "POST.DISPATCH", LOG).toString());
108: channel.send(xchng);
109: }
110: } catch (Exception ex) {
111: LOG.log(Level.SEVERE,
112: new org.apache.cxf.common.i18n.Message(
113: "ERROR.SEND.MESSAGE", LOG).toString(), ex);
114: }
115: }
116:
117: }
|