1 16 package org.apache.axis2.transport; 17 18 import org.apache.axis2.Constants; 19 import org.apache.axis2.addressing.AddressingConstants; 20 import org.apache.axis2.addressing.EndpointReference; 21 import org.apache.axis2.context.ConfigurationContext; 22 import org.apache.axis2.context.MessageContext; 23 import org.apache.axis2.description.HandlerDescription; 24 import org.apache.axis2.description.TransportOutDescription; 25 import org.apache.axis2.engine.AxisFault; 26 import org.apache.axis2.handlers.AbstractHandler; 27 import org.apache.axis2.om.OMElement; 28 import org.apache.axis2.om.OMOutput; 29 import org.apache.axis2.soap.SOAPEnvelope; 30 import org.apache.axis2.transport.http.HTTPTransportUtils; 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 34 import javax.xml.namespace.QName ; 35 import java.io.OutputStream ; 36 37 43 public abstract class AbstractTransportSender extends AbstractHandler implements 44 TransportSender { 45 48 private Log log = LogFactory.getLog(getClass()); 49 50 51 52 55 public static final QName NAME = new QName ("http://axis.ws.apache.org", 56 "TransportSender"); 57 58 61 public AbstractTransportSender() { 62 init(new HandlerDescription(NAME)); 63 } 64 65 public void init(ConfigurationContext confContext, 66 TransportOutDescription transportOut) throws AxisFault { 67 68 } 69 70 76 public void invoke(MessageContext msgContext) throws AxisFault { 77 msgContext.setDoingMTOM(HTTPTransportUtils.doWriteMTOM(msgContext)); 80 81 OutputStream out = null; 82 83 EndpointReference epr = null; 84 85 if (msgContext.getTo() != null 86 && !AddressingConstants.Submission.WSA_ANONYMOUS_URL 87 .equals(msgContext.getTo().getAddress()) 88 && !AddressingConstants.Final.WSA_ANONYMOUS_URL 89 .equals(msgContext.getTo().getAddress())) { 90 epr = msgContext.getTo(); 91 } 92 93 if (epr != null) { 94 out = openTheConnection(epr, msgContext); 95 OutputStream newOut = startSendWithToAddress(msgContext, out); 96 if (newOut != null) { 97 out = newOut; 98 } 99 writeMessage(msgContext, out); 100 finalizeSendWithToAddress(msgContext, out); 101 } else { 102 out = (OutputStream ) msgContext 103 .getProperty(MessageContext.TRANSPORT_OUT); 104 if (out != null) { 105 startSendWithOutputStreamFromIncomingConnection(msgContext, out); 106 writeMessage(msgContext, out); 107 finalizeSendWithOutputStreamFromIncomingConnection(msgContext, 108 out); 109 } else { 110 throw new AxisFault( 111 "Both the TO and Property MessageContext.TRANSPORT_WRITER is Null, No where to send"); 112 } 113 } 114 if (msgContext.getOperationContext() != null) { 117 msgContext.getOperationContext().setProperty( 118 Constants.RESPONSE_WRITTEN, Constants.VALUE_TRUE); 119 } 120 } 121 122 public void writeMessage(MessageContext msgContext, OutputStream out) 123 throws AxisFault { 124 SOAPEnvelope envelope = msgContext.getEnvelope(); 125 OMElement outputMessage = envelope; 126 127 if (envelope != null && msgContext.isDoingREST()) { 128 outputMessage = envelope.getBody().getFirstElement(); 129 } 130 131 if (outputMessage != null) { 132 OMOutput omOutput = null; 133 134 try { 135 if (msgContext.isDoingMTOM()) { 136 omOutput = new OMOutput(out, true); 137 outputMessage.serialize(omOutput); 138 omOutput.flush(); 139 omOutput.complete(); 140 out.flush(); 141 } else { 142 omOutput = new OMOutput(out, false); 143 outputMessage.serialize(omOutput); 144 omOutput.flush(); 145 out.flush(); 146 } 147 } catch (Exception e) { 148 throw new AxisFault("Stream error", e); 149 } 150 } else { 151 throw new AxisFault("the OUTPUT message is Null, nothing to write"); 152 } 153 } 154 155 public abstract OutputStream startSendWithToAddress( 156 MessageContext msgContext, OutputStream out) throws AxisFault; 157 158 public abstract void finalizeSendWithToAddress(MessageContext msgContext, 159 OutputStream out) throws AxisFault; 160 161 public abstract OutputStream startSendWithOutputStreamFromIncomingConnection( 162 MessageContext msgContext, OutputStream out) throws AxisFault; 163 164 public abstract void finalizeSendWithOutputStreamFromIncomingConnection( 165 MessageContext msgContext, OutputStream out) throws AxisFault; 166 167 protected abstract OutputStream openTheConnection(EndpointReference epr, 168 MessageContext msgctx) throws AxisFault; 169 } | Popular Tags |