On April 1, 2021, we are moving all of our QuotaGuard Support Documentation to 


https://quotaguard.github.io/qg-docs/


 Please Update Your Support Bookmarks


 Documentation for this article will be maintained at 


https://quotaguard.github.io/qg-docs/quickstart-java.html 


Please click on the above link to ensure you are reading 

the most recent and updated documentation.


Installation

When you sign up you will be provided with a unique username and
password and a connection string that you can use when configuring your
proxy service in your application.

http://username:password@static.statica.io:9293

All requests that you make via this proxy will appear to the destination
server to originate from one of the two static IPs you will be assigned
when you sign up.

We recommend you store the connection string in an environment
variable QUOTAGUARD\_URL

If you have signed up via the add-ons platform on Heroku or cloudControl
this variable will be automatically set in your production environment.

Integration

You can use the standard Java libraries with QuotaGuard to access HTTP
and HTTPS APIs via your static IP. 

The below examples uses a custom class to encapsulate the QuotaGuard
proxy logic and an HttpURLConnection to make the HTTP request.

This sample uses the Java 8 Base64 class to encode the authorization
String.

If using a version less than Java 8 you should use another Base64
encoder implementation, such as the Apache Commons Codec

    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.Authenticator;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.zip.GZIPInputStream;
    public class HTTPProxyDemo {
      public static void main(String[] args) {
        new HTTPProxyDemo();
      }
      
      public HTTPProxyDemo(){
        StaticaProxyAuthenticator proxy = new StaticaProxyAuthenticator();
        String testUrl = "http://ip.jsontest.com/";
        System.out.println(getResponse(proxy, testUrl));
      }
      
      public String getResponse(StaticaProxyAuthenticator proxy, String urlToRead) {
            String result = "";
            try {
             URL url = new URL(urlToRead);
               HttpURLConnection conn = (HttpURLConnection) url.openConnection();
               conn.setRequestProperty("Proxy-Authorization", "Basic " + proxy.getEncodedAuth());
               conn.setRequestProperty("Accept-Encoding", "gzip");
               Authenticator.setDefault(proxy.getAuth());
               conn.setRequestMethod("GET");
               InputStream is = conn.getInputStream();
               if(conn.getContentEncoding()!=null && conn.getContentEncoding().equalsIgnoreCase("gzip")){
                 is = new GZIPInputStream(is);
               }
               byte[] buffer = new byte[1024];
               int len;
               ByteArrayOutputStream bos = new ByteArrayOutputStream();
               while (-1 != (len = is.read(buffer))) {
                 bos.write(buffer, 0, len);
               }           
               result = new String(bos.toByteArray());
               is.close();
            } catch (IOException e) {
               e.printStackTrace();
            } catch (Exception e) {
               e.printStackTrace();
            }
            return result;
         }    
    }
    import java.net.Authenticator;
    import java.net.MalformedURLException;
    import java.net.PasswordAuthentication;
    import java.net.URL;
    
    public class StaticaProxyAuthenticator extends Authenticator{
      private String user, password, host;  
      private int port;
      private ProxyAuthenticator auth;
    
      public StaticaProxyAuthenticator() {
          String proxyUrlEnv = System.getenv("STATICA_URL");
        if(proxyUrlEnv!=null){
          try {
            URL proxyUrl = new URL(proxyUrlEnv);
            String authString = proxyUrl.getUserInfo();
            user = authString.split(":")[0];
            password = authString.split(":")[1];
            host = proxyUrl.getHost();
            port = proxyUrl.getPort();    
            auth = new ProxyAuthenticator(user,password);
            setProxy();
          } catch (MalformedURLException e) {
            e.printStackTrace();
          }     
        }
        else{
          System.err.println("You need to set the environment variable STATICA_URL!");
        }
        
        }
       
        private void setProxy(){
          System.setProperty("http.proxyHost", host);
            System.setProperty("http.proxyPort", String.valueOf(port));
            System.setProperty("https.proxyHost",host);
            System.setProperty("https.proxyPort", String.valueOf(port));
        }
        
        public String getEncodedAuth(){
          //If not using Java8 you will have to use another Base64 encoded, e.g. apache commons codec.
          String encoded = java.util.Base64.getEncoder().encodeToString((user + ":" + password).getBytes());      
          return encoded;
        }
        
        public ProxyAuthenticator getAuth(){
          return auth;
        }
        
        class ProxyAuthenticator extends Authenticator {
    
            private String user, password;
    
            public ProxyAuthenticator(String user, String password) {
                this.user = user;
                this.password = password;
            }
    
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user, password.toCharArray());
            }
        }
        
    }

We have examples for other common libraries as well, but if you can't
find what you are looking for just send us a Support ticket and we'll get one sent to you.


If you have questions, or if this solution doesn’t work or fit your use case, please reach out to us at Support so we can help figure it out with you.


Working with sensitive data, like HIPAA, Financial, or Personally Identifiable Information (PII)? 
    Then you will want to check out our QuotaGuard Shield solution, it's the same as QuotaGuard Static, but with stronger end to end security for your requests. We can also help migrate current Static customers to Shield for free, just reach out to us at Support to request assistance.