users@jersey.java.net

Re: [Jersey] Problem getting mulitpart data

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Sun, 31 May 2009 08:25:34 -0700

Hi Don,

I do not recognize the error message you have presented i.e. i am not
sure it is part of Jersey. What does the GF log present? any exceptions?

Do you know if the request is hitting the Jersey servlet or the error
is returned before that by GF?

Perhaps you can enable server-side logging in Jersey:

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

To determine if the request reaches the Jersey servlet and then we can
see what information is sent by the iPhone.

Paul.

On May 30, 2009, at 1:29 PM, Don Szromba wrote:

> Hello,
>
> I'm writing an app that, in part, needs to send some multipart data
> from an iPhone app to a web service (including an image or picture).
> I have implemented Jersey MultiPart in a restful service using
> Netbeans following patterns I am finding on the web. Deploying to
> Glassfish 2.1 (and/or Tomcat 6) results in the following error:
>
> The server refused this request because the request entity is in a
> format not supported by the requested resource for the requested
> method
>
> I'm not sure if this is a problem in the Objective-C client or in
> the web service.
>
> Any help with this will be greatly appreciated.
>
> This is what I'm doing in the client (for testing I'm just trying to
> send an image):
>
> NSData *imageData = UIImagePNGRepresentation(selectedImage);
>
> NSURL *url = [NSURL URLWithString:@"http://192.168.0.100:1417/MyService/resources/cards
> "];
> NSMutableURLRequest *theRequest = [NSMutableURLRequest
> requestWithURL:url];
> NSString* boundary = @"AaB03x";
>
> [theRequest addValue: @"multipart/mixed; boundary=AaB03x"
> forHTTPHeaderField:@"Content-Type"];
>
> [theRequest setHTTPMethod:@"POST"];
>
> NSMutableData *body = [NSMutableData data];
>
> [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r
> \n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
> [body appendData:[[NSString stringWithString:@"Content-Disposition:
> form-data; name=\"cardImage\"\r\n"]
> dataUsingEncoding:NSUTF8StringEncoding]];
> [body appendData:[[NSString stringWithString:@"Content-Type:
> application/octet-stream\r\n\r\n"]
> dataUsingEncoding:NSUTF8StringEncoding]];
> [body appendData:imageData];
>
> [body appendData:[[NSString stringWithFormat:@"\r\n--%_at_--\r
> \n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
>
> [theRequest setHTTPBody:body];
>
> NSURLConnection *theConnection = [[NSURLConnection alloc]
> initWithRequest:theRequest delegate:self];
>
>
> In my service I have this...
>
>
> @POST
> @Consumes(MultiPartMediaTypes.MULTIPART_MIXED)
>
> public Response getFormData(MultiPart multiPart) {
> BodyPartEntity bpe = (BodyPartEntity)
> multiPart.getBodyParts().get(0).getEntity();
>
> String status = "";
>
> InputStream stream = bpe.getInputStream();
> if (stream != null)
> {
> status = "got an inputStream!!";
> }
>
> try
> {
> CardServices services = new CardServices();
> if (status != null && status.length() > 0)
> {
> if (services.connectionTest(status))
> {
> return
> Response.status(Response.Status.ACCEPTED).entity("hit service with
> action!!!").type(MediaType.TEXT_PLAIN).build();
> }
> else
> {
> return
> Response.status(Response.Status.BAD_REQUEST).entity("got status but
> didn't hit service").type(MediaType.TEXT_PLAIN).build();
> }
> }
> else
> {
> if (services.connectionTest("nothing"))
> {
> return
> Response.status(Response.Status.ACCEPTED).entity("hit the service
> with nothing").type(MediaType.TEXT_PLAIN).build();
> }
> else
> {
> return
> Response.status(Response.Status.BAD_REQUEST).entity("no status and
> didn't hit the service").type(MediaType.TEXT_PLAIN).build();
> }
> }
> }
> catch(Exception ex)
> {
> return
> Response.status(Response.Status.BAD_REQUEST).entity("exception
> calling service").type(MediaType.TEXT_PLAIN).build();
> }
> }
>
>