RESTful POST method using JAX-RS Jersey

Asked by steve chu on 2011-05-02

I tested GET method of Glance RESTful interface. It works fine. I am wondering if you could provide sample code to PUT image to glance via its RESTful interface. When PUT is being called, how to set Meta data properties. Thanks!

Question information

Language:
English Edit question
Status:
Answered
For:
Glance Edit question
Assignee:
No assignee Edit question
Last query:
2011-05-04
Last reply:
2011-05-09
steve chu (steve-chu) said : #1

The following Java code is from RESTful client.
                ... ...

  Client client = Client.create();

  // get GET
  WebResource getWebResource = client.resource("http://your-host-ip:9292/images/detail");
  String s = getWebResource.get(String.class);
  System.out.println("response is " + s);
  JSONObject jsonObj = null;
  try {
   jsonObj = new JSONObject(s);
   JSONArray jsonArray = jsonObj.getJSONArray("images");
   int index = jsonArray.length();
   JSONObject imageObj = null;
   for (int i = 0; i < index; i++) {
    imageObj = jsonArray.getJSONObject(i);
    String imageName = imageObj.getString("name");
    System.out.println("name is " + imageName);
    Long imageSize = imageObj.getLong("size");
    System.out.println("image size is " + imageSize.toString());
   }

   JSONObject newImage = jsonArray.getJSONObject(0);
  } catch (JSONException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  // test DELETE
  WebResource deleteWebResource = client.resource("http://your-host-ip:9292/images/3");
  deleteWebResource.delete();

The above GET and DELETE method seem working. The output of GET is only Meta data. Where is the real image? I could not find details description from Glance documentation.
I can not get PUT or POST to work and I am getting HTTP error 400 and 404. Do you have documentation to describe the RESTful interface? If you can provide sample code (Java), that would be great!

Jay Pipes (jaypipes) said : #2

Hi Steve,

Sorry, I do not have any Java sample code. However, the REST interface is documented in the Glance Documentation online here:

http://glance.openstack.org/glanceapi.html

A call to GET /images/<ID> returns the image in the request body, and metadata about the image in the HTTP headers.

Cheers,
jay

steve chu (steve-chu) said : #3

I am building RESTful client using JAX-RS Jersey and having problem with POSTing images to Glance repository. The Meta data in ClientRequest headers are not processed by Glance. The Image in the ClientRequest entity was added to the Glance repository.

Does Glance support JAX-RS Jersey?

Jay Pipes (jaypipes) said : #4

Sorry, Steve, I don't have any experience with writing Java-based REST clients. :( I'll ask around and see if someone else would have some more information.

justinsb (justin-fathomdb) said : #5

Hi Steve - sorry about the delay. You should be able to set custom headers with the JAX-RS client using the header() method, though I'm only going by the docs here, not having used it myself:
http://jersey.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/api/client/WebResource.html

However, for an image upload, there's a much bigger issue, which is that your client library really has to be designed not to buffer the entire posted data (i.e. the image) in memory first. I did dig through the Jersey source code to try to see whether this was the case, it looks like it could be OK, but you _may_ have to provider a custom adapter.

Personally, I would use a lower-level library for image upload & download. Apache HttpClient seems to have changed everything in v4, but here's a sample which explains how to do chunked uploading without buffering (apparently), it also references using a "FileEntity" class which I presume is exactly what you actually want...
http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientChunkEncodedPost.java

Can you help with this problem?

Provide an answer of your own, or ask steve chu for more information if necessary.

To post a message you must log in.