CAMEL-8157 Fixed NPE error of camel-example-servlet-tomcat with the streamcache enabled.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/b3237601 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/b3237601 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/b3237601 Branch: refs/heads/camel-2.13.x Commit: b323760152c76f60cc7a6a0c11a5e6ef0a5517f4 Parents: 3f81b29 Author: Willem Jiang <willem.ji...@gmail.com> Authored: Thu Dec 18 12:23:36 2014 +0800 Committer: Willem Jiang <willem.ji...@gmail.com> Committed: Thu Dec 18 15:19:41 2014 +0800 ---------------------------------------------------------------------- .../camel/processor/CamelInternalProcessor.java | 7 ++- .../HttpClientRouteExampleSpringTest.java | 48 +++++++++++++++++ .../component/servlet/example-camelContext.xml | 52 ++++++++++++++++++ .../camel/component/servlet/web-example.xml | 57 ++++++++++++++++++++ 4 files changed, 163 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/b3237601/camel-core/src/main/java/org/apache/camel/processor/CamelInternalProcessor.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/processor/CamelInternalProcessor.java b/camel-core/src/main/java/org/apache/camel/processor/CamelInternalProcessor.java index 7ebd726..adc4ce8 100644 --- a/camel-core/src/main/java/org/apache/camel/processor/CamelInternalProcessor.java +++ b/camel-core/src/main/java/org/apache/camel/processor/CamelInternalProcessor.java @@ -726,7 +726,12 @@ public class CamelInternalProcessor extends DelegateAsyncProcessor { @Override public void after(Exchange exchange, StreamCache sc) throws Exception { - Object body = exchange.getIn().getBody(); + Object body = null; + if (exchange.hasOut()) { + body = exchange.getOut().getBody(); + } else { + body = exchange.getIn().getBody(); + } if (body != null && body instanceof StreamCache) { // reset so the cache is ready to be reused after processing ((StreamCache) body).reset(); http://git-wip-us.apache.org/repos/asf/camel/blob/b3237601/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteExampleSpringTest.java ---------------------------------------------------------------------- diff --git a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteExampleSpringTest.java b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteExampleSpringTest.java new file mode 100644 index 0000000..7530776 --- /dev/null +++ b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteExampleSpringTest.java @@ -0,0 +1,48 @@ +/** + * 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.component.servlet; + +import com.meterware.httpunit.GetMethodWebRequest; +import com.meterware.httpunit.WebRequest; +import com.meterware.httpunit.WebResponse; +import com.meterware.servletunit.ServletUnitClient; +import org.junit.Before; +import org.junit.Test; + +public class HttpClientRouteExampleSpringTest extends ServletCamelRouterTestSupport { + @Test + public void testHttpRestricMethod() throws Exception { + + ServletUnitClient client = newClient(); + // Send a web get method request + WebRequest req = new GetMethodWebRequest(CONTEXT_URL + "/services/hello"); + WebResponse response = client.getResponse(req); + + System.out.println(response.getText()); + } + + @Before + public void setUp() throws Exception { + startCamelContext = false; + super.setUp(); + } + + protected String getConfiguration() { + return "/org/apache/camel/component/servlet/web-example.xml"; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/b3237601/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/example-camelContext.xml ---------------------------------------------------------------------- diff --git a/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/example-camelContext.xml b/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/example-camelContext.xml new file mode 100644 index 0000000..13637de --- /dev/null +++ b/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/example-camelContext.xml @@ -0,0 +1,52 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:camel="http://camel.apache.org/schema/spring" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd + "> + + <camelContext id="camel" streamCache="true" xmlns="http://camel.apache.org/schema/spring" > + <route id="helloRoute"> + <!-- incoming requests from the servlet is routed --> + <from uri="servlet:///hello"/> + <choice> + <when> + <!-- is there a header with the key name? --> + <header>name</header> + <!-- yes so return back a message to the user --> + <transform> + <simple>Hello ${header.name} how are you?</simple> + </transform> + </when> + <otherwise> + <!-- if no name parameter then output a syntax to the user --> + <transform> + <constant>Add a name parameter to uri, eg ?name=foo</constant> + </transform> + </otherwise> + </choice> + </route> + </camelContext> + + + + +</beans> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/b3237601/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web-example.xml ---------------------------------------------------------------------- diff --git a/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web-example.xml b/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web-example.xml new file mode 100644 index 0000000..c39b313 --- /dev/null +++ b/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web-example.xml @@ -0,0 +1,57 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> + +<!DOCTYPE web-app + PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" + "http://java.sun.com/dtd/web-app_2_3.dtd"> + +<!-- + 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. +--> +<!-- START SNIPPET: web --> +<web-app> + + <!-- tell Spring where it should load the XML file --> + <context-param> + <param-name>contextConfigLocation</param-name> + <param-value>classpath:org/apache/camel/component/servlet/example-camelContext.xml</param-value> + </context-param> + + <!-- spring context listener which loads the XML file --> + <listener> + <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> + </listener> + + <!-- Camel Servlet --> + <servlet> + <servlet-name>CamelServlet</servlet-name> + <display-name>Camel Http Transport Servlet</display-name> + <servlet-class>org.apache.camel.component.servlet.CamelHttpTransportServlet</servlet-class> + <init-param> + <param-name>matchOnUriPrefix</param-name> + <param-value>true</param-value> + </init-param> + </servlet> + + <!-- Camel Servlet mappings --> + <servlet-mapping> + <servlet-name>CamelServlet</servlet-name> + <url-pattern>/services/*</url-pattern> + </servlet-mapping> + +</web-app> +<!-- END SNIPPET: web --> \ No newline at end of file