Quantcast

java.lang.IllegalArgumentException: Error parsing media type

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

java.lang.IllegalArgumentException: Error parsing media type

Michael Andrews-3
Hi,

I am trying to associate the MIME type "test/plain;javascript" with a JSON Provider.  From the many examples I have gleaned from the mailing list and docs, I tried this:

ClientConfig config = new DefaultClientConfig(); config.getClasses().add(JSONRootElementProvider.class);        Client client = Client.create(config);         WebResource webResource = client.resource(uri); //webResource.accept(MediaType.APPLICATION_JSON).type("text/plain;javascript"); webResource.accept("text/plain;javascript");         String text = webResource.get(String.class);

But I get a this exception (as well as a long stack trace):

Exception in thread "main" java.lang.IllegalArgumentException: Error parsing media type 'text/plain;javascript'

Thanks for your help,

Michael

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: java.lang.IllegalArgumentException: Error parsing media type

Paul Sandoz
Administrator
Hi Michael,

The media type "text/plain;javascript" is not a valid media type as  
defined by the HTTP 1.1 specification [1].

Parameters require a value. Note that content negotiation will only  
take into account the type and subtype of the media type and not any  
parameters.


On Jun 17, 2009, at 2:11 AM, Michael Andrews wrote:

> Hi,
>
> I am trying to associate the MIME type "test/plain;javascript" with  
> a JSON Provider.  From the many examples I have gleaned from the  
> mailing list and docs, I tried this:
>
> ClientConfig config = new DefaultClientConfig();  
> config.getClasses().add(JSONRootElementProvider.class);        
> Client client = Client.create(config);


You do not need to explicitly add JSONRootElementProvider, it is  
implicitly included if you include the Jersey JSON jar in the class  
path. So in this case you can just do:

   Client client = Client.create();

Also you are not really dealing with "javascript" it is "json" which  
is different, the latter is a data format that is a subset of the  
javascript language and needs to be processed different from  
javascript itself for security reasons.

Thus you should use "application/json" if you can do so.

Paul.

[1] http://greenbytes.de/tech/webdav/rfc2616.html#media.types
      http://greenbytes.de/tech/webdav/rfc2616.html#transfer.codings

>

> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

java.lang.IllegalArgumentException: Error parsing media type

Michael Andrews-3
In reply to this post by Michael Andrews-3
Hi Paul,

Thanks for your quick reply.

> Hi Michael,
>
> The media type "text/plain;javascript" is not a valid media type as
> defined by the HTTP 1.1 specification [1].
>
> Parameters require a value. Note that content negotiation will only
> take into account the type and subtype of the media type and not any
> parameters.

I figured as much, but I have little control over enforcing the
specification since it is an external URI.  Basically the service returns
'application/json' except that it is labeled 'text/plain;javascript'.  Is
there a technique in the API to rewrite this header before the Client
assigns a Provider?

Michael


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: java.lang.IllegalArgumentException: Error parsing media type

Paul Sandoz
Administrator

On Jun 17, 2009, at 7:33 PM, Michael Andrews wrote:

> Hi Paul,
>
> Thanks for your quick reply.
>
>> Hi Michael,
>>
>> The media type "text/plain;javascript" is not a valid media type as
>> defined by the HTTP 1.1 specification [1].
>>
>> Parameters require a value. Note that content negotiation will only
>> take into account the type and subtype of the media type and not any
>> parameters.
>
> I figured as much, but I have little control over enforcing the
> specification since it is an external URI.  Basically the service  
> returns
> 'application/json' except that it is labeled 'text/
> plain;javascript'.  Is
> there a technique in the API to rewrite this header before the Client
> assigns a Provider?
>

Yes, you can utilize a filter to modify the Content-Type from "text/
plain;javascript" to a well formed media type:

   https://jersey.dev.java.net/nonav/apidocs/1.1.0-ea/jersey/com/sun/jersey/api/client/filter/ClientFilter.html

and you can add an instance of your filter to the Client or  
WebResource using the addFilter method.

In your filter you can set the Content-Type by getting the headers.

      class AppClientFilter extends ClientFilter {

          public ClientResponse handle(ClientRequest cr) {
              // Call the next client handler in the filter chain
              ClientResponse resp = getNext().handle(mcr);

              String contentType = resp.getHeaders().getFirst("Content-
Type");
              String newContentType = ...
              resp.getHeaders().putSingle("Content-Type",  
newContentType);
              return resp;
          }
      }

Paul.

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Loading...