摘要:以,在瀏覽器里訪問這個,得到輸出從到的距離。如何讓一個部署到云平臺的應用也能訪問到該呢首先在云平臺里創建一個,維護的在代碼里使用云平臺里創建的然后使用讀取里配置的部署到云平臺之后,在里看到結果云平臺顯示如下瀏覽器訪問如下
以Internet Service http://maps.googleapis.com/ma...,
在瀏覽器里訪問這個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:
ConnectivityServlet com.sap.cloud.sample.connectivity.ConnectivityServlet ConnectivityServlet / connectivityConfiguration com.sap.core.connectivity.api.configuration.ConnectivityConfiguration
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); } } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/69366.html
摘要:為什么云平臺要選擇支持給出的答案是基于的容器編排技術近年來已經成為行業里最流行的云原生應用開發范式之一。而云平臺通過提供的給使用者提供了這種自由度。 截至本文發稿(2019-2-10, 農歷大年初六)時為止,訪問SAP云平臺的官方網站:https://cloudplatform.sap.com... 能看到下面的網頁:SAP云平臺上的Kubernetes環境,Coming Soon(即...
摘要:關于的跨域問題的討論網上有太多的資源了。直入主題,最近我正在做一個原型開發通過云平臺和把系統上的暴露出來,給微信消費。請求得到響應之后,再將其發送給。回到我的原型開發需求,云平臺上的消費如今通過來實現,為簡單起見,我將硬編碼在里。 關于JavaScript的跨域問題(Cross Domain)的討論, 網上有太多的資源了。國內的程序猿寫了非常多的優秀文章,Jerry這里就不再重復了。 ...
摘要:關于的跨域問題的討論網上有太多的資源了。直入主題,最近我正在做一個原型開發通過云平臺和把系統上的暴露出來,給微信消費。請求得到響應之后,再將其發送給。回到我的原型開發需求,云平臺上的消費如今通過來實現,為簡單起見,我將硬編碼在里。 關于JavaScript的跨域問題(Cross Domain)的討論, 網上有太多的資源了。國內的程序猿寫了非常多的優秀文章,Jerry這里就不再重復了。 ...
摘要:首先按照我這篇文章在云平臺上給您的賬號分配服務如何在云平臺的環境下添加新的然后從這個鏈接下載提供的例子程序。部署成功后,在里能看到該應用處于狀態訪問該應用,能看到如下輸出該輸出是產生的 首先按照我這篇文章在SAP云平臺上給您的賬號分配MongboDB服務:如何在SAP云平臺的Cloud Foundry環境下添加新的Service 然后從這個鏈接下載SAP提供的例子程序。 showImg...
閱讀 2919·2023-04-25 19:08
閱讀 1416·2021-11-16 11:45
閱讀 1965·2021-10-13 09:40
閱讀 4128·2021-09-30 09:47
閱讀 2415·2019-08-30 15:44
閱讀 2261·2019-08-30 13:03
閱讀 1387·2019-08-30 12:56
閱讀 1890·2019-08-26 14:04