Pivotal Knowledge Base


How to access a Service from a Java application in PWS using Statica socks


 Product  Version
 Java  7


The purpose of this article is to describe how to connect to a service or endpoint with Java using Statica in Pivotal Web Services.


Prior to your service call, you need to register java.net.Authenticator with your Statica username and password. After that, set the System Properties socksProxyHost and socksProxyPort with Statica host and port. All that information can be found in the Statica connection string. You could put all that information in a property file and read it from there or you could also get it by parsing VCAP_SERVICES. For simplicity, the following example will hardcode the configuration:

String staticaUser = "statica1234";
String staticaPass = "e4b5dadd6802178x";

Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
PasswordAuthentication p = new PasswordAuthentication(staticaUser, staticaPass.toCharArray());
return p;

String staticaHost = "us-east-1-static-hopper.statica.io";
String staticaPort = "1080"; systemProperties.setProperty("socksProxyHost",staticaHost);
// Do you service call here.

Additional Information

An example of a socks connection string is: socks5://statica1234:e4b5dadd6802178x@us-east-1-static-hopper.statica.io:1080

This is an example of how to get the credentials and host from VCAP_SERVICES using org.json library. In VCAP_SERVICES, only the HTTP URL is present and not the socks port. We won't be getting the socks port from here.

//To get VCAP_SERVICES String vcapServices = System.getenv("VCAP_SERVICES"); //To get the JSON object JSONObject vcapServicesJSON =new JSONObject(vcapServices); //To get the Statica url String staticaUrl = vcapServicesJSON.getJSONArray("statica").getJSONObject(0).getJSONObject("credentials").getString("STATICA_URL"); //To get rid of the "https:// string at the beginning staticaUrl = staticaUrl.substring(7); //To split the url using @ as separator in order to get the credentials in one string (result[0])
// and host and port in other (result[1]) String[] result = staticaUrl.split("@"); //To get the credentials strings consisting of 2 strings, user and password,
// using : as separator String[] credentials=result[0].split(":"); //To get host and port, using : as separator String[] hostAndPort=result[1].split(":"); //To get the Statica User String staticaUser=credentials[0]; //To get the Statica pass String staticaPass=credentials[1];

//To get the statica host
String staticaHost=hostAndPort[0];

You could encapsulate the Authenticator code in an auxiliary class like StaticaProxyAuthenticator described in this Statica tutorial.


Powered by Zendesk