commonshttptransportsender.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 377 行 · 第 1/2 页
JAVA
377 行
epr = new EndpointReference(transportURL);
} else if (msgContext.getTo() != null
&& !msgContext.getTo().hasAnonymousAddress()) {
epr = msgContext.getTo();
}
// Check for the REST behavior, if you desire rest behavior
// put a <parameter name="doREST" value="true"/> at the
// server.xml/client.xml file
// ######################################################
// Change this place to change the wsa:toepr
// epr = something
// ######################################################
if (epr != null) {
if (!epr.hasNoneAddress()) {
writeMessageWithCommons(msgContext, epr, format);
TransportUtils.setResponseWritten(msgContext, true);
}
} else {
if (msgContext.getProperty(MessageContext.TRANSPORT_OUT) != null) {
sendUsingOutputStream(msgContext, format);
TransportUtils.setResponseWritten(msgContext, true);
} else {
throw new AxisFault("Both the TO and MessageContext.TRANSPORT_OUT property " +
"are null, so nowhere to send");
}
}
} catch (FactoryConfigurationError e) {
log.debug(e);
throw AxisFault.makeFault(e);
} catch (IOException e) {
log.debug(e);
throw AxisFault.makeFault(e);
}
return InvocationResponse.CONTINUE;
}
/**
* Send a message (which must be a response) via the OutputStream sitting in the
* MessageContext TRANSPORT_OUT property. Since this class is used for both requests and
* responses, we split the logic - this method always gets called when we're
* writing to the HTTP response stream, and sendUsingCommons() is used for requests.
*
* @param msgContext the active MessageContext
* @param format output formatter for our message
* @throws AxisFault if a general problem arises
*/
private void sendUsingOutputStream(MessageContext msgContext,
OMOutputFormat format) throws AxisFault {
OutputStream out = (OutputStream) msgContext.getProperty(MessageContext.TRANSPORT_OUT);
// I Don't think we need this check.. Content type needs to be set in
// any case. (thilina)
// if (msgContext.isServerSide()) {
OutTransportInfo transportInfo = (OutTransportInfo) msgContext
.getProperty(Constants.OUT_TRANSPORT_INFO);
if (transportInfo == null) throw new AxisFault("No transport info in MessageContext");
ServletBasedOutTransportInfo servletBasedOutTransportInfo = null;
if (transportInfo instanceof ServletBasedOutTransportInfo) {
servletBasedOutTransportInfo =
(ServletBasedOutTransportInfo) transportInfo;
List customHeaders = (List) msgContext.getProperty(HTTPConstants.HTTP_HEADERS);
if (customHeaders != null) {
Iterator iter = customHeaders.iterator();
while (iter.hasNext()) {
Header header = (Header) iter.next();
if (header != null) {
servletBasedOutTransportInfo
.addHeader(header.getName(), header.getValue());
}
}
}
}
format.setAutoCloseWriter(true);
MessageFormatter messageFormatter = TransportUtils.getMessageFormatter(msgContext);
if (messageFormatter == null) throw new AxisFault("No MessageFormatter in MessageContext");
// Once we get to this point, exceptions should NOT be turned into faults and sent,
// because we're already sending! So catch everything and log it, but don't pass
// upwards.
try {
transportInfo.setContentType(
messageFormatter.getContentType(msgContext, format, findSOAPAction(msgContext)));
Object gzip = msgContext.getOptions().getProperty(HTTPConstants.MC_GZIP_RESPONSE);
if (gzip != null && JavaUtils.isTrueExplicitly(gzip)) {
if (servletBasedOutTransportInfo != null)
servletBasedOutTransportInfo.addHeader(HTTPConstants.HEADER_CONTENT_ENCODING,
HTTPConstants.COMPRESSION_GZIP);
try {
out = new GZIPOutputStream(out);
out.write(messageFormatter.getBytes(msgContext, format));
((GZIPOutputStream) out).finish();
out.flush();
} catch (IOException e) {
throw new AxisFault("Could not compress response");
}
} else {
messageFormatter.writeTo(msgContext, format, out, false);
}
} catch (AxisFault axisFault) {
log.error(axisFault.getMessage(), axisFault);
}
}
private void writeMessageWithCommons(MessageContext messageContext,
EndpointReference toEPR, OMOutputFormat format)
throws AxisFault {
try {
URL url = new URL(toEPR.getAddress());
// select the Message Sender depending on the REST status
AbstractHTTPSender sender;
sender = new HTTPSender();
if (messageContext.getProperty(HTTPConstants.CHUNKED) != null) {
chunked = JavaUtils.isTrueExplicitly(messageContext
.getProperty(HTTPConstants.CHUNKED));
}
if (messageContext.getProperty(HTTPConstants.HTTP_PROTOCOL_VERSION) != null) {
httpVersion = (String) messageContext
.getProperty(HTTPConstants.HTTP_PROTOCOL_VERSION);
}
// Following order needed to be preserved because,
// HTTP/1.0 does not support chunk encoding
sender.setChunked(chunked);
sender.setHttpVersion(httpVersion);
sender.setFormat(format);
sender.send(messageContext, url, findSOAPAction(messageContext));
} catch (MalformedURLException e) {
log.debug(e);
throw AxisFault.makeFault(e);
} catch (HttpException e) {
log.debug(e);
throw AxisFault.makeFault(e);
} catch (IOException e) {
log.debug(e);
throw AxisFault.makeFault(e);
}
}
private static String findSOAPAction(MessageContext messageContext) {
String soapActionString = null;
Parameter parameter =
messageContext.getTransportOut().getParameter(HTTPConstants.OMIT_SOAP_12_ACTION);
if (parameter != null && JavaUtils.isTrueExplicitly(parameter.getValue()) &&
!messageContext.isSOAP11()) {
return "\"\"";
}
Object disableSoapAction = messageContext.getOptions().getProperty(
Constants.Configuration.DISABLE_SOAP_ACTION);
if (!JavaUtils.isTrueExplicitly(disableSoapAction)) {
// first try to get the SOAP action from message context
soapActionString = messageContext.getSoapAction();
if ((soapActionString == null) || (soapActionString.length() == 0)) {
// now let's try to get WSA action
soapActionString = messageContext.getWSAAction();
if (messageContext.getAxisOperation() != null
&& ((soapActionString == null) || (soapActionString
.length() == 0))) {
// last option is to get it from the axis operation
soapActionString = messageContext.getAxisOperation()
.getSoapAction();
}
}
}
//Since action is optional for SOAP 1.2 we can return null here.
if (soapActionString == null && messageContext.isSOAP11()) {
soapActionString = "\"\"";
}
return soapActionString;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?