Worklight适配器从rest服务获取pdf文件
我试图访问一个公开pdf文件的Rest服务,但调用该过程时得到以下响应:Worklight适配器从rest服务获取pdf文件,rest,pdf,ibm-mobilefirst,worklight-adapters,worklight-server,Rest,Pdf,Ibm Mobilefirst,Worklight Adapters,Worklight Server,我试图访问一个公开pdf文件的Rest服务,但调用该过程时得到以下响应: { "errors": [ "Failed to parse the payload from backend (procedure: HttpRequest)" ], "info": [ ], "isSuccessful": false, "responseHeaders": { "Content-Type": "application\/octet-strea
{
"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": [
]
}
{
“错误”:[
“未能从后端分析负载(过程:HttpRequest)”
],
“信息”:[
],
“isSuccessful”:错误,
“负责人”:{
“内容类型”:“应用程序\/octet流;类型=\“应用程序\/xop+xml\”边界=\“uuid:****************”“开始=\”;开始信息=“应用程序\/pdf\”,
“日期”:“2013年11月7日星期四14:44:54 GMT”,
“服务器”:“Apache郊狼\/1.1”,
“传输编码”:“分块”,
“X-Powered-By”:“Servlet 2.5;**********”,
“内容处置”:“附件;文件名=*********.PDF”
},
“响应时间”:5329,
“状态代码”:200,
“状态原因”:“确定”,
“总时间”:9923,
“警告”:[
]
}
我可以使用worklight适配器获取pdf文件吗?还有别的办法吗 您需要在适配器实现中更改“returnedContentType”参数。我猜现在您已经将其设置为“xml”。由于从后端检索的pdf不是XML格式,因此您将收到该错误消息 例如:
function getPDF() {
var input = {
method : 'get',
returnedContentType : 'plain',
path : "/test.pdf"
};
return WL.Server.invokeHttp(input);
}
您需要在适配器实现中更改“returnedContentType”参数。我猜现在您已经将其设置为“xml”。由于从后端检索的pdf不是XML格式,因此您将收到该错误消息 例如:
function getPDF() {
var input = {
method : 'get',
returnedContentType : 'plain',
path : "/test.pdf"
};
return WL.Server.invokeHttp(input);
}
我可以通过从适配器调用Java例程来调用后端服务来实现这一点,后端服务将PDF作为
字节[]
返回。一旦检索到字节[]
,我将base64
在Java例程中对其进行编码,URI在适配器中对其进行编码,然后在客户端js代码中执行相反的操作。Java代码使用ApacheHttpClient
。我尝试了普通返回类型以及其他类型,但没有成功
适配器代码:
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;
}
}
公共类服务客户端{
公共字符串getUnsecureContentBase64(字符串url)
抛出ClientProtocolException,IOException{
字节[]结果=getUnsecureContent(url);
返回Base64.encodeBase64String(结果);
}
公共字节[]getUnsecureContent(字符串url)
抛出ClientProtocolException,IOException{
字节[]结果=空;
CloseableHttpClient httpclient=null;
试一试{
httpclient=HttpClientBuilder.create()
.setRedirectStrategy(新策略()).build();
HttpGet HttpGet=新的HttpGet(url);
//创建一个响应处理程序
ResponseHandler=新ResponseHandler(){
公共字节[]HandlerResponse(HttpResponse响应)
抛出ClientProtocolException,IOException{
HttpEntity=response.getEntity();
如果(实体!=null){
返回EntityUtils.toByteArray(实体);
}否则{
返回null;
}
}
};
结果=httpclient.execute(httpget,handler);
}最后{
if(httpclient!=null){
httpclient.close();
}
}
返回结果;
}
}
我可以通过从适配器调用Java例程来调用后端服务,该服务将PDF作为字节[]返回。
。一旦检索到字节[]
,我将base64
在Java例程中对其进行编码,URI在适配器中对其进行编码,然后在客户端js代码中执行相反的操作。Java代码使用ApacheHttpClient
。我尝试了普通返回类型以及其他类型,但没有成功
适配器代码:
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;
}
}
公共类服务客户端{
公共字符串getUnsecureContentBase64(字符串url)
抛出ClientProtocolException,IOException{
字节[]结果=getUnsecureContent(url);
返回Base64.encodeBase64String(结果);
}
公共字节[]getUnsecureContent(字符串url)
抛出ClientProtocolException,IOException{
字节[]结果=空;
CloseableHttpClient httpclient=null;
试一试{
httpclient=HttpClientBuilder.create()
.setRedirectStrategy(新策略()).build();
HttpGet HttpGet=新的HttpGet(url);
//创建一个响应处理程序
ResponseHandler=新ResponseHandler(){
公共字节[]HandlerResponse(HttpResponse响应)
抛出ClientProtocolException,IOException{
HttpEntity=response.getEntity();
如果(实体!=null){
返回EntityUtils.toByteArray(实体);
}否则{
返回null;
}
}
};
结果=httpclient.execute(httpget,handler);
}最后{
if(httpclient!=null){
httpclient.close();
}
}
返回结果;
}
}
我可以使用returnContentType:“plain”检索PDF,但它无法调用successHandler。它显示以下错误:-[/pdf/apps/services/api/pdf/common/query]异常。TypeError:无法读取未定义worklight的属性'isSuccessful'。js:4556未捕获语法错误:意外的worklight数。js:850未捕获TypeError:无法读取未定义worklight的属性'isSuccessful'。您应该在问题中发布一个单独的问题。如果没有看到您的一些代码,就很难调试您的问题。我发布了一个单独的问题:-我可以使用returnContentType:“plain”检索PDF,但它无法调用successHandler。它显示以下错误:-[/pdf/apps/services/api/pdf/common/query]异常。TypeError:无法读取未定义worklight的属性'isSuccessful'。js:4556未捕获语法错误:意外的worklight数。js:850未捕获TypeError:无法读取未定义worklight的属性'isSuccessful'。您应该在问题中发布一个单独的问题。