2

I got a problem with a httpPost from a Java-Application which is running on HCP-Trial-Account in a Tomcat7-Container. I use HttpClient 4.5.3.

The code runs on my local Tomcat7-Server fine and works. However, if deploy it to the HCP there occurs a problem.

public static Notebook getAllNotebooks(String code, String redirectUri) throws IOException, URISyntaxException{
        ClassLoader classLoader = Connection.class.getClassLoader();
        URL resource = classLoader.getResource("org/apache/http/impl/client/HttpClientBuilder.class");
        String returnUri = "https://login.live.com/oauth20_token.srf";
        HttpPost tokenRequest = new HttpPost(returnUri);
        HttpClient client = new DefaultHttpClient();//HttpClientBuilder.create().build(); //Exception: http://stackoverflow.com/questions/22330848/httpclient-example-exception-in-thread-main-java-lang-nosuchfielderror-inst

        tokenRequest.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
        tokenRequest.setEntity(new UrlEncodedFormEntity(Connection.getParametersForURLBody(code, redirectUri), Consts.UTF_8));
        tokenRequest.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0");
        HttpResponse tokenResponse = client.execute(tokenRequest); //Here it gets stuck

The problem occurs when the tokenRequest is executed. The application gets stuck and the tokenRequest runs forever.

Here are the threads that are running forever which I can see in debug mode:

SAP JVM Debug Target    
Daemon Thread [NioBlockingSelector.BlockPoller-1] (Running) 
Daemon Thread [NioBlockingSelector.BlockPoller-2] (Running) 
Daemon Thread [RMI TCP Connection(1)-127.0.0.1] (Running)   
Thread [Timer-0] (Running)  
Daemon Thread [RMI TCP Connection(2)-10.117.35.76] (Running)    
Thread [pool-1-thread-1] (Running)  
Thread [Timer-1] (Running)  
Daemon Thread [ContainerBackgroundProcessor[StandardEngine[Catalina]]] (Running)    
Daemon Thread [JCoTimeoutChecker] (Running) 
Daemon Thread [http-bio-8001-Acceptor-0] (Running)  
Daemon Thread [http-bio-8001-AsyncTimeout] (Running)    
Daemon Thread [http-nio-127.0.0.1-9001-ClientPoller-0] (Running)    
Daemon Thread [http-nio-127.0.0.1-9001-Acceptor-0] (Running)    
Daemon Thread [http-nio-8041-ClientPoller-0] (Running)  
Daemon Thread [http-nio-8041-Acceptor-0] (Running)  
Daemon Thread [ajp-bio-8009-Acceptor-0] (Running)   
Daemon Thread [ajp-bio-8009-AsyncTimeout] (Running) 
Daemon Thread [RMI TCP Connection(10)-127.0.0.1] (Running)  
Daemon Thread [RMI TCP Connection(11)-10.117.10.34] (Running)   
Thread [main] (Running) 
Daemon Thread [http-nio-8041-exec-1] (Running)  
Daemon Thread [http-nio-8041-exec-2] (Running)  
Daemon Thread [http-nio-8041-exec-3] (Running)  
Daemon Thread [http-nio-8041-exec-4] (Running)  
Daemon Thread [http-nio-8041-exec-5] (Running)  
Thread [pool-2-thread-1] (Running)  
Daemon Thread [http-nio-8041-exec-6] (Stepping) 
Daemon Thread [http-nio-8041-exec-7] (Running)  
Daemon Thread [http-nio-8041-exec-8] (Running)  
Daemon Thread [http-nio-8041-exec-9] (Running)  
Daemon Thread [http-nio-8041-exec-10] (Running) 
Daemon Thread [RMI TCP Connection(idle)] (Running)  
Daemon Thread [RMI TCP Connection(idle)] (Running)  

The program gets stucked at this point.

I have no clue what to do and would really appreciate some hints and help :).

Greetings Maverin

MaverinCode
  • 469
  • 1
  • 4
  • 15
  • Most probably you need to configure a proxy. See the documentation referenced in the given answer. – vap78 Feb 27 '17 at 19:46
  • Thats all fine, but the examples are all with UrlConnection, I don´t know how to execute it with httpclient. Would be really thankfull for some more help :). – MaverinCode Feb 28 '17 at 12:50
  • You can check https://hc.apache.org/httpcomponents-client-ga/examples.html for an example with apache http client and proxy – vap78 Feb 28 '17 at 14:15

2 Answers2

0

Normally, you should use the Connectivity service in order to access Internet resources from within HCP-deployed applications. I guess that trying to do it directly causes you app to freeze / crash. Might also be something else that I am missing.

You can find info about doing this in the official documentation: help.sap.com.

In a nutshell, you will have to:

  • Create a destination (either in your HCP account or directly in the Java app) towards the given site.
  • Because you use HTTPS, you will most likely have to import the certificate of the remote host into the trust store (docu link).
  • Declare the connectivity configuration in the web.xml.
  • Use the JNDI (i.e. InitialContext) to obtain the connectivity configuration in your java code. This configuration can be then used to obtain an URL towards the remote resource. You can use this URL to either open a direct connection or to pass it to the HttpClient (the URL class has a toURI method).
Serban Petrescu
  • 5,127
  • 2
  • 17
  • 34
  • In the HCP Tomcat7 runtime the connectivity service is mainly a secure storage for configurations (i.e. URL, user/password etc). All the stuff like http client generation, trust store configuration etc is done by the application. – vap78 Feb 27 '17 at 19:45
  • I followed this example: https://help.hana.ondemand.com/help/frameset.htm?4da3b13c88ce4220bbd56a4361799668.html But I don´t know how to do it with httpclient instead of ur-connection? – MaverinCode Feb 28 '17 at 12:23
  • 1
    The problem is, I tried all the examples for tomcat 7, but I really don´t know how to do it with httpclient instead of url-connection. – MaverinCode Feb 28 '17 at 13:28
0

I've had this problem too, and in the end, I found a solution that works with Tomcat8 aswell. The key is to make the connection with a builder and then use useSystemProperties(). This way you get the proxy properties thats needed for the HCP. The extra stuff with the connectionmanager isn't strictly needed, but it helps with the performance. The param-syncro is legacy stuff.

org.apache.http.impl.conn.PoolingHttpClientConnectionManager cm
public static HttpClient getHttpClient(){
    if(cm == null){
        synchronized(params){
            cm = new PoolingHttpClientConnectionManager();
            // Increase max total connection to 200
            cm.setMaxTotal(200);
            // Increase default max connection per route to 20
            cm.setDefaultMaxPerRoute(200);
        }
    }
    org.apache.http.impl.client.CloseableHttpClient httpClient = HttpClients.custom()
            .setConnectionManager(cm)
            .useSystemProperties()
            .build();
    return httpClient;
}