Message Boards

REST API outside Liferay

Irina Dozortseva, modified 7 Years ago.

REST API outside Liferay

New Member Posts: 3 Join Date: 3/31/14 Recent Posts
I am working on moving existing code into jsonws API. The code gets PDF from external system , converts it to byte [], and displays the PDF in the browser when link to the document is clicked. This new API will be called by mobile app (not using Liferay sdk). What is the best way of passing PDF in json?

This is what I have tried:

public JSONObject urldisplay(String urlDoc){
JSONObject res= JSONFactoryUtil.createJSONObject();

String urlDoc1=urlDoc.substring(0, urlDoc.indexOf("?"));
String queryPDF[]=urlDoc.substring(urlDoc.indexOf("?"),urlDoc.length()).split;
HttpClient m_httpClient = new HttpClient();
GetMethod getMethod = new GetMethod(urlDoc1);
NameValuePair[] params= new NameValuePair[queryPDF.length];
for(int i=0;i<queryPDF.length;i++)
{
NameValuePair param=new NameValuePair(queryPDF.split("=")[0],queryPDF.split("=")[1]);
params=param;
_log.info("counter i="+i);
_log.info("param i="+param.toString());
}
getMethod.setQueryString(params);
byte[] responsePDF = null;

try {
m_httpClient.executeMethod(getMethod);
responsePDF = getMethod.getResponseBody();
int length=responsePDF.length;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
if (params[2].getValue().contains("PDF")) {
try{
bos.write(responsePDF, 0, length);

String pdfBase64String = org.apache.commons.codec.binary.StringUtils.newStringUtf8(org.apache.commons.codec.binary.Base64.encodeBase64(bos.toByteArray()));
res.put("status", "success");
res.put("pdf",pdfBase64String);
}catch(Exception ex)
{
res.put("status", "failed");
res.put("message",ex.getMessage());
}

Any suggestions, please let me know....