[英]IBM Worklight- HTTP adapter
如何創建HTTP適配器以檢索Web主機上的文件。 我感到困惑,因為HTTP適配器用於檢索用於RSS feed的json輸出。 我如何定位文件(例如.jpg)。
謝謝。
擴展Idan的答案:
提供遠程映像URL作為適配器的參數:
適配器JS:
function getImage() {
WL.Logger.info("################### getImage ######################");
var val = com.company.ProjectName.ImageEncoder.getImage("http://Some-Domain/../.../id.gif");
WL.Logger.info("################### IMAGE IS ######################");
WL.Logger.info(val);
WL.Logger.info("#####################################################");
var imageData = {"data":val};
WL.Logger.info(JSON.stringify(imageData));
return imageData;
}
適配器XML:
將過程添加到適配器:
<procedure name="getImage"/>
將自定義Java代碼添加到您的服務器:
Java代碼:
package com.company.ProjectName;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.MalformedInputException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class ImageEncoder {
public static String getImage(String imageUrl)
throws MalformedURLException, IOException {
String imageDataString = "";
URL url = null;
int i;
try {
url = new URL(imageUrl);
System.out.println(imageUrl);
HttpURLConnection connection = null;
String protocol = url.getProtocol();
System.out.println(protocol);
// this is to trust any certificates from the target server
if("https".equalsIgnoreCase(protocol)){
// Create a trust manager that does not validate certificate chains
System.out.println("inside If");
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
connection = (HttpsURLConnection)url.openConnection();
System.out.println("connection"+connection.getContentLength());
}else{
connection=(HttpURLConnection) url.openConnection();
}
InputStream input = connection.getInputStream();
byte[] bytes = org.apache.commons.io.IOUtils.toByteArray(input);
input.close();
imageDataString = encodeImage(bytes);
return imageDataString;
} catch (MalformedInputException malformedInputException) {
malformedInputException.printStackTrace();
imageDataString = malformedInputException.toString();
return ("exception while reading the imag <" + imageDataString + ">");
} catch (IOException ioException) {
ioException.printStackTrace();
imageDataString = ioException.toString();
return ("exception while reading the imag <" + imageDataString + ">");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
imageDataString = e.toString();
return ("exception while reading the imag <" + imageDataString + ">");
} catch (KeyManagementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
imageDataString = e.toString();
return ("exception while reading the imag <" + imageDataString + ">");
}
public static String encodeImage(byte[] imageData) {
// TODO Auto-generated method stub
org.apache.commons.codec.binary.Base64 base = new org.apache.commons.codec.binary.Base64(
false);
return base.encodeToString(imageData);
}
}
Java代碼目錄:
注意:不要信任所有證書。您需要添加自己的信任管理器。這僅用於測試
您可以按照此博客文章中提供的說明進行操作。
步驟如下:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.