Advertisement
Guest User

Retrofit Builder

a guest
Mar 14th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.81 KB | None | 0 0
  1. package com.mpc.ebanking.services.rest.service.impl;
  2.  
  3. import java.io.IOException;
  4. import java.net.InetSocketAddress;
  5. import java.net.Proxy;
  6. import java.util.concurrent.TimeUnit;
  7.  
  8. import org.apache.commons.codec.binary.Base64;
  9. import org.apache.commons.logging.Log;
  10. import org.apache.commons.logging.LogFactory;
  11.  
  12. import com.mpc.ebanking.services.rest.service.interceptor.BasicAuthInterceptor;
  13.  
  14. import okhttp3.Authenticator;
  15. import okhttp3.ConnectionPool;
  16. import okhttp3.HttpUrl;
  17. import okhttp3.OkHttpClient;
  18. import okhttp3.OkHttpClient.Builder;
  19. import okhttp3.logging.HttpLoggingInterceptor;
  20. import okhttp3.logging.HttpLoggingInterceptor.Level;
  21. import okhttp3.Request;
  22. import okhttp3.Response;
  23. import okhttp3.Route;
  24. import retrofit2.Retrofit;
  25. import retrofit2.converter.jackson.JacksonConverterFactory;
  26. import sun.net.www.protocol.http.logging.HttpLogFormatter;
  27.  
  28. public class RetrofitBuilder {
  29.     protected static final Log log = LogFactory.getLog(RetrofitBuilder.class);
  30.     private String proxyHost;
  31.     private int proxyPort;
  32.     private boolean useProxy, useUsernamePassword;
  33.     private String proxyUsername, proxyPassword;
  34.     private Retrofit retrofit;
  35.     private String baseUrl;
  36.     private String baseUsername;
  37.     private String basePassword;
  38.  
  39.     public RetrofitBuilder(String proxyHost, int proxyPort, boolean useProxy, boolean useUsernamePassword,
  40.             String proxyUsername, String proxyPassword, String baseUrl,String baseUsername, String basePassword) {
  41.         super();
  42.         this.proxyHost = proxyHost;
  43.         this.proxyPort = proxyPort;
  44.         this.useProxy = useProxy;
  45.         this.useUsernamePassword = useUsernamePassword;
  46.         this.proxyUsername = proxyUsername;
  47.         this.proxyPassword = proxyPassword;
  48.         this.baseUrl = baseUrl;
  49.         this.baseUsername = baseUsername;
  50.         this.basePassword = basePassword;
  51.     }
  52.  
  53.     public Retrofit getRetrofit() {
  54.         return retrofit;
  55.     }
  56.  
  57.     public void setRetrofit(Retrofit retrofit) {
  58.         this.retrofit = retrofit;
  59.     }
  60.  
  61.     public OkHttpClient getClient(){
  62.         Builder builder = new OkHttpClient.Builder().connectionPool(new ConnectionPool(100, 60, TimeUnit.SECONDS))
  63.                 .connectTimeout(60, TimeUnit.SECONDS).readTimeout(60, TimeUnit.SECONDS)
  64.                 .writeTimeout(60, TimeUnit.SECONDS);
  65.  
  66.         HttpLoggingInterceptor logging = new HttpLoggingInterceptor();  
  67.         logging.setLevel(Level.BASIC);
  68.        
  69.         builder.addInterceptor(new BasicAuthInterceptor(baseUsername, basePassword));
  70.         builder.addInterceptor(logging);
  71.        
  72.         if (useProxy){
  73.             builder.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)));
  74.  
  75.             if (useUsernamePassword) builder.proxyAuthenticator(authenticator());
  76.         }
  77.  
  78.         return builder.build();
  79.     }
  80.  
  81.     private Authenticator authenticator(){
  82.         Authenticator authenticator = new Authenticator() {
  83.             @Override
  84.             public Request authenticate(Route arg0, Response arg1) throws IOException {
  85.                 String password = proxyUsername + ":" + proxyPassword;
  86.                 byte[] encodedPassword = Base64.encodeBase64(password.getBytes());
  87.                
  88.                 return arg1.request().newBuilder()
  89.                        .header("Proxy-Authorization", new String(encodedPassword))
  90.                        .build();
  91.             }
  92.         };
  93.        
  94.         return authenticator;
  95.     }
  96.    
  97.     private void init(){
  98.         this.retrofit = new Retrofit.Builder()
  99.                 .baseUrl(HttpUrl.parse(baseUrl))
  100.                 .addConverterFactory(JacksonConverterFactory.create())
  101.                 .client(this.getClient())
  102.                 .build();
  103.     }
  104.    
  105.     public static RetrofitBuilder build(String proxyHost, int proxyPort,
  106.             boolean useProxy, boolean useUsernamePassword,
  107.             String proxyUsername, String proxyPassword, String baseUrl,
  108.             String baseUsername, String basePassword) {
  109.  
  110.         RetrofitBuilder retrofitBuilder = new RetrofitBuilder(proxyHost,
  111.                 proxyPort, useProxy, useUsernamePassword, proxyUsername,
  112.                 proxyPassword, baseUrl, baseUsername, basePassword);
  113.  
  114.         retrofitBuilder.init();
  115.  
  116.         return retrofitBuilder;
  117.     }
  118.    
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement