5 Replies Latest reply on Nov 29, 2011 9:12 PM by Yasir Mian

    Need help in retrieving Serialized objects via REST

    Yasir Mian Newbie

      Hi,

       

      I am trying to retrieve Java Serialized object via REST API. I can successfully POST application/x-java-serialized-object but my problem is I can't retrieve it back.

       

      Please find details of what I am using.

       

      1). Infinispan-5.0.1.FINAL

      2). JDK 1.7

      3). Tomcat 7

       

      I am a newbie and trying to extend Infinispan example of simple Java client by doing POST and GET application/x-java-serialized-object but its not working.

       

      I can get byte-array back but that comes back as a String. What I would like is same Java serialized object back which I had posted to infinispan. Wondering if there is any way to get the object back?

       

      I am using Java client code from here.

       

      https://docs.jboss.org/author/display/ISPN/Infinispan+REST+Server

       

      Thank you for your help.

       

      Mian

        • 1. Re: Need help in retrieving Serialized objects via REST
          Yasir Mian Newbie

          Can I please get an answer for this? I just need to know if it is possible and if yes then which content-type I should use to deserialize the object. Many Thanks.

          • 2. Re: Need help in retrieving Serialized objects via REST
            Galder Zamarreño Master

            If you have a look at our testsuite, you'll find tests for storing and retrieving serializable data via REST:

            https://github.com/infinispan/infinispan/blob/master/server/rest/src/test/scala/org/infinispan/rest/IntegrationTest.scala#L551

             

            I dunno why you're getting back a String. Maybe you can provide a full test case to have a closer look?

            • 3. Re: Need help in retrieving Serialized objects via REST
              Yasir Mian Newbie

              Hi Galder, Thank you very much for your prompt reply. Much appreciated.

               

              Above example link tought me qutie a few things and I have managed to get back objects easily. I wasn't using Apache commons. I was working off rest example which ships with rest server doc as mentioned in my first post. I was trying something like this and I wasn't able to get back ObjectInputStream.

               

                      URL address = new URL(urlServerAddress);

                      System.out.println("executing request " + urlServerAddress);

                      HttpURLConnection connection = (HttpURLConnection) address.openConnection();

                      connection.setRequestMethod("GET");

                      connection.setRequestProperty("Content-Type", "application/x-java-serialized-object");

               

               

              Thank you again.

              • 4. Re: Need help in retrieving Serialized objects via REST
                Galder Zamarreño Master

                Glad you got that sorted. I'm not an expert on this API, but looking around there're some examples out there of using standard API to retrieve serialized data, see for example http://www.brantb.com/2006/06/that-dang-urlconnection-class-bit-me.html

                 

                If you figure out a way, it'd be great to contribute the example to our docu. Let us know

                • 5. Re: Need help in retrieving Serialized objects via REST
                  Yasir Mian Newbie

                  Sure thing. Please find the example below. In this example I have used similar code for putting Java serialized objects as discussed in the https://docs.jboss.org/author/display/ISPN/Infinispan+REST+Server and to retrieve it I have used Apache Commons.

                   

                  To get this example to work make sure you use correct REST server URL.

                   

                   

                  import java.io.ObjectInputStream;

                  import java.io.ObjectOutputStream;

                  import java.net.HttpURLConnection;

                   

                  import java.io.*;

                  import java.net.URL;

                   

                  import org.apache.commons.httpclient.*;

                  import org.apache.commons.httpclient.HttpMethod;

                  import org.apache.commons.httpclient.methods.*;

                   

                  public class TestRestAPI {

                      public static void main(String[] args) {

                          TestObject testObject = new TestObject();

                          testObject.setStr("Hello There!");

                          String serverURL = "http://localhost:9090/infinispan/rest/___defaultcache/hello";

                          try {

                              putObject(serverURL, testObject);

                              TestObject tObj = (TestObject) getObject();

                              System.out.println("Got the object back successfully: "+tObj.getStr());

                          } catch (Exception e) {

                              e.printStackTrace();

                          }

                      }

                   

                      private static Object getObject() throws Exception{

                          HttpClient client = new HttpClient();

                          HttpMethod get = new GetMethod("http://localhost:9090/infinispan/rest/___defaultcache/hello");

                          get.setRequestHeader("Accept", "application/x-java-serialized-object");

                          client.executeMethod(get);

                          System.out.println(get.getStatusCode());

                          ObjectInputStream in = new ObjectInputStream(get.getResponseBodyAsStream());

                          return in.readObject();

                      }

                   

                      private static void putObject(String urlServerAddress, Object obj) throws IOException{

                          System.out.println("----------------------------------------");

                          System.out.println("Executing PUT");

                          System.out.println("----------------------------------------");

                          URL address = new URL(urlServerAddress);

                          System.out.println("executing request " + urlServerAddress);

                          HttpURLConnection connection = (HttpURLConnection) address.openConnection();

                          System.out.println("Executing put method of value: " + obj);

                          connection.setRequestMethod("POST");

                          connection.setRequestProperty("Content-Type", "application/x-java-serialized-object");

                          connection.setDoOutput(true);

                   

                   

                          ObjectOutputStream outputStreamWriter = new ObjectOutputStream(connection.getOutputStream());

                          outputStreamWriter.writeObject(obj);

                   

                          connection.connect();

                          outputStreamWriter.flush();

                   

                          System.out.println("----------------------------------------");

                          System.out.println(connection.getResponseCode() + " " + connection.getResponseMessage());

                          System.out.println("----------------------------------------");

                   

                          connection.disconnect();

                   

                      }

                   

                  }

                   

                  class TestObject implements Serializable{

                      String str;

                   

                      public String getStr() {

                          return str;

                      }

                   

                      public void setStr(String str) {

                          this.str = str;

                      }

                   

                      @Override

                      public String toString() {

                          return "TestObject{" +

                                  "str='" + str + '\'' +

                                  '}';

                      }

                  }