258 lines
		
	
	
		
			8.7 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
			
		
		
	
	
			258 lines
		
	
	
		
			8.7 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
| package com.novelbook.android.netutils;
 | |
| 
 | |
| import android.text.TextUtils;
 | |
| import android.util.Log;
 | |
| 
 | |
| import com.novelbook.android.netapi.RandomHost;
 | |
| import com.novelbook.android.utils.Constants;
 | |
| import com.novelbook.android.utils.REUtil;
 | |
| 
 | |
| import org.json.JSONException;
 | |
| 
 | |
| import java.io.IOException;
 | |
| 
 | |
| import okhttp3.Interceptor;
 | |
| import okhttp3.Request;
 | |
| import okhttp3.Response;
 | |
| 
 | |
| public class RetryInterceptor implements Interceptor {
 | |
|     private static final String TAG=RetryInterceptor.class.getSimpleName();
 | |
|     int RetryCount = 10;
 | |
|     public RetryInterceptor(){
 | |
| 
 | |
|     }
 | |
| 
 | |
| 
 | |
|    /* public RetryInterceptor(RandomHost rh) {
 | |
|         this.rh=rh;
 | |
|     }*/
 | |
| 
 | |
|     @Override
 | |
|     public Response intercept(Chain chain) throws IOException {
 | |
|         Request request = chain.request();
 | |
|         String oldUrl = request.url().toString();
 | |
|         Log.d(TAG, "HttpMethods intercept: request url:"+oldUrl);
 | |
|         String prefix = REUtil.match("(?i)http[s]?://[^/]+", oldUrl);
 | |
|         Log.d(TAG, "HttpMethods intercept: prefix:"+prefix);
 | |
| 
 | |
|         if (prefix == null){
 | |
|             Response response = doRequest(chain, request);
 | |
| 
 | |
|             if(response!=null)
 | |
|             {
 | |
|                 return response;
 | |
|             }else
 | |
|               {
 | |
|                   int i=0;
 | |
|                   while(response==null && i<3){
 | |
|                       response = doRequest(chain, request);
 | |
|                       i++;
 | |
|                       Log.d(TAG, String.format("HttpMethods intercept:a retried %s, response is null? %s",i, response == null));
 | |
|                   }
 | |
|                   if(response==null) {
 | |
|                       throw new IOException();
 | |
|                   }else {
 | |
|                     return   response;
 | |
|                   }
 | |
|             }
 | |
|         }
 | |
|         String path = oldUrl.substring(prefix.length());
 | |
|         Log.d(TAG, "HttpMethods intercept: path:"+path);
 | |
|         RandomHost rh = null;
 | |
|         boolean isMainApi =path.equals("/api/g/");
 | |
|         if(oldUrl.indexOf("xiaoshuofenxiang.com") == -1 || !path.startsWith("/api/") || NetUtil.getHost(isMainApi)==null){
 | |
|           //  rh = null;
 | |
|             if(oldUrl.indexOf("xiaoshuofenxiang.com")!=-1) {
 | |
|                 Request.Builder requestBuilder = request.newBuilder();
 | |
|                 requestBuilder.removeHeader("User-Agent").addHeader("User-Agent", HttpMethods.LOCALUSERAGENT);
 | |
|                 request =requestBuilder.build();
 | |
|             }
 | |
| 
 | |
|             Response response = doRequest(chain, request);
 | |
|             if(response!=null)
 | |
|             {
 | |
|                 return response;
 | |
|             }else
 | |
|             {
 | |
|                 int i=0;
 | |
|                 while(response==null && i<3){
 | |
| 
 | |
|                     try {
 | |
|                         Thread.sleep(10);
 | |
|                     } catch (InterruptedException e) {
 | |
|                         e.printStackTrace();
 | |
|                     }
 | |
|                     response = doRequest(chain, request);
 | |
|                     i++;
 | |
|                     Log.d(TAG, String.format("HttpMethods intercept:b retried %s,%s,response is null? %s",i,request.url(), response == null));
 | |
|                 }
 | |
|                 if(response==null) {
 | |
|                     throw new IOException();
 | |
|                 }else {
 | |
|                     return   response;
 | |
|                 }
 | |
| 
 | |
|             }
 | |
|         }
 | |
|         try {
 | |
|                 rh = new RandomHost(NetUtil.getHost(isMainApi), path);
 | |
|                 Log.d(TAG, "HttpMethods intercept: create new RandomHost--------------------------");
 | |
| 
 | |
|         } catch (JSONException e) {
 | |
|             Log.e(TAG, "intercept: ", e);
 | |
| 
 | |
| 
 | |
|         }
 | |
| 
 | |
| 
 | |
|         Response response = null;
 | |
|          while (true) {
 | |
| 
 | |
|             String newPrefix = rh.next();
 | |
|             if ("".equals(newPrefix)) break;
 | |
| 
 | |
|             String newUrl = newPrefix + path;
 | |
|             Log.d(TAG, "HttpMethods intercept: trying url is " + newUrl);
 | |
| 
 | |
|             Request newRequest = null;
 | |
|             try {
 | |
| 
 | |
|                 Request.Builder requestBuilder = request.newBuilder();
 | |
|                 requestBuilder.removeHeader("User-Agent").addHeader("User-Agent", HttpMethods.LOCALUSERAGENT)
 | |
|                         .url(newUrl);
 | |
|                 if (path.equals("/api/g/")) {
 | |
|                     //  if(new Date().getTime() - Constants.LAST_G > Constants.MAXAGE_G){ //
 | |
|                     if (Constants.LAST_G == 0) {
 | |
|                         requestBuilder.header("Cache-Control", "public, max-age=0");
 | |
|                         Log.d(TAG, "prepare book access main api with force maxage=0");
 | |
|                     }
 | |
|                  }
 | |
| 
 | |
| 
 | |
| 
 | |
|                 newRequest = requestBuilder.build();
 | |
|             } catch (Exception e) {
 | |
|                 Log.e(TAG, "HttpMethods intercept: " + newUrl);
 | |
|                 Log.e(TAG, "HttpMethods intercept: ", e);
 | |
|                 continue;
 | |
|             }
 | |
| 
 | |
| 
 | |
|             if (response != null) {  try {response.close();} catch (IllegalStateException e) {} }
 | |
| 
 | |
|             response = doRequest(chain, newRequest);
 | |
| 
 | |
|             Log.d(TAG, String.format("HttpMethods intercept: %s, response is null? %s, ua %s",newRequest.url(), response == null,newRequest.header("User-Agent")));
 | |
| 
 | |
|   /*           int i=0;
 | |
|              while((response==null || !response.isSuccessful() )&& i<5){
 | |
|                  try {
 | |
|                      Thread.sleep(10);
 | |
|                  } catch (InterruptedException e) {
 | |
|                      e.printStackTrace();
 | |
|                  }
 | |
|                  response = doRequest(chain, newRequest);
 | |
|                  i++;
 | |
|                  Log.d(TAG, String.format("HttpMethods intercept:C retried %s,%s,  response is null? %s",i,newRequest.url(), response == null));
 | |
|              }*/
 | |
| 
 | |
|             if (response != null) {
 | |
|                 Log.d(TAG, String.format("HttpMethods intercept: response .code? %s", response.code()));
 | |
|             }
 | |
| 
 | |
|             if (response != null && response.isSuccessful()) {
 | |
|               //  rh = null;
 | |
|                 Log.d(TAG, String.format("HttpMethods intercept isHostExpires  get path %s" ,path));
 | |
|                 if (path.equals("/api/g/") || path.equals("/api/g")) {
 | |
|                     Log.d(TAG, String.format("HttpMethods intercept isHostExpires  get main api %s" ,path));
 | |
|                     String cacheControl = response.header("Cache-Control");
 | |
|                     Log.d(TAG, String.format("HttpMethods intercept isHostExpires, cacheControl %s" ,cacheControl));
 | |
|                     if(!TextUtils.isEmpty(cacheControl)) {
 | |
|                         try {
 | |
| 
 | |
|                             Constants.MAXAGE_G = Integer.parseInt(cacheControl.substring("max-age=".length()));
 | |
|                             Log.d(TAG, String.format("HttpMethods intercept isHostExpires  Constants.MAXAGE_G: %s" ,Constants.MAXAGE_G));
 | |
|                         }catch (Exception e)
 | |
|                         {
 | |
|                             Log.e(TAG, "intercept: parse max age error", e);
 | |
|                         }
 | |
|                     }
 | |
|                 }
 | |
|                 return response;
 | |
|             }
 | |
|         }
 | |
|        /* int tryCount=0;
 | |
|         String url =oldUrl;
 | |
|         while ( (response==null ||!response.isSuccessful()) && tryCount <= RetryCount) {
 | |
| 
 | |
|             // if(tryCount>3)
 | |
|             { //三次不成功后换其他url
 | |
|                 url = switchServer(path);
 | |
|                 if (url.equals(oldUrl)) {
 | |
|                     url = switchServer(path);
 | |
|                 }
 | |
|                 if(TextUtils.isEmpty(url)){
 | |
| 
 | |
|                     return null;
 | |
|                 }
 | |
|             }
 | |
|             Request newRequest = null;
 | |
|             try {
 | |
|                 newRequest = request.newBuilder().url(url).build();
 | |
|             } catch (Exception e) {
 | |
|                 Log.e(TAG, "HttpMethods intercept: "+url);
 | |
|                 Log.e(TAG, "HttpMethods intercept: ", e);
 | |
| 
 | |
|                 return null;
 | |
|             }
 | |
| 
 | |
| 
 | |
|             Log.d("HttpMethods intercept", "Request is not successful - " + tryCount);
 | |
| 
 | |
|             Log.d(TAG, "HttpMethods intercept: trying url is "+url);
 | |
|             tryCount++;
 | |
|             // retry the request
 | |
|             response = doRequest(chain, newRequest);
 | |
|         }*/
 | |
|         Log.d(TAG, String.format("HttpMethods intercept: set rh null,return response"));
 | |
|         rh = null;
 | |
|         if (response == null) {
 | |
| 
 | |
|             throw new IOException();
 | |
|         }
 | |
|         return response;
 | |
|     }
 | |
| 
 | |
|     private Response doRequest(Chain chain, Request request) throws IOException {
 | |
|         Response response = null;
 | |
| 
 | |
|         Log.d(TAG, String.format("HttpMethods ua: %s,  ua %s",request.url(), request.header("User-Agent")));
 | |
|         try {
 | |
|             response = chain.proceed(request);
 | |
|         } catch (Exception e) {
 | |
|           Log.e(TAG, "doRequest: error, " + request.url(),e );
 | |
|         }
 | |
| 
 | |
|         return response;
 | |
|     }
 | |
| 
 | |
|    /*private String switchServer(String key) {
 | |
|         String newUrl = rh.next();
 | |
| 
 | |
|         if(TextUtils.isEmpty(newUrl)){
 | |
|             return newUrl;
 | |
|         }
 | |
| 
 | |
|        *//* if(!newUrl.endsWith("/")){
 | |
|             newUrl+="/";
 | |
|         }*//*
 | |
|         return newUrl +key;
 | |
|     }*/
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| }
 |