-
1. Re: Need help in retrieving Serialized objects via REST
ymian Nov 21, 2011 12:16 AM (in response to ymian)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.zamarreno Nov 21, 2011 9:28 AM (in response to ymian)If you have a look at our testsuite, you'll find tests for storing and retrieving serializable data via REST:
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
ymian Nov 21, 2011 4:04 PM (in response to galder.zamarreno)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.zamarreno Nov 28, 2011 5:10 AM (in response to ymian)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
ymian Nov 29, 2011 9:12 PM (in response to galder.zamarreno)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 + '\'' +
'}';
}
}