Author: wtam
Date: Wed Feb  4 04:16:47 2009
New Revision: 740599

URL: http://svn.apache.org/viewvc?rev=740599&view=rev
Log:
Merged revisions 740596 via svnmerge from 
https://svn.apache.org/repos/asf/camel/trunk

........
  r740596 | wtam | 2009-02-03 23:06:28 -0500 (Tue, 03 Feb 2009) | 1 line
  
  [CAMEL-1312]  Restlet component should allow response content type and return 
code to be set
........

Added:
    
camel/branches/camel-1.x/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletResponseTest.java
      - copied unchanged from r740596, 
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletResponseTest.java
Modified:
    camel/branches/camel-1.x/   (props changed)
    
camel/branches/camel-1.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
    
camel/branches/camel-1.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConstants.java
    
camel/branches/camel-1.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/converter/RestletConverter.java
    
camel/branches/camel-1.x/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletPostContentTest.java

Propchange: camel/branches/camel-1.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Feb  4 04:16:47 2009
@@ -1 +1 @@
-/camel/trunk:739733,739904,740251,740295,740306
+/camel/trunk:739733,739904,740251,740295,740306,740596

Propchange: camel/branches/camel-1.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: 
camel/branches/camel-1.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java?rev=740599&r1=740598&r2=740599&view=diff
==============================================================================
--- 
camel/branches/camel-1.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
 (original)
+++ 
camel/branches/camel-1.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
 Wed Feb  4 04:16:47 2009
@@ -23,6 +23,7 @@
 
 import org.apache.camel.Exchange;
 import org.apache.camel.HeaderFilterStrategyAware;
+import org.apache.camel.Message;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.converter.jaxp.StringSource;
 import org.apache.camel.spi.HeaderFilterStrategy;
@@ -34,6 +35,7 @@
 import org.restlet.data.MediaType;
 import org.restlet.data.Request;
 import org.restlet.data.Response;
+import org.restlet.data.Status;
 
 /**
  * Default Restlet binding implementation
@@ -68,6 +70,10 @@
             }
         }
         
+        if (!request.isEntityAvailable()) {
+            return;
+        }
+        
         Form form = new Form(request.getEntity());
         if (form != null) {
             for (Map.Entry<String, String> entry : 
form.getValuesMap().entrySet()) {
@@ -152,15 +158,27 @@
      */
     public void populateRestletResponseFromExchange(Exchange exchange,
             Response response) {
-        Object body = exchange.getOut().getBody();
-        MediaType mediaType = MediaType.TEXT_PLAIN;
-        if (body instanceof String) {
+        
+        // get content type
+        Message out = exchange.getOut();
+        MediaType mediaType = out.getHeader(RestletConstants.MEDIA_TYPE, 
MediaType.class);
+        if (mediaType == null) {
+            Object body = out.getBody();
             mediaType = MediaType.TEXT_PLAIN;
-        } else if (body instanceof StringSource || body instanceof DOMSource) {
-            mediaType = MediaType.TEXT_XML;
+            if (body instanceof String) {
+                mediaType = MediaType.TEXT_PLAIN;
+            } else if (body instanceof StringSource || body instanceof 
DOMSource) {
+                mediaType = MediaType.TEXT_XML;
+            }
         }
                 
-        for (Map.Entry<String, Object> entry : 
exchange.getOut().getHeaders().entrySet()) {
+        // get response code
+        Integer responseCode = out.getHeader(RestletConstants.RESPONSE_CODE, 
Integer.class);
+        if (responseCode != null) {
+            response.setStatus(Status.valueOf(responseCode));
+        }
+
+        for (Map.Entry<String, Object> entry : out.getHeaders().entrySet()) {
             if 
(!headerFilterStrategy.applyFilterToCamelHeaders(entry.getKey(), 
                     entry.getValue())) {
                 response.getAttributes().put(entry.getKey(), entry.getValue());
@@ -171,7 +189,7 @@
             }
         }
         
-        String text = exchange.getOut().getBody(String.class);
+        String text = out.getBody(String.class);
         if (LOG.isDebugEnabled()) {
             LOG.debug("Populate Restlet response from exchange body: " + text);
         }

Modified: 
camel/branches/camel-1.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConstants.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConstants.java?rev=740599&r1=740598&r2=740599&view=diff
==============================================================================
--- 
camel/branches/camel-1.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConstants.java
 (original)
+++ 
camel/branches/camel-1.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConstants.java
 Wed Feb  4 04:16:47 2009
@@ -25,6 +25,8 @@
     
     public static final String LOGIN = "org.apache.camel.restlet.auth.login";
     public static final String PASSWORD = 
"org.apache.camel.restlet.auth.password";
+    public static final String MEDIA_TYPE = 
"org.apache.camel.restlet.mediaType";
+    public static final String RESPONSE_CODE = 
"org.apache.camel.restlet.responseCode";
 
     private RestletConstants() {
     }

Modified: 
camel/branches/camel-1.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/converter/RestletConverter.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/converter/RestletConverter.java?rev=740599&r1=740598&r2=740599&view=diff
==============================================================================
--- 
camel/branches/camel-1.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/converter/RestletConverter.java
 (original)
+++ 
camel/branches/camel-1.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/converter/RestletConverter.java
 Wed Feb  4 04:16:47 2009
@@ -17,6 +17,7 @@
 package org.apache.camel.component.restlet.converter;
 
 import org.apache.camel.Converter;
+import org.restlet.data.MediaType;
 import org.restlet.data.Method;
 
 /**
@@ -30,5 +31,10 @@
     public Method toMethod(String name) {
         return Method.valueOf(name.toUpperCase());
     }
+    
+    @Converter
+    public MediaType toMediaType(String name) {
+        return MediaType.valueOf(name);
+    }
 
 }

Modified: 
camel/branches/camel-1.x/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletPostContentTest.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletPostContentTest.java?rev=740599&r1=740598&r2=740599&view=diff
==============================================================================
--- 
camel/branches/camel-1.x/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletPostContentTest.java
 (original)
+++ 
camel/branches/camel-1.x/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletPostContentTest.java
 Wed Feb  4 04:16:47 2009
@@ -58,10 +58,14 @@
     
     public void testPostBody() throws Exception {
         HttpMethod method = new 
PostMethod("http://localhost:9080/users/homer";);
-        RequestEntity requestEntity = new StringRequestEntity(MSG_BODY, null, 
null);
-        ((EntityEnclosingMethod)method).setRequestEntity(requestEntity);
-        HttpClient client = new HttpClient();
-        assertEquals(200, client.executeMethod(method));
+        try {
+            RequestEntity requestEntity = new StringRequestEntity(MSG_BODY, 
null, null);
+            ((EntityEnclosingMethod)method).setRequestEntity(requestEntity);
+            HttpClient client = new HttpClient();
+            assertEquals(200, client.executeMethod(method));
+        } finally {
+            method.releaseConnection();
+        }
 
     }
 }


Reply via email to