以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的更多相关文章

  1. 使用SAP云平台的destination消费Internet上的OData service

    通过SAP云平台上的destination我们可以消费Internet上的OData service或者其他通过HTTP方式暴露出来的服务. 创建一个新的destination: 维护如下属性: 点击 ...

  2. SAP云平台的Document Service

    SAP云平台以微服务的方式提供了Document的CRUD(增删改查)操作.该微服务基于标准的CMIS协议(Content Management Interoperability Service). ...

  3. 用JavaScript访问SAP云平台上的服务遇到跨域问题该怎么办

    关于JavaScript的跨域问题(Cross Domain)的讨论, 网上有太多的资源了.国内的程序猿写了非常多的优秀文章,Jerry这里就不再重复了. 直入主题,最近我正在做一个原型开发:通过SA ...

  4. 如何在SAP云平台的Cloud Foundry环境下添加新的Service(服务)

    我想在SAP云平台的Cloud Foundry环境下使用MongoDB的服务,但是我在Service Marketplace上找不到这个服务. cf marketplace返回的结果也没有. 解决方案 ...

  5. 在SAP云平台的CloudFoundry环境下消费ABAP On-Premise OData服务

    我的前一篇文章 使用Java+SAP云平台+SAP Cloud Connector调用ABAP On-Premise系统里的函数介绍了在SAP云平台的Neo环境下如何通过SAP Cloud Conne ...

  6. 使用Java+SAP云平台+SAP Cloud Connector调用ABAP On-Premise系统里的函数

    最近Jerry接到一个原型开发的任务,需要在微信里调用ABAP On Premise系统(SAP CRM On-Premise)里的某些函数.具体场景和我之前的公众号文章 Cloud for Cust ...

  7. 如何在SAP云平台上使用MongoDB服务

    首先按照我这篇文章在SAP云平台上给您的账号分配MongboDB服务:如何在SAP云平台的Cloud Foundry环境下添加新的Service 然后从这个链接下载SAP提供的例子程序. 1. 使用命 ...

  8. SAP云平台运行环境Cloud Foundry和Neo的区别

    SAP云平台提供了两套运行环境:Cloud Foundry和Neo 从下图能发现,Cloud Foundry的运行环境,基础设施由第三方公司提供,比如Amazon亚马逊和Microsoft微软,SAP ...

  9. 企业数字化转型与SAP云平台

    我们生活在一个数字化时代.信息领域里发展迅猛的数字技术和成本不断降低的硬件设备,正以前所未有的方式改变着我们工作和生活的方式. Digital Mesh 美国一家著名的从事信息技术研究和提供咨询服务的 ...

随机推荐

  1. .net core webapi +ddd(领域驱动)+nlog配置+swagger配置 学习笔记(2)

    DDD领域驱动模型设计 什么是DDD 软件开发不是一蹴而就的事情,我们不可能在不了解产品(或行业领域)的前提下进行软件开发,在开发前,通常需要进行大量的业务知识梳理,而后到达软件设计的层面,最后才是开 ...

  2. 解决Navicat无法连接到Mysql

    Navicat无法连接到Mysql,返回的错误码是Lost connection to MySQL server at ‘reading initial communication packet’, ...

  3. Linux调优(内存,CPU)

    一.相关概念简介 system call:系统调用 time slice:cpu时间片 O(1):Linux系统进程调度器 page frame:分页 RSS:常驻内存集,无法被页面化的数据 MMU: ...

  4. MySQL的高可用实现:MySQL系列之十四

    MySQL的高可以有三种实现方式:多主模式(Multi Master MySQL),MHA(Master High Availability)和 Galera Cluster:wresp 一.MHA ...

  5. CentOS系统日志

    日志分类: 一./var目录 一些数据库如MySQL则在/var/lib下,用户未读的邮件的默认存放地点为/var/spool/mail 二.:/var/log/ 系统的引导日志:/var/log/b ...

  6. atom快捷键

    文件切换 ctrl-shift-s 保存所有打开的文件 cmd-shift-o 打开目录 cmd-\ 显示或隐藏目录树 ctrl-0 焦点移到目录树 目录树下,使用a,m,delete来增加,修改和删 ...

  7. putty提示Network error:Software caused connection abort

    在 sshd host 的 /etc/ssh/sshd_config 设定: TCPKeepAlive yes,和将LoginGraceTime的值设为0,默认为2m,然后使用service sshd ...

  8. POJ1045 Bode Plot

    题目来源:http://poj.org/problem?id=1045 题目大意: 如图所示的交流电路,假设电路处于稳定状态,Vs为电源电压,w是频率,单位为弧度每秒,t表示时间. 则:V1 = Vs ...

  9. State模式(状态设计模式)

    State??? State模式中,我们用类来表示状态.以类来表示状态后,我们就能通过切换类来方便地改变对象的状态.当需要增加新的状态时,如何修改代码这个问题也会很明确. 直接用状态代替硬编码 依赖于 ...

  10. 我在B站学习 清华大学教授带你学习c++(进阶)构造函数

    B站av11459203的一系列视频,跳过了基础篇直接进入进阶,从此难度开始加大.这里做出一些笔记分享一下. 我是1.25速度看的..对应分P 37-38 构造函数的作用 将对象初始化为一个特定的初始 ...