use of org.apache.axiom.om.OMOutputFormat in project wso2-synapse by wso2.
the class MessageUtils method getOMOutputFormat.
public static OMOutputFormat getOMOutputFormat(MessageContext msgContext) {
OMOutputFormat format;
if (msgContext.getProperty(BridgeConstants.MESSAGE_OUTPUT_FORMAT) != null) {
format = (OMOutputFormat) msgContext.getProperty(BridgeConstants.MESSAGE_OUTPUT_FORMAT);
} else {
format = new OMOutputFormat();
}
msgContext.setDoingMTOM(TransportUtils.doWriteMTOM(msgContext));
msgContext.setDoingSwA(TransportUtils.doWriteSwA(msgContext));
msgContext.setDoingREST(TransportUtils.isDoingREST(msgContext));
/*
* BridgeConstants.INVOKED_REST set to true here if isDoingREST is true -
* this enables us to check whether the original request to the endpoint was a
* REST request inside DeferredMessageBuilder (which we need to convert
* text/xml content type into application/xml if the request was not a SOAP
* request.
*/
if (msgContext.isDoingREST()) {
msgContext.setProperty(BridgeConstants.INVOKED_REST, true);
}
format.setSOAP11(msgContext.isSOAP11());
format.setDoOptimize(msgContext.isDoingMTOM());
format.setDoingSWA(msgContext.isDoingSwA());
format.setCharSetEncoding(TransportUtils.getCharSetEncoding(msgContext));
Object mimeBoundaryProperty = msgContext.getProperty(Constants.Configuration.MIME_BOUNDARY);
if (mimeBoundaryProperty != null) {
format.setMimeBoundary((String) mimeBoundaryProperty);
}
return format;
}
use of org.apache.axiom.om.OMOutputFormat in project wso2-synapse by wso2.
the class TargetRequest method start.
public void start(NHttpClientConnection conn) throws IOException, HttpException {
if (pipe != null) {
TargetContext.get(conn).setWriter(pipe);
}
String path = fullUrl || (route.getProxyHost() != null && !route.isTunnelled()) ? url.toString() : url.getPath() + (url.getQuery() != null ? "?" + url.getQuery() : "");
long contentLength = -1;
String contentLengthHeader = null;
LinkedHashSet<String> httpContentLengthHeader = headers.get(HTTP.CONTENT_LEN);
if (httpContentLengthHeader != null && httpContentLengthHeader.iterator().hasNext()) {
contentLengthHeader = httpContentLengthHeader.iterator().next();
}
if (contentLengthHeader != null) {
contentLength = Long.parseLong(contentLengthHeader);
headers.remove(HTTP.CONTENT_LEN);
}
MessageContext requestMsgCtx = TargetContext.get(conn).getRequestMsgCtx();
if (requestMsgCtx.getProperty(PassThroughConstants.PASSTROUGH_MESSAGE_LENGTH) != null) {
contentLength = (Long) requestMsgCtx.getProperty(PassThroughConstants.PASSTROUGH_MESSAGE_LENGTH);
}
// fix for POST_TO_URI
if (requestMsgCtx.isPropertyTrue(NhttpConstants.POST_TO_URI)) {
path = url.toString();
}
// fix GET request empty body
if ((PassThroughConstants.HTTP_GET.equals(requestMsgCtx.getProperty(Constants.Configuration.HTTP_METHOD))) || (RelayUtils.isDeleteRequestWithoutPayload(requestMsgCtx))) {
hasEntityBody = false;
MessageFormatter formatter = MessageProcessorSelector.getMessageFormatter(requestMsgCtx);
OMOutputFormat format = PassThroughTransportUtils.getOMOutputFormat(requestMsgCtx);
if (formatter != null && format != null) {
URL _url = formatter.getTargetAddress(requestMsgCtx, format, url);
if (_url != null && !_url.toString().isEmpty()) {
if (requestMsgCtx.getProperty(NhttpConstants.POST_TO_URI) != null && Boolean.TRUE.toString().equals(requestMsgCtx.getProperty(NhttpConstants.POST_TO_URI))) {
path = _url.toString();
} else {
path = _url.getPath() + ((_url.getQuery() != null && !_url.getQuery().isEmpty()) ? ("?" + _url.getQuery()) : "");
}
}
headers.remove(HTTP.CONTENT_TYPE);
}
}
Object o = requestMsgCtx.getProperty(MessageContext.TRANSPORT_HEADERS);
if (o != null && o instanceof TreeMap) {
Map _headers = (Map) o;
String trpContentType = (String) _headers.get(HTTP.CONTENT_TYPE);
if (trpContentType != null && !trpContentType.equals("")) {
if (!TargetRequestFactory.isMultipartContent(trpContentType) && !requestMsgCtx.isDoingSwA()) {
addHeader(HTTP.CONTENT_TYPE, trpContentType);
}
}
}
if (hasEntityBody) {
request = new BasicHttpEntityEnclosingRequest(method, path, version != null ? version : HttpVersion.HTTP_1_1);
BasicHttpEntity entity = new BasicHttpEntity();
boolean forceContentLength = requestMsgCtx.isPropertyTrue(NhttpConstants.FORCE_HTTP_CONTENT_LENGTH);
boolean forceContentLengthCopy = requestMsgCtx.isPropertyTrue(PassThroughConstants.COPY_CONTENT_LENGTH_FROM_INCOMING);
if (forceContentLength) {
entity.setChunked(false);
if (forceContentLengthCopy && contentLength != -1) {
entity.setContentLength(contentLength);
}
} else {
if (contentLength != -1) {
entity.setChunked(false);
entity.setContentLength(contentLength);
} else {
entity.setChunked(chunk);
}
}
((BasicHttpEntityEnclosingRequest) request).setEntity(entity);
} else {
request = new BasicHttpRequest(method, path, version != null ? version : HttpVersion.HTTP_1_1);
}
Set<Map.Entry<String, LinkedHashSet<String>>> entries = headers.entrySet();
for (Map.Entry<String, LinkedHashSet<String>> entry : entries) {
if (entry.getKey() != null) {
Iterator<String> i = entry.getValue().iterator();
while (i.hasNext()) {
request.addHeader(entry.getKey(), i.next());
}
}
}
// setup wsa action..
if (request != null) {
String soapAction = requestMsgCtx.getSoapAction();
if (soapAction == null) {
soapAction = requestMsgCtx.getWSAAction();
requestMsgCtx.getAxisOperation().getInputAction();
}
if (requestMsgCtx.isSOAP11() && soapAction != null && soapAction.length() > 0) {
Header existingHeader = request.getFirstHeader(HTTPConstants.HEADER_SOAP_ACTION);
if (existingHeader != null) {
request.removeHeader(existingHeader);
}
MessageFormatter messageFormatter = MessageFormatterDecoratorFactory.createMessageFormatterDecorator(requestMsgCtx);
request.setHeader(HTTPConstants.HEADER_SOAP_ACTION, messageFormatter.formatSOAPAction(requestMsgCtx, null, soapAction));
// request.setHeader(HTTPConstants.USER_AGENT,"Synapse-PT-HttpComponents-NIO");
}
}
request.setParams(new DefaultedHttpParams(request.getParams(), targetConfiguration.getHttpParams()));
// Chunking is not performed for request has "http 1.0" and "GET" http method
if (!((request.getProtocolVersion().equals(HttpVersion.HTTP_1_0)) || (PassThroughConstants.HTTP_GET.equals(requestMsgCtx.getProperty(Constants.Configuration.HTTP_METHOD))) || RelayUtils.isDeleteRequestWithoutPayload(requestMsgCtx) || !(hasEntityBody))) {
this.processChunking(conn, requestMsgCtx);
}
if (!keepAlive) {
request.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
}
// Pre-process HTTP request
HttpContext httpContext = conn.getContext();
httpContext.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
if (port == -1) {
httpContext.setAttribute(ExecutionContext.HTTP_TARGET_HOST, new HttpHost(url.getHost()));
} else {
httpContext.setAttribute(ExecutionContext.HTTP_TARGET_HOST, new HttpHost(url.getHost(), port));
}
conn.getContext().setAttribute(ExecutionContext.HTTP_REQUEST, request);
httpContext.setAttribute(PassThroughConstants.PROXY_PROFILE_TARGET_HOST, requestMsgCtx.getProperty(PassThroughConstants.PROXY_PROFILE_TARGET_HOST));
// start the request
targetConfiguration.getHttpProcessor().process(request, httpContext);
if (targetConfiguration.getProxyAuthenticator() != null && route.getProxyHost() != null && !route.isTunnelled()) {
targetConfiguration.getProxyAuthenticator().authenticatePreemptively(request, httpContext);
}
conn.submitRequest(request);
if (hasEntityBody) {
TargetContext.updateState(conn, ProtocolState.REQUEST_HEAD);
} else {
TargetContext.updateState(conn, ProtocolState.REQUEST_DONE);
}
}
use of org.apache.axiom.om.OMOutputFormat in project wso2-synapse by wso2.
the class PassThroughTransportUtils method getOMOutputFormat.
public static OMOutputFormat getOMOutputFormat(MessageContext msgContext) {
OMOutputFormat format = null;
if (msgContext.getProperty(PassThroughConstants.MESSAGE_OUTPUT_FORMAT) != null) {
format = (OMOutputFormat) msgContext.getProperty(PassThroughConstants.MESSAGE_OUTPUT_FORMAT);
} else {
format = new OMOutputFormat();
}
// Keep the formatter information to prevent multipart boundary override (this will be the content writing to header)
msgContext.setProperty(PassThroughConstants.MESSAGE_OUTPUT_FORMAT, format);
msgContext.setDoingMTOM(TransportUtils.doWriteMTOM(msgContext));
msgContext.setDoingSwA(TransportUtils.doWriteSwA(msgContext));
msgContext.setDoingREST(TransportUtils.isDoingREST(msgContext));
/**
* PassThroughConstants.INVOKED_REST set to true here if isDoingREST is true -
* this enables us to check whether the original request to the endpoint was a
* REST request inside DefferedMessageBuilder (which we need to convert
* text/xml content type into application/xml if the request was not a SOAP
* request.
*/
if (msgContext.isDoingREST()) {
msgContext.setProperty(PassThroughConstants.INVOKED_REST, true);
}
format.setSOAP11(msgContext.isSOAP11());
format.setDoOptimize(msgContext.isDoingMTOM());
format.setDoingSWA(msgContext.isDoingSwA());
format.setCharSetEncoding(TransportUtils.getCharSetEncoding(msgContext));
Object mimeBoundaryProperty = msgContext.getProperty(Constants.Configuration.MIME_BOUNDARY);
if (mimeBoundaryProperty != null) {
format.setMimeBoundary((String) mimeBoundaryProperty);
}
return format;
}
use of org.apache.axiom.om.OMOutputFormat in project wso2-synapse by wso2.
the class TargetRequestFactory method getContentType.
private static String getContentType(MessageContext msgCtx, boolean isContentTypePreservedHeader) throws AxisFault {
Object trpHeaders = msgCtx.getProperty(MessageContext.TRANSPORT_HEADERS);
String setEncoding = (String) msgCtx.getProperty(PassThroughConstants.SET_CHARACTER_ENCODING);
// Need to avoid this for multipart headers, need to add MIME Boundary property
if (trpHeaders != null && trpHeaders instanceof Map && ((Map) trpHeaders).get(HTTPConstants.HEADER_CONTENT_TYPE) != null && (isContentTypePreservedHeader || PassThroughConstants.VALUE_FALSE.equals(setEncoding)) && !isMultipartContent(((Map) trpHeaders).get(HTTPConstants.HEADER_CONTENT_TYPE).toString())) {
if (msgCtx.getProperty(Constants.Configuration.CONTENT_TYPE) != null) {
return (String) msgCtx.getProperty(Constants.Configuration.CONTENT_TYPE);
} else if (msgCtx.getProperty(Constants.Configuration.MESSAGE_TYPE) != null) {
return (String) msgCtx.getProperty(Constants.Configuration.MESSAGE_TYPE);
}
}
MessageFormatter formatter = MessageProcessorSelector.getMessageFormatter(msgCtx);
OMOutputFormat format = PassThroughTransportUtils.getOMOutputFormat(msgCtx);
if (formatter != null) {
String contentType = formatter.getContentType(msgCtx, format, msgCtx.getSoapAction());
return contentType;
} else {
String contentType = (String) msgCtx.getProperty(Constants.Configuration.CONTENT_TYPE);
if (contentType != null) {
return contentType;
} else {
return new SOAPMessageFormatter().getContentType(msgCtx, format, msgCtx.getSoapAction());
}
}
}
use of org.apache.axiom.om.OMOutputFormat in project wso2-synapse by wso2.
the class PassThroughHttpSender method submitResponse.
public void submitResponse(MessageContext msgContext) throws IOException, HttpException {
SourceConfiguration sourceConfiguration = (SourceConfiguration) msgContext.getProperty(PassThroughConstants.PASS_THROUGH_SOURCE_CONFIGURATION);
NHttpServerConnection conn = (NHttpServerConnection) msgContext.getProperty(PassThroughConstants.PASS_THROUGH_SOURCE_CONNECTION);
if (conn == null) {
ServerWorker serverWorker = (ServerWorker) msgContext.getProperty(Constants.OUT_TRANSPORT_INFO);
if (serverWorker != null) {
MessageContext requestContext = serverWorker.getRequestContext();
conn = (NHttpServerConnection) requestContext.getProperty(PassThroughConstants.PASS_THROUGH_SOURCE_CONNECTION);
sourceConfiguration = (SourceConfiguration) requestContext.getProperty(PassThroughConstants.PASS_THROUGH_SOURCE_CONFIGURATION);
} else {
throw new IllegalStateException("Unable to correlate the response to a request");
}
}
// Handle ETag caching
if (msgContext.getProperty(PassThroughConstants.HTTP_ETAG_ENABLED) != null && (Boolean) msgContext.getProperty(PassThroughConstants.HTTP_ETAG_ENABLED)) {
try {
RelayUtils.buildMessage(msgContext);
} catch (IOException e) {
handleException("IO Error occurred while building the message", e);
} catch (XMLStreamException e) {
handleException("XML Error occurred while building the message", e);
}
String hash = digestGenerator.getDigest(msgContext);
Map headers = (Map) msgContext.getProperty(MessageContext.TRANSPORT_HEADERS);
headers.put(HttpHeaders.ETAG, "\"" + hash + "\"");
}
if (msgContext.getProperty(Constants.Configuration.ENABLE_MTOM) != null && !Boolean.TRUE.equals(msgContext.getProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED))) {
try {
RelayUtils.buildMessage(msgContext);
} catch (IOException e) {
handleException("IO Error occurred while building the message", e);
} catch (XMLStreamException e) {
handleException("XML Error occurred while building the message", e);
}
}
SourceRequest sourceRequest = SourceContext.getRequest(conn);
if (sourceRequest == null) {
// this is a special case we dropped source connection where message size exceeds the user defined threshold
if (conn.getContext().getAttribute(PassThroughConstants.SOURCE_CONNECTION_DROPPED) != null && (Boolean) conn.getContext().getAttribute(PassThroughConstants.SOURCE_CONNECTION_DROPPED)) {
// already submitted response for this case, hence return
return;
}
log.warn("Trying to submit a response to an already closed connection : " + conn);
return;
}
SourceResponse sourceResponse = SourceResponseFactory.create(msgContext, sourceRequest, sourceConfiguration);
sourceResponse.checkResponseChunkDisable(msgContext);
conn.getContext().setAttribute(PassThroughConstants.RESPONSE_MESSAGE_CONTEXT, msgContext);
SourceContext.setResponse(conn, sourceResponse);
Boolean noEntityBody = (Boolean) msgContext.getProperty(PassThroughConstants.NO_ENTITY_BODY);
Pipe pipe = (Pipe) msgContext.getProperty(PassThroughConstants.PASS_THROUGH_PIPE);
if ((noEntityBody == null || !noEntityBody) || pipe != null) {
if (pipe == null) {
pipe = new Pipe(sourceConfiguration.getBufferFactory().getBuffer(), "Test", sourceConfiguration);
msgContext.setProperty(PassThroughConstants.PASS_THROUGH_PIPE, pipe);
msgContext.setProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED, Boolean.TRUE);
}
pipe.attachConsumer(conn);
sourceResponse.connect(pipe);
}
Integer errorCode = (Integer) msgContext.getProperty(PassThroughConstants.ERROR_CODE);
if (errorCode != null) {
sourceResponse.setStatus(HttpStatus.SC_BAD_GATEWAY);
SourceContext.get(conn).setShutDown(true);
}
ProtocolState state = SourceContext.getState(conn);
if (state != null && state.compareTo(ProtocolState.REQUEST_DONE) <= 0) {
// start sending the response if we
boolean noEntityBodyResponse = false;
if (noEntityBody != null && Boolean.TRUE == noEntityBody && pipe != null) {
OutputStream out = pipe.getOutputStream();
out.write(new byte[0]);
pipe.setRawSerializationComplete(true);
out.close();
noEntityBodyResponse = true;
}
if (!noEntityBodyResponse && msgContext.isPropertyTrue(PassThroughConstants.MESSAGE_BUILDER_INVOKED) && pipe != null) {
OutputStream out = pipe.getOutputStream();
// when there is no SOAPAction.
if (Constants.VALUE_TRUE.equals(msgContext.getProperty(Constants.Configuration.ENABLE_MTOM)) || Constants.VALUE_TRUE.equals(msgContext.getProperty(Constants.Configuration.ENABLE_SWA))) {
Object contentType = msgContext.getProperty(Constants.Configuration.CONTENT_TYPE);
if (Objects.isNull(contentType) || !((String) contentType).trim().startsWith(PassThroughConstants.CONTENT_TYPE_MULTIPART_RELATED)) {
msgContext.setProperty(Constants.Configuration.CONTENT_TYPE, PassThroughConstants.CONTENT_TYPE_MULTIPART_RELATED);
}
msgContext.setProperty(Constants.Configuration.MESSAGE_TYPE, PassThroughConstants.CONTENT_TYPE_MULTIPART_RELATED);
}
MessageFormatter formatter = MessageFormatterDecoratorFactory.createMessageFormatterDecorator(msgContext);
OMOutputFormat format = PassThroughTransportUtils.getOMOutputFormat(msgContext);
setContentType(msgContext, sourceResponse, formatter, format, sourceConfiguration);
try {
formatter.writeTo(msgContext, format, out, false);
} catch (RemoteException fault) {
IOUtils.closeQuietly(out);
throw fault;
} finally {
// Serialization should be set as complete so that the state of the socket can be
// reset to readable
pipe.setSerializationComplete(true);
}
out.close();
}
conn.requestOutput();
} else {
// nothing much to do as we have started the response already
if (errorCode != null) {
if (log.isDebugEnabled()) {
log.warn("A Source connection is closed because of an " + "error in target: " + conn);
}
} else {
log.debug("A Source Connection is closed, because source handler " + "is already in the process of writing a response while " + "another response is submitted: " + conn);
}
pipe.consumerError();
SourceContext.updateState(conn, ProtocolState.CLOSED);
sourceConfiguration.getSourceConnections().shutDownConnection(conn, true);
}
}
Aggregations