Sono stato in grado di farlo chiamando una routine Java dall'adattatore per chiamare il servizio back-end che restituisce il PDF come un byte[]
. Una volta il byte[]
è recuperato i base64
codificalo nella routine Java, URI codifica nell'adattatore, quindi esegui il contrario nel codice JS client. Il codice Java utilizza Apache HttpClient
. Ho provato il tipo di ritorno semplice e altri senza fortuna.
Codice dell'adattatore:
var service = new ServiceClient();
var pdf = service.getUnsecureContentBase64(url);
result.pdf = encodeURIComponent(pdf);
return result;
Codice client:
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
});
}
Codice 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;
}
}