以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的原创技术文章,请关注公众号"汪子熙"或者扫描下面二维码:
相关推荐
这个是个人写的小程序用于初学者的练习Struts和 Hibernate和Ajax 三个方面都有而且容易看懂!
ssm+maven+jndi+jdbc,完整的ssm框架,内含mysql和oracle两种连库方式,和jndi与数据库连接池两种,spring采用5.0.4,mybatis采用3.4.6
Tomcat6+spring+jndi配置数据源说明.docx
JSP+Serlet+JNDI实现酒店管理系统,真个项目系统源码及数据库分离出来打包下载,Tomcat6.0服务器下测试运行成功。
关于ibatis的jndi链接步骤和设置。以mysql为例。
Springmvc +JNDI 在Tomcat下 配置数据源 一、 简介 二、 tomcat配置jndi有三种方式。 第一种:单个应用独享数据源 第二种:配置全局JNDI数据源,应用到单个应用 三、 数据源配置在Tomcat/conf/ context.xml 文件...
详细讲解了JNDI概念和目录服务的配置,以LDAP服务器为平台,全面讲解JNDI操作LDAP.详细这本书是很多人都需要的,在大型项目中是经常使用的.
我知道好多人在做JNDI的时候都报了错,tomcat5和tomcat6配置都是一样的,不要听别人在哪里忽悠,我今晚做过测试的,现在我做了一个小例子,jar包我没有放进来,1.就是必须把数据库驱动必须加在tomcat的lib目录下,当前项目...
JNDI开发jar 本软件包是JNDI的"文件系统服务提供者",解压缩后,将lib文件夹下的providerutil.jar及fscontext.jar添加到classpath,即可实现有关文件系统的命名\目录服务.
jboss配置MySql的JNDI
c3p0配置数据库连接池、数据库连接池 已经测试通过,可以提供给新入门的朋友学习之用,如果是要结合spring就不用下载了,网上其他地方应该很多资料,简单修改一下,原理都是一样的。
ssh整合来开发网站,通过MVC框架,用于快速开发Java Web应用。
配置了tomcat之后发现jndi好简单啊,可是碰到了hibernate该怎么做呢,本例详细解析
jboss+mysql+jndi
WikiMNV-LDAP-测试 测试OpenLDAP + JNDI
hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+...
附件为springboot+jndi+tomcat的事例代码, 工程中的jndi.txt有详细说明
eclipse+mysql+tomcat配置JNDI实现例子 博文链接:https://uuplace.iteye.com/blog/88878
java 采用JNDI访问数据库(三种方法)