Getting Image full URL path for REST API

Dear Experts,

Following up with

I added image to the document type thus now the Document Type call Orang (Person) has the following fields a name (nama) and age and an image.

I used essential → Bean Writer to generate the Java files
I used essential → REST Service Setup to generate the API (generic and manual)

In the generic REST Resource I get this very nice JSON

{  
   "id":"a54902a6-114f-45c1-9535-032cab9ab862",
   "name":"cubaorang",
   "displayName":"cubaorang",
   "type":"xxxxxx:OrangDT",
   "locale":"document-type-locale",
   "pubState":"published",
   "pubwfCreationDate":"2018-11-26T13:13:57.287+08:00",
   "pubwfLastModificationDate":"2018-11-26T13:57:37.254+08:00",
   "pubwfPublicationDate":"2018-11-26T13:57:39.528+08:00",
   "items":{  
      "xxxxxx:nama":"Sola",
      "xxxxxx:age":48,
      "xxxxxx:image":{  
         "type":"hippogallerypicker:imagelink",
         "link":{  
            "type":"binary",
            "url":"http://localhost:8080/site/binaries/content/gallery/xxxxxx/orang/test_image.jpg"
         }
      }
   }
}

My intention was to hold the image URL for example:
http://localhost:8080/site/binaries/content/gallery/xxxxxx/orang/test_image.jpg

package org.finexus.xxxxxx.beans;
public class Orang_POJO{
	public String nama;
	public String age;
	public String imageURL;

	public Orang_POJO(Orang data) {
		nama = data.getNama();
		age = data.getAge();
		imageURL = data.getImage.getPath(); <---- this one
	}
	public String getNama() {return nama;}
	public void setNama(String nama) {this.nama = nama;}
	public String getAge() {return age;}
	public void setAge(String age) {this.age = age;}	
	public String getImageURL() {return imageURL;}
	public void setImageURL(String age) {this.imageURL = imageURL;}	
}

BUT for my java file above (refer to ← this one) it shows me only the xpath below
“content/gallery/xxxxxx/orang/test_image.jpg”
missing the entire “http://localhost:8080/site/binaries/

I can always add the missing part to my string but that will be cheating.
How do I get the full path like the one in generic?

Regards
Sola Lee

          // ----> like this:
          HstRequestContext requestContext = RequestContextProvider.get();
          HstLink hstLink = requestContext.getHstLinkCreator().create(data.getImage(), requestContext);
          imageURL = hstLink.toUrlForm(requestContext, true);
1 Like

Dear @woonsanko

Thank you for your reply,

I am going to try this our and if successful I will share the codes (Edited: Yes it works beautiful)

	@GET
	@Path("/test")
	public String test(@Context HttpServletRequest request) throws Exception {
		Session session = null;
		String myURL = null;
		try {
			HippoRepository repository = HippoRepositoryFactory.getHippoRepository("vm://");
			session = repository.login("admin","admin".toCharArray());	
			Node node = session.getRootNode().getNode("content/documents/xxxxxx/cubaorang/cubaorang[3]/xxxxxx:Orang");	
			HstRequestContext context = RequestContextProvider.get();		
			Orang org = (Orang) context.getObjectConverter().getObject(node);

			HstLink hstLink = context.getHstLinkCreator().create(org.getImage(), context);
			myURL = hstLink.toUrlForm(context, true);
			System.out.println(myURL);	

		}catch(Exception e) {
			e.printStackTrace();
			throw  e;	
		}finally {
			session.logout();			
		}
		return Response.ok(myURL ,MediaType.TEXT_HTML).build();
	}

The terminal screen will show

http://localhost:8080/site/binaries/content/gallery/xxxxxx/orang/test_image.jpg

Orang org is generated by HIPPO (via essential bean writer) which extends BaseDocument
and org.getImage() will return a HippoGalleryImageSet.

Regards
Sola Lee