You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by wt...@apache.org on 2009/02/04 05:06:29 UTC
svn commit: r740596 - in /camel/trunk/components/camel-restlet/src:
main/java/org/apache/camel/component/restlet/
main/java/org/apache/camel/component/restlet/converter/
test/java/org/apache/camel/component/restlet/
Author: wtam
Date: Wed Feb 4 04:06:28 2009
New Revision: 740596
URL: http://svn.apache.org/viewvc?rev=740596&view=rev
Log:
[CAMEL-1312] Restlet component should allow response content type and return code to be set
Added:
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletResponseTest.java (with props)
Modified:
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConstants.java
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/converter/RestletConverter.java
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletPostContentTest.java
Modified: camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java?rev=740596&r1=740595&r2=740596&view=diff
==============================================================================
--- camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java (original)
+++ camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java Wed Feb 4 04:06:28 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/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConstants.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConstants.java?rev=740596&r1=740595&r2=740596&view=diff
==============================================================================
--- camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConstants.java (original)
+++ camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConstants.java Wed Feb 4 04:06:28 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/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/converter/RestletConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/converter/RestletConverter.java?rev=740596&r1=740595&r2=740596&view=diff
==============================================================================
--- camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/converter/RestletConverter.java (original)
+++ camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/converter/RestletConverter.java Wed Feb 4 04:06:28 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/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletPostContentTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletPostContentTest.java?rev=740596&r1=740595&r2=740596&view=diff
==============================================================================
--- camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletPostContentTest.java (original)
+++ camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletPostContentTest.java Wed Feb 4 04:06:28 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();
+ }
}
}
Added: camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletResponseTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletResponseTest.java?rev=740596&view=auto
==============================================================================
--- camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletResponseTest.java (added)
+++ camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletResponseTest.java Wed Feb 4 04:06:28 2009
@@ -0,0 +1,61 @@
+/**
+ * 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.restlet;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.methods.PostMethod;
+
+/**
+ *
+ * @version $Revision$
+ */
+public class RestletResponseTest extends ContextTestSupport {
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("restlet:http://localhost:9080/users/{username}?restletMethod=POST").process(new Processor() {
+ public void process(Exchange exchange) throws Exception {
+ exchange.getOut().setHeader(RestletConstants.RESPONSE_CODE, "417");
+ exchange.getOut().setHeader(RestletConstants.MEDIA_TYPE, "application/JSON");
+ }
+ });
+ }
+ };
+ }
+
+ public void testCustomResponse() throws Exception {
+ HttpMethod method = new PostMethod("http://localhost:9080/users/homer");
+ try {
+ HttpClient client = new HttpClient();
+ assertEquals(417, client.executeMethod(method));
+ assertTrue(method.getResponseHeader("Content-Type").getValue()
+ .startsWith("application/JSON"));
+ } finally {
+ method.releaseConnection();
+ }
+
+ }
+}
Propchange: camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletResponseTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletResponseTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date