You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by zr...@apache.org on 2017/03/16 12:19:06 UTC
camel git commit: CAMEL-11026 RestletProducer should allow multip...
Repository: camel
Updated Branches:
refs/heads/master ab6fb50ab -> 5846f22c3
CAMEL-11026 RestletProducer should allow multip...
...le values in HTTP Accept header
This adds support for converting from `String` to `MediaType[]` and adds
support for the multiple `MediaType`s for the `Accept` HTTP header.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/5846f22c
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/5846f22c
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/5846f22c
Branch: refs/heads/master
Commit: 5846f22c3229030afdac6407767e4853f7e5e01d
Parents: ab6fb50
Author: Zoran Regvart <zr...@apache.org>
Authored: Thu Mar 16 13:18:27 2017 +0100
Committer: Zoran Regvart <zr...@apache.org>
Committed: Thu Mar 16 13:18:27 2017 +0100
----------------------------------------------------------------------
.../restlet/DefaultRestletBinding.java | 17 +++++---
.../restlet/converter/RestletConverter.java | 15 +++++++
.../restlet/converter/RestletConverterTest.java | 41 ++++++++++++++++++++
3 files changed, 67 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/5846f22c/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
----------------------------------------------------------------------
diff --git a/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java b/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
index be75a8e..c4f62b1 100644
--- a/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
+++ b/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
@@ -55,6 +55,7 @@ import org.restlet.data.CacheDirective;
import org.restlet.data.ChallengeResponse;
import org.restlet.data.ChallengeScheme;
import org.restlet.data.CharacterSet;
+import org.restlet.data.ClientInfo;
import org.restlet.data.Form;
import org.restlet.data.Header;
import org.restlet.data.MediaType;
@@ -278,15 +279,19 @@ public class DefaultRestletBinding implements RestletBinding, HeaderFilterStrate
// accept
String accept = exchange.getIn().getHeader("Accept", String.class);
+ final ClientInfo clientInfo = request.getClientInfo();
+ final List<Preference<MediaType>> acceptedMediaTypesList = clientInfo.getAcceptedMediaTypes();
if (accept != null) {
- MediaType acceptedMediaType = exchange.getContext().getTypeConverter().tryConvertTo(MediaType.class, exchange, accept);
- if (acceptedMediaType != null) {
- request.getClientInfo().getAcceptedMediaTypes().add(new Preference<MediaType>(acceptedMediaType));
+ final MediaType[] acceptedMediaTypes = exchange.getContext().getTypeConverter().tryConvertTo(MediaType[].class, exchange, accept);
+ for (final MediaType acceptedMediaType : acceptedMediaTypes) {
+ acceptedMediaTypesList.add(new Preference<MediaType>(acceptedMediaType));
}
}
- MediaType acceptedMediaType = exchange.getIn().getHeader(Exchange.ACCEPT_CONTENT_TYPE, MediaType.class);
- if (acceptedMediaType != null) {
- request.getClientInfo().getAcceptedMediaTypes().add(new Preference<MediaType>(acceptedMediaType));
+ final MediaType[] acceptedMediaTypes = exchange.getIn().getHeader(Exchange.ACCEPT_CONTENT_TYPE, MediaType[].class);
+ if (acceptedMediaTypes != null) {
+ for (final MediaType acceptedMediaType : acceptedMediaTypes) {
+ acceptedMediaTypesList.add(new Preference<MediaType>(acceptedMediaType));
+ }
}
}
http://git-wip-us.apache.org/repos/asf/camel/blob/5846f22c/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/converter/RestletConverter.java
----------------------------------------------------------------------
diff --git a/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/converter/RestletConverter.java b/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/converter/RestletConverter.java
index 3fab0cc..aa148ab 100644
--- a/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/converter/RestletConverter.java
+++ b/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/converter/RestletConverter.java
@@ -18,8 +18,10 @@ package org.apache.camel.component.restlet.converter;
import java.util.ArrayList;
import java.util.List;
+import java.util.regex.Pattern;
import org.apache.camel.Converter;
+import org.apache.camel.util.StringHelper;
import org.restlet.data.MediaType;
import org.restlet.data.Method;
@@ -54,4 +56,17 @@ public final class RestletConverter {
return MediaType.valueOf(name);
}
+ @Converter
+ public static MediaType[] toMediaTypes(final String name) {
+ final String[] strings = name.split(",");
+ final List<MediaType> answer = new ArrayList<>(strings.length);
+ for (int i = 0; i < strings.length; i++) {
+ final MediaType mediaType = toMediaType(strings[i]);
+ if (mediaType != null) {
+ answer.add(mediaType);
+ }
+ }
+
+ return answer.toArray(new MediaType[answer.size()]);
+ }
}
http://git-wip-us.apache.org/repos/asf/camel/blob/5846f22c/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/converter/RestletConverterTest.java
----------------------------------------------------------------------
diff --git a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/converter/RestletConverterTest.java b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/converter/RestletConverterTest.java
new file mode 100644
index 0000000..9e9ea1b
--- /dev/null
+++ b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/converter/RestletConverterTest.java
@@ -0,0 +1,41 @@
+/**
+ * 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.converter;
+
+import org.junit.Test;
+import org.restlet.data.MediaType;
+
+import static org.junit.Assert.assertEquals;
+
+public class RestletConverterTest {
+
+ @Test
+ public void shouldConvertMediaTypes() {
+ final MediaType[] mediaTypes = RestletConverter.toMediaTypes("a/b, c/d");
+
+ assertEquals("Expecting two parsed media types", 2, mediaTypes.length);
+ assertEquals("Expecting first to be a/b", MediaType.valueOf("a/b"), mediaTypes[0]);
+ assertEquals("Expecting second to be c/d", MediaType.valueOf("c/d"), mediaTypes[1]);
+ }
+
+ @Test
+ public void shouldConvertNoMediaTypes() {
+ final MediaType[] mediaTypes = RestletConverter.toMediaTypes("");
+
+ assertEquals("Expecting no parsed media types", 0, mediaTypes.length);
+ }
+}