Worklight Adapter从休息服务获取pdf文件 [英] Worklight Adapter getting pdf file from rest service

查看:107
本文介绍了Worklight Adapter从休息服务获取pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问暴露pdf文件的Rest服务,但是在调用该过程时我得到了这个响应:

I am trying to access to a Rest Service who exposes a pdf file, but I get this response when invoking the procedure:

{
   "errors": [
      "Failed to parse the payload from backend (procedure: HttpRequest)"
   ],
   "info": [
   ],
   "isSuccessful": false,
   "responseHeaders": {
      "Content-Type": "application\/octet-stream; type=\"application\/xop+xml\"; boundary=\"uuid:****************\"; start=\"<pdf>\"; start-info=\"application\/pdf\"",
      "Date": "Thu, 07 Nov 2013 14:44:54 GMT",
      "Server": "Apache-Coyote\/1.1",
      "Transfer-Encoding": "chunked",
      "X-Powered-By": "Servlet 2.5; **********",
      "content-disposition": "attachment; filename = ********.PDF"
   },
   "responseTime": 5329,
   "statusCode": 200,
   "statusReason": "OK",
   "totalTime": 9923,
   "warnings": [
   ]
}

我可以使用worklight适配器获取pdf文件吗?还有其他办法吗?

Can I get a pdf file using a worklight adapter? is there any alternative way?

推荐答案

我能够通过从适配器调用Java例程来调用返回PDF的后端服务作为 byte [] 。一旦检索到 byte [] ,我 base64 在Java例程中对其进行编码,URI在适配器中对其进行编码,然后在客户端js代码中执行相反的操作。 Java代码使用Apache HttpClient 。我尝试了普通的返回类型以及没有运气的其他类型。

I was able to do this with calling a Java routine from the adapter to call the back-end service that returns the PDF as a byte[]. Once the byte[] is retrieved I base64 encode it in the Java routine, URI encode it in the adapter, then do the reverse in the client js code. The Java code uses the Apache HttpClient. I tried the plain return type as well as others with no luck.

此处的Java示例

适配器代码:

var service = new ServiceClient(); 
var pdf = service.getUnsecureContentBase64(url);
result.pdf = encodeURIComponent(pdf);

return result;

客户代码:

onSuccess : function (response){

        var pdfText = decodeURIComponent(response.invocationResult.pdf);

        var pdf = base64DecToArr(pdfText);

        //use pdf byte[] to pass to Mozilla pdf.js
        var pdfText = decodeURIComponent(response.invocationResult.pdf);

        var pdf = base64DecToArr(pdfText);

        PDFJS.disableWorker = false;
        PDFJS.getDocument(pdf).then(function (pdfDoc) {
            //use pdfDoc to render
        });
    }

Java代码:

public class ServiceClient {

public String getUnsecureContentBase64(String url)
        throws ClientProtocolException, IOException {

    byte[] result = getUnsecureContent(url);

    return Base64.encodeBase64String(result);
}

public byte[] getUnsecureContent(String url)
        throws ClientProtocolException, IOException {

    byte[] result = null;
    CloseableHttpClient httpclient = null;

    try {
        httpclient = HttpClientBuilder.create()
                .setRedirectStrategy(new LaxRedirectStrategy()).build();

        HttpGet httpget = new HttpGet(url);

        // Create a response handler
        ResponseHandler<byte[]> handler = new ResponseHandler<byte[]>() {
            public byte[] handleResponse(HttpResponse response)
                    throws ClientProtocolException, IOException {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    return EntityUtils.toByteArray(entity);
                } else {
                    return null;
                }
            }
        };

        result = httpclient.execute(httpget, handler);

    } finally {
        if (httpclient != null) {
            httpclient.close();
        }
    }

    return result;
}
} 

这篇关于Worklight Adapter从休息服务获取pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