资讯专栏INFORMATION COLUMN

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

libin19890520 / 2221人阅读

摘要:以,在浏览器里访问这个,得到输出从到的距离。如何让一个部署到云平台的应用也能访问到该呢首先在云平台里创建一个,维护的在代码里使用云平台里创建的然后使用读取里配置的部署到云平台之后,在里看到结果云平台显示如下浏览器访问如下

以Internet Service http://maps.googleapis.com/ma...,

在浏览器里访问这个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:




    
    
        ConnectivityServlet
        com.sap.cloud.sample.connectivity.ConnectivityServlet
    
    
        ConnectivityServlet
        /
    

    
    
    connectivityConfiguration
    com.sap.core.connectivity.api.configuration.ConnectivityConfiguration
    
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);
        }
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/69366.html

相关文章

  • SAP平台对Kubernetes的支持

    摘要:为什么云平台要选择支持给出的答案是基于的容器编排技术近年来已经成为行业里最流行的云原生应用开发范式之一。而云平台通过提供的给使用者提供了这种自由度。 截至本文发稿(2019-2-10, 农历大年初六)时为止,访问SAP云平台的官方网站:https://cloudplatform.sap.com... 能看到下面的网页:SAP云平台上的Kubernetes环境,Coming Soon(即...

    joyqi 评论0 收藏0
  • 用JavaScript访问SAP平台上的服务遇到跨域问题该怎么办

    摘要:关于的跨域问题的讨论网上有太多的资源了。直入主题,最近我正在做一个原型开发通过云平台和把系统上的暴露出来,给微信消费。请求得到响应之后,再将其发送给。回到我的原型开发需求,云平台上的消费如今通过来实现,为简单起见,我将硬编码在里。 关于JavaScript的跨域问题(Cross Domain)的讨论, 网上有太多的资源了。国内的程序猿写了非常多的优秀文章,Jerry这里就不再重复了。 ...

    用户84 评论0 收藏0
  • 用JavaScript访问SAP平台上的服务遇到跨域问题该怎么办

    摘要:关于的跨域问题的讨论网上有太多的资源了。直入主题,最近我正在做一个原型开发通过云平台和把系统上的暴露出来,给微信消费。请求得到响应之后,再将其发送给。回到我的原型开发需求,云平台上的消费如今通过来实现,为简单起见,我将硬编码在里。 关于JavaScript的跨域问题(Cross Domain)的讨论, 网上有太多的资源了。国内的程序猿写了非常多的优秀文章,Jerry这里就不再重复了。 ...

    Jinkey 评论0 收藏0
  • 如何在SAP平台使用MongoDB服务

    摘要:首先按照我这篇文章在云平台上给您的账号分配服务如何在云平台的环境下添加新的然后从这个链接下载提供的例子程序。部署成功后,在里能看到该应用处于状态访问该应用,能看到如下输出该输出是产生的 首先按照我这篇文章在SAP云平台上给您的账号分配MongboDB服务:如何在SAP云平台的Cloud Foundry环境下添加新的Service 然后从这个链接下载SAP提供的例子程序。 showImg...

    frolc 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<