使用SAP雲平臺 + JNDI訪問Internet Service
在瀏覽器裡訪問這個url,得到輸出:從Walldorf到Berlin的距離。
如何讓一個部署到SAP雲平臺的Java應用也能訪問到該internet service呢?
首先在SAP雲平臺裡建立一個destination,維護service的end point:
在Java程式碼裡使用SAP雲平臺裡建立的destination:
然後使用JNDI service讀取destination裡配置的url:
部署到SAP雲平臺之後,在Eclipse裡看到preview結果:
SAP雲平臺Cockpit顯示如下:
瀏覽器訪問如下:
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="" xmlns="" xmlns:web="/web-app_2_5.xsd" xsi:schemaLocation=" /web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- Main sample servlet mapped to / so that the integration test harness can detect readiness (generic for all samples) --> <servlet> <servlet-name>ConnectivityServlet</servlet-name> <servlet-class>com.sap.cloud.sample.connectivity.ConnectivityServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ConnectivityServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Declare the JNDI lookup of destination --> <resource-ref> <res-ref-name>connectivityConfiguration</res-ref-name> <res-type>com.sap.core.connectivity.api.configuration.ConnectivityConfiguration</res-type> </resource-ref> </web-app>
package com.sap.cloud.sample.connectivity; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.URL; import javax.annotation.Resource; import javax.naming.Context; import javax.naming.InitialContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.sap.cloud.account.TenantContext; import com.sap.core.connectivity.api.configuration.ConnectivityConfiguration; import com.sap.core.connectivity.api.configuration.DestinationConfiguration; public class ConnectivityServlet extends HttpServlet { @Resource private TenantContext tenantContext; private static final long serialVersionUID = 1L; private static final int COPY_CONTENT_BUFFER_SIZE = 1024; private static final Logger LOGGER = LoggerFactory.getLogger(ConnectivityServlet.class); private static final String ON_PREMISE_PROXY = "OnPremise"; @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpURLConnection urlConnection = null;
String destinationName = request.getParameter("destname"); if (destinationName == null) {
destinationName = "google_map";
} try {
Context ctx = new InitialContext();
ConnectivityConfiguration configuration = (ConnectivityConfiguration) ctx.lookup("java:comp/env/connectivityConfiguration");
DestinationConfiguration destConfiguration = configuration.getConfiguration(destinationName); if (destConfiguration == null) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
String.format("Destination %s is not found. Hint:" + " Make sure to have the destination configured.", destinationName)); return;
}
String value = destConfiguration.getProperty("URL");
URL url = new URL(value + "xml?origins=Walldorf&destinations=Paris");
String proxyType = destConfiguration.getProperty("ProxyType");
Proxy proxy = getProxy(proxyType);
urlConnection = (HttpURLConnection) url.openConnection(proxy);
injectHeader(urlConnection, proxyType);
InputStream instream = urlConnection.getInputStream();
OutputStream outstream = response.getOutputStream();
copyStream(instream, outstream);
} catch (Exception e) {
String errorMessage = "Connectivity operation failed with reason: " + e.getMessage()
+ ". See " + "logs for details. Hint: Make sure to have an HTTP proxy configured in your " + "local environment in case your environment uses " + "an HTTP proxy for the outbound Internet " + "communication.";
LOGGER.error("Connectivity operation failed", e);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
errorMessage);
}
} private Proxy getProxy(String proxyType) {
Proxy proxy = Proxy.NO_PROXY;
String proxyHost = null;
String proxyPort = null; if (ON_PREMISE_PROXY.equals(proxyType)) { // Get proxy for on-premise destinations proxyHost = System.getenv("HC_OP_HTTP_PROXY_HOST");
proxyPort = System.getenv("HC_OP_HTTP_PROXY_PORT");
} else { // Get proxy for internet destinations proxyHost = System.getProperty("https.proxyHost");
proxyPort = System.getProperty("https.proxyPort");
} if (proxyPort != null && proxyHost != null) { int proxyPortNumber = Integer.parseInt(proxyPort);
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPortNumber));
} return proxy;
} private void injectHeader(HttpURLConnection urlConnection, String proxyType) { if (ON_PREMISE_PROXY.equals(proxyType)) { // Insert header for on-premise connectivity with the consumer account name urlConnection.setRequestProperty("SAP-Connectivity-ConsumerAccount",
tenantContext.getTenant().getAccount().getId());
}
} private void copyStream(InputStream inStream, OutputStream outStream) throws IOException { byte[] buffer = new byte[COPY_CONTENT_BUFFER_SIZE]; int len; while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
}
}
要獲取更多Jerry的原創技術文章,請關注公眾號"汪子熙"或者掃描下面二維碼:
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2154397/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 使用SAP雲平臺的destination消費Internet上的OData service
- SAP雲平臺的Document Service
- 用JavaScript訪問SAP雲平臺上的服務遇到跨域問題該怎麼辦JavaScript跨域
- 如何在SAP雲平臺上使用MongoDB服務MongoDB
- testin 雲測平臺使用問題
- SAP雲平臺架構概述架構
- SAP 業務技術平臺(BTP) 上的 Business Rules Service 使用介紹
- 刪除註釋雲平臺JS,加快DISCUZ訪問JS
- 如何在SAP雲平臺的Cloud Foundry環境下新增新的Service(服務)Cloud
- 使用JDBC操作SAP雲平臺上的HANA資料庫JDBC資料庫
- SAP雲平臺部署應用時遇到的502 Updating service failed - Bad GatewayAIGateway
- Jbuilder無法訪問Jboss4.0的JNDIUI
- SAP雲平臺對Kubernetes的支援
- Netweaver和SAP雲平臺的quota管理
- 使用SAP iRPA Studio建立的本地專案,如何部署到SAP雲平臺上?
- SAP Business Application Studio和SAP雲平臺DestinationAPP
- Android Service 訪問安全Android
- 當SAP雲平臺account的service Marke place裡找不到Machine Learning服務該怎麼辦Mac
- SAP雲平臺上部署應用時遇到disk quota不夠的問題
- 讓SAP雲平臺上的Web應用使用destination服務Web
- 最簡單的SAP雲平臺開發教程 - 如何開發UI5應用並執行在SAP雲平臺上UI
- SAP CX Upscale Commerce : SAP全新推出的電商雲平臺
- win10無internet訪問怎麼辦_win10無internet訪問許可權如何修復Win10訪問許可權
- 使用Java+SAP雲平臺+SAP Cloud Connector呼叫ABAP On-Premise系統裡的函式JavaCloudREM函式
- SAP雲平臺上的SSO Principal Propagation設定
- 企業數字化轉型與SAP雲平臺
- 使用cf curl檢視SAP雲平臺上的應用各項明細
- 使用Eclipse開發Java應用並部署到SAP雲平臺SCP上去EclipseJava
- 使用Eclipse連線SAP雲平臺上的HANA資料庫例項Eclipse資料庫
- 一步步使用SAP雲平臺的WebIDE開發SAP UI5應用WebIDEUI
- 訪問 SAP 電商雲 Storefront 時遇到的 HTTP 403 錯誤HTTP
- 國內外雲平臺訪問控制(Access Control)機制對比分析
- 在SAP雲平臺的API portal裡建立和管理APIAPI
- 在 SAP 雲平臺上部署和執行 Docker 應用Docker
- SAP 雲平臺 ABAP 程式設計環境的前世今生程式設計
- SAP雲平臺裡Global Account和Sub Account的關係
- 使用ngrok讓微信公眾平臺通過80埠訪問本機
- 如何在 SAP BTP 平臺上重用另一個已經開發好的 service