In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
How to use SAP cloud platform + JNDI to access Internet Service. In view of this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Take Internet Service http://maps.googleapis.com/maps/api/distancematrix/xml?origins=Walldorf&destinations=Berlin as an example
Visit the url in the browser and get the output: the distance from Walldorf to Berlin.
How can a Java application deployed to the SAP cloud platform also access the internet service?
First, create a destination in the SAP cloud platform to maintain the end point of service:
Use the destination created in the SAP cloud platform in the Java code:
Then use JNDI service to read the url configured in destination:
After deploying to the SAP cloud platform, you can see the preview result in Eclipse:
The Cockpit of SAP cloud platform is shown as follows:
The browser visits are as follows:
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);} this is the answer to the question on how to access Internet Service using SAP cloud platform + JNDI. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.