You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by sandp <sa...@gmail.com> on 2015/02/02 20:28:41 UTC

Camel-Restlet 2.14.0 Accept Header with multiple values

I came across a situation where the client was passing multiple values for
Accept header,

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,,text/xml

The restlet service serves  content with mime type of *text/xml * only. When
a request 
for a resource is made, a camel processor pre-processes the request, checks
for Accept 
header, If the Accept="text/xml", the resource is served, else 406 is
returned. This works 
just fine as long the client passed a single value for Accept header.

Now the scenario is where the client passed multiple values  e.g.  
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,text/xml


*The Question is:*
*1. *org.restlet.util.Series.getValuesArray(String name, boolean ignoreCase)
does not return an 
array of all the values associated to the given parameter name. It returns a 
single String e.g  "Accept:
text/html,application/xhtml,application/xml,text/xml"


*2* Is there another way to check for the multi value header?


* HEADERS from Camel Message *
org.restlet.http.headers=[[Content-type: text/xml], [Host: localhost:8090],
[Accept-
encoding: gzip,deflate], [Connection: keep-alive], [Accept-language:
en-US,en;q=0.5], 
[User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101
Firefox/35.0], 
[Accept: text/html,application/xhtml,application/xml,text/xml]]


On the server, the Accept header is processed as below:

public void process(Exchange exchange) throws Exception {
Map<String,Object> headerMap= message.getHeaders();
Series<NamedValue&lt;String>> series = (Series<NamedValue&lt;String>>)
headerMap.get

(HeaderConstants.ATTRIBUTE_HEADERS);	
if((checkHeaderAndValue(series, HeaderConstants.HEADER_ACCEPT)==false)){
				throw new RestletConsumerOperationException

(restUtil.buildErrorResponse(Status.CLIENT_ERROR_NOT_ACCEPTABLE));
			}


*  *
private boolean checkHeaderAndValue(Series<NamedValue&lt;String>> series,
String key){
		String[] headerValues =series.getValuesArray(key,true);	//*This returns a 

single string with all the values in it. As per the definition, it should
return an array of 

all the values associated to the given parameter name.

 *
		if(null!=headerValues && headerValues.length>0){
			 List<String> headerValueList = Arrays.asList(headerValues); 
			 for(String headerValue : headerValueList){
				//Some logic
                              return true;
				 }				 
			 }
		}		
		return false;
	}





--
View this message in context: http://camel.465427.n5.nabble.com/Camel-Restlet-2-14-0-Accept-Header-with-multiple-values-tp5762339.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Camel-Restlet 2.14.0 Accept Header with multiple values

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

Have you asked the restlet guys if there is some issue in restlet itself?
If you think that method should return an array or something?

Its a bit hard to follow what you see as the problem in Camel / Restlet

On Mon, Feb 2, 2015 at 8:28 PM, sandp <sa...@gmail.com> wrote:
> I came across a situation where the client was passing multiple values for
> Accept header,
>
> Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,,text/xml
>
> The restlet service serves  content with mime type of *text/xml * only. When
> a request
> for a resource is made, a camel processor pre-processes the request, checks
> for Accept
> header, If the Accept="text/xml", the resource is served, else 406 is
> returned. This works
> just fine as long the client passed a single value for Accept header.
>
> Now the scenario is where the client passed multiple values  e.g.
> Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,text/xml
>
>
> *The Question is:*
> *1. *org.restlet.util.Series.getValuesArray(String name, boolean ignoreCase)
> does not return an
> array of all the values associated to the given parameter name. It returns a
> single String e.g  "Accept:
> text/html,application/xhtml,application/xml,text/xml"
>
>
> *2* Is there another way to check for the multi value header?
>
>
> * HEADERS from Camel Message *
> org.restlet.http.headers=[[Content-type: text/xml], [Host: localhost:8090],
> [Accept-
> encoding: gzip,deflate], [Connection: keep-alive], [Accept-language:
> en-US,en;q=0.5],
> [User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101
> Firefox/35.0],
> [Accept: text/html,application/xhtml,application/xml,text/xml]]
>
>
> On the server, the Accept header is processed as below:
>
> public void process(Exchange exchange) throws Exception {
> Map<String,Object> headerMap= message.getHeaders();
> Series<NamedValue&lt;String>> series = (Series<NamedValue&lt;String>>)
> headerMap.get
>
> (HeaderConstants.ATTRIBUTE_HEADERS);
> if((checkHeaderAndValue(series, HeaderConstants.HEADER_ACCEPT)==false)){
>                                 throw new RestletConsumerOperationException
>
> (restUtil.buildErrorResponse(Status.CLIENT_ERROR_NOT_ACCEPTABLE));
>                         }
>
>
> *  *
> private boolean checkHeaderAndValue(Series<NamedValue&lt;String>> series,
> String key){
>                 String[] headerValues =series.getValuesArray(key,true); //*This returns a
>
> single string with all the values in it. As per the definition, it should
> return an array of
>
> all the values associated to the given parameter name.
>
>  *
>                 if(null!=headerValues && headerValues.length>0){
>                          List<String> headerValueList = Arrays.asList(headerValues);
>                          for(String headerValue : headerValueList){
>                                 //Some logic
>                               return true;
>                                  }
>                          }
>                 }
>                 return false;
>         }
>
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Camel-Restlet-2-14-0-Accept-Header-with-multiple-values-tp5762339.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
hawtio: http://hawt.io/
fabric8: http://fabric8.io/