users@jersey.java.net

Re: [Jersey] URLDecoder: Incomplete trailing escape (%) pattern

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 05 Jan 2009 17:15:38 +0100

Hi,

I wrote a very simple test using the Jersey client and it worked fine.
I used the string data for the first unsuccessful test (chunk number
11).

Is there any way you can get you log what the HTTP client outputs as a
HTTP request? I want to know what is going on the wire to check if the
request is malformed.

BTW you do this:

         public String store(
                 @FormParam("imageSid") Decimal imageSid,
                 @FormParam("chunkNumber") int chunkNumber,
                 @FormParam("data") String base64Data ) {

And if there is a parsing error for the Decimal or int type a 400
(Client error) will be returned.

Paul.

     @Path("/")
     public static class Base64Form {

         @POST
         @Produces("text/plain")
         @Consumes("application/x-www-form-urlencoded")
         public String store( @FormParam("imageSid") String imageSid,
@FormParam("chunkNumber") String chunkNumber,
                 @FormParam("data") String base64Data ) {
             System.out.println("imageSid: " + imageSid);
             System.out.println("chunkNumber: " + chunkNumber);
             System.out.println("data: " + base64Data);
             return "OK";
         }
     }

     public void testBase64Form() {
         initiateWebApplication(Base64Form.class);

         WebResource r = resource("/");

         Form f = new Form();
         f.add("imageSid", "1");
         f.add("chunkNumber", "11");
         f.add("data", "hjBMjlVYMqE4BIXgsWb5a+QviV/
wUH8P6aLpNWv7IQxiWKKzi1CD7Qc/MFBL7gQDhQRtII8sfNgfQHjf/
hHPGEkltcGOSK7gLFoomFuVRMCMO20B25ViNvzHgkAGvnIfs/fA1HkuX
+GPgfxHdMolnute8M6VrEyTTLu+afU7S4dyjYIAfbjaNvPPw
+ZZhjKbn7JKaTioqTkkrygm3a
+tm0koq8mntv8AcLBRnBVadWlTqxaalJNyTS1SsrNpJu0vdST3dj4L8af8FDtcvor6x8C2xf
cJIo7zV9Ugkgt44GYE29tHL5zxrGqADDYOxGbPNfn/
APEz9pfxV4rvL8+KPiGXDTNay6Pp19OEgddjtbm20Y3EiOTG6Fbgx5ZHRypBNfrr8ZfgZ8EZ
IvD9jqfww+GWi2Wra1YQah/
YPhHQdP1yTS9Kkl1nWLSzOn2BcPdWGkvZsmElJ1K2iUA3BRvOv2v/AIW/C3wf4TsrD4L+A/
BPge48DJPqPjvSPBOgaXpcWn6Fqlhp0mmaN4g1CGBbTXPEUt9bNcTC3v8AVL2K3snaeGAPOjf0j4FeDEvFXB4vOcbia+T4HC4uWFhUpYdV8TiFBKc6kKk+WNOUGrX5NpaLRs/lfxy8X8T4cZnh8pw/s84zHGUo4m9StVw2HpRm4qi3TotKcrSb5XGPwvmjrc/I74XePvC6+JtV8R61orNonhvRtRu7aS+tEma91+5EFloyQ6fLc7Jrhb27jnt5rkPDDJGjTQSKpjb9a/2cv+Crv7Wv7IXjZfij8MfiT4m+L3wj0ye3Pxe/Zw+I3ifV/EOjjQ7CaZLjxH8L7rVLjVtV8HWItormGW0sJjBpty9nM/h680e3e3sPx90m/wBN13xLf61qTwnRfDei6v45vwkIMV1/YkjWXh+3ljTyBcR3viKTTbcRnO6RvmQqSw4nQfEFzA8d3LdyQ7NW0mXUJliE0dpDcziG/RopVaGSF4bxruPaNjwy2crbXSJY/wCs8u8NOF8iy+pw9gstpYmNa9GrisS51MZianMl7arXrOo4VE/hUJQhF6Rio6H8v51xjxLn2MoZvmmYz5lCOIo4ek1ToYaly8zjTpUI01KDSvJzUpzs3KTauf6lH7DP/BUT9kL9vTRYX+CfxGhtvHFpptnqPiD4VeMlGifEPRYruCC5kL6PcOV1ixga4WL+2NAn1LSnbaBco4aFP0jS4iIADAkBd2CCBkHHORwcNg4wdp9DX+QD47uPHf7OfxF8B+LfB3ibWfD2p+AvEWlv4V8YeGtV1HRNavfBGtwv4hs9HfUtO1GPULeHT7CbUdFhMVzDLeadKskiPK5eT9ufgF/wXX/4KMeDvD8PhXwn4li+NN/peg==");
         String s = r.post(String.class, f);
         assertEquals("OK", s);
     }


On Jan 5, 2009, at 4:36 PM, John Gant wrote:

> Paul,
>
> Thanks for the reply. I've shortened the problem code and have
> included it in the message.
> One thing I have noticed is that many of the base64 data elements
> include the "==" that you mentioned, and of those many are successful.
>
> Nonetheless I have included the web service method. Lastly, I've
> included the output of the simple client for the image.
>
> Thanks again for your help.
> John
>
> <WEB-SERVICE METHOD>
>
> package edu.louisville.cecs.webservice;
>
>
> import java.io.UnsupportedEncodingException;
> import java.math.BigDecimal;
>
> import javax.naming.InitialContext;
> import javax.naming.NamingException;
> import javax.ws.rs.Consumes;
> import javax.ws.rs.FormParam;
>
> import javax.ws.rs.POST;
> import javax.ws.rs.Path;
> import javax.ws.rs.Produces;
>
> import org.apache.commons.codec.binary.Base64;
> import org.apache.log4j.Logger;
>
> import edu.louisville.cecs.ejb.Worker;
>
>
> @Path("/store")
> public class Store {
>
> private Logger logger = Logger.getLogger( Store.class );
>
> @POST
> @Produces("text/plain")
> @Consumes("application/x-www-form-urlencoded")
>
> public String store( @FormParam("imageSid") String imageSid,
> @FormParam("chunkNumber") String chunkNumber,
> @FormParam("data") String base64Data ) {
> InitialContext ic = null;
>
> Boolean success = Boolean.TRUE;
> try {
> ic = new InitialContext();
> Worker finder = (Worker)
> ic.lookup( Worker.class.getName() );
> BigDecimal sid = new BigDecimal( imageSid );
>
> Integer number = Integer.valueOf( chunkNumber );
> byte [] decodedData =
> Base64.decodeBase64( base64Data.getBytes( "UTF-8" ) );
> success = finder.store( sid, number, decodedData );
>
> }
> catch ( NamingException e ) {
> logger.error( e );
> success = Boolean.FALSE;
> }
> catch ( UnsupportedEncodingException e ) {
> logger.error( e );
>
> success = Boolean.FALSE;
> }
> finally {
> try {
> ic.close();
> }
> catch ( NamingException e ) {
> logger.error( e );
>
> }
> }
> logger.debug( "storage for sid: "
> + imageSid
> + " and chunk: "
> + chunkNumber
> + " was "
>
> + ( success == true ? "successful" :
> "unsuccessful" ) );
> return success.toString();
> }
>
> }
>
> </WEB-SERVICE METHOD>
>
> <CLIENT-CODE>
>
> package edu.louisville.cecs;
>
>
> import java.io.File;
> import java.io.FileInputStream;
> import java.io.FileNotFoundException;
> import java.io.IOException;
> import java.math.BigDecimal;
>
> import org.apache.commons.codec.binary.Base64;
>
> import org.apache.commons.httpclient.HttpClient;
> import org.apache.commons.httpclient.NameValuePair;
> import org.apache.commons.httpclient.methods.PostMethod;
>
> public class ProblemTester {
> public static void main( String [] args ) {
>
> PostMethod post = null;
> long FIELD_SIZE = 1024;
> String imageStoreWebServiceUrl = "http://localhost:8080/ImageService/actions/store
> ";
>
> File file = new File( "/Users/jgant/Pictures/cramer125.jpg" );
> BigDecimal imageSid = new BigDecimal( 1 );
> HttpClient client = new HttpClient();
> try {
> FileInputStream fis = null;
>
> try {
> fis = new FileInputStream( file );
> }
> catch ( FileNotFoundException e ) {
> e.printStackTrace();
> }
> if ( fis != null ) {
>
> byte [] array = new
> byte[ Long.valueOf( FIELD_SIZE ).intValue() ];
> int chunkNumber = 0;
> post = new PostMethod( imageStoreWebServiceUrl );
> try {
> while ( fis.read( array ) != -1 ) {
>
> chunkNumber++;
> byte [] base64Data =
> Base64.encodeBase64( array );
> String base64String = new
> String( base64Data, "UTF-8" );
> NameValuePair [] nvp =
>
> {
> new
> NameValuePair( "imageSid", imageSid.toString() ),
> new
> NameValuePair( "chunkNumber", Integer.toString( chunkNumber ) ),
>
> new NameValuePair( "data",
> base64String ) };
> int sendCnt = 0;
> post.setRequestBody( nvp ); //set parameters
> client.executeMethod( post ); //execute
>
> String response =
> post.getResponseBodyAsString(); //get entire response
> Boolean success = Boolean.valueOf( response );
> if ( success ) {
> System.out.println( "request
> successful" );
>
> System.out.println( "uri: " +
> post.getURI() );
> for ( NameValuePair nvp2 :
> post.getParameters() ) {
> System.out.println( nvp2.getName() +
> ":" + nvp2.getValue() );
>
> }
> }
> else {
> System.err.println( "request
> unsuccessful".toUpperCase() );
> System.err.println( "uri: " +
> post.getURI() );
>
> for ( NameValuePair nvp2 :
> post.getParameters() ) {
> System.err.println( nvp2.getName() +
> ":" + nvp2.getValue() );
> }
>
> }
> sendCnt++;
> array = new
> byte[ Long.valueOf( FIELD_SIZE ).intValue() ];
> }
> }
> catch ( IOException e ) {
>
> e.printStackTrace();
> }
> }
> }
> catch ( Exception e ) {
> e.printStackTrace();
> }
> finally {
> if ( post != null ) {
>
> post.releaseConnection();
> }
> }
> }
>
> }
> </CLIENT-CODE>