ESBCOMP-124 Provide a response filter to add content-length header when using StringProvider from JErsey
parent
aaacd6a72a
commit
bc2b214984
|
@ -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<String, String> initParams = new HashMap<String, String>();
|
||||
|
||||
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<String, String> e : initParams.entrySet()) {
|
||||
adapter.addInitParameter(e.getKey(), e.getValue());
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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<MediaType> 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<String, String> responseHeaderMap = NMPropertiesUtil.getDynamicNMProperties(replyMsg, NMProps.NM_RESPONSE_HEADERS_PROP);
|
||||
|
|
Loading…
Reference in New Issue