使用SAP云平台 + JNDI访问Internet Service
以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的原创技术文章,请关注公众号"汪子熙"或者扫描下面二维码:
使用SAP云平台 + JNDI访问Internet Service的更多相关文章
- 使用SAP云平台的destination消费Internet上的OData service
通过SAP云平台上的destination我们可以消费Internet上的OData service或者其他通过HTTP方式暴露出来的服务. 创建一个新的destination: 维护如下属性: 点击 ...
- SAP云平台的Document Service
SAP云平台以微服务的方式提供了Document的CRUD(增删改查)操作.该微服务基于标准的CMIS协议(Content Management Interoperability Service). ...
- 用JavaScript访问SAP云平台上的服务遇到跨域问题该怎么办
关于JavaScript的跨域问题(Cross Domain)的讨论, 网上有太多的资源了.国内的程序猿写了非常多的优秀文章,Jerry这里就不再重复了. 直入主题,最近我正在做一个原型开发:通过SA ...
- 如何在SAP云平台的Cloud Foundry环境下添加新的Service(服务)
我想在SAP云平台的Cloud Foundry环境下使用MongoDB的服务,但是我在Service Marketplace上找不到这个服务. cf marketplace返回的结果也没有. 解决方案 ...
- 在SAP云平台的CloudFoundry环境下消费ABAP On-Premise OData服务
我的前一篇文章 使用Java+SAP云平台+SAP Cloud Connector调用ABAP On-Premise系统里的函数介绍了在SAP云平台的Neo环境下如何通过SAP Cloud Conne ...
- 使用Java+SAP云平台+SAP Cloud Connector调用ABAP On-Premise系统里的函数
最近Jerry接到一个原型开发的任务,需要在微信里调用ABAP On Premise系统(SAP CRM On-Premise)里的某些函数.具体场景和我之前的公众号文章 Cloud for Cust ...
- 如何在SAP云平台上使用MongoDB服务
首先按照我这篇文章在SAP云平台上给您的账号分配MongboDB服务:如何在SAP云平台的Cloud Foundry环境下添加新的Service 然后从这个链接下载SAP提供的例子程序. 1. 使用命 ...
- SAP云平台运行环境Cloud Foundry和Neo的区别
SAP云平台提供了两套运行环境:Cloud Foundry和Neo 从下图能发现,Cloud Foundry的运行环境,基础设施由第三方公司提供,比如Amazon亚马逊和Microsoft微软,SAP ...
- 企业数字化转型与SAP云平台
我们生活在一个数字化时代.信息领域里发展迅猛的数字技术和成本不断降低的硬件设备,正以前所未有的方式改变着我们工作和生活的方式. Digital Mesh 美国一家著名的从事信息技术研究和提供咨询服务的 ...
随机推荐
- C/C++中的static用法总结
C中: 1. static修饰函数中的变量(栈变量):改变变量的生存期,作用域不变仍为所在函数. 只被初始化一次. 2. static修饰全局变量:限制全局变量只能被模块内访问,不可以在别的模块中用e ...
- 程序员笔记|详解Eureka 缓存机制
引言 Eureka是Netflix开源的.用于实现服务注册和发现的服务.Spring Cloud Eureka基于Eureka进行二次封装,增加了更人性化的UI,使用更为方便.但是由于Eureka本身 ...
- Dedecms本地上传缩略图无法自动添加水印的解决方法
客户遇到一个问题,DEDECMS(V5.7)后台添加文档时,本地上传缩略图无法自动添加水印(系统设置里的图片水印设置没有问题),找了半天,终于找到了解决方法,留个记号: 打开dede/archives ...
- mybatis复习01
1.mybatis的历史: mybatis是apache的一个开源项目,2010被google收购,转移到google code. mybatis是一个优秀的持久层框架,对jdbc操作进行了封装,是操 ...
- tp5模型事件回调函数中不能使用$this
tp5模型事件回调函数中不能使用$this,使用会报错,涉及到数据库操作使用Db类,不能使用$this->save()之类的方式 如果回调函数中需要使用类内函数,需要将函数定义为static,通 ...
- poj 1236 Network of Schools (强连通分量+缩点)
题目大概: 每个学校都可以把软件复制好,交给它名单上的学校. 问题A:把软件复制成几份,然后交给不同的学校,所有学校才能够都有软件. 问题B:添加几条边,能使得这个图变成强连通图. 思路: 找出所有的 ...
- selenium框架安装及webdriver安装
本文介绍的是selenium安装及webdriver安装.小实例 1.selenium介绍 selenium是一个用于web应用程序测试的工具. Selenium测试直接运行在浏览器,就向真正的用户操 ...
- JS——数组、==和===的区别
创建数组的方式: 1) <script type='text/javascript'> var aRr = new Array(1,2,3,4,'abc',3) </script&g ...
- 如何直接修改cf,of等标志位的值?
如何直接修改 cf,of 等 标志寄存器位的值? 我记得在哪个教程里见过,但是不太记得了… 貌似是在yjx驱动教程里面… 我想弄这个的原因是想验证 网上查到的 各种跳转语句(ja,jl,jg等) 需要 ...
- CodeForces - 93B(贪心+vector<pair<int,double> >+double 的精度操作
题目链接:http://codeforces.com/problemset/problem/93/B B. End of Exams time limit per test 1 second memo ...