Skip to content

Commit

Permalink
CAMEL-11026 RestletProducer should allow multip...
Browse files Browse the repository at this point in the history
...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.
  • Loading branch information
zregvart committed Mar 16, 2017
1 parent ab6fb50 commit 5846f22
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
Expand Up @@ -55,6 +55,7 @@
import org.restlet.data.ChallengeResponse; import org.restlet.data.ChallengeResponse;
import org.restlet.data.ChallengeScheme; import org.restlet.data.ChallengeScheme;
import org.restlet.data.CharacterSet; import org.restlet.data.CharacterSet;
import org.restlet.data.ClientInfo;
import org.restlet.data.Form; import org.restlet.data.Form;
import org.restlet.data.Header; import org.restlet.data.Header;
import org.restlet.data.MediaType; import org.restlet.data.MediaType;
Expand Down Expand Up @@ -278,15 +279,19 @@ public void populateRestletRequestFromExchange(Request request, Exchange exchang


// accept // accept
String accept = exchange.getIn().getHeader("Accept", String.class); String accept = exchange.getIn().getHeader("Accept", String.class);
final ClientInfo clientInfo = request.getClientInfo();
final List<Preference<MediaType>> acceptedMediaTypesList = clientInfo.getAcceptedMediaTypes();
if (accept != null) { if (accept != null) {
MediaType acceptedMediaType = exchange.getContext().getTypeConverter().tryConvertTo(MediaType.class, exchange, accept); final MediaType[] acceptedMediaTypes = exchange.getContext().getTypeConverter().tryConvertTo(MediaType[].class, exchange, accept);
if (acceptedMediaType != null) { for (final MediaType acceptedMediaType : acceptedMediaTypes) {
request.getClientInfo().getAcceptedMediaTypes().add(new Preference<MediaType>(acceptedMediaType)); acceptedMediaTypesList.add(new Preference<MediaType>(acceptedMediaType));
} }
} }
MediaType acceptedMediaType = exchange.getIn().getHeader(Exchange.ACCEPT_CONTENT_TYPE, MediaType.class); final MediaType[] acceptedMediaTypes = exchange.getIn().getHeader(Exchange.ACCEPT_CONTENT_TYPE, MediaType[].class);
if (acceptedMediaType != null) { if (acceptedMediaTypes != null) {
request.getClientInfo().getAcceptedMediaTypes().add(new Preference<MediaType>(acceptedMediaType)); for (final MediaType acceptedMediaType : acceptedMediaTypes) {
acceptedMediaTypesList.add(new Preference<MediaType>(acceptedMediaType));
}
} }


} }
Expand Down
Expand Up @@ -18,8 +18,10 @@


import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Pattern;


import org.apache.camel.Converter; import org.apache.camel.Converter;
import org.apache.camel.util.StringHelper;
import org.restlet.data.MediaType; import org.restlet.data.MediaType;
import org.restlet.data.Method; import org.restlet.data.Method;


Expand Down Expand Up @@ -54,4 +56,17 @@ public static MediaType toMediaType(String name) {
return MediaType.valueOf(name); 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()]);
}
} }
@@ -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);
}
}

0 comments on commit 5846f22

Please sign in to comment.