camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1188859 - in /camel/trunk/components/camel-web/src/main: java/org/apache/camel/web/resources/EndpointResource.java webapp/WEB-INF/org/apache/camel/web/resources/EndpointResource.send.ssp
Date Tue, 25 Oct 2011 19:08:13 GMT
Author: davsclaus
Date: Tue Oct 25 19:08:12 2011
New Revision: 1188859

URL: http://svn.apache.org/viewvc?rev=1188859&view=rev
Log:
CAMEL-4003: Added form to enter headers when sending to endpoint in camel-web. Thanks to Bilgin
for the patch.

Modified:
    camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/resources/EndpointResource.java
    camel/trunk/components/camel-web/src/main/webapp/WEB-INF/org/apache/camel/web/resources/EndpointResource.send.ssp

Modified: camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/resources/EndpointResource.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/resources/EndpointResource.java?rev=1188859&r1=1188858&r2=1188859&view=diff
==============================================================================
--- camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/resources/EndpointResource.java
(original)
+++ camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/resources/EndpointResource.java
Tue Oct 25 19:08:12 2011
@@ -18,9 +18,9 @@ package org.apache.camel.web.resources;
 
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 import javax.ws.rs.Consumes;
 import javax.ws.rs.POST;
 import javax.ws.rs.Path;
@@ -32,6 +32,7 @@ import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response;
 
 import com.sun.jersey.api.representation.Form;
+
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
@@ -42,8 +43,6 @@ import org.apache.camel.web.model.Endpoi
 
 /**
  * A Camel <a href="http://camel.apache.org/endpoint.html">Endpoint</a>
- *
- * @version 
  */
 public class EndpointResource extends CamelChildResourceSupport {
     private final String key;
@@ -102,7 +101,8 @@ public class EndpointResource extends Ca
     @POST
     @Consumes({MediaType.TEXT_PLAIN, MediaType.TEXT_HTML, MediaType.TEXT_XML, MediaType.APPLICATION_XML})
     public Response postMessage(@Context HttpHeaders headers, final String body) throws URISyntaxException
{
-        sendMessage(headers, body);
+        Map<String, Object> messageHeaders = multivalueMapToMap(headers.getRequestHeaders());
+        sendMessage(messageHeaders, body);
         return Response.ok().build();
     }
 
@@ -113,36 +113,58 @@ public class EndpointResource extends Ca
     @POST
     @Consumes("application/x-www-form-urlencoded")
     public Response postMessageForm(@Context HttpHeaders headers, Form formData) throws URISyntaxException
{
-        String body = formData.getFirst("body", String.class);
-        sendMessage(headers, body);
+        String body = getBodyData(formData);
+
+        Map<String, Object> messageHeaders = multivalueMapToMap(headers.getRequestHeaders());
+        messageHeaders.putAll(getHeadersData(formData));
+
+        sendMessage(messageHeaders, body);
         return Response.seeOther(new URI(getHref())).build();
     }
 
-    protected void sendMessage(final HttpHeaders headers, final String body) {
+    private Map<String, Object> getHeadersData(Form formData) {
+        Map<String, Object> headers = new HashMap<String, Object>();
+        Map<String, Object> formMap = multivalueMapToMap(formData);
+        for (Map.Entry<String, Object> entry : formMap.entrySet()) {
+            if (entry.getKey().startsWith("header_")) {
+                headers.put((String) entry.getValue(), getHeaderValueForKey(entry.getKey(),
formMap));
+            }
+        }
+        return headers;
+    }
+
+    private Object getHeaderValueForKey(String key, Map<String, Object> headers) {
+        return headers.get("value_" + key);
+    }
+
+    private String getBodyData(Form formData) {
+        return formData.getFirst("body", String.class);
+    }
+
+    private Map<String, Object> multivalueMapToMap(MultivaluedMap<String, String>
multivaluedMap) {
+        Map<String, Object> resultMap = new HashMap<String, Object>();
+        if (multivaluedMap != null) {
+            for (Map.Entry<String, List<String>> entry : multivaluedMap.entrySet())
{
+                List<String> values = entry.getValue();
+                int size = values.size();
+                if (size == 1) {
+                    resultMap.put(entry.getKey(), entry.getValue().get(0));
+                } else if (size > 0) {
+                    resultMap.put(entry.getKey(), entry.getValue());
+                }
+            }
+        }
+        return resultMap;
+    }
+
+    protected void sendMessage(final Map<String, Object> headers, final String body)
{
         getTemplate().send(endpoint, new Processor() {
             public void process(Exchange exchange) throws Exception {
                 Message in = exchange.getIn();
                 in.setBody(body);
-
-                // lets pass in all the HTTP headers
-                if (headers != null) {
-                    MultivaluedMap<String, String> requestHeaders = headers.getRequestHeaders();
-
-                    Set<Map.Entry<String, List<String>>> entries = requestHeaders.entrySet();
-                    for (Map.Entry<String, List<String>> entry : entries) {
-                        String key = entry.getKey();
-                        List<String> values = entry.getValue();
-                        int size = values.size();
-                        if (size == 1) {
-                            in.setHeader(key, values.get(0));
-                        } else if (size > 0) {
-                            in.setHeader(key, values);
-                        }
-                    }
-                }
+                in.setHeaders(headers);
             }
         });
     }
 
-
 }

Modified: camel/trunk/components/camel-web/src/main/webapp/WEB-INF/org/apache/camel/web/resources/EndpointResource.send.ssp
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-web/src/main/webapp/WEB-INF/org/apache/camel/web/resources/EndpointResource.send.ssp?rev=1188859&r1=1188858&r2=1188859&view=diff
==============================================================================
--- camel/trunk/components/camel-web/src/main/webapp/WEB-INF/org/apache/camel/web/resources/EndpointResource.send.ssp
(original)
+++ camel/trunk/components/camel-web/src/main/webapp/WEB-INF/org/apache/camel/web/resources/EndpointResource.send.ssp
Tue Oct 25 19:08:12 2011
@@ -6,7 +6,16 @@
 <h1>Endpoint: ${it.getUri}</h1>
 
 <form action='${uri(it.getHref)}' method="post" name="sendMessage">
-  <input type="submit" value="Send"> <input type="reset"> <br>
+  <input type="submit" value="Send"> <input type="reset"> <br/>
+
+  <br/>Headers(name : value)<br/>
+  <input type="text" name="header_1"> : <input type="text" name="value_header_1"><br/>
+  <input type="text" name="header_2"> : <input type="text" name="value_header_2"><br/>
+  <input type="text" name="header_3"> : <input type="text" name="value_header_3"><br/>
+  <input type="text" name="header_4"> : <input type="text" name="value_header_4"><br/>
+  <input type="text" name="header_5"> : <input type="text" name="value_header_5"><br/>
+
+  <br/>Body<br/>
   <textarea name="body" rows="30" cols="80"></textarea>
 </form>
 



Mime
View raw message