Example usage for java.net Proxy Proxy

List of usage examples for java.net Proxy Proxy

Introduction

In this page you can find the example usage for java.net Proxy Proxy.

Prototype

public Proxy(Type type, SocketAddress sa) 

Source Link

Document

Creates an entry representing a PROXY connection.

Usage

From source file:be.fedict.eid.dss.client.DSSProxySelector.java

/**
 * Sets the proxy for a certain location URL. If the proxyHost is
 * <code>null</code, we go DIRECT.
 * // w  ww.  jav a  2s  .  c o  m
 * @param location
 *            the location to proxy.
 * @param proxyHost
 *            proxy hostname
 * @param proxyPort
 *            proxy port
 */
public void setProxy(String location, String proxyHost, int proxyPort) {
    String hostname;
    try {
        hostname = new URL(location).getHost();
    } catch (MalformedURLException e) {
        throw new RuntimeException("URL error: " + e.getMessage(), e);
    }
    if (null == proxyHost) {
        LOG.debug("removing proxy for: " + hostname);
        this.proxies.remove(hostname);
    } else {
        LOG.debug("setting proxy for: " + hostname);
        this.proxies.put(hostname, new Proxy(Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)));
    }
}

From source file:com.jkoolcloud.jesl.net.socket.SocketClient.java

/**
 * Create JESL HTTP[S} client stream with given attributes
 * //from  www .j a v  a 2  s  . c  o  m
 * @param host JESL host server
 * @param port JESL host port number
 * @param secure use SSL if secure, standard sockets if false
 * @param proxyHost proxy host name if any, null if none
 * @param proxyPort proxy port number if any, 0 of none
 * @param logger event sink used for logging, null if none
 * 
 */
public SocketClient(String host, int port, boolean secure, String proxyHost, int proxyPort, EventSink logger) {
    this(host, port, secure, logger);
    if (!StringUtils.isEmpty(proxyHost)) {
        proxyAddr = new InetSocketAddress(proxyHost, proxyPort);
        proxy = new Proxy(Proxy.Type.SOCKS, proxyAddr);
    }
}

From source file:org.kawanfw.test.parms.ProxyLoader.java

public Proxy getProxy() throws IOException, URISyntaxException {
    if (FrameworkSystemUtil.isAndroid()) {
        return null;
    }/*w ww. j av a2s  .  c o m*/

    System.setProperty("java.net.useSystemProxies", "true");
    List<Proxy> proxies = ProxySelector.getDefault().select(new URI("http://www.google.com/"));

    if (proxies != null && proxies.size() >= 1) {
        System.out.println("Loading proxy file info...");

        if (proxies.get(0).type().equals(Proxy.Type.DIRECT)) {
            return null;
        }

        File file = new File(NEOTUNNEL_TXT);
        if (file.exists()) {
            String proxyValues = FileUtils.readFileToString(file);
            String username = StringUtils.substringBefore(proxyValues, " ");
            String password = StringUtils.substringAfter(proxyValues, " ");

            username = username.trim();
            password = password.trim();

            proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", 8080));

            passwordAuthentication = new PasswordAuthentication(username, password.toCharArray());

            System.out.println("USING PROXY WITH AUTHENTICATION: " + proxy + " / " + username + " " + password);
        } else {
            throw new FileNotFoundException("proxy values not found. No file " + file);
        }
    }

    return proxy;
}

From source file:be.fedict.eid.idp.sp.protocol.saml2.artifact.ArtifactProxySelector.java

/**
 * Sets the proxy for a certain location URL. If the proxyHost is null, we
 * go DIRECT.//from w  ww.j  a  v  a  2  s  . co m
 * 
 * @param location
 *            location
 * @param proxyHost
 *            proxy hostname
 * @param proxyPort
 *            proxy port
 */
public void setProxy(String location, String proxyHost, int proxyPort) {
    String hostname;
    try {
        hostname = new URL(location).getHost();
    } catch (MalformedURLException e) {
        throw new RuntimeException("URL error: " + e.getMessage(), e);
    }
    if (null == proxyHost) {
        LOG.debug("removing proxy for: " + hostname);
        this.proxies.remove(hostname);
    } else {
        LOG.debug("setting proxy for: " + hostname);
        this.proxies.put(hostname, new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)));
    }
}

From source file:com.ipservices.alarmcallback.victorops.VictorOpsClient.java

