Example usage for org.apache.http.client HttpClient getParams

List of usage examples for org.apache.http.client HttpClient getParams

Introduction

In this page you can find the example usage for org.apache.http.client HttpClient getParams.

Prototype

@Deprecated
HttpParams getParams();

Source Link

Document

Obtains the parameters for this client.

Usage

From source file:nextflow.fs.dx.DxFileSystemProvider.java

public InputStream newInputStream(Path file, OpenOption... options) throws IOException {
    if (options.length > 0) {
        for (OpenOption opt : options) {
            if (opt != StandardOpenOption.READ)
                throw new UnsupportedOperationException("'" + opt + "' not allowed");
        }//w  w  w  . j  av  a  2  s. c o m
    }

    final DxPath path = toDxPath(file);
    final String fileId = path.getFileId();
    final Map<String, Object> download = api.fileDownload(fileId);
    final String url = (String) download.get("url");
    final Map<String, String> headers = (Map<String, String>) download.get("headers");

    final HttpClient client = DxHttpClient.getInstance().http();
    client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

    HttpGet get = new HttpGet(url);
    get.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES);
    for (Map.Entry<String, String> item : headers.entrySet()) {
        get.setHeader(item.getKey(), item.getValue());
    }

    return client.execute(get).getEntity().getContent();
}

From source file:nya.miku.wishmaster.http.cloudflare.CloudflareChecker.java

/**
 *  -?  cloudflare/*from   w ww.j  av a  2  s. co  m*/
 * @param exception Cloudflare ?
 * @param httpClient HTTP 
 * @param task ?? 
 * @param activity ?,  ?    WebView (webkit)
 * @return ? cookie  null, ?     ,       
 */
public Cookie checkAntiDDOS(CloudflareException exception, HttpClient httpClient, CancellableTask task,
        Activity activity) {
    if (exception.isRecaptcha())
        throw new IllegalArgumentException();

    HttpHost proxy = null;
    if (httpClient instanceof ExtendedHttpClient) {
        proxy = ((ExtendedHttpClient) httpClient).getProxy();
    } else if (httpClient != null) {
        try {
            proxy = ConnRouteParams.getDefaultProxy(httpClient.getParams());
        } catch (Exception e) {
            /*ignore*/ }
    }
    if (proxy != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        if (httpClient instanceof ExtendedHttpClient) {
            return InterceptingAntiDDOS.getInstance().check(exception, (ExtendedHttpClient) httpClient, task,
                    activity);
        } else {
            throw new IllegalArgumentException(
                    "cannot run anti-DDOS checking with proxy settings; http client is not instance of ExtendedHttpClient");
        }
    } else {
        return checkAntiDDOS(exception, proxy, task, activity);
    }
}

From source file:com.android.callstat.common.net.ConnectionThread.java

/**
 * A helper method to send or retrieve data through HTTP protocol.
 * //  ww w  .  j av  a  2 s  .  c  o  m
 * @param token
 *            The token to identify the sending progress.
 * @param url
 *            The URL used in a GET request. Null when the method is
 *            HTTP_POST_METHOD.
 * @param pdu
 *            The data to be POST. Null when the method is HTTP_GET_METHOD.
 * @param method
 *            HTTP_POST_METHOD or HTTP_GET_METHOD.
 * @return A byte array which contains the response data. If an HTTP error
 *         code is returned, an IOException will be thrown.
 * @throws IOException
 *             if any error occurred on network interface or an HTTP error
 *             code(&gt;=400) returned from the server.
 */
