CAMEL-8642 Supports to use custom classloader when deserializeJavaObjectFromStream in HttpHelper
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/8bb4ad50 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/8bb4ad50 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/8bb4ad50 Branch: refs/heads/master Commit: 8bb4ad505f3688e13369b37890ce372f7d0b4ae5 Parents: 3b3a6e6 Author: Willem Jiang <willem.ji...@gmail.com> Authored: Fri Apr 17 10:52:14 2015 +0800 Committer: Willem Jiang <willem.ji...@gmail.com> Committed: Fri Apr 17 14:16:53 2015 +0800 ---------------------------------------------------------------------- .../camel/util/CamelObjectInputStream.java | 46 ++++++++++++++++++++ .../component/http/DefaultHttpBinding.java | 2 +- .../camel/component/http/HttpProducer.java | 2 +- .../camel/component/http/helper/HttpHelper.java | 20 ++++++++- .../jetty/DefaultJettyHttpBinding.java | 2 +- 5 files changed, 68 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/8bb4ad50/camel-core/src/main/java/org/apache/camel/util/CamelObjectInputStream.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/util/CamelObjectInputStream.java b/camel-core/src/main/java/org/apache/camel/util/CamelObjectInputStream.java new file mode 100644 index 0000000..a5cc591 --- /dev/null +++ b/camel-core/src/main/java/org/apache/camel/util/CamelObjectInputStream.java @@ -0,0 +1,46 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.util; + +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInputStream; +import java.io.ObjectStreamClass; + +import org.apache.camel.CamelContext; + +public class CamelObjectInputStream extends ObjectInputStream { + private final ClassLoader classLoader; + + public CamelObjectInputStream(InputStream in, CamelContext context) throws IOException { + super(in); + if (context != null) { + this.classLoader = context.getApplicationContextClassLoader(); + } else { + this.classLoader = null; + } + } + + protected Class<?> resolveClass(ObjectStreamClass desc) throws ClassNotFoundException, IOException { + if (classLoader != null) { + return Class.forName(desc.getName(), false, classLoader); + } else { + // If the application classloader is not set we just fallback to use old behaivor + return super.resolveClass(desc); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/8bb4ad50/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java ---------------------------------------------------------------------- diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java b/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java index 651c2ac..e1fa34e 100644 --- a/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java +++ b/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java @@ -138,7 +138,7 @@ public class DefaultHttpBinding implements HttpBinding { if (request.getContentType() != null && HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(request.getContentType())) { try { InputStream is = endpoint.getCamelContext().getTypeConverter().mandatoryConvertTo(InputStream.class, body); - Object object = HttpHelper.deserializeJavaObjectFromStream(is); + Object object = HttpHelper.deserializeJavaObjectFromStream(is, message.getExchange().getContext()); if (object != null) { message.setBody(object); } http://git-wip-us.apache.org/repos/asf/camel/blob/8bb4ad50/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java index 9774118..0269ee4 100644 --- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java +++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java @@ -294,7 +294,7 @@ public class HttpProducer extends DefaultProducer { InputStream response = doExtractResponseBodyAsStream(is, exchange); // if content type is a serialized java object then de-serialize it back to a Java object if (contentType != null && contentType.equals(HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT)) { - return HttpHelper.deserializeJavaObjectFromStream(response); + return HttpHelper.deserializeJavaObjectFromStream(response, exchange.getContext()); } else { return response; } http://git-wip-us.apache.org/repos/asf/camel/blob/8bb4ad50/components/camel-http/src/main/java/org/apache/camel/component/http/helper/HttpHelper.java ---------------------------------------------------------------------- diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/helper/HttpHelper.java b/components/camel-http/src/main/java/org/apache/camel/component/http/helper/HttpHelper.java index dade973..36374a2 100644 --- a/components/camel-http/src/main/java/org/apache/camel/component/http/helper/HttpHelper.java +++ b/components/camel-http/src/main/java/org/apache/camel/component/http/helper/HttpHelper.java @@ -26,9 +26,11 @@ import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import java.util.Map; + import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; +import org.apache.camel.CamelContext; import org.apache.camel.Exchange; import org.apache.camel.Producer; import org.apache.camel.RuntimeExchangeException; @@ -40,6 +42,7 @@ import org.apache.camel.component.http.HttpMethods; import org.apache.camel.component.http.HttpServletUrlRewrite; import org.apache.camel.converter.IOConverter; import org.apache.camel.converter.stream.CachedOutputStream; +import org.apache.camel.util.CamelObjectInputStream; import org.apache.camel.util.IOHelper; import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.URISupport; @@ -118,14 +121,29 @@ public final class HttpHelper { * @return the java object, or <tt>null</tt> if input stream was <tt>null</tt> * @throws ClassNotFoundException is thrown if class not found * @throws IOException can be thrown + * @deprecated Camel 3.0 + * Please use the one which has the parameter of camel context */ public static Object deserializeJavaObjectFromStream(InputStream is) throws ClassNotFoundException, IOException { + return deserializeJavaObjectFromStream(is, null); + } + + /** + * Deserializes the input stream to a Java object + * + * @param is input stream for the Java object + * @param context the camel context which could help us to apply the customer classloader + * @return the java object, or <tt>null</tt> if input stream was <tt>null</tt> + * @throws ClassNotFoundException is thrown if class not found + * @throws IOException can be thrown + */ + public static Object deserializeJavaObjectFromStream(InputStream is, CamelContext context) throws ClassNotFoundException, IOException { if (is == null) { return null; } Object answer = null; - ObjectInputStream ois = new ObjectInputStream(is); + ObjectInputStream ois = new CamelObjectInputStream(is, context); try { answer = ois.readObject(); } finally { http://git-wip-us.apache.org/repos/asf/camel/blob/8bb4ad50/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java index 8c55d3e..dabafd5 100644 --- a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java +++ b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java @@ -172,7 +172,7 @@ public class DefaultJettyHttpBinding implements JettyHttpBinding { if (contentType != null && HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(contentType)) { try { InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, httpExchange.getResponseContentBytes()); - return HttpHelper.deserializeJavaObjectFromStream(is); + return HttpHelper.deserializeJavaObjectFromStream(is, exchange.getContext()); } catch (Exception e) { throw new RuntimeCamelException("Cannot deserialize body to Java object", e); }