2
\$\begingroup\$

I have a java service class which will call a REST service and get response back.
It is working fine in developer environment where less requests are made, but I still wanted to get a review.

My service method call is like:

private String getResponse(Object request, String cUri) {
String responseStr = StringUtils.EMPTY;
    try {

        Gson requestJson = new GsonBuilder().disableHtmlEscaping().create();
        String inputJson =  requestJson.toJson(request);
        logger.debug("Input JSON : " + inputJson);

        responseStr = connect.getResponse(cUri, inputJson,config.getHttpClient());
        logger.debug("Outut JSON for provided request" + responseStr);

    } catch (Exception e) {
        logger.error("Exception while getting response from service : " + e,e);
    }
    return responseStr;
}

Here config.getHttpClient()); is as follows:

public class Test {

    private static CloseableHttpClient httpClient;
    private static PoolingHttpClientConnectionManager connectionManager;

    public CloseableHttpClient getHttpClient() {

        if(httpClient != null){
            return httpClient;
        }

        try {

            SSLContext sslContext = getSSLContext();

            SSLConnectionSocketFactory trustSelfSignedSocketFactory = new SSLConnectionSocketFactory(
                    sslContext == null 
                    ? SSLContext.getDefault() : sslContext
                            ,new String[] { "TLSv1", "TLSv1.2", "TLSv1.1" }
                            ,null
                            ,NoopHostnameVerifier.INSTANCE);

            Registry<ConnectionSocketFactory> socketFactoryRegistry= RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("https", trustSelfSignedSocketFactory)
                    .register("http", PlainConnectionSocketFactory.getSocketFactory()).build();

            connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
            connectionManager.setMaxTotal(Integer.parseInt(props.getProperty(Constants._CONNECTION_COUNT)));
            connectionManager.setDefaultMaxPerRoute(Integer.valueOf(props.getProperty(Constants._MAX_PER_ROUTE)));

            httpClient = HttpClientBuilder
                    .create()
                    .setConnectionManager(connectionManager)
                    .setConnectionManagerShared(true)
                    .build();

        } catch (Exception e) {
            logger.error("Error in getting rest server connection" + e, e);
        }

        return httpClient;

    }

    private SSLContext getSSLContext(){
        //return sslContext;
    }

}

And this Test class is initialized as Singleton bean.

And public String getResponse(String uri, String inputJson, CloseableHttpClient httpClient) is as follows:

public class ConnectionHelper{

    public String getResponse(String uri, String inputJson, CloseableHttpClient httpClient) throws Exception{

        String responseStr = StringUtils.EMPTY;
        HttpPost request = new HttpPost(uri);
        StringEntity params = new StringEntity(inputJson);
        request.addHeader("Content-type", "application/json");
        request.setEntity(params);

        try(final CloseableHttpResponse response= httpClient.execute(request)){
            responseStr = EntityUtils.toString(response.getEntity(),"UTF-8");
        }catch(Exception e){
            logger.error("Error while exeucting on request " + e,e);
        }finally {
            httpClient.close();
        }

        return responseStr;
    }

}
\$\endgroup\$

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.