1 package com.xtremelabs.robolectric.shadows; 2 3 import com.xtremelabs.robolectric.Robolectric; 4 import com.xtremelabs.robolectric.internal.Implementation; 5 import com.xtremelabs.robolectric.internal.Implements; 6 import com.xtremelabs.robolectric.internal.RealObject; 7 import com.xtremelabs.robolectric.tester.org.apache.http.HttpRequestInfo; 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 import org.apache.http.ConnectionReuseStrategy; 11 import org.apache.http.HttpException; 12 import org.apache.http.HttpHost; 13 import org.apache.http.HttpRequest; 14 import org.apache.http.HttpResponse; 15 import org.apache.http.client.AuthenticationHandler; 16 import org.apache.http.client.HttpRequestRetryHandler; 17 import org.apache.http.client.RedirectHandler; 18 import org.apache.http.client.UserTokenHandler; 19 import org.apache.http.conn.ClientConnectionManager; 20 import org.apache.http.conn.ConnectionKeepAliveStrategy; 21 import org.apache.http.conn.routing.HttpRoutePlanner; 22 import org.apache.http.impl.client.DefaultRequestDirector; 23 import org.apache.http.params.HttpParams; 24 import org.apache.http.protocol.HttpContext; 25 import org.apache.http.protocol.HttpProcessor; 26 import org.apache.http.protocol.HttpRequestExecutor; 27 28 import java.io.IOException; 29 30 @SuppressWarnings({"UnusedDeclaration"}) 31 @Implements(DefaultRequestDirector.class) 32 public class ShadowDefaultRequestDirector { 33 @RealObject DefaultRequestDirector realObject; 34 35 protected Log log; 36 protected ClientConnectionManager connectionManager; 37 protected HttpRoutePlanner httpRoutePlanner; 38 protected ConnectionReuseStrategy connectionReuseStrategy; 39 protected ConnectionKeepAliveStrategy connectionKeepAliveStrategy; 40 protected HttpRequestExecutor httpRequestExecutor; 41 protected HttpProcessor httpProcessor; 42 protected HttpRequestRetryHandler httpRequestRetryHandler; 43 protected RedirectHandler redirectHandler; 44 protected AuthenticationHandler targetAuthenticationHandler; 45 protected AuthenticationHandler proxyAuthenticationHandler; 46 protected UserTokenHandler userTokenHandler; 47 protected HttpParams httpParams; 48 49 com.xtremelabs.robolectric.tester.org.apache.http.impl.client.DefaultRequestDirector redirector; 50 51 public void __constructor__( 52 Log log, 53 HttpRequestExecutor requestExec, 54 ClientConnectionManager conman, 55 ConnectionReuseStrategy reustrat, 56 ConnectionKeepAliveStrategy kastrat, 57 HttpRoutePlanner rouplan, 58 HttpProcessor httpProcessor, 59 HttpRequestRetryHandler retryHandler, 60 RedirectHandler redirectHandler, 61 AuthenticationHandler targetAuthHandler, 62 AuthenticationHandler proxyAuthHandler, 63 UserTokenHandler userTokenHandler, 64 HttpParams params) { 65 this.log = log; 66 this.httpRequestExecutor = requestExec; 67 this.connectionManager = conman; 68 this.connectionReuseStrategy = reustrat; 69 this.connectionKeepAliveStrategy = kastrat; 70 this.httpRoutePlanner = rouplan; 71 this.httpProcessor = httpProcessor; 72 this.httpRequestRetryHandler = retryHandler; 73 this.redirectHandler = redirectHandler; 74 this.targetAuthenticationHandler = targetAuthHandler; 75 this.proxyAuthenticationHandler = proxyAuthHandler; 76 this.userTokenHandler = userTokenHandler; 77 this.httpParams = params; 78 79 try { 80 redirector = new com.xtremelabs.robolectric.tester.org.apache.http.impl.client.DefaultRequestDirector( 81 log, 82 requestExec, 83 conman, 84 reustrat, 85 kastrat, 86 rouplan, 87 httpProcessor, 88 retryHandler, 89 redirectHandler, 90 targetAuthHandler, 91 proxyAuthHandler, 92 userTokenHandler, 93 params 94 ); 95 } catch (IllegalArgumentException ignored) { 96 Robolectric.getFakeHttpLayer().interceptHttpRequests(true); 97 } 98 } 99 100 public void __constructor__( 101 HttpRequestExecutor requestExec, 102 ClientConnectionManager conman, 103 ConnectionReuseStrategy reustrat, 104 ConnectionKeepAliveStrategy kastrat, 105 HttpRoutePlanner rouplan, 106 HttpProcessor httpProcessor, 107 HttpRequestRetryHandler retryHandler, 108 RedirectHandler redirectHandler, 109 AuthenticationHandler targetAuthHandler, 110 AuthenticationHandler proxyAuthHandler, 111 UserTokenHandler userTokenHandler, 112 HttpParams params) { 113 __constructor__( 114 LogFactory.getLog(DefaultRequestDirector.class), 115 requestExec, 116 conman, 117 reustrat, 118 kastrat, 119 rouplan, 120 httpProcessor, 121 retryHandler, 122 redirectHandler, 123 targetAuthHandler, 124 proxyAuthHandler, 125 userTokenHandler, 126 params); 127 } 128 129 public static HttpRequest getSentHttpRequest(int index) { 130 return getSentHttpRequestInfo(index).getHttpRequest(); 131 } 132 133 public static HttpRequest getLatestSentHttpRequest() { 134 return getLatestSentHttpRequestInfo().getHttpRequest(); 135 } 136 137 public static HttpRequestInfo getLatestSentHttpRequestInfo() { 138 int requestCount = Robolectric.getFakeHttpLayer().getSentHttpRequestInfos().size(); 139 return Robolectric.getFakeHttpLayer().getSentHttpRequestInfo(requestCount - 1); 140 } 141 142 public static HttpRequestInfo getSentHttpRequestInfo(int index) { 143 return Robolectric.getFakeHttpLayer().getSentHttpRequestInfo(index); 144 } 145 146 @Implementation 147 public HttpResponse execute(HttpHost httpHost, HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException { 148 if (Robolectric.getFakeHttpLayer().isInterceptingHttpRequests()) { 149 return Robolectric.getFakeHttpLayer().emulateRequest(httpHost, httpRequest, httpContext, realObject); 150 } else { 151 return redirector.execute(httpHost, httpRequest, httpContext); 152 } 153 } 154 155 public Log getLog() { 156 return log; 157 } 158 159 public ClientConnectionManager getConnectionManager() { 160 return connectionManager; 161 } 162 163 public HttpRoutePlanner getHttpRoutePlanner() { 164 return httpRoutePlanner; 165 } 166 167 public ConnectionReuseStrategy getConnectionReuseStrategy() { 168 return connectionReuseStrategy; 169 } 170 171 public ConnectionKeepAliveStrategy getConnectionKeepAliveStrategy() { 172 return connectionKeepAliveStrategy; 173 } 174 175 public HttpRequestExecutor getHttpRequestExecutor() { 176 return httpRequestExecutor; 177 } 178 179 public HttpProcessor getHttpProcessor() { 180 return httpProcessor; 181 } 182 183 public HttpRequestRetryHandler getHttpRequestRetryHandler() { 184 return httpRequestRetryHandler; 185 } 186 187 public RedirectHandler getRedirectHandler() { 188 return redirectHandler; 189 } 190 191 public AuthenticationHandler getTargetAuthenticationHandler() { 192 return targetAuthenticationHandler; 193 } 194 195 public AuthenticationHandler getProxyAuthenticationHandler() { 196 return proxyAuthenticationHandler; 197 } 198 199 public UserTokenHandler getUserTokenHandler() { 200 return userTokenHandler; 201 } 202 203 public HttpParams getHttpParams() { 204 return httpParams; 205 } 206 }