users@jersey.java.net

Re: help on query string

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 27 Mar 2008 13:45:52 +0100

vineet harbhajanka wrote:
> hi.. i want to develop a web service that needs to input a vector
> query in the request.. But is it possible to send a vector object in
> the request body? i am using the httpclient API by apache at the
> client side.. and all it allows is to attach a name value data pair
> object with the request body.. plz help. !!
>

I am not sure if you are asking for help on the server side or
specifically with the Apache HttpClient API. I cannot help you with the
Apache HttpClient API as i am not familiar with it.

With the Jersey client and server you can create your own reader/writer
to process a List type. See below for an example, note that this is
using the code from the trunk, if you are using 0.6 you will slightly
have to change the impl.

This assumes the list will be serialized as a comma separated list of
strings.

Hope this helps,
Paul.

import com.sun.net.httpserver.HttpServer;
import com.sun.ws.rest.api.client.Client;
import com.sun.ws.rest.api.client.WebResource;
import com.sun.ws.rest.api.client.config.ClientConfig;
import com.sun.ws.rest.api.client.config.DefaultClientConfig;
import com.sun.ws.rest.api.container.httpserver.HttpServerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;


public class Main {

     @Provider
     public static class ListMessageBody implements
MessageBodyWriter<List>, MessageBodyReader<List> {

         public boolean isWriteable(Class<?> c, Type t, Annotation[] a) {
             return List.class.isAssignableFrom(c);
         }

         public boolean isReadable(Class<?> c, Type t, Annotation[] a) {
             return List.class == c;
         }

         public long getSize(List l) {
             return -1;
         }

         public void writeTo(List l, Class<?> v, Type t, Annotation[] a,
                 MediaType m, MultivaluedMap<String, Object> h,
                 OutputStream out) throws IOException {
             if (l.isEmpty())
                 return;

             StringBuffer sb = new StringBuffer();
             for (Object o : l) {
                 if (sb.length() > 0)
                     sb.append(",");
                 sb.append(o.toString());
             }
             OutputStreamWriter w = new OutputStreamWriter(out, "UTF-8");
             w.write(sb.toString());
             w.flush();
         }

         public List readFrom(Class<List> c, Type t,
                 MediaType m, Annotation[] a, MultivaluedMap<String,
String> h,
                 InputStream in) throws IOException {
             Reader reader = new InputStreamReader(in, "UTF-8");
             StringBuilder sb = new StringBuilder();
             char[] cs = new char[1024];
             int l;
             while ((l = reader.read(cs)) != -1) {
                 sb.append(cs, 0, l);
             }

             String[] vs = sb.toString().split(",");
             List<String> ls = new ArrayList<String>();
             for (String v : vs) ls.add(v);
             return ls;
         }
     }

     @Path("/")
     public static class NameValuePair {
         @POST
         public List post(List l) {
             return l;
         }
     }

     public static void client() {
         ClientConfig cc = new DefaultClientConfig();
         cc.getProviderClasses().add(ListMessageBody.class);
         Client c = Client.create(cc);
         WebResource r = c.resource("http://localhost:9999/");

         List<String> request = new ArrayList<String>();
         request.add("BANANA");
         request.add("MANGO");
         request.add("PEAR");
         List<String> response = r.post(List.class, request);
         for (String v : response) {
             System.out.println(v);
         }
    }

     public static void main(String[] args) throws Exception {
         HttpServer s = HttpServerFactory.create("http://localhost:9999/");
         s.start();

         try {
             client();
         } finally {
             s.stop(0);
         }
     }
}

-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109