private byte[] httpConnection(Context context, boolean isProxySet, MyHttpClient client, String proxyHost,
        int proxyPort, Request request) throws IOException {
    if (request.getUri() == null) {
        throw new IllegalArgumentException("URL must not be null.");
    }
    int timeout = (int) request.getmTimeout();
    if (timeout != 0 && timeout < 5000) {
        timeout = 5000;
    }
    client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
    client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
    try {
        URI hostUrl = new URI(request.getUri());
        HttpHost target = new HttpHost(hostUrl.getHost(), hostUrl.getPort(), HttpHost.DEFAULT_SCHEME_NAME);
        HttpLog.d("URL:" + request.getUri() + " Host:" + hostUrl.getHost() + " Port:" + hostUrl.getPort());

        HttpRequest req = null;
        switch (request.getMethod()) {
        case HTTP_POST_METHOD:
            HttpPost post = new HttpPost(request.getUri());
            HttpEntity content = request.getHttpEntity();
            if (content != null) {
                post.setEntity(content);
                if (content instanceof StringEntity) {
                    final StringEntity stringEntity = (StringEntity) content;
                    post.setHeader(stringEntity.getContentEncoding());
                    post.setHeader(stringEntity.getContentType());
                }
            }
            req = post;
            break;
        case HTTP_GET_METHOD:
            req = new HttpGet(request.getUri());
            break;
        default:
            HttpLog.e("Unknown HTTP method: " + request.getMethod() + ". Must be one of POST["
                    + HTTP_POST_METHOD + "] or GET[" + HTTP_GET_METHOD + "].");
            return null;
        }
        HttpResponse response = null;
        if (CallStatUtils.isOMS()) {
            setRequest(response, isProxySet, req, client, proxyHost, proxyPort, request);
            response = client.execute(target, req);
        } else {
            if (CallStatUtils.isMOBILE(mContext)) {
                String apn = CallStatUtils.getAPN(mContext);
                if (!StringUtil.isNullOrWhitespaces(apn)) {
                    if (apn.equals("CMWAP")) {
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpHost proxy = new HttpHost("10.0.0.172", 80, "http");
                        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
                        response = httpclient.execute(target, req);
                    } else {
                        setRequest(response, isProxySet, req, client, proxyHost, proxyPort, request);
                        response = client.execute(target, req);
                    }
                } else {
                    setRequest(response, isProxySet, req, client, proxyHost, proxyPort, request);
                    response = client.execute(target, req);
                }
            } else {
                setRequest(response, isProxySet, req, client, proxyHost, proxyPort, request);
                response = client.execute(target, req);
            }

        }
        StatusLine status = response.getStatusLine();
        request.getEventHandler().status(status.getProtocolVersion().getMajor(),
                status.getProtocolVersion().getMinor(), status.getStatusCode(), status.getReasonPhrase());
        switch (status.getStatusCode()) {
        case 200:
            break;
        case 304:
            request.getEventHandler().endData(null, 0);
            return null;
        default:
            request.getEventHandler().endData(null, 0);
            throw new IOException(
                    "HTTP error: " + status.getReasonPhrase() + " CODE:" + status.getStatusCode());
        }
        Headers headers = new Headers();
        readResponseHeaders(headers, response);
        request.getEventHandler().headers(headers);

        HttpEntity entity = response.getEntity();
        byte[] body = null;
        if (entity != null) {
            try {
                int contentLength = (int) entity.getContentLength();
                if (contentLength > 0) {
                    body = new byte[contentLength];
                    // DataInputStream dis = new
                    // DataInputStream(entity.getContent());
                    InputStream in = entity.getContent();
                    int offset = 0;
                    int length = contentLength;
                    try {
                        while (length > 0) {
                            int result = in.read(body, offset, length);
                            HttpLog.v("################result:" + result);
                            offset += result;
                            length -= result;
                            if (length <= 0) {
                                request.getEventHandler().endData(body, contentLength);
                            }
                        }
                    } finally {
                        try {
                            in.close();
                            // request.mLoadListener.loaded(body,
                            // contentLength);
                            // if(length == 0)
                            // CallbackProxy.getHandler().onFinishResourceLoading(body,
                            // contentLength, request.mLoadListener);
                        } catch (IOException e) {
                            HttpLog.e("Error closing input stream: " + e.getMessage());
                        }
                    }
                } else {
                    ByteArrayBuilder dataBuilder = new ByteArrayBuilder();
                    body = new byte[8192];
                    InputStream in = entity.getContent();
                    int result = 0;
                    int count = 0;
                    int lowWater = body.length / 2;
                    try {
                        while (result != -1) {
                            result = in.read(body, count, body.length - count);
                            if (result != -1) {
                                HttpLog.v("################result:" + result);
                                count += result;
                            }
                            if (result == -1 || count >= lowWater) {
                                dataBuilder.append(body, 0, count);
                                count = 0;
                            }
                            if (result == -1) {
                                if (dataBuilder.getByteSize() > 0) {
                                    byte[] cert = new byte[dataBuilder.getByteSize()];
                                    int offset = 0;
                                    while (true) {
                                        ByteArrayBuilder.Chunk c = dataBuilder.getFirstChunk();
                                        if (c == null)
                                            break;
                                        if (c.mLength != 0) {
                                            System.arraycopy(c.mArray, 0, cert, offset, c.mLength);
                                            offset += c.mLength;
                                        }
                                        c.release();
                                    }
                                    request.getEventHandler().endData(cert, cert.length);
                                }
                            }
                        }
                    } finally {
                        try {
                            in.close();
                        } catch (IOException e) {
                            HttpLog.e("Error closing input stream: " + e.getMessage());
                        }
                    }

                }
            } finally {
                if (entity != null) {
                    entity.consumeContent();
                }
            }
        }
        return body;
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        request.getEventHandler().error(EventHandler.ERROR_BAD_URL, e.getMessage());
        e.printStackTrace();
    } catch (IllegalStateException e) {
        request.getEventHandler().error(EventHandler.ERROR, e.getMessage());
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        request.getEventHandler().error(EventHandler.ERROR_BAD_URL, e.getMessage());
        e.printStackTrace();
    } catch (SocketException e) {
        request.getEventHandler().error(EventHandler.ERROR, e.getMessage());
        e.printStackTrace();
    } catch (ConnectTimeoutException e) {
        request.getEventHandler().error(EventHandler.ERROR_TIMEOUT, e.getMessage());
        e.printStackTrace();
    } catch (Exception e) {
        request.getEventHandler().error(EventHandler.ERROR, e.getMessage());
        e.printStackTrace();
    }
    return null;
}

From source file:com.cloud.network.utils.HttpClientWrapper.java

public static HttpClient wrapClient(HttpClient base) {
    try {/*from  ww  w. j a  v a 2  s. co  m*/
        SSLContext ctx = SSLUtils.getSSLContext();
        X509TrustManager tm = new X509TrustManager() {

            @Override
            public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        X509HostnameVerifier verifier = new X509HostnameVerifier() {

            @Override
            public void verify(String string, SSLSocket ssls) throws IOException {
            }

            @Override
            public void verify(String string, X509Certificate xc) throws SSLException {
            }

            @Override
            public void verify(String string, String[] strings, String[] strings1) throws SSLException {
            }

            @Override
            public boolean verify(String string, SSLSession ssls) {
                return true;
            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(verifier);
        ClientConnectionManager ccm = base.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", ssf, 443));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:com.googlecode.noweco.webmail.lotus.LotusWebmailConnection.java

@SuppressWarnings("unchecked")
public LotusWebmailConnection(final HttpClient httpclient, final HttpHost host, final String prefix)
        throws IOException {
    this.prefix = prefix;
    HttpGet httpGet;//from   w w w .j  a v  a  2s  . c  o m
    HttpResponse rsp;
    HttpEntity entity;

    String baseName = getClass().getPackage().getName().replace('.', '/') + "/lotus";
    ResourceBundle bundleBrowser = null;

    for (Header header : (Collection<Header>) httpclient.getParams()
            .getParameter(ClientPNames.DEFAULT_HEADERS)) {
        if (header.getName().equals("Accept-Language")) {
            Matcher matcher = LANGUAGE_PATTERN.matcher(header.getValue());
            if (matcher.find()) {
                String region = matcher.group(2);
                if (region != null && region.length() != 0) {
                    bundleBrowser = ResourceBundle.getBundle(baseName, new Locale(matcher.group(1), region));
                } else {
                    bundleBrowser = ResourceBundle.getBundle(baseName, new Locale(matcher.group(1)));
                }
            }
        }
    }

    ResourceBundle bundleEnglish = ResourceBundle.getBundle(baseName, new Locale("en"));

    String deletePattern = "(?:" + Pattern.quote(bundleEnglish.getString("Delete"));
    if (bundleBrowser != null) {
        deletePattern = deletePattern + "|" + Pattern.quote(bundleBrowser.getString("Delete")) + ")";
    }
    String emptyTrashPattern = "(?:" + Pattern.quote(bundleEnglish.getString("EmptyTrash"));
    if (bundleBrowser != null) {
        emptyTrashPattern = emptyTrashPattern + "|" + Pattern.quote(bundleBrowser.getString("EmptyTrash"))
                + ")";
    }

    deleteDeletePattern = Pattern.compile(
            "_doClick\\('([^/]*/\\$V\\d+ACTIONS/[^']*)'[^>]*>" + deletePattern + "\\\\" + deletePattern);
    deleteEmptyTrashPattern = Pattern.compile(
            "_doClick\\('([^/]*/\\$V\\d+ACTIONS/[^']*)'[^>]*>" + deletePattern + "\\\\" + emptyTrashPattern);

    httpGet = new HttpGet(prefix + "/($Inbox)?OpenView");

    rsp = httpclient.execute(host, httpGet);

    entity = rsp.getEntity();
    String string = EntityUtils.toString(entity);
    if (entity != null) {
        LOGGER.trace("inbox content : {}", string);
        EntityUtils.consume(entity);
    }

    Matcher matcher = MAIN_PAGE_PATTERN.matcher(string);
    if (!matcher.find()) {
        throw new IOException("Unable to parse main page");
    }
    pagePrefix = matcher.group(1);

    this.httpclient = httpclient;
    this.host = host;
}

From source file:dev.memento.MementoClient.java

/**
  * Make http requests to the Timegate at the proxy server to obtain a Memento 
  * and its TimeMap.  This is done in a background thread so the UI is not locked up.
  * If an error occurs, mErrorMessage is set to an error message which is shown
  * to the user./*  w ww . ja va2s . c o  m*/
  * @param initUrl The URL whose Memento is to be discovered
  */
private void makeHttpRequests(String initUrl) {

    // Contact Memento proxy with chosen Accept-Datetime:
    // http://mementoproxy.lanl.gov/aggr/timegate/http://example.com/
    // Accept-Datetime: Tue, 24 Jul 2001 15:45:04 GMT             

    HttpClient httpclient = getHttpClient();

    // Disable automatic redirect handling so we can process the 302 ourself 
    httpclient.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, false);

    String url = mDefaultTimegateUri + initUrl;
    HttpGet httpget = new HttpGet(url);

    // Change the request date to 23:00:00 if this is the first memento.
    // Otherwise we'll be out of range.

    String acceptDatetime;

    if (mFirstMemento != null && mFirstMemento.getDateTime().equals(mDateChosen)) {
        log.debug("Changing chosen time to 23:59 since datetime matches first Memento.");
        SimpleDateTime dt = new SimpleDateTime(mDateChosen);
        dt.setToLastHour();
        acceptDatetime = dt.longDateFormatted();
    } else {
        acceptDatetime = mDateChosen.longDateFormatted();
    }

    httpget.setHeader("Accept-Datetime", acceptDatetime);
    httpget.setHeader("User-Agent", mUserAgent);

    log.debug("Accessing: " + httpget.getURI());
    log.debug("Accept-Datetime: " + acceptDatetime);

    log.debug("HC mHR Requesting...");
    HttpResponse response = null;
    try {
        response = httpclient.execute(httpget);

        log.debug("Response code = " + response.getStatusLine());

    } catch (Exception e) {
        mErrorMessage = "Sorry, we are having problems contacting the server. Please " + "try again later.";
        log.error("Exception when performing query: ", e);
        return;
    } finally {
        // Deallocate all system resources
        httpclient.getConnectionManager().shutdown();
    }
    log.debug("HC mHR Responded.");

    // Get back:
    // 300 (TCN: list with multiple Mementos to choose from)
    // or 302 (TCN: choice) 
    // or 404 (no Mementos for this URL)
    // or 406 (TCN: list with only first and last Mementos)

    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode == 300) {
        // TODO: Implement.  Right now the lanl proxy doesn't appear to be returning this
        // code, so let's just ignore it for now.
        //FIXME log.debug("Pick a URL from list - NOT IMPLEMENTED");         
    } else if (statusCode == 301) {
        mErrorMessage = mDefaultErrorMessage;
        log.info("Got 301 pointing to: " + response.getHeaders("Location")[0]);
        log.error("Status code 301 not supported!");
    } else if (statusCode == 302) {
        // Send browser to Location header URL
        // Note that the date/time of this memento is not given in the Location but can
        // be found when parsing the Link header.

        Header[] headers = response.getHeaders("Location");
        if (headers.length == 0) {
            mErrorMessage = mDefaultErrorMessage;
            log.error("Error: Location header not found in response headers.");
        } else {
            final String redirectUrl = headers[0].getValue();

            // We can't update the view directly since we're running
            // in a thread, so use mUpdateResults to show a toast message
            // if accessing a different date than what was requested.

            //mHandler.post(mUpdateResults);

            // Parse various Links
            headers = response.getHeaders("Link");
            if (headers.length == 0) {
                log.error("Error: Link header not found in response headers.");
                mErrorMessage = "Sorry, but the Memento could not be accessed. Try again in 5 minutes.";
            } else {
                String linkValue = headers[0].getValue();

                mTimeMaps.clear();
                mTimeBundle = null;
                mMementos.clear();

                // Get the datetime of this mememnto which should be supplied in the
                // Link: headers
                // Do not add the mementos to the global list of mementos because
                // the global list will be created when we process the timemap later.
                Memento memento = parseCsvLinks(linkValue, false);

                if (mTimeMaps.size() > 0)
                    if (!accessTimeMap() && mErrorMessage == null)
                        mErrorMessage = "There were problems accessing the Memento's TimeMap. "
                                + "Please try again later.";
            }
        }
    } else if (statusCode == 404) {
        //FIXME log.debug("Received 404 from proxy so no mementos for " + initUrl);
        mErrorMessage = "Sorry, there are no Mementos for this web page.";
    } else if (statusCode == 406) {

        // Parse various Links
        Header[] headers = response.getHeaders("Link");

        if (headers.length == 0) {
            log.debug("Error: Link header not found in 406 response headers.");
            //mErrorMessage = "Sorry, but there was an error in retreiving this Memento.";

            // The lanl proxy has it wrong.  It should return 404 when the URL is not
            // present, so we'll just pretend this is a 404.
            mErrorMessage = "Sorry, but there are no Mementos for this URL.";
        } else {
            String linkValue = headers[0].getValue();

            mTimeMaps.clear();
            mTimeBundle = null;
            mMementos.clear();

            parseCsvLinks(linkValue, false);

            if (mTimeMaps.size() > 0)
                accessTimeMap();

            if (mFirstMemento == null || mLastMemento == null) {
                log.error("Could not find first or last Memento in 406 response for " + url);
                mErrorMessage = "Sorry, but there was an error in retreiving this Memento.";
            } else {
                log.debug("Not available in this date range (" + mFirstMemento.getDateTimeSimple() + " to "
                        + mLastMemento.getDateTimeSimple() + ")");

                // According to Rob Sanderson (LANL), we will only get 406 when the date is too
                // early, so redirect to first Memento

                // FIXME ?

            }
        }
    } else {
        mErrorMessage = "Sorry, but there was an unexpected error that will "
                + "prevent the Memento from being displayed. Try again in 5 minutes.";
        log.error("Unexpected response code in makeHttpRequests = " + statusCode);
    }
}

From source file:de.fhg.iais.asc.oai.strategy.AbstractHarvester.java

/**
 * @param uri The repository url.//ww  w .ja  v  a2s .  c o m
 * @param metadataPrefix The metadata prefix to harvest.
 * @param proxyHost
 * @param proxyPort
 * @param fromDateParameter
 * @param untilDateParameter
 * @param config The current ASC configuration, non-<code>null</code>.
 * @param ascState the state object to report the progress, non-<code>null</code>.
 * @param ingestEvent The ingest event in which the harvesting takes place, non-<code>null</code>.
 */
public AbstractHarvester(String uri, String metadataPrefix, String proxyHost, Integer proxyPort,
        String fromDateParameter, String untilDateParameter, AscConfiguration config, ASCState ascState,
        AscProviderIngest ingestEvent) {

    Check.notNull(config, "config must be non-null");
    Check.notNull(ascState, "ascState must be non-null");
    Check.notNull(ingestEvent, "ingestEvent must be non-null");

    this.uri = uri;
    this.metadataPrefix = metadataPrefix;

    this.fromDate = StringUtils.defaultIfEmpty(fromDateParameter, null);
    this.untilDate = StringUtils.defaultIfEmpty(untilDateParameter, null);

    int connectionTimeout = config.get(AscConfiguration.HARVESTING_CONNECTION_TIMEOUT, 600000);
    LOG.info("set connection and socket timeouts to approx. " + (connectionTimeout / 1000) + " seconds"); // request from a.schenk DDB-724

    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeout);
    HttpConnectionParams.setSoTimeout(httpParams, connectionTimeout);
    HttpClient client = new DefaultHttpClient(httpParams);

    // set some parameters that might help but will not harm
    // see: http://hc.apache.org/httpclient-legacy/preference-api.html

    // the user-agent:
    // tell them who we are (they see that from the IP anyway), thats a good habit,
    // shows that we are professional and not some script kiddies
    // and this is also a little bit of viral marketing :-)
    client.getParams().setParameter("http.useragent", "myCortex Harvester; http://www.iais.fraunhofer.de/");
    // the following option "can result in noticeable performance improvement" (see api docs)
    // it may switch on a keep-alive, may reduce load on server side (if they are smart)
    // and might reduce latency
    client.getParams().setParameter("http.protocol.expect-continue", true);

    // ignore all cookies because some OAI-PMH implementations don't know how to handle cookies
    client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES);

    // setting of the proxy if needed
    if (proxyHost != null && !proxyHost.isEmpty()) {
        HttpHost proxy = new HttpHost(proxyHost, proxyPort);
        client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }

    this.server = new OaiPmhServer(client, this.uri);

    this.config = config;
    this.ascState = ascState;
    this.ingestEvent = ingestEvent;
}

From source file:custom.application.search.java

private Document execute(String query, int start) throws ApplicationException {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpget;/*from   w  ww .j a  v a  2 s . c  o  m*/
    try {
        httpget = new HttpGet(createRequestString(query, start == 0 ? 1 : start));
        httpClient.getParams().setParameter(HttpProtocolParams.HTTP_CONTENT_CHARSET, "UTF-8");

        HttpResponse response = httpClient.execute(httpget);
        InputStream instream = response.getEntity().getContent();

        Document document = new Document();
        document.load(instream);
        if (document.getRoot().getElementsByTagName("errors").size() > 0 && i++ < ids.length - 1) {
            CUSTOM_SEARCH_ENGINE_ID = ids[i];
            API_KEY = keys[i];
            System.out.println("Using:" + ids[i]);

            httpget = new HttpGet(createRequestString(query, start == 0 ? 1 : start));
            httpClient.getParams().setParameter(HttpProtocolParams.HTTP_CONTENT_CHARSET, "UTF-8");

            response = httpClient.execute(httpget);
            instream = response.getEntity().getContent();

            document.load(instream);
        }

        return document;
    } catch (ClientProtocolException e) {
        throw new ApplicationException(e.getMessage(), e);
    } catch (IOException e) {
        throw new ApplicationException(e.getMessage(), e);
    } catch (ParseException e) {
        throw new ApplicationException(e.getMessage(), e);
    }

}

From source file:dev.memento.MementoClient.java

/**
 *  Helper to create a web-proxy-aware HttpClient:
 * @return/*from   ww  w  .  j  a  va2s  .  c  om*/
 */
private HttpClient getHttpClient() {
    HttpClient httpclient = new DefaultHttpClient();
    if (System.getProperty("http.proxyHost") != null) {
        HttpHost proxy = new HttpHost(System.getProperty("http.proxyHost"),
                Integer.parseInt(System.getProperty("http.proxyPort")), "http");
        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        log.debug("Proxying via " + proxy);
    } else {
        log.debug("No web proxy.");
    }
    return httpclient;
}

From source file:net.sourceforge.subsonic.service.PodcastService.java

private void doDownloadEpisode(PodcastEpisode episode) {
    InputStream in = null;//  w ww .j  av a2s. c om
    OutputStream out = null;

    if (getEpisode(episode.getId(), false) == null) {
        LOG.info("Podcast " + episode.getUrl() + " was deleted. Aborting download.");
        return;
    }

    LOG.info("Starting to download Podcast from " + episode.getUrl());

    HttpClient client = new DefaultHttpClient();
    try {
        PodcastChannel channel = getChannel(episode.getChannelId());

        HttpConnectionParams.setConnectionTimeout(client.getParams(), 2 * 60 * 1000); // 2 minutes
        HttpConnectionParams.setSoTimeout(client.getParams(), 10 * 60 * 1000); // 10 minutes
        HttpGet method = new HttpGet(episode.getUrl());

        HttpResponse response = client.execute(method);
        in = response.getEntity().getContent();

        File file = getFile(channel, episode);
        out = new FileOutputStream(file);

        episode.setStatus(PodcastStatus.DOWNLOADING);
        episode.setBytesDownloaded(0L);
        episode.setErrorMessage(null);
        episode.setPath(file.getPath());
        podcastDao.updateEpisode(episode);

        byte[] buffer = new byte[4096];
        long bytesDownloaded = 0;
        int n;
        long nextLogCount = 30000L;

        while ((n = in.read(buffer)) != -1) {
            out.write(buffer, 0, n);
            bytesDownloaded += n;

            if (bytesDownloaded > nextLogCount) {
                episode.setBytesDownloaded(bytesDownloaded);
                nextLogCount += 30000L;
                if (getEpisode(episode.getId(), false) == null) {
                    break;
                }
                podcastDao.updateEpisode(episode);
            }
        }

        if (getEpisode(episode.getId(), false) == null) {
            LOG.info("Podcast " + episode.getUrl() + " was deleted. Aborting download.");
            IOUtils.closeQuietly(out);
            file.delete();
        } else {
            episode.setBytesDownloaded(bytesDownloaded);
            podcastDao.updateEpisode(episode);
            LOG.info("Downloaded " + bytesDownloaded + " bytes from Podcast " + episode.getUrl());
            IOUtils.closeQuietly(out);
            episode.setStatus(PodcastStatus.COMPLETED);
            podcastDao.updateEpisode(episode);
            deleteObsoleteEpisodes(channel);
        }

    } catch (Exception x) {
        LOG.warn("Failed to download Podcast from " + episode.getUrl(), x);
        episode.setStatus(PodcastStatus.ERROR);
        episode.setErrorMessage(x.toString());
        podcastDao.updateEpisode(episode);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
        client.getConnectionManager().shutdown();
    }
}