You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2010/02/17 17:02:31 UTC
svn commit: r911034 - in /camel/trunk/components/camel-stringtemplate/src:
main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java
test/java/org/apache/camel/component/stringtemplate/StringTemplateOutHeaderTest.java
Author: davsclaus
Date: Wed Feb 17 16:02:30 2010
New Revision: 911034
URL: http://svn.apache.org/viewvc?rev=911034&view=rev
Log:
CAMEL-2478: Applied patch with thanks to Christian Mueller.
Added:
camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateOutHeaderTest.java (with props)
Modified:
camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java
Modified: camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java?rev=911034&r1=911033&r2=911034&view=diff
==============================================================================
--- camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java (original)
+++ camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java Wed Feb 17 16:02:30 2010
@@ -18,6 +18,7 @@
import java.io.StringWriter;
import java.util.Map;
+import java.util.Map.Entry;
import org.antlr.stringtemplate.AutoIndentWriter;
import org.antlr.stringtemplate.StringTemplate;
@@ -52,9 +53,10 @@
}
@Override
+ @SuppressWarnings("unchecked")
protected void onExchange(Exchange exchange) throws Exception {
StringWriter buffer = new StringWriter();
- Map variableMap = ExchangeHelper.createVariableMap(exchange);
+ Map<String, Object> variableMap = ExchangeHelper.createVariableMap(exchange);
// getResourceAsInputStream also considers the content cache
String text = exchange.getContext().getTypeConverter().mandatoryConvertTo(String.class, getResourceAsInputStream());
@@ -69,6 +71,9 @@
Message out = exchange.getOut();
out.setBody(buffer.toString());
out.setHeader(StringTemplateConstants.STRINGTEMPLATE_RESOURCE, getResource());
+ Map<String, Object> headers = (Map<String, Object>) variableMap.get("headers");
+ for (Entry<String, Object> entry : headers.entrySet()) {
+ out.setHeader(entry.getKey(), entry.getValue());
+ }
}
-
}
\ No newline at end of file
Added: camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateOutHeaderTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateOutHeaderTest.java?rev=911034&view=auto
==============================================================================
--- camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateOutHeaderTest.java (added)
+++ camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateOutHeaderTest.java Wed Feb 17 16:02:30 2010
@@ -0,0 +1,82 @@
+/**
+ * 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.stringtemplate;
+
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.InvalidPayloadException;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+/**
+ * @version $Revision$
+ */
+public class StringTemplateOutHeaderTest extends CamelTestSupport {
+
+ @Test
+ public void testReceivesFooResponse() throws Exception {
+ assertRespondsWith("cheese", "foo", "<hello>foo</hello>");
+ }
+
+ @Test
+ public void testReceivesBarResponse() throws Exception {
+ assertRespondsWith("cheese", "bar", "<hello>bar</hello>");
+ }
+
+ @Test
+ public void testRespectHeaderNamesUpperCase() throws Exception {
+ assertRespondsWith("Cheese", "bar", "<hello>bar</hello>");
+ }
+
+ @Test
+ public void testRespectHeaderNamesCamelCase() throws Exception {
+ assertRespondsWith("CheesE", "bar", "<hello>bar</hello>");
+ }
+
+ protected void assertRespondsWith(final String headerName, final String headerValue, String expectedBody) throws InvalidPayloadException {
+ Exchange response = template.request("direct:a", new Processor() {
+ public void process(Exchange exchange) throws Exception {
+ Message in = exchange.getIn();
+ in.setHeader(headerName, headerValue);
+ }
+ });
+ assertOutMessageBodyEquals(response, expectedBody);
+
+ Set<Map.Entry<String, Object>> entrySet = response.getOut().getHeaders().entrySet();
+ boolean keyFound = false;
+ for (Map.Entry<String, Object> entry : entrySet) {
+ if (entry.getKey().equals(headerName)) {
+ keyFound = true;
+ }
+ }
+ assertTrue("Header should been found", keyFound);
+ }
+
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ public void configure() {
+ from("direct:a").to("string-template:org/apache/camel/component/stringtemplate/template.tm");
+ }
+ };
+ }
+}
+
Propchange: camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateOutHeaderTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateOutHeaderTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date