使用SAP云平台 + JNDI访问Internet Service

简介: 使用SAP云平台 + JNDI访问Internet Service

image.pngimage.pngimage.pngimage.pngimage.pngimage.pngimage.pngweb.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);
        }
    }
}
相关文章
|
7月前
|
移动开发 IDE Java
SAP 云平台从 Neo 到 Multi-Cloud 的演化历史
SAP 云平台从 Neo 到 Multi-Cloud 的演化历史
158 0
|
6月前
|
数据库 数据库管理
如何在 SAP BTP 平台上重用另一个已经开发好的 service
如何在 SAP BTP 平台上重用另一个已经开发好的 service
66 1
|
2月前
|
机器学习/深度学习 人工智能 监控
SAP Sales Cloud,Service Cloud 和 SAP BTP 平台上的 AI 集成场景
SAP Sales Cloud,Service Cloud 和 SAP BTP 平台上的 AI 集成场景
64 0
|
6月前
关于 SAP 电商云 Spartacus UI Navigation Service 执行的一些明细
关于 SAP 电商云 Spartacus UI Navigation Service 执行的一些明细
46 0
|
7月前
|
数据中心
什么是 SAP 云平台的 multi-cloud architecture
什么是 SAP 云平台的 multi-cloud architecture
59 1
|
7月前
|
搜索推荐
SAP 电商云 Assisted Service Module (ASM) 功能模块讲解
SAP 电商云 Assisted Service Module (ASM) 功能模块讲解
53 0
|
7月前
|
JavaScript
SAP 电商云 Spartacus UI Event Service 实现明细介绍
SAP 电商云 Spartacus UI Event Service 实现明细介绍
38 0
|
7月前
|
SQL JavaScript 前端开发
SAP Field Service Management 和微信集成的案例分享和实现介绍
SAP Field Service Management 和微信集成的案例分享和实现介绍
60 0
|
7月前
|
数据采集 存储 JavaScript
SAP Spartacus 的 TMS 和 Event Service 实现的关联关系
SAP Spartacus 的 TMS 和 Event Service 实现的关联关系
52 0
|
7月前
|
机器学习/深度学习 JavaScript 前端开发
SAP 云平台 ABAP 编程环境的前世今生
SAP 云平台 ABAP 编程环境的前世今生
64 0