With Internet Service http://maps.googleapis.com/maps/api/distancematrix/xml?origins=Walldorf&destinations= Berlin, for example,
Access this URL in the browser to get the output: the distance from Walldorf to Berlin.
How can a Java application deployed to the SAP Cloud Platform also have access to the Internet service?
Start by creating a destination in the SAP Cloud platform to maintain the end point of the service:
Use the destination created in the SAP cloud Platform in Java code:
Then use the Jndi service to read the URL configured in destination:
After deploying to the SAP cloud Platform, see the preview results in eclipse:
The SAP cloud platform cockpit appears as follows:
Browser access is as follows:
Xml:
<?xmlVersion= "1.0" encoding= "UTF-8"?><web-appxmlns: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 Sam Ples) -- <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 classConnectivityservletextendsHttpServlet {@Resource PrivateTenantcontext Tenantcontext;Private Static Final LongSerialversionuid = 1L;Private Static Final intCopy_content_buffer_size =1024x768;Private Static FinalLogger Logger = loggerfactory.GetLogger(Connectivityservlet.class);Private Static FinalString On_premise_proxy ="Onpremise";@Override Public void Doget(HttpServletRequest request, httpservletresponse response)throwsServletexception, IOException {httpurlconnection urlconnection =NULL; String destinationname = Request.GetParameter("Destname");if(Destinationname = =NULL) {Destinationname ="Google_map"; }Try{Context CTX =NewInitialContext (); 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%sis not found. Hint: "+"Make sure to the destination configured.", Destinationname));return; } String value = Destconfiguration.GetProperty("URL"); URL url =NewURL (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 a 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); } }PrivateProxyGetProxy(String Proxytype) {Proxy proxy = proxy.}No_proxy; String ProxyHost =NULL; String ProxyPort =NULL;if(On_premise_proxy.equals(Proxytype)) {//Get proxy for on-premise destinationsProxyHost = System.getenv("Hc_op_http_proxy_host"); ProxyPort = System.getenv("Hc_op_http_proxy_port"); }Else{//Get proxy for Internet destinationsProxyHost = System.GetProperty("Https.proxyhost"); ProxyPort = System.GetProperty("Https.proxyport"); }if(ProxyPort! =NULL&& ProxyHost! =NULL) {intProxyportnumber = Integer.parseint(ProxyPort); Proxy =NewProxy (proxy.Type.HTTP,NewInetsocketaddress (ProxyHost, Proxyportnumber)); }returnProxy }Private void Injectheader(HttpURLConnection urlconnection, String proxytype) {if(On_premise_proxy.equals(Proxytype)) {//Insert header for On-premise connectivity with the consumer account nameURLConnection.Setrequestproperty("Sap-connectivity-consumeraccount", Tenantcontext.gettenant().Getaccount().getId()); } }Private void CopyStream(InputStream instream, OutputStream outstream)throwsIOException {byte[] buffer =New byte[Copy_content_buffer_size];intLen while(len = instream.Read(buffer)) != -1) {OutStream.Write(Buffer,0, Len); } }}
To get more original Jerry's technical articles, please follow the public number "Wang Zixi" or scan the QR code below:
Access Internet service using SAP Cloud Platform + jndi