以Internet Service http://maps.googleapis.com/maps/api/distancematrix/xml?origins=Walldorf&destinations=Berlin为例,

在浏览器里访问这个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="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/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的原创技术文章,请关注公众号"汪子熙"或者扫描下面二维码:

最新文章

  1. window7 桌面新建快捷方式方法
  2. 如何修改SQL SERVER服务器的端口
  3. Hyper-V架构与VMware ESXi的差异
  4. 【javascript基础】7、继承
  5. 《OpenGL游戏编程》第9章-PlanarShadow关键代码注释
  6. SharePoint 2013 更新多个用户字段(Person or Group)
  7. sed的选项与命令简要
  8. xcode导出ipa的几种方式-by
  9. 初始化Direct3D
  10. 初识Eclipse!!
  11. mysql 表结构及基本操作
  12. python TextMining
  13. Vue项目的打包
  14. Spring Boot笔记五: Web开发之Webjar和静态资源映射规则
  15. python实现双向链表
  16. VIVADO 入门之仿真与逻辑分析仪使用
  17. HAProxy基础原理介绍
  18. redis之数据类型以及使用
  19. Revit API单位转换类
  20. JavaSE入门学习7:Java基础语法之语句(下)

热门文章

  1. AIM Tech Round 5 (rated, Div. 1 + Div. 2) E(思维,构造)
  2. 发邮件的python脚本
  3. 洛谷P3006 [USACO11JAN]瓶颈Bottleneck(堆模拟)
  4. 使用shell脚本分析Nagios的status.dat文件
  5. 聊聊tcpdump与Wireshark抓包分析
  6. eslint规则 中文备注
  7. CSU 1453: 平衡序列 学会线段树后必做
  8. Lecture--9 Sorting
  9. 四,JVM 自带命令行工具之JStack
  10. 分布式锁-session-事务相关