From bc2b214984cffaea54f70bdf21de1e7ffbef9108 Mon Sep 17 00:00:00 2001 From: David BRASSELY Date: Sun, 25 Jan 2015 20:59:56 +0100 Subject: [PATCH] ESBCOMP-124 Provide a response filter to add content-length header when using StringProvider from JErsey --- .../jbi/restbc/jbiadapter/RestComponent.java | 6 +++- .../inbound/ContentLengthResponseFilter.java | 24 ++++++++++++++ .../jbiadapter/inbound/InboundDelegator.java | 31 +++++++++---------- 3 files changed, 44 insertions(+), 17 deletions(-) create mode 100644 ojc-core/restbc/jbiadapter/src/com/sun/jbi/restbc/jbiadapter/inbound/ContentLengthResponseFilter.java diff --git a/ojc-core/restbc/jbiadapter/src/com/sun/jbi/restbc/jbiadapter/RestComponent.java b/ojc-core/restbc/jbiadapter/src/com/sun/jbi/restbc/jbiadapter/RestComponent.java index b1a0f9b94..0be001146 100755 --- a/ojc-core/restbc/jbiadapter/src/com/sun/jbi/restbc/jbiadapter/RestComponent.java +++ b/ojc-core/restbc/jbiadapter/src/com/sun/jbi/restbc/jbiadapter/RestComponent.java @@ -28,6 +28,8 @@ import com.sun.grizzly.http.servlet.ServletAdapter; import com.sun.jbi.common.qos.config.ComponentConfig; import com.sun.jbi.common.qos.config.ConfigPersistence; import com.sun.jbi.configuration.RuntimeConfigurationHelper; +import com.sun.jbi.restbc.jbiadapter.inbound.CharsetResponseFilter; +import com.sun.jbi.restbc.jbiadapter.inbound.ContentLengthResponseFilter; import com.sun.jbi.restbc.jbiadapter.inbound.InboundDelegator; import com.sun.jbi.restbc.jbiadapter.inbound.InboundHttpListener; import com.sun.jbi.restbc.jbiadapter.mbeans.RuntimeConfig; @@ -366,7 +368,9 @@ public class RestComponent implements Component, ComponentLifeCycle { final Map initParams = new HashMap(); initParams.put("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jbi.restbc.jbiadapter.inbound.JerseyRootResourceApplication"); - initParams.put("com.sun.jersey.spi.container.ContainerResponseFilters", "com.sun.jbi.restbc.jbiadapter.inbound.CharsetResponseFilter"); + initParams.put("com.sun.jersey.spi.container.ContainerResponseFilters", + CharsetResponseFilter.class.getName() + ';' + + ContentLengthResponseFilter.class.getName()); for (Map.Entry e : initParams.entrySet()) { adapter.addInitParameter(e.getKey(), e.getValue()); diff --git a/ojc-core/restbc/jbiadapter/src/com/sun/jbi/restbc/jbiadapter/inbound/ContentLengthResponseFilter.java b/ojc-core/restbc/jbiadapter/src/com/sun/jbi/restbc/jbiadapter/inbound/ContentLengthResponseFilter.java new file mode 100644 index 000000000..8926dd9f5 --- /dev/null +++ b/ojc-core/restbc/jbiadapter/src/com/sun/jbi/restbc/jbiadapter/inbound/ContentLengthResponseFilter.java @@ -0,0 +1,24 @@ +package com.sun.jbi.restbc.jbiadapter.inbound; + +import com.sun.jersey.spi.container.ContainerRequest; +import com.sun.jersey.spi.container.ContainerResponse; +import com.sun.jersey.spi.container.ContainerResponseFilter; + +/** + * + * @author David BRASSELY (brasseld at gmail.com) + * @author OpenESB Community + */ +public class ContentLengthResponseFilter implements ContainerResponseFilter { + + public ContainerResponse filter(ContainerRequest request, ContainerResponse response) { + String contentLength = (String) response.getHttpHeaders().getFirst("X-Content-Length"); + if (contentLength != null) { + response.getHttpHeaders().remove("Transfer-Encoding"); + response.getHttpHeaders().remove("X-Content-Length"); + response.getHttpHeaders().putSingle("Content-Length", contentLength); + } + + return response; + } +} diff --git a/ojc-core/restbc/jbiadapter/src/com/sun/jbi/restbc/jbiadapter/inbound/InboundDelegator.java b/ojc-core/restbc/jbiadapter/src/com/sun/jbi/restbc/jbiadapter/inbound/InboundDelegator.java index 7a9877fbf..08398a578 100755 --- a/ojc-core/restbc/jbiadapter/src/com/sun/jbi/restbc/jbiadapter/inbound/InboundDelegator.java +++ b/ojc-core/restbc/jbiadapter/src/com/sun/jbi/restbc/jbiadapter/inbound/InboundDelegator.java @@ -308,16 +308,17 @@ public class InboundDelegator { boolean isContentTypeSet = false; if (responsePayload != null) { // NOI18N + String content = ""; + boolean isDataHandler = false; + if (responsePayload instanceof Source) { Source xmlPayload = (Source) responsePayload; List acceptableMediaTypes = headers.getAcceptableMediaTypes(); - if (acceptableMediaTypes.size() == 0) { + if (acceptableMediaTypes.isEmpty()) { // if accept header not present, return entity as XML if (!method.equalsIgnoreCase("head")) { - responseBuilder.entity(JbiMessageUtil.convertXmlToString(xmlPayload)); - } else { - responseBuilder.entity(""); + content = JbiMessageUtil.convertXmlToString(xmlPayload); } responseBuilder.type(MediaType.APPLICATION_XML_TYPE); isContentTypeSet = true; @@ -325,9 +326,7 @@ public class InboundDelegator { for (MediaType acceptableMediaType : acceptableMediaTypes) { if (PathUtil.isXMLMediaType(acceptableMediaType)) { if (!method.equalsIgnoreCase("head")) { - responseBuilder.entity(JbiMessageUtil.convertXmlToString(xmlPayload)); - } else { - responseBuilder.entity(""); + content = JbiMessageUtil.convertXmlToString(xmlPayload); } String retMediaType = acceptableMediaType.isWildcardType() ? "application" : acceptableMediaType.getType(); String retMediaSubType = acceptableMediaType.isWildcardSubtype() ? "xml" : acceptableMediaType.getSubtype(); @@ -342,12 +341,10 @@ public class InboundDelegator { com.sun.jbi.restbc.jbiadapter.org.json.JSONObject jsonObject = com.sun.jbi.restbc.jbiadapter.org.json.XML.toJSONObject(xmlPayloadAsString); if (jsonObject != null) { - responseBuilder.entity(jsonObject.toString()); + content = jsonObject.toString(); } else { - responseBuilder.entity(xmlPayloadAsString); + content = xmlPayloadAsString; } - } else { - responseBuilder.entity(""); } String retMediaType = acceptableMediaType.isWildcardType() ? "application" : acceptableMediaType.getType(); String retMediaSubType = acceptableMediaType.isWildcardSubtype() ? "json" : acceptableMediaType.getSubtype(); @@ -356,9 +353,7 @@ public class InboundDelegator { break; } else { if (!method.equalsIgnoreCase("head")) { - responseBuilder.entity(JbiMessageUtil.convertXmlToString(xmlPayload)); - } else { - responseBuilder.entity(""); + content = JbiMessageUtil.convertXmlToString(xmlPayload); } break; } @@ -368,8 +363,7 @@ public class InboundDelegator { if (!method.equalsIgnoreCase("head")) { DataHandler streamPayload = (DataHandler) responsePayload; responseBuilder.entity(streamPayload.getInputStream()); - } else { - responseBuilder.entity(""); + isDataHandler = true; } } @@ -386,6 +380,11 @@ public class InboundDelegator { } } } + + if (! isDataHandler) { + responseBuilder.entity(content); + responseBuilder.header("X-Content-Length", Integer.toString(content.length())); + } } Map responseHeaderMap = NMPropertiesUtil.getDynamicNMProperties(replyMsg, NMProps.NM_RESPONSE_HEADERS_PROP);