Java Quick Start Guide with QuotaGuard Static IP's

    Table of contents

    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@eu-west-shield-01.quotaguard.com: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, 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(){
        QuotaGuardProxyAuthenticator proxy = new QuotaGuardProxyAuthenticator();
        String testUrl = "http://ip.jsontest.com/";
        System.out.println(getResponse(proxy, testUrl));
      }
      
      public String getResponse(QuotaGuardProxyAuthenticator 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 QuotaGuardProxyAuthenticator extends Authenticator{
      private String user, password, host;  
      private int port;
      private ProxyAuthenticator auth;
    
      public QuotaGuardProxyAuthenticator() {
          String proxyUrlEnv = System.getenv("QUOTAGUARD_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 QUOTAGUARD_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.


    Ready to Get Started?

    Get in touch or create a free trial account