[jboss-svn-commits] JBL Code SVN: r21339 - in labs/jbossesb/trunk/product/rosetta: tests/src/org/jboss/soa/esb/actions/routing and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Aug 4 10:41:26 EDT 2008


Author: kevin.conner at jboss.com
Date: 2008-08-04 10:41:25 -0400 (Mon, 04 Aug 2008)
New Revision: 21339

Added:
   labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/routing/http/
   labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/routing/http/HttpRouterUnitTest.java
Modified:
   labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/HttpRouter.java
Log:
Added header processing to HttpRouter: JBESB-1866

Modified: labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/HttpRouter.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/HttpRouter.java	2008-08-04 12:50:06 UTC (rev 21338)
+++ labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/routing/http/HttpRouter.java	2008-08-04 14:41:25 UTC (rev 21339)
@@ -39,6 +39,7 @@
 import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.util.ArrayList;
 import java.util.Properties;
 
 /**
@@ -61,7 +62,9 @@
     private ResponseType responseType;
     private String contentType;
     private ConfigTree[] requestHeaders;
-
+    
+    private final String[] mappedHeaderList ;
+    
     public HttpRouter(ConfigTree config) throws ConfigurationException {
         super(config);
         this.config = config;
@@ -76,9 +79,13 @@
         extractHttpClientProps(config);
         httpclient = HttpClientFactory.createHttpClient(httpClientProps);
         method = config.getRequiredAttribute("method");
+        
         responseType = ResponseType.valueOf(config.getAttribute("responseType", ResponseType.STRING.toString()));
-        methodFactory = HttpMethodFactory.Factory.getInstance(method, config, endpointUrl);
-        contentType = config.getAttribute("Content-Type", "text/xml;charset=UTF-8");
+        methodFactory = HttpMethodFactory.Factory.getInstance(method.toUpperCase(), config, endpointUrl);
+        contentType = config.getAttribute("Content-Type");
+
+        mappedHeaderList = extractMappedHeaderListConfig();
+        
         requestHeaders = config.getChildren("header");
     }
 
@@ -88,7 +95,7 @@
             method = methodFactory.getInstance(message);
 
             try {
-                setRequestHeaders(method);
+                setRequestHeaders(method, message);
                 
                 int responseCode = httpclient.executeMethod(method);
                 if(responseCode != HttpStatus.SC_OK) {
@@ -119,6 +126,25 @@
 
         return message;
     }
+    
+    private String[] extractMappedHeaderListConfig() throws ConfigurationException {
+        final String mappedHeaders = config.getAttribute("MappedHeaderList");
+        if (mappedHeaders != null) {
+            final String[] headerList = mappedHeaders.split(",");
+            final int numHeaders = headerList.length ;
+            final ArrayList<String> headers = new ArrayList<String>(numHeaders) ;
+            
+            for(int count = 0 ; count < numHeaders ; count++) {
+                final String header = headerList[count].trim() ;
+                if (header.length() > 0) {
+                    headers.add(header) ;
+                }
+            }
+            return (String[])headers.toArray(new String[headers.size()]) ;
+        } else {
+            return new String[0] ;
+        }
+    }
 
     private void attachResponseDetails(Message message, HttpMethodBase method, int responseCode) {
         HttpResponse response = new HttpResponse(responseCode);
@@ -134,9 +160,12 @@
         message.getBody().add(HttpResponse.RESPONSE_KEY, response);
     }
 
-    private void setRequestHeaders(HttpMethodBase method) {
-        method.setRequestHeader("Content-Type", contentType);
-
+    private void setRequestHeaders(HttpMethodBase method, Message message) {
+        //Try best to keep all the well-known HTTP headers that were sent from the client. 
+        setMappedHttpHeaders(method, message);
+        
+        //The setting of HTTP headers from config still takes precedence. So do not set 
+        //a HTTP header in config if you want to keep the value sent from the client.
         for (int i = 0; i < requestHeaders.length; i++) {
             ConfigTree header = requestHeaders[i];
             String name = header.getAttribute("name");
@@ -148,8 +177,42 @@
                 logger.error("null Http request header name/value: '" + name + "':'" + value + "'.");
             }
         }
+        if (contentType != null) {
+            method.setRequestHeader("Content-Type", contentType);
+        } else if (method.getRequestHeader("Content-Type") == null) {
+            method.setRequestHeader("Content-Type", "text/xml;charset=UTF-8") ;
+        }
     }
+    
+    private void setMappedHttpHeaders(HttpMethodBase method, Message message) {
+        for (String headerName : mappedHeaderList) {
+            final Object header = getHttpHeaders(message, headerName);
+            if (header != null) {
+                final String headerValue = header.toString() ;
+                if (headerValue.length() > 0) {
+                    method.setRequestHeader(headerName, headerValue);
+                }
+            }
+        }
+    }
+    
+    // HTTP header field names are case-insensitive
+    private Object getHttpHeaders(Message esbMessage, String headerName) {
+        
+        org.jboss.soa.esb.message.Properties ps = esbMessage.getProperties();
+        for(String name : ps.getNames()) {
+            if (name.equalsIgnoreCase(headerName)) {
+                return ps.getProperty(name);
+            }
+        }
+        
+        return null;
+    }
 
+    public String[] getMappedHeaderList() {
+        return mappedHeaderList;
+    }
+    
     public void route(Object object) throws ActionProcessingException {
         // Not used!
     }

Added: labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/routing/http/HttpRouterUnitTest.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/routing/http/HttpRouterUnitTest.java	                        (rev 0)
+++ labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/routing/http/HttpRouterUnitTest.java	2008-08-04 14:41:25 UTC (rev 21339)
@@ -0,0 +1,82 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.soa.esb.actions.routing.http;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.JUnit4TestAdapter;
+
+import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.junit.Before;
+import org.junit.Test;
+
+public class HttpRouterUnitTest
+{
+    @Before
+    public void setUp()
+    {
+    }
+
+    @Test
+    public void testRouter_EmptyMappedHeaderList() throws ConfigurationException
+    {    
+        ConfigTree tree = new ConfigTree("EmptyMappedHeaderList");
+        tree.setAttribute("endpointUrl", "http://foo.bar");
+        tree.setAttribute("method", "post");
+        HttpRouter router = new HttpRouter(tree);
+        String[] headerList = router.getMappedHeaderList();
+        assertEquals(0, headerList.length);
+    }
+
+    @Test
+    public void testRouter_ValidMappedHeaderList() throws ConfigurationException
+    {    
+        ConfigTree tree = new ConfigTree("ValidMappedHeaderList");
+        tree.setAttribute("endpointUrl", "http://foo.bar");
+        tree.setAttribute("method", "post");
+        tree.setAttribute("MappedHeaderList", "SOAPAction, Content-Type, Accept");
+        HttpRouter router = new HttpRouter(tree);
+        String[] headerList = router.getMappedHeaderList();
+        assertEquals(3, headerList.length);
+
+        List<String> headers = new ArrayList<String>();
+        headers.add("SOAPAction");
+        headers.add("Content-Type");        
+        headers.add("Accept");    
+        for(String h : headerList) {
+            if(headers.contains(h)) {
+                headers.remove(h);
+            }
+        }
+
+        assertEquals(0, headers.size());
+    }
+    
+    public static junit.framework.Test suite() {
+        return new JUnit4TestAdapter(HttpRouterUnitTest.class);
+    }
+}


Property changes on: labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/routing/http/HttpRouterUnitTest.java
___________________________________________________________________
Name: svn:keywords
   + Rev Date
Name: svn:eol-style
   + native




More information about the jboss-svn-commits mailing list