|
| 1 | +package net.lightbody.bmp.proxy; |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableList; |
| 4 | +import com.google.common.net.HostAndPort; |
| 5 | +import net.lightbody.bmp.BrowserMobProxy; |
| 6 | +import net.lightbody.bmp.BrowserMobProxyServer; |
| 7 | +import net.lightbody.bmp.client.ClientUtil; |
| 8 | +import net.lightbody.bmp.proxy.auth.AuthType; |
| 9 | +import net.lightbody.bmp.proxy.dns.AdvancedHostResolver; |
| 10 | +import org.slf4j.Logger; |
| 11 | +import org.slf4j.LoggerFactory; |
| 12 | + |
| 13 | +import java.net.InetAddress; |
| 14 | +import java.net.InetSocketAddress; |
| 15 | +import java.net.UnknownHostException; |
| 16 | +import java.util.Collection; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | +import java.util.regex.Pattern; |
| 21 | + |
| 22 | +/** |
| 23 | + * Allows the legacy REST API to use the new ${@link BrowserMobProxy} interface. |
| 24 | + * <b>Embedded mode clients should not use this class. It will be removed in a future release.</b> |
| 25 | + * |
| 26 | + * @deprecated Use {@link BrowserMobProxyServer}. This class will be removed in a future release. |
| 27 | + */ |
| 28 | +@Deprecated |
| 29 | +public class BrowserMobProxyServerLegacyAdapter extends BrowserMobProxyServer { |
| 30 | + private static final Logger log = LoggerFactory.getLogger(BrowserMobProxyServerLegacyAdapter.class); |
| 31 | + |
| 32 | + /** |
| 33 | + * When true, throw an UnsupportedOperationException instead of logging a warning when an operation is not supported by the LittleProxy-based |
| 34 | + * implementation of the BrowserMobProxy interface. Once all operations are implemented and the legacy interface is retired, this will be removed. |
| 35 | + */ |
| 36 | + private volatile boolean errorOnUnsupportedOperation = false; |
| 37 | + |
| 38 | + /** |
| 39 | + * The port to start the proxy on, if set using {@link #setPort(int)}. |
| 40 | + */ |
| 41 | + private volatile int port; |
| 42 | + |
| 43 | + /** |
| 44 | + * The address to listen on, if set using {@link #setLocalHost(InetAddress)}. |
| 45 | + */ |
| 46 | + private volatile InetAddress clientBindAddress; |
| 47 | + |
| 48 | + /** |
| 49 | + * When true, this implementation of BrowserMobProxy will throw an UnsupportedOperationException when a method is not supported. This |
| 50 | + * helps identify functionality that is not supported by the LittleProxy-based implementation. By default, this implementation will |
| 51 | + * log a warning rather than throw an UnsupportedOperationException. |
| 52 | + * |
| 53 | + * @param errorOnUnsupportedOperation when true, throws an exception when an operation is not supported |
| 54 | + */ |
| 55 | + public void setErrorOnUnsupportedOperation(boolean errorOnUnsupportedOperation) { |
| 56 | + this.errorOnUnsupportedOperation = errorOnUnsupportedOperation; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @deprecated specify the port using {@link #start(int)} or other start() methods with port parameters |
| 61 | + */ |
| 62 | + @Deprecated |
| 63 | + public void setPort(int port) { |
| 64 | + this.port = port; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @deprecated use {@link #getClientBindAddress()} |
| 69 | + */ |
| 70 | + @Deprecated |
| 71 | + public InetAddress getLocalHost() { |
| 72 | + return this.getClientBindAddress(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @deprecated use {@link ClientUtil#getConnectableAddress()} |
| 77 | + */ |
| 78 | + @Deprecated |
| 79 | + public InetAddress getConnectableLocalHost() throws UnknownHostException { |
| 80 | + return ClientUtil.getConnectableAddress(); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @deprecated use {@link #start(int, InetAddress)} or {@link #start(int, InetAddress, InetAddress)} |
| 85 | + */ |
| 86 | + @Deprecated |
| 87 | + public void setLocalHost(InetAddress localHost) { |
| 88 | + this.clientBindAddress = localHost; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void start() { |
| 93 | + super.start(port, clientBindAddress); |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + public void setRetryCount(int count) { |
| 98 | + if (errorOnUnsupportedOperation) { |
| 99 | + throw new UnsupportedOperationException("No LittleProxy implementation for operation: " + new Throwable().getStackTrace()[0].getMethodName()); |
| 100 | + } else { |
| 101 | + log.warn("No LittleProxy implementation for operation: " + new Throwable().getStackTrace()[0].getMethodName()); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @deprecated use {@link #setWriteBandwidthLimit(long)} |
| 107 | + */ |
| 108 | + @Deprecated |
| 109 | + public void setDownstreamKbps(long downstreamKbps) { |
| 110 | + this.setWriteBandwidthLimit(downstreamKbps * 1024); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @deprecated use {@link #setReadBandwidthLimit(long)} |
| 115 | + */ |
| 116 | + @Deprecated |
| 117 | + public void setUpstreamKbps(long upstreamKbps) { |
| 118 | + this.setReadBandwidthLimit(upstreamKbps * 1024); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @deprecated use {@link #setLatency(long, TimeUnit)} |
| 123 | + */ |
| 124 | + @Deprecated |
| 125 | + public void setLatency(long latencyMs) { |
| 126 | + setLatency(latencyMs, TimeUnit.MILLISECONDS); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @deprecated use {@link #setReadBandwidthLimit(long)} |
| 131 | + */ |
| 132 | + @Deprecated |
| 133 | + public void setReadLimitKbps(long readLimitKbps) { |
| 134 | + setReadBandwidthLimit(readLimitKbps * 1024); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * @deprecated use {@link #setWriteBandwidthLimit(long)} |
| 139 | + */ |
| 140 | + @Deprecated |
| 141 | + public void setWriteLimitKbps(long writeLimitKbps) { |
| 142 | + setWriteBandwidthLimit(writeLimitKbps * 1024); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @deprecated use getBlacklist() |
| 147 | + */ |
| 148 | + @Deprecated |
| 149 | + public List<BlacklistEntry> getBlacklistedRequests() { |
| 150 | + return ImmutableList.copyOf(getBlacklist()); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * @deprecated use {@link #getBlacklist()} |
| 155 | + */ |
| 156 | + @Deprecated |
| 157 | + public Collection<BlacklistEntry> getBlacklistedUrls() { |
| 158 | + return getBlacklist(); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * @deprecated use {@link #getWhitelistStatusCode()} |
| 163 | + */ |
| 164 | + @Deprecated |
| 165 | + public int getWhitelistResponseCode() { |
| 166 | + return getWhitelistStatusCode(); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * @deprecated use {@link #disableWhitelist()} |
| 171 | + */ |
| 172 | + @Deprecated |
| 173 | + public void clearWhitelist() { |
| 174 | + this.disableWhitelist(); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * @deprecated use {@link #getWhitelistUrls()} |
| 179 | + */ |
| 180 | + @Deprecated |
| 181 | + public List<Pattern> getWhitelistRequests() { |
| 182 | + ImmutableList.Builder<Pattern> builder = ImmutableList.builder(); |
| 183 | + |
| 184 | + for (String patternString : getWhitelistUrls()) { |
| 185 | + Pattern pattern = Pattern.compile(patternString); |
| 186 | + builder.add(pattern); |
| 187 | + } |
| 188 | + |
| 189 | + return builder.build(); |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * @deprecated use {@link #waitForQuiescence(long, long, TimeUnit)} |
| 194 | + */ |
| 195 | + @Deprecated |
| 196 | + public void waitForNetworkTrafficToStop(long quietPeriodInMs, long timeoutInMs) { |
| 197 | + waitForQuiescence(quietPeriodInMs, timeoutInMs, TimeUnit.MILLISECONDS); |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * @deprecated use {@link #getHarCaptureTypes()} to check for relevant {@link CaptureType} |
| 202 | + */ |
| 203 | + @Deprecated |
| 204 | + public boolean isCaptureHeaders() { |
| 205 | + return getHarCaptureTypes().containsAll(CaptureType.getHeaderCaptureTypes()); |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * @deprecated use {@link #setHarCaptureTypes(java.util.Set)} to set the appropriate {@link CaptureType} |
| 210 | + */ |
| 211 | + @Deprecated |
| 212 | + public void setCaptureHeaders(boolean captureHeaders) { |
| 213 | + if (captureHeaders) { |
| 214 | + enableHarCaptureTypes(CaptureType.getHeaderCaptureTypes()); |
| 215 | + } else { |
| 216 | + disableHarCaptureTypes(CaptureType.getHeaderCaptureTypes()); |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * @deprecated use {@link #getHarCaptureTypes()} to check for relevant {@link CaptureType} |
| 222 | + */ |
| 223 | + @Deprecated |
| 224 | + public boolean isCaptureContent() { |
| 225 | + return getHarCaptureTypes().containsAll(CaptureType.getNonBinaryContentCaptureTypes()); |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * @deprecated use {@link #setHarCaptureTypes(java.util.Set)} to set the appropriate {@link CaptureType} |
| 230 | + */ |
| 231 | + @Deprecated |
| 232 | + public void setCaptureContent(boolean captureContent) { |
| 233 | + if (captureContent) { |
| 234 | + enableHarCaptureTypes(CaptureType.getAllContentCaptureTypes()); |
| 235 | + } else { |
| 236 | + disableHarCaptureTypes(CaptureType.getAllContentCaptureTypes()); |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + /** |
| 241 | + * @deprecated use {@link #getHarCaptureTypes()} to check for relevant {@link CaptureType} |
| 242 | + */ |
| 243 | + @Deprecated |
| 244 | + public boolean isCaptureBinaryContent() { |
| 245 | + return getHarCaptureTypes().containsAll(CaptureType.getBinaryContentCaptureTypes()); |
| 246 | + } |
| 247 | + |
| 248 | + /** |
| 249 | + * @deprecated use {@link #setHarCaptureTypes(java.util.Set)} to set the appropriate {@link CaptureType} |
| 250 | + */ |
| 251 | + @Deprecated |
| 252 | + public void setCaptureBinaryContent(boolean captureBinaryContent) { |
| 253 | + if (captureBinaryContent) { |
| 254 | + enableHarCaptureTypes(CaptureType.getBinaryContentCaptureTypes()); |
| 255 | + } else { |
| 256 | + disableHarCaptureTypes(CaptureType.getBinaryContentCaptureTypes()); |
| 257 | + } |
| 258 | + } |
| 259 | + |
| 260 | + /** |
| 261 | + * @deprecated this method has no effect and will be removed from a future version |
| 262 | + */ |
| 263 | + @Deprecated |
| 264 | + public void cleanup() { |
| 265 | + } |
| 266 | + |
| 267 | + /** |
| 268 | + * @deprecated Remap hosts directly using {@link AdvancedHostResolver#remapHost(String, String)}. |
| 269 | + * See {@link #getHostNameResolver()} and the default implementation in {@link net.lightbody.bmp.proxy.dns.ChainedHostResolver}. |
| 270 | + */ |
| 271 | + @Deprecated |
| 272 | + public void remapHost(String source, String target) { |
| 273 | + getHostNameResolver().remapHost(source, target); |
| 274 | + } |
| 275 | + |
| 276 | + /** |
| 277 | + * @deprecated Manipulate the DNS cache directly using {@link AdvancedHostResolver#clearDNSCache()}. |
| 278 | + * See {@link #getHostNameResolver()} and the default implementation in {@link net.lightbody.bmp.proxy.dns.ChainedHostResolver}. |
| 279 | + */ |
| 280 | + @Deprecated |
| 281 | + public void clearDNSCache() { |
| 282 | + getHostNameResolver().clearDNSCache(); |
| 283 | + } |
| 284 | + |
| 285 | + /** |
| 286 | + * @deprecated Manipulate the DNS cache directly using {@link AdvancedHostResolver#setNegativeDNSCacheTimeout(int, TimeUnit)} |
| 287 | + * and {@link AdvancedHostResolver#setPositiveDNSCacheTimeout(int, TimeUnit)}. |
| 288 | + * See {@link #getHostNameResolver()} and the default implementation in {@link net.lightbody.bmp.proxy.dns.ChainedHostResolver}. |
| 289 | + */ |
| 290 | + @Deprecated |
| 291 | + public void setDNSCacheTimeout(int timeout) { |
| 292 | + AdvancedHostResolver resolver = getHostNameResolver(); |
| 293 | + resolver.setPositiveDNSCacheTimeout(timeout, TimeUnit.SECONDS); |
| 294 | + resolver.setNegativeDNSCacheTimeout(timeout, TimeUnit.SECONDS); |
| 295 | + } |
| 296 | + |
| 297 | + |
| 298 | + /** |
| 299 | + * @deprecated use {@link #setConnectTimeout(int, TimeUnit)} |
| 300 | + */ |
| 301 | + @Deprecated |
| 302 | + public void setConnectionTimeout(int connectionTimeoutMs) { |
| 303 | + setConnectTimeout(connectionTimeoutMs, TimeUnit.MILLISECONDS); |
| 304 | + } |
| 305 | + |
| 306 | + /** |
| 307 | + * @deprecated use {@link #setIdleConnectionTimeout(int, TimeUnit)} |
| 308 | + */ |
| 309 | + @Deprecated |
| 310 | + public void setSocketOperationTimeout(int readTimeoutMs) { |
| 311 | + setIdleConnectionTimeout(readTimeoutMs, TimeUnit.MILLISECONDS); |
| 312 | + } |
| 313 | + |
| 314 | + /** |
| 315 | + * @deprecated use {@link #setRequestTimeout(int, TimeUnit)} |
| 316 | + */ |
| 317 | + @Deprecated |
| 318 | + public void setRequestTimeout(int requestTimeoutMs) { |
| 319 | + setRequestTimeout(requestTimeoutMs, TimeUnit.MILLISECONDS); |
| 320 | + } |
| 321 | + |
| 322 | + /** |
| 323 | + * @deprecated use {@link #autoAuthorization(String, String, String, AuthType)} |
| 324 | + */ |
| 325 | + @Deprecated |
| 326 | + public void autoBasicAuthorization(String domain, String username, String password) { |
| 327 | + autoAuthorization(domain, username, password, AuthType.BASIC); |
| 328 | + } |
| 329 | + |
| 330 | + /** |
| 331 | + * @deprecated use {@link #setChainedProxy(InetSocketAddress)} to set an upstream proxy |
| 332 | + */ |
| 333 | + @Deprecated |
| 334 | + public void setOptions(Map<String, String> options) { |
| 335 | + if (options == null || options.isEmpty()) { |
| 336 | + return; |
| 337 | + } |
| 338 | + |
| 339 | + String httpProxy = options.get("httpProxy"); |
| 340 | + if (httpProxy != null) { |
| 341 | + log.warn("Chained proxy support through setOptions is deprecated. Use setUpstreamProxy() to enable chained proxy support."); |
| 342 | + |
| 343 | + HostAndPort hostAndPort = HostAndPort.fromString(httpProxy); |
| 344 | + this.setChainedProxy(new InetSocketAddress(hostAndPort.getHost(), hostAndPort.getPortOrDefault(80))); |
| 345 | + } else { |
| 346 | + if (errorOnUnsupportedOperation) { |
| 347 | + throw new UnsupportedOperationException("The LittleProxy-based implementation of BrowserMob Proxy does not support the setOptions method. Use the methods defined in the BrowserMobProxy interface to set connection parameters."); |
| 348 | + } else { |
| 349 | + log.warn("The LittleProxy-based implementation of BrowserMob Proxy does not support the setOptions method. Use the methods defined in the BrowserMobProxy interface to set connection parameters."); |
| 350 | + } |
| 351 | + } |
| 352 | + } |
| 353 | +} |
0 commit comments