public void sendAlert(Stream stream, final AlertCondition.CheckResult checkResult, final String entityId)
        throws AlarmCallbackException {
    final HttpURLConnection conn;

    try {//from   ww  w .  ja v a2  s.c om
        if (proxyUri != null) {
            final InetSocketAddress proxyAddress = new InetSocketAddress(proxyUri.getHost(),
                    proxyUri.getPort());
            final Proxy proxy = new Proxy(Proxy.Type.HTTP, proxyAddress);
            conn = (HttpURLConnection) apiUrl.openConnection(proxy);
        } else {
            conn = (HttpURLConnection) apiUrl.openConnection();
        }
        conn.setRequestMethod("POST");
    } catch (IOException e) {
        throw new AlarmCallbackException("Error while opening connection to VictorOps API.", e);
    }

    conn.setDoOutput(true);
    try (final OutputStream requestStream = conn.getOutputStream()) {
        final VictorOpsAlert alert = new VictorOpsAlert(messageType, baseUrl);
        alert.build(stream, checkResult, entityId);

        requestStream.write(objectMapper.writeValueAsBytes(alert));
        requestStream.flush();

        final InputStream responseStream;
        if (conn.getResponseCode() == 200) {
            responseStream = conn.getInputStream();
        } else {
            LOG.warn("Received response {} from server.", conn.getResponseCode());
            responseStream = conn.getErrorStream();
        }

        final VictorOpsAlertReply reply = objectMapper.readValue(responseStream, VictorOpsAlertReply.class);
        if ("success".equals(reply.result)) {
            LOG.debug("Successfully sent alert to VictorOps with entity id {}", reply.entityId);
        } else {
            LOG.warn("Error while creating VictorOps alert: {}", reply.message);
            throw new AlarmCallbackException("Error while creating VictorOps event: " + reply.message);
        }
    } catch (IOException e) {
        throw new AlarmCallbackException("Could not POST alert notification to VictorOps API.", e);
    }
}

From source file:com.canoo.cog.sonar.SonarService.java

String callSonarAuth(String path, String userName, String password) throws SonarException {
    URL url;/*from  ww  w  . ja  v a2 s  .c  o m*/
    try {
        url = new URL(baseUrl + path);
    } catch (MalformedURLException e) {
        throw new SonarException("Error in Calling Sonar Service.", e);
    }

    try {
        String authPropertyKey;
        HttpURLConnection connection;
        String encoding = new String(Base64.encodeBase64((userName + ":" + password).getBytes()));

        if (isProxyUsed()) {
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("webproxy.balgroupit.com", 3128));
            authPropertyKey = "Proxy-Authorization";
            connection = (HttpURLConnection) url.openConnection(proxy);
        } else {
            authPropertyKey = "Authorization";
            connection = (HttpURLConnection) url.openConnection();
        }

        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        connection.setRequestProperty(authPropertyKey, "Basic " + encoding);
        InputStream content = connection.getInputStream();
        BufferedReader in = new BufferedReader(new InputStreamReader(content));

        if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
            throw new SonarException("HTTP Get call to " + url.getPath() + " was not successful.");
        }

        return IOUtils.toString(in);
    } catch (ProtocolException e) {
        throw new SonarException("Error in Calling Sonar Service.", e);
    } catch (IOException e) {
        throw new SonarException("Error in Calling Sonar Service.", e);
    }
}

From source file:org.scribe.model.ProxyRequest.java

private void createConnection() throws IOException {
    final String completeUrl = getCompleteUrl();
    if (this.connection == null) {
        if (this.connectionKeepAlive) {
            this.connection.setRequestProperty("Connection", "keep-alive");
        }//from w w  w  . j a va2s  .  c o m
        if (StringUtils.isNotBlank(this.proxyHost)) {
            final Proxy proxy = new Proxy(Proxy.Type.HTTP,
                    new InetSocketAddress(this.proxyHost, this.proxyPort));
            this.connection = (HttpURLConnection) new URL(completeUrl).openConnection(proxy);
        } else {
            this.connection = (HttpURLConnection) new URL(completeUrl).openConnection();
        }
    }
}

From source file:com.rowoerowanie.endoexporttokml.ExportKml.java

private InputStream loadData(String url) throws MalformedURLException, IOException {
    System.out.println("czenie z " + url);

    HttpURLConnection connection;
    if (StringUtils.isNotBlank(this.proxyHost)) {
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(this.proxyHost, proxyPort));
        connection = (HttpURLConnection) new URL(url).openConnection(proxy);
    } else {//from   ww w .  j av  a  2s  .c om
        connection = (HttpURLConnection) new URL(url).openConnection();
    }

    return connection.getInputStream();
}

From source file:com.googlecode.jmxtrans.model.output.LibratoWriterFactory.java

private Proxy initProxy(String proxyHost, Integer proxyPort) {
    if (proxyHost == null)
        return null;

    return new Proxy(Proxy.Type.HTTP,
            new InetSocketAddress(proxyHost, checkNotNull(proxyPort, "Proxy port needs to be specified.")));
}

From source file:uk.gov.nationalarchives.discovery.taxonomy.common.repository.legacy.impl.LegacySystemRepositoryImpl.java

private ResponseEntity<LegacySearchResponse> submitSearchRequest(String url, String parameterValue, int page) {
    SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
    requestFactory.setProxy(new Proxy(Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)));
    try {//w  ww .  jav  a  2  s .c o  m
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        ResponseEntity<LegacySearchResponse> entityResponse = restTemplate
                .getForEntity(legacySystemHostUrl + url, LegacySearchResponse.class, page, parameterValue);
        return entityResponse;
    } catch (Exception e) {
        logger.error(".getLegacyCategoriesForCatDocRef : exception occured", e);
        return null;
    }
}