1

Can I control or configure the proxy server information for an individual webapp deployed in Tomcat? At work, I am behind a corporate firewall, so any code that accesses external web resources (3rd party REST API, in my case) will work only if I set proxy information in the executing environment:

System.setProperty("https.proxyHost", "proxy.company.com");
System.setProperty("https.proxyPort", "2345");

But I don't want to include this in the code, because the production environment does not involve the proxy server.

How can I externalize this configuration so I can set proxy server configuration/flags in the development environment but skip the step in the production environment? Whatever the solution is, it should affect only a particular web application and not all web applications on the Tomcat server.

Edited 11/21/2016: I am using Spring's RestTemplate to invoke the REST API method.

@RequestMapping(value="/{userId}",
        method = RequestMethod.GET)
public ResponseEntity<String> getUserInfo(@PathVariable String userId,
        @RequestHeader String apikey) {

    String url = UserAPIProperties.getUsersUrl() + "/users/{userId}";

    // Set headers for the request
    Map<String,Object> headersMap = new HashMap<String,Object>();
    headersMap.put("apiKey", apiKey);
    headersMap.put("Accept", "application/json");
    HttpEntity<?> httpEntity = getHttpEntity(headersMap);

    log.debug("API Key: {}, User ID: {}", apikey, userId);

    RestTemplate restTemplate = new RestTemplate();
    return restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class, userId);
}
Web User
  • 7,438
  • 14
  • 64
  • 92

1 Answers1

0

Don't do this using system properties.

Instead, use the REST API's actual API to set the proxy information directly. Otherwise, you'll affect the entire JVM which is probably not what you want to do.

Since you are using Spring's RestTemplate, this SO answer should help you programmatically change the proxy being used:

Community
  • 1
  • 1
Christopher Schultz
  • 20,221
  • 9
  • 60
  • 77
  • Yes, that's exactly what I am trying to avoid. Can you include a code snippet on how I can achieve that using API? Also, this applies to development only, so I don't really want to impact the REST API code. In other words, production code should not be setting any proxy information. – Web User Nov 18 '16 at 20:28
  • You never mentioned what 3rd-party REST API you were using, so no, I can't provide a code sample. – Christopher Schultz Nov 18 '16 at 21:27
  • I am not sure why a specific 3rd party service would be relevant to the code. All I have is their service URL (incl. the operation) and the API key. If I set the proxy server info in the JVM, the REST call returns a response just fine. If I don't set the proxy server info, the call times out. I am aware of only two options... (i) set the info in the environment or (ii) conditionally use a `java.net.Proxy` and some type of `org.springframework.http.client.ClientHttpRequestFactory` if the environment corresponds to development. I wanted to avoid impacting the entire JVM, or polluting the code. – Web User Nov 18 '16 at 22:00
  • 1
    Your use of the phrase "3rd party REST API" wasn't clear that you were talking about the HTTP portion of the API or if a Java-language binding had been provided and you were using that. The important part is *which Java API you are using*... it doesn't matter what service vendor you are talking about on the other end of the HTTP conversation. – Christopher Schultz Nov 21 '16 at 17:24
  • I am using Spring's `RestTemplate`. I added a relevant snippet to my question. Thanks for clarifying your comment. – Web User Nov 21 '16 at 23:19