Search in sources :

Example 1 with Proxy

use of java.net.Proxy in project hadoop by apache.

the class SocksSocketFactory method setProxy.

/**
   * Set the proxy of this socket factory as described in the string
   * parameter
   * 
   * @param proxyStr the proxy address using the format "host:port"
   */
private void setProxy(String proxyStr) {
    String[] strs = proxyStr.split(":", 2);
    if (strs.length != 2)
        throw new RuntimeException("Bad SOCKS proxy parameter: " + proxyStr);
    String host = strs[0];
    int port = Integer.parseInt(strs[1]);
    this.proxy = new Proxy(Proxy.Type.SOCKS, InetSocketAddress.createUnresolved(host, port));
}
Also used : Proxy(java.net.Proxy)

Example 2 with Proxy

use of java.net.Proxy in project hadoop by apache.

the class JobEndNotifier method setConf.

/**
   * Parse the URL that needs to be notified of the end of the job, along
   * with the number of retries in case of failure, the amount of time to
   * wait between retries and proxy settings
   * @param conf the configuration 
   */
public void setConf(Configuration conf) {
    this.conf = conf;
    numTries = Math.min(conf.getInt(MRJobConfig.MR_JOB_END_RETRY_ATTEMPTS, 0) + 1, conf.getInt(MRJobConfig.MR_JOB_END_NOTIFICATION_MAX_ATTEMPTS, 1));
    waitInterval = Math.min(conf.getInt(MRJobConfig.MR_JOB_END_RETRY_INTERVAL, 5000), conf.getInt(MRJobConfig.MR_JOB_END_NOTIFICATION_MAX_RETRY_INTERVAL, 5000));
    waitInterval = (waitInterval < 0) ? 5000 : waitInterval;
    timeout = conf.getInt(JobContext.MR_JOB_END_NOTIFICATION_TIMEOUT, JobContext.DEFAULT_MR_JOB_END_NOTIFICATION_TIMEOUT);
    userUrl = conf.get(MRJobConfig.MR_JOB_END_NOTIFICATION_URL);
    proxyConf = conf.get(MRJobConfig.MR_JOB_END_NOTIFICATION_PROXY);
    //proxyType@proxyHostname:port
    if (proxyConf != null && !proxyConf.equals("") && proxyConf.lastIndexOf(":") != -1) {
        int typeIndex = proxyConf.indexOf("@");
        Proxy.Type proxyType = Proxy.Type.HTTP;
        if (typeIndex != -1 && proxyConf.substring(0, typeIndex).compareToIgnoreCase("socks") == 0) {
            proxyType = Proxy.Type.SOCKS;
        }
        String hostname = proxyConf.substring(typeIndex + 1, proxyConf.lastIndexOf(":"));
        String portConf = proxyConf.substring(proxyConf.lastIndexOf(":") + 1);
        try {
            int port = Integer.parseInt(portConf);
            proxyToUse = new Proxy(proxyType, new InetSocketAddress(hostname, port));
            Log.getLog().info("Job end notification using proxy type \"" + proxyType + "\" hostname \"" + hostname + "\" and port \"" + port + "\"");
        } catch (NumberFormatException nfe) {
            Log.getLog().warn("Job end notification couldn't parse configured" + "proxy's port " + portConf + ". Not going to use a proxy");
        }
    }
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress)

Example 3 with Proxy

use of java.net.Proxy in project dropwizard by dropwizard.

the class JerseyClientBuilderTest method usesACustomHttpRoutePlanner.

@Test
public void usesACustomHttpRoutePlanner() {
    final HttpRoutePlanner customHttpRoutePlanner = new SystemDefaultRoutePlanner(new ProxySelector() {

        @Override
        public List<Proxy> select(URI uri) {
            return ImmutableList.of(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);
}
Also used : ProxySelector(java.net.ProxySelector) Proxy(java.net.Proxy) HttpRoutePlanner(org.apache.http.conn.routing.HttpRoutePlanner) InetSocketAddress(java.net.InetSocketAddress) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) URI(java.net.URI) SystemDefaultRoutePlanner(org.apache.http.impl.conn.SystemDefaultRoutePlanner) Test(org.junit.Test)

Example 4 with Proxy

use of java.net.Proxy in project storymaker by StoryMaker.

the class StorymakerDownloadManager method downloadWithTor.

private void downloadWithTor(boolean useTor, Uri uri, String title, String desc, File targetFile) {
    initNotificationManager();
    // generate id/tag for notification
    String nTag = indexItem.getExpansionId();
    int nId = 0;
    if (fileName.contains(scal.io.liger.Constants.MAIN)) {
        nId = Integer.parseInt(indexItem.getExpansionFileVersion());
    } else if (fileName.contains(scal.io.liger.Constants.PATCH)) {
        nId = Integer.parseInt(indexItem.getPatchFileVersion());
    }
    // incompatible with lungcast certificate
    // StrongHttpsClient httpClient = getHttpClientInstance();
    OkHttpClient httpClient = new OkHttpClient();
    // we're now using this method to support non-tor downloads as well, so settings must be checked
    if (useTor) {
        if (checkTor(context)) {
            Timber.d("DOWNLOAD WITH TOR PROXY: " + scal.io.liger.Constants.TOR_PROXY_HOST + "/" + scal.io.liger.Constants.TOR_PROXY_PORT);
            SocketAddress torSocket = new InetSocketAddress(scal.io.liger.Constants.TOR_PROXY_HOST, scal.io.liger.Constants.TOR_PROXY_PORT);
            Proxy torProxy = new Proxy(Proxy.Type.HTTP, torSocket);
            httpClient.setProxy(torProxy);
        } else {
            Timber.e("CANNOT DOWNLOAD WITH TOR, TOR IS NOT ACTIVE");
            if (context instanceof Activity) {
                // FIXME move to strings
                Utils.toastOnUiThread((Activity) context, "Check settings, can't use tor if orbot isn't running", true);
            }
            StorymakerQueueManager.checkQueueFinished(context, targetFile.getName());
            return;
        }
    }
    // disable attempts to retry (more retries ties up connection and prevents failure handling)
    httpClient.setRetryOnConnectionFailure(false);
    // set modest timeout (longer timeout ties up connection and prevents failure handling)
    httpClient.setConnectTimeout(3000, TimeUnit.MILLISECONDS);
    String actualFileName = targetFile.getName().substring(0, targetFile.getName().lastIndexOf("."));
    Timber.d("CHECKING URI: " + uri.toString());
    try {
        // FIXME - adding the "Connection: close" header to resolve an issue that seemed to be caused
        // FIXME - by a lingering connection.  when possible, the better solution would be to track
        // FIXME - down the possible end states and add appropriate cleanup steps.
        Request request = new Request.Builder().url(uri.toString()).addHeader("Connection", "close").build();
        // check for partially downloaded file
        File partFile = new File(targetFile.getPath().replace(".tmp", ".part"));
        if (partFile.exists()) {
            long partBytes = partFile.length();
            Timber.d("PARTIAL FILE " + partFile.getPath() + " FOUND, SETTING RANGE HEADER: " + "Range" + " / " + "bytes=" + Long.toString(partBytes) + "-");
            // request.setHeader("Range", "bytes=" + Long.toString(partBytes) + "-");
            request = new Request.Builder().url(uri.toString()).addHeader("Connection", "close").addHeader("Range", "bytes=" + Long.toString(partBytes) + "-").build();
        } else {
            Timber.d("PARTIAL FILE " + partFile.getPath() + " NOT FOUND, STARTING AT BYTE 0");
        }
        Response response = httpClient.newCall(request).execute();
        int statusCode = response.code();
        if ((statusCode == 200) || (statusCode == 206)) {
            Timber.d("DOWNLOAD SUCCEEDED, STATUS CODE: " + statusCode);
            // queue item here, "download" doesn't start until after we get a status code
            // queue item, use date to get a unique long, subtract to get a negative number (to distinguish from download manager items)
            Date startTime = new Date();
            long queueId = 0 - startTime.getTime();
            StorymakerQueueManager.addToQueue(context, queueId, targetFile.getName(), queueDao);
            targetFile.getParentFile().mkdirs();
            Timber.d("DOWNLOAD SUCCEEDED, GETTING ENTITY...");
            BufferedInputStream responseInput = new BufferedInputStream(response.body().byteStream());
            try {
                FileOutputStream targetOutput = new FileOutputStream(targetFile);
                byte[] buf = new byte[1024];
                int i;
                int oldPercent = 0;
                long thisTime;
                long lastTime = -1;
                int lastPercent = 0;
                while ((i = responseInput.read(buf)) > 0) {
                    // create status bar notification
                    int nPercent = StorymakerDownloadHelper.getDownloadPercent(context, fileName, installedDao);
                    thisTime = System.currentTimeMillis();
                    if (oldPercent == nPercent) {
                    // need to cut back on notification traffic
                    } else {
                        if (nPercent % 10 == 0 && nPercent != lastPercent) {
                            if (lastTime == -1 || (thisTime - lastTime) > 1000) {
                                lastPercent = nPercent;
                                oldPercent = nPercent;
                                lastTime = thisTime;
                                Notification nProgress = new Notification.Builder(context).setContentTitle(mAppTitle + " content download").setContentText(// assignment file names are meaningless uuids
                                indexItem.getTitle() + " - " + (nPercent / 10.0) + "%").setSmallIcon(android.R.drawable.arrow_down_float).setProgress(100, (nPercent / 10), false).setWhen(startTime.getTime()).build();
                                nManager.notify(nTag, nId, nProgress);
                            //Log.d("Storymaker Download Manager", "** NOTIFICATION ** " + nPercent );
                            }
                        }
                    }
                    targetOutput.write(buf, 0, i);
                }
                targetOutput.close();
                responseInput.close();
                Timber.d("SAVED DOWNLOAD TO " + targetFile);
            } catch (ConnectTimeoutException cte) {
                Timber.e(cte, "FAILED TO SAVE DOWNLOAD TO " + actualFileName + " (CONNECTION EXCEPTION)");
            } catch (SocketTimeoutException ste) {
                Timber.e(ste, "FAILED TO SAVE DOWNLOAD TO " + actualFileName + " (SOCKET EXCEPTION)");
            } catch (IOException ioe) {
                Timber.e(ioe, "FAILED TO SAVE DOWNLOAD TO " + actualFileName + " (IO EXCEPTION)");
            }
            // remove from queue here, regardless of success
            StorymakerQueueManager.removeFromQueue(context, queueId, queueDao);
            // remove notification, regardless of success
            nManager.cancel(nTag, nId);
            // (assumes .tmp file will exist if download is interrupted)
            if (!handleFile(targetFile)) {
                Timber.e("ERROR DURING FILE PROCESSING FOR " + actualFileName);
            }
        } else {
            Timber.e("DOWNLOAD FAILED FOR " + actualFileName + ", STATUS CODE: " + statusCode);
            StorymakerQueueManager.checkQueueFinished(context, targetFile.getName());
        }
    // clean up connection
    // EntityUtils.consume(entity);
    // request.abort();
    // request.releaseConnection();
    } catch (IOException ioe) {
        Timber.e(ioe, "DOWNLOAD FAILED FOR " + actualFileName + ", EXCEPTION THROWN");
        StorymakerQueueManager.checkQueueFinished(context, targetFile.getName());
    }
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) InetSocketAddress(java.net.InetSocketAddress) Request(com.squareup.okhttp.Request) Activity(android.app.Activity) IOException(java.io.IOException) Date(java.util.Date) Notification(android.app.Notification) Response(com.squareup.okhttp.Response) HttpResponse(ch.boye.httpclientandroidlib.HttpResponse) Proxy(java.net.Proxy) SocketTimeoutException(java.net.SocketTimeoutException) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) File(java.io.File) ConnectTimeoutException(ch.boye.httpclientandroidlib.conn.ConnectTimeoutException)

Example 5 with Proxy

use of java.net.Proxy in project phonegap-facebook-plugin by Wizcorp.

the class HttpURLConnectionImpl method processResponseHeaders.

/**
   * Returns the retry action to take for the current response headers. The
   * headers, proxy and target URL or this connection may be adjusted to
   * prepare for a follow up request.
   */
private Retry processResponseHeaders() throws IOException {
    Proxy selectedProxy = httpEngine.connection != null ? httpEngine.connection.getRoute().getProxy() : requestedProxy;
    final int responseCode = getResponseCode();
    switch(responseCode) {
        case HTTP_PROXY_AUTH:
            if (selectedProxy.type() != Proxy.Type.HTTP) {
                throw new ProtocolException("Received HTTP_PROXY_AUTH (407) code while not using proxy");
            }
        // fall-through
        case HTTP_UNAUTHORIZED:
            boolean credentialsFound = HttpAuthenticator.processAuthHeader(getResponseCode(), httpEngine.getResponseHeaders().getHeaders(), rawRequestHeaders, selectedProxy, url);
            return credentialsFound ? Retry.SAME_CONNECTION : Retry.NONE;
        case HTTP_MULT_CHOICE:
        case HTTP_MOVED_PERM:
        case HTTP_MOVED_TEMP:
        case HTTP_SEE_OTHER:
        case HTTP_TEMP_REDIRECT:
            if (!getInstanceFollowRedirects()) {
                return Retry.NONE;
            }
            if (++redirectionCount > MAX_REDIRECTS) {
                throw new ProtocolException("Too many redirects: " + redirectionCount);
            }
            if (responseCode == HTTP_TEMP_REDIRECT && !method.equals("GET") && !method.equals("HEAD")) {
                // the user agent MUST NOT automatically redirect the request"
                return Retry.NONE;
            }
            String location = getHeaderField("Location");
            if (location == null) {
                return Retry.NONE;
            }
            URL previousUrl = url;
            url = new URL(previousUrl, location);
            if (!url.getProtocol().equals("https") && !url.getProtocol().equals("http")) {
                // Don't follow redirects to unsupported protocols.
                return Retry.NONE;
            }
            boolean sameProtocol = previousUrl.getProtocol().equals(url.getProtocol());
            if (!sameProtocol && !followProtocolRedirects) {
                // This client doesn't follow redirects across protocols.
                return Retry.NONE;
            }
            boolean sameHost = previousUrl.getHost().equals(url.getHost());
            boolean samePort = getEffectivePort(previousUrl) == getEffectivePort(url);
            if (sameHost && samePort && sameProtocol) {
                return Retry.SAME_CONNECTION;
            } else {
                return Retry.DIFFERENT_CONNECTION;
            }
        default:
            return Retry.NONE;
    }
}
Also used : ProtocolException(java.net.ProtocolException) Proxy(java.net.Proxy) URL(java.net.URL)

Aggregations

Proxy (java.net.Proxy)132 InetSocketAddress (java.net.InetSocketAddress)71 URL (java.net.URL)44 IOException (java.io.IOException)35 Test (org.junit.Test)22 URI (java.net.URI)21 ProxySelector (java.net.ProxySelector)20 HttpURLConnection (java.net.HttpURLConnection)19 SocketAddress (java.net.SocketAddress)19 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)13 URISyntaxException (java.net.URISyntaxException)11 ServerSocket (java.net.ServerSocket)10 Socket (java.net.Socket)9 PasswordAuthentication (java.net.PasswordAuthentication)8 InterruptedIOException (java.io.InterruptedIOException)6 URLConnection (java.net.URLConnection)6 SSLServerSocket (javax.net.ssl.SSLServerSocket)6 List (java.util.List)5 SSLSocket (javax.net.ssl.SSLSocket)5 OkHttpClient (okhttp3.OkHttpClient)5