APISonar


java.net.Proxy.Type.HTTP

> java > net > Proxy > Type > HTTP
java APIs net APIs Proxy APIs Type APIs HTTP APIs

Example 1
public void shouldServeAsHttpProxy() throws Exception {
        final Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", port));

        // request for http://test/path will be proxied by http://localhost:port
        // and diverted to http://localhost:originPort/path
        final HttpURLConnection connection = (HttpURLConnection) new URL("http://test/path").openConnection(proxy);

        try (InputStream stream = connection.getInputStream()) {
            assertThat(IOUtils.readLines(stream, StandardCharsets.UTF_8)).containsOnly("ORIGIN SERVER");
        }
    }
Example 2
public void testEncodedProxyAuth() throws Exception {
    Proxy proxy = ProxyHelper.createProxy("http://foo:b%40rb%40z@my.example.com");
    assertEquals(Proxy.Type.HTTP, proxy.type());
    assertThat(proxy.toString()).endsWith(":80");
    assertEquals(System.getProperty("http.proxyHost"), "my.example.com");
    assertEquals(System.getProperty("http.proxyPort"), "80");
    assertEquals(System.getProperty("http.proxyUser"), "foo");
    assertEquals(System.getProperty("http.proxyPassword"), "b@rb@z");
  }
Example 3
public void testSelectWithByPass() {
    MyProxySelector selector = new MyProxySelector();
    final MyProxy http = new MyProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", 8080)), MyProxyType.HTTP);
    selector.add(http);
    assertEquals(http.getProxy(), selector.select(httpURI).get(0));
    selector.addByPassHost(httpURI.getHost());
    assertEquals(Proxy.NO_PROXY, selector.select(httpURI).get(0));
  }
Example 4
public static ClientHttpRequestFactory getRequestFactory() {
		Properties properties = System.getProperties();
		String proxyHost = properties.getProperty("http.proxyHost");
		int proxyPort = properties.containsKey("http.proxyPort") ? Integer.valueOf(properties.getProperty("http.proxyPort")) : 80;
		if (HTTP_COMPONENTS_AVAILABLE) {
			return HttpComponentsClientRequestFactoryCreator.createRequestFactory(proxyHost, proxyPort);
		} else {
			SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
			if (proxyHost != null) {
				requestFactory.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)));
			}
			return requestFactory;
		}
	}
Example 5
private static Proxy proxyFor(Optional<ProxySelector> selector, URI uri) {
        if (!selector.isPresent()) {
            return null;
        }
        URI requestURI = createRequestURI(uri); // Based on the HTTP scheme
        List<Proxy> pl = selector.get().select(requestURI);
        if (pl.isEmpty()) {
            return null;
        }
        Proxy proxy = pl.get(0);
        if (proxy.type() != Proxy.Type.HTTP) {
            return null;
        }
        return proxy;
    }
Example 6
protected URLConnection openConnection(URL u, Proxy p) throws IOException {
        if (p.type() == Proxy.Type.DIRECT) {
            return openConnection(u);
        } else if (p.type() != Proxy.Type.HTTP) {
            return null;
        }
        URLConnection c = httpProxyConnections.get(u.toString());
        if (c != null) {
            return doOpenConnection(u, c);
        } else {
            throw new ConnectException(u.toExternalForm());
        }
    }
Example 7
public void testEnumSet() throws Exception {
        Lister l = Lister.create(getClass().getDeclaredField("set").getGenericType());
        l.add(Type.HTTP);
        l.add(Type.SOCKS);
        Set col = (Set)l.toCollection();
        assertTrue(col instanceof EnumSet);
        assertTrue(col.contains(Type.HTTP));
    }
Example 8
public Optional<Proxy> createProxy(String proxy)
            throws MalformedURLException {
        Optional<URL> url = determineProxyUrl(proxy);
        if (url.isPresent()) {
            String proxyHost = url.get().getHost();
            int proxyPort = url.get().getPort() == -1 ? 80
                    : url.get().getPort();
            return Optional.of(new Proxy(Proxy.Type.HTTP,
                    new InetSocketAddress(proxyHost, proxyPort)));
        }
        return empty();
    }
Example 9
public Socket createSocket(final HttpContext context) throws IOException {
        InetSocketAddress socketAddress = (InetSocketAddress) context.getAttribute("socketAddress");
        Proxy proxy = new Proxy(Proxy.Type.HTTP, socketAddress);
        return new Socket(proxy);
    }
Example 10
private static Proxy newHttpProxy(String proxyHost, int proxyPort) {
        InetSocketAddress saddr = null;
        final String phost = proxyHost;
        final int pport = proxyPort < 0 ? httpsPortNumber : proxyPort;
        try {
            saddr = java.security.AccessController.doPrivileged(new
                java.security.PrivilegedExceptionAction<InetSocketAddress>() {
                public InetSocketAddress run() {
                    return new InetSocketAddress(phost, pport);
                }});
        } catch (java.security.PrivilegedActionException pae) {
        }
        return new Proxy(Proxy.Type.HTTP, saddr);
    }
Example 11
public static void main(String... args) throws IOException {

        URL weburl = new URL(URL_STRING);
        Proxy webProxy 
          = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 3128));
        HttpURLConnection webProxyConnection 
            = (HttpURLConnection) weburl.openConnection(webProxy);
        System.out.println(UrlConnectionUtils.contentAsString(webProxyConnection));
    }
Example 12
public Proxy getProxy() {
        Proxy proxy = null;

        String proxyHost = System.getProperty("installer.proxy.host", null);
        String proxyPort = System.getProperty("installer.proxy.port", null);

        if (proxyHost != null && proxyPort != null) {
            proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, Integer.valueOf(proxyPort)));
        } else {
            proxy = Proxy.NO_PROXY;
        }

        return proxy;
    }
Example 13
public void canCreateWithExplicitCredentials() throws Exception {
    Proxied proxied = new Proxied(
        TrustingSSLSocketFactory.get(), null,
        new Proxy(Type.HTTP, proxyAddress), "user", "password");
    assertThat(proxied).isNotNull();
    assertThat(proxied.getCredentials()).isNotBlank();

    HttpURLConnection connection = proxied.getConnection(new URL("http://www.example.com"));
    assertThat(connection).isNotNull().isInstanceOf(HttpURLConnection.class);
  }
Example 14
public ManualProxySelector(String proxyHost, int proxyPort, String[] excludes) {
        this(new Proxy(Proxy.Type.HTTP,
                InetSocketAddress.createUnresolved(proxyHost, proxyPort)), excludes);
    }
Example 15
public static Proxy createProxy(String host, String name, int port, String noProxyHost) {
        if (host!=null && noProxyHost!=null) {
            for (Pattern p : getNoProxyHostPatterns(noProxyHost)) {
                if (p.matcher(host).matches())
                    return Proxy.NO_PROXY;
            }
        }
        return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(name,port));
    }
Example 16
public static void installExternalSystemProxy() {
        // see proxies list at http://proxylist.hidemyass.com/
        Proxy systemProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("162.8.230.7", 11180));
        ProxySelector mockedProxySelector = Mockito.mock(ProxySelector.class);
        when(mockedProxySelector.select(Mockito.<URI>any())).thenReturn(Lists.newArrayList(systemProxy));
        ProxySelector.setDefault(mockedProxySelector);
    }
Example 17
public void usesACustomHttpRoutePlanner() {
        final HttpRoutePlanner customHttpRoutePlanner = new SystemDefaultRoutePlanner(new ProxySelector() {
            @Override
            public List<Proxy> select(URI uri) {
                return Collections.singletonList(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.53.12", 8080)));
            }

            @Override
            public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {

            }
        });
        builder.using(customHttpRoutePlanner);
        verify(apacheHttpClientBuilder).using(customHttpRoutePlanner);
    }
Example 18
private static InputStream request(final String url) throws IOException, MalformedURLException {
        final Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", PROXY_PORT));

        final HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(proxy);
        // when debugging comment out the following two lines otherwise
        // the test will terminate regardless of the execution being
        // paused at the breakpoint
        connection.setConnectTimeout(1000);
        connection.setReadTimeout(1000);

        return connection.getInputStream();
    }
Example 19
public void testProxyHttp() throws UnknownHostException {
        final Settings settings = Settings.builder()
            .put(buildClientCredSettings())
            .put("proxy_host", "127.0.0.1")
            .put("proxy_port", 8080)
            .put("proxy_type", "http")
            .build();
        final AzureStorageService mock = storageServiceWithSettings(settings);
        final Proxy defaultProxy = mock.storageSettings.getProxy();

        assertThat(defaultProxy, notNullValue());
        assertThat(defaultProxy.type(), is(Proxy.Type.HTTP));
        assertThat(defaultProxy.address(), is(new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 8080)));
    }
Example 20
public void testBadHttpProxyFails() throws IOException {
        final int testPort = 58273;
        try {
            new ServerSocket(testPort).close();
        } catch (IOException e) {
            fail("This assumes nothing is running on port " + testPort);
        }

        URL url = new URL("https://github.com");
        NetCipher.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", testPort)));
        HttpURLConnection connection = NetCipher.getHttpURLConnection(url);
        connection.getContent();
        fail();
    }
Example 21
public static void main( String[] args ) throws URISyntaxException {
		ExampleClient c = new ExampleClient( new URI( "ws://echo.websocket.org" ) );
		c.setProxy( new Proxy( Proxy.Type.HTTP, new InetSocketAddress( "proxyaddress", 80 ) ) );
		c.connect();
	}
Example 22
private Proxy getProxy(ProxyOptions proxyOptions) {
    if (proxyOptions.getAddress() != null && !proxyOptions.getAddress().isEmpty()) {
      return new Proxy(
          Proxy.Type.HTTP,
          new InetSocketAddress(proxyOptions.getAddress(), proxyOptions.getPort())
      );
    } else {
      return null;
    }
  }
Example 23
public void connect(InetAddress host, int port, Proxy proxy) throws IOException {
        this.port = port;
        if (JavaVersion.isJava7OrEarlier() && proxy.type() == Proxy.Type.HTTP) {
            // Java7 and earlier have no support for HTTP Connect proxies, return our custom socket.
            socket = new Jdk7HttpProxySocket(proxy);
        } else {
            socket = new Socket(proxy);
        }
        socket.connect(new InetSocketAddress(host, port), connectTimeout);
        onConnect();
    }
Example 24
static void doClient(BadAuthProxyServer server) throws IOException {
        // url doesn't matter since we will never make the connection
        URL url = new URL("https://anythingwilldo/");
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(
                      new Proxy(Proxy.Type.HTTP,
                      new InetSocketAddress("localhost", server.getPort())));
        try (InputStream is = conn.getInputStream()) {
        } catch(IOException unused) {
            // no real server, IOException is expected.
            // failure if StackOverflowError
        } finally {
            server.done();
        }
    }
Example 25
public ClientApi (String zapAddress, int zapPort, boolean debug) {
		proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(zapAddress, zapPort));
		this.debug = debug;
	}