Skip to content

Commit 515c822

Browse files
committed
CAMEL-9309: Make it easier to turn on|off java transport over http
1 parent c349d13 commit 515c822

File tree

3 files changed

+58
-18
lines changed

3 files changed

+58
-18
lines changed

components/camel-http-common/src/main/java/org/apache/camel/http/common/DefaultHttpBinding.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public class DefaultHttpBinding implements HttpBinding {
7474
private boolean useReaderForPayload;
7575
private boolean eagerCheckContentAvailable;
7676
private boolean transferException;
77+
private boolean allowJavaSerializedObject;
7778
private HeaderFilterStrategy headerFilterStrategy = new HttpHeaderFilterStrategy();
7879

7980
public DefaultHttpBinding() {
@@ -88,6 +89,7 @@ public DefaultHttpBinding(HeaderFilterStrategy headerFilterStrategy) {
8889
public DefaultHttpBinding(HttpCommonEndpoint endpoint) {
8990
this.headerFilterStrategy = endpoint.getHeaderFilterStrategy();
9091
this.transferException = endpoint.isTransferException();
92+
this.allowJavaSerializedObject = endpoint.getComponent().isAllowJavaSerializedObject();
9193
}
9294

9395
public void readRequest(HttpServletRequest request, HttpMessage message) {
@@ -151,14 +153,18 @@ public void readRequest(HttpServletRequest request, HttpMessage message) {
151153

152154
// if content type is serialized java object, then de-serialize it to a Java object
153155
if (request.getContentType() != null && HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(request.getContentType())) {
154-
try {
155-
InputStream is = message.getExchange().getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, body);
156-
Object object = HttpHelper.deserializeJavaObjectFromStream(is, message.getExchange().getContext());
157-
if (object != null) {
158-
message.setBody(object);
156+
if (allowJavaSerializedObject || isTransferException()) {
157+
try {
158+
InputStream is = message.getExchange().getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, body);
159+
Object object = HttpHelper.deserializeJavaObjectFromStream(is, message.getExchange().getContext());
160+
if (object != null) {
161+
message.setBody(object);
162+
}
163+
} catch (Exception e) {
164+
throw new RuntimeCamelException("Cannot deserialize body to Java object", e);
159165
}
160-
} catch (Exception e) {
161-
throw new RuntimeCamelException("Cannot deserialize body to Java object", e);
166+
} else {
167+
throw new RuntimeCamelException("Content-type " + HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT + " is not allowed");
162168
}
163169
}
164170

@@ -358,13 +364,17 @@ protected void doWriteDirectResponse(Message message, HttpServletResponse respon
358364
// if content type is serialized Java object, then serialize and write it to the response
359365
String contentType = message.getHeader(Exchange.CONTENT_TYPE, String.class);
360366
if (contentType != null && HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(contentType)) {
361-
try {
362-
Object object = message.getMandatoryBody(Serializable.class);
363-
HttpHelper.writeObjectToServletResponse(response, object);
364-
// object is written so return
365-
return;
366-
} catch (InvalidPayloadException e) {
367-
throw new IOException(e);
367+
if (allowJavaSerializedObject || isTransferException()) {
368+
try {
369+
Object object = message.getMandatoryBody(Serializable.class);
370+
HttpHelper.writeObjectToServletResponse(response, object);
371+
// object is written so return
372+
return;
373+
} catch (InvalidPayloadException e) {
374+
throw new IOException(e);
375+
}
376+
} else {
377+
throw new RuntimeCamelException("Content-type " + HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT + " is not allowed");
368378
}
369379
}
370380

components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpCommonComponent.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public abstract class HttpCommonComponent extends HeaderFilterStrategyComponent
2222

2323
protected HttpBinding httpBinding;
2424
protected HttpConfiguration httpConfiguration;
25+
protected boolean allowJavaSerializedObject;
2526

2627
public HttpCommonComponent(Class<? extends HttpCommonEndpoint> endpointClass) {
2728
super(endpointClass);
@@ -72,4 +73,18 @@ public void setHttpConfiguration(HttpConfiguration httpConfiguration) {
7273
this.httpConfiguration = httpConfiguration;
7374
}
7475

76+
public boolean isAllowJavaSerializedObject() {
77+
return allowJavaSerializedObject;
78+
}
79+
80+
/**
81+
* Whether to allow java serialization when a request uses context-type=application/x-java-serialized-object
82+
* <p/>
83+
* This is by default turned off. If you enable this then be aware that Java will deserialize the incoming
84+
* data from the request to Java and that can be a potential security risk.
85+
*/
86+
public void setAllowJavaSerializedObject(boolean allowJavaSerializedObject) {
87+
this.allowJavaSerializedObject = allowJavaSerializedObject;
88+
}
89+
7590
}

components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpCommonEndpoint.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.net.URI;
2020
import java.net.URISyntaxException;
2121

22+
import org.apache.camel.Component;
2223
import org.apache.camel.impl.DefaultEndpoint;
2324
import org.apache.camel.spi.HeaderFilterStrategy;
2425
import org.apache.camel.spi.HeaderFilterStrategyAware;
@@ -69,8 +70,12 @@ public abstract class HttpCommonEndpoint extends DefaultEndpoint implements Head
6970
int proxyPort;
7071
@UriParam(label = "producer", enums = "Basic,Digest,NTLM", description = "Authentication method for proxy, either as Basic, Digest or NTLM.")
7172
String authMethodPriority;
72-
@UriParam(description = "Option to disable throwing the HttpOperationFailedException in case of failed responses from the remote server."
73-
+ " This allows you to get all responses regardless of the HTTP status code.")
73+
@UriParam(description = "If enabled and an Exchange failed processing on the consumer side, and if the caused Exception was send back serialized"
74+
+ " in the response as a application/x-java-serialized-object content type."
75+
+ " On the producer side the exception will be deserialized and thrown as is, instead of the HttpOperationFailedException."
76+
+ " The caused exception is required to be serialized."
77+
+ " This is by default turned off. If you enable this then be aware that Java will deserialize the incoming"
78+
+ " data from the request to Java and that can be a potential security risk.")
7479
boolean transferException;
7580
@UriParam(label = "consumer",
7681
description = "Specifies whether to enable HTTP TRACE for this Jetty consumer. By default TRACE is turned off.")
@@ -113,6 +118,11 @@ public void disconnect(HttpConsumer consumer) throws Exception {
113118
component.disconnect(consumer);
114119
}
115120

121+
@Override
122+
public HttpCommonComponent getComponent() {
123+
return (HttpCommonComponent) super.getComponent();
124+
}
125+
116126
public boolean isLenientProperties() {
117127
// true to allow dynamic URI options to be configured and passed to external system for eg. the HttpProducer
118128
return true;
@@ -291,8 +301,13 @@ public boolean isTransferException() {
291301
}
292302

293303
/**
294-
* Option to disable throwing the HttpOperationFailedException in case of failed responses from the remote server.
295-
* This allows you to get all responses regardless of the HTTP status code.
304+
* If enabled and an Exchange failed processing on the consumer side, and if the caused Exception was send back serialized
305+
* in the response as a application/x-java-serialized-object content type.
306+
* On the producer side the exception will be deserialized and thrown as is, instead of the HttpOperationFailedException.
307+
* The caused exception is required to be serialized.
308+
* <p/>
309+
* This is by default turned off. If you enable this then be aware that Java will deserialize the incoming
310+
* data from the request to Java and that can be a potential security risk.
296311
*/
297312
public void setTransferException(boolean transferException) {
298313
this.transferException = transferException;

0 commit comments

Comments
 (0)