Example usage for org.apache.http.client.params ClientPNames HANDLE_REDIRECTS

List of usage examples for org.apache.http.client.params ClientPNames HANDLE_REDIRECTS

Introduction

In this page you can find the example usage for org.apache.http.client.params ClientPNames HANDLE_REDIRECTS.

Prototype

String HANDLE_REDIRECTS

To view the source code for org.apache.http.client.params ClientPNames HANDLE_REDIRECTS.

Click Source Link

Document

Defines whether redirects should be handled automatically

This parameter expects a value of type Boolean .

Usage

From source file:org.hammer.santamaria.mapper.dataset.CKANDataSetInput.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] pArgs) throws Exception {
    String id = "proportion-of-children-under-5-years-who-have-ever-breastfed-by-county-xls-2005-6";
    String sId = EncodeURIComponent(id);
    String url = "https://africaopendata.org/api/action";

    BSONObject dataset = new BasicBSONObject();
    dataset.put("datasource", "Test");
    dataset.put("id", id);

    LOG.info("---> id " + id + " - " + sId);

    HttpClient client = new HttpClient();
    client.getHttpConnectionManager().getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, false);
    LOG.info(/*w w w .  ja va2s  .  c o  m*/
            "******************************************************************************************************");
    LOG.info(" ");
    LOG.info(url + PACKAGE_GET + sId);
    LOG.info(" ");
    LOG.info(
            "******************************************************************************************************");

    GetMethod method = new GetMethod(url + PACKAGE_GET + sId);

    method.setRequestHeader("User-Agent", "Hammer Project - SantaMaria crawler");
    method.getParams().setParameter(HttpMethodParams.USER_AGENT, "Hammer Project - SantaMaria crawler");
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    try {
        int statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            throw new Exception("Method failed: " + method.getStatusLine());
        }
        byte[] responseBody = method.getResponseBody();
        LOG.debug(new String(responseBody));
        Document doc = Document.parse(new String(responseBody));

        if (doc != null && doc.containsKey("result")) {
            Document result = new Document();
            LOG.info(doc.get("result").getClass().toString());
            if (doc.get("result") instanceof Document) {
                LOG.info("!!! Document result !!!!");
                result = (Document) doc.get("result");
            } else if (doc.get("result") instanceof ArrayList) {
                LOG.info("!!! Document list !!!!");

                result = (Document) (((ArrayList) doc.get("result")).get(0));
            } else {
                LOG.info("!!! NOT FOUND !!!!");
                result = null;
            }
            LOG.info("result find!");
            if (result != null) {
                dataset.put("title", result.get("title"));
                dataset.put("author", result.get("author"));
                dataset.put("author_email", result.get("author_email"));
                dataset.put("license_id", result.get("license_id"));
            }

            ArrayList<String> tags = new ArrayList<String>();
            ArrayList<String> meta = new ArrayList<String>();
            ArrayList<String> other_tags = new ArrayList<String>();

            if (result.containsKey("author") && result.get("author") != null)
                other_tags.add(result.get("author").toString());
            if (result.containsKey("title") && result.get("title") != null)
                other_tags.addAll(DSSUtils.GetKeyWordsFromText(result.get("title").toString()));
            if (result.containsKey("description") && result.get("description") != null)
                other_tags.addAll(DSSUtils.GetKeyWordsFromText(result.get("description").toString()));

            ArrayList<Document> resources = new ArrayList<Document>();
            if (result != null && result.containsKey("resources")) {
                resources = (ArrayList<Document>) result.get("resources");
                for (Document resource : resources) {
                    if (resource.getString("format").toUpperCase().equals("JSON")) {
                        dataset.put("dataset-type", "JSON");
                        dataset.put("url", resource.get("url"));
                        dataset.put("created", resource.get("created"));
                        dataset.put("description", resource.get("description"));
                        dataset.put("revision_timestamp", resource.get("revision_timestamp"));
                        meta = DSSUtils.GetMetaByResource(resource.get("url").toString());
                    }
                }
            }

            if (result != null && result.containsKey("tags")) {
                ArrayList<Document> tagsFromCKAN = (ArrayList<Document>) result.get("tags");
                for (Document tag : tagsFromCKAN) {
                    if (tag.containsKey("state") && tag.getString("state").toUpperCase().equals("ACTIVE")) {
                        tags.add(tag.getString("display_name").trim().toLowerCase());
                    } else if (tag.containsKey("display_name")) {
                        tags.add(tag.getString("display_name").trim().toLowerCase());
                    }
                }

            }

            dataset.put("tags", tags);
            dataset.put("meta", meta);
            dataset.put("resources", resources);
            dataset.put("other_tags", other_tags);

        }

    } catch (Exception e) {
        e.printStackTrace();
        LOG.error(e);
    } finally {
        method.releaseConnection();
    }

    //GetMetaByDocument("http://catalog.data.gov/api/action/package_show?id=1e68f387-5f1c-46c0-a0d1-46044ffef5bf");
}

From source file:com.subgraph.vega.internal.http.requests.BasicHttpClientFactory.java

static HttpClient createHttpClient() {
    final HttpParams params = createHttpParams();
    final ClientConnectionManager ccm = createConnectionManager(params);
    final DefaultHttpClient client = new DefaultHttpClient(ccm, params);

    client.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);
    client.clearRequestInterceptors();//from   ww w  .  j  a  va 2s . com
    client.clearResponseInterceptors();
    client.addRequestInterceptor(new RequestCopyHeadersInterceptor());
    return client;
}

From source file:com.subgraph.vega.internal.http.requests.UnencodedHttpClientFactory.java

static HttpClient createHttpClient() {
    final HttpParams params = createHttpParams();
    final ClientConnectionManager ccm = createConnectionManager(params);
    final DefaultHttpClient client = new DefaultHttpClient(ccm, params);

    client.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);
    client.addRequestInterceptor(new RequestCopyHeadersInterceptor());
    return client;
}

From source file:org.apache.marmotta.platform.core.util.http.HttpRequestUtil.java

/**
 * Configure whether redirects for this request should be handled automatically.
 * //from ww  w  .j  a  v a 2  s. c o  m
 * @param request the request to modify
 * @param followRedirect <code>true</code> if redirects (HTTP response code 3xx) should be
 *            handled automatically.
 */
public static void setFollowRedirect(HttpRequestBase request, boolean followRedirect) {
    request.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, followRedirect);
}

From source file:com.rackspacecloud.blueflood.http.HttpClientVendor.java

public HttpClientVendor() {
    client = new DefaultHttpClient(buildConnectionManager(20));
    client.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true);
    client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
    client.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 30000);

    // Wait this long for an available connection. Setting this correctly is important in order to avoid
    // connectionpool timeouts.
    client.getParams().setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, 5000);
}

From source file:org.apache.camel.component.restlet.RestletRedirectTest.java

@Test
public void testRedirect() throws Exception {
    HttpGet get = new HttpGet("http://localhost:" + portNum + "/users/homer");

    // do not follow redirects
    HttpClient client = new DefaultHttpClient();
    client.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);

    HttpResponse response = client.execute(get);

    for (Header header : response.getAllHeaders()) {
        log.info("Header {}", header);
    }/* w  w w.  j ava 2s .  c o  m*/

    assertEquals(302, response.getStatusLine().getStatusCode());
    assertTrue("Should have location header", response.containsHeader("Location"));
    assertEquals("http://somewhere.com", response.getFirstHeader("Location").getValue());
    assertEquals("bar", response.getFirstHeader("Foo").getValue());
}

From source file:org.openrepose.core.services.httpclient.impl.HttpConnectionPoolProvider.java

public static HttpClient genClient(PoolType poolConf) {

    PoolingClientConnectionManager cm = new PoolingClientConnectionManager();

    cm.setDefaultMaxPerRoute(poolConf.getHttpConnManagerMaxPerRoute());
    cm.setMaxTotal(poolConf.getHttpConnManagerMaxTotal());

    //Set all the params up front, instead of mutating them? Maybe this matters
    HttpParams params = new BasicHttpParams();
    params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES);
    params.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, poolConf.getHttpSocketTimeout());
    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, poolConf.getHttpConnectionTimeout());
    params.setParameter(CoreConnectionPNames.TCP_NODELAY, poolConf.isHttpTcpNodelay());
    params.setParameter(CoreConnectionPNames.MAX_HEADER_COUNT, poolConf.getHttpConnectionMaxHeaderCount());
    params.setParameter(CoreConnectionPNames.MAX_LINE_LENGTH, poolConf.getHttpConnectionMaxLineLength());
    params.setParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, poolConf.getHttpSocketBufferSize());
    params.setBooleanParameter(CHUNKED_ENCODING_PARAM, poolConf.isChunkedEncoding());

    final String uuid = UUID.randomUUID().toString();
    params.setParameter(CLIENT_INSTANCE_ID, uuid);

    //Pass in the params and the connection manager
    DefaultHttpClient client = new DefaultHttpClient(cm, params);

    SSLContext sslContext = ProxyUtilities.getTrustingSslContext();
    SSLSocketFactory ssf = new SSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    SchemeRegistry registry = cm.getSchemeRegistry();
    Scheme scheme = new Scheme("https", DEFAULT_HTTPS_PORT, ssf);
    registry.register(scheme);/*from   w  w w.ja  v  a  2  s . c o m*/

    client.setKeepAliveStrategy(new ConnectionKeepAliveWithTimeoutStrategy(poolConf.getKeepaliveTimeout()));

    LOG.info("HTTP connection pool {} with instance id {} has been created", poolConf.getId(), uuid);

    return client;
}

From source file:com.parking.auth.LoginUserAsyncTask.java

protected Boolean doInBackground(String... tokens) {
    try {/*  www  . j  av  a  2s.c  o m*/
        /** Don't follow redirects */
        http_client.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true);

        HttpPost httppost = new HttpPost(ParkingConstants.serverUrl + "/loginServlet");

        /** Add your data */
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("username", ParkingApplication.getAccount().name));
        nameValuePairs.add(new BasicNameValuePair("isDevice", "true"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response;
        response = http_client.execute(httppost);
        Log.v(TAG, "Respone is " + response.getStatusLine().getStatusCode());
        Log.v(TAG, response.getStatusLine().toString());

        if (response.getStatusLine().getStatusCode() == 200) {
            /** Extract JSON user message from user response */
            ObjectMapper objectMapper = new ObjectMapper();
            LicensePlateJsonObject licensePlate = objectMapper.readValue(response.getEntity().getContent(),
                    LicensePlateJsonObject.class);
            result = true;
            AppPreferences.getInstance()
                    .setLicensePlateString(licensePlate.getLicensePlateList().toUpperCase());
        }

    } catch (Exception e) {
        e.printStackTrace();
        result = false;
    } finally {
        http_client.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true);
    }
    return result;
}

From source file:org.red5.server.util.HttpConnectionUtil.java

/**
 * Returns a client with all our selected properties / params.
 * //from w w w  .java  2  s.c o m
 * @return client
 */
public static final DefaultHttpClient getClient() {
    // create a singular HttpClient object
    DefaultHttpClient client = new DefaultHttpClient(connectionManager);
    // dont retry
    client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));
    // get the params for the client
    HttpParams params = client.getParams();
    // establish a connection within x seconds
    params.setParameter(CoreConnectionPNames.SO_TIMEOUT, connectionTimeout);
    // no redirects
    params.setParameter(ClientPNames.HANDLE_REDIRECTS, false);
    // set custom ua
    params.setParameter(CoreProtocolPNames.USER_AGENT, userAgent);
    // set the proxy if the user has one set
    if ((System.getProperty("http.proxyHost") != null) && (System.getProperty("http.proxyPort") != null)) {
        HttpHost proxy = new HttpHost(System.getProperty("http.proxyHost").toString(),
                Integer.valueOf(System.getProperty("http.proxyPort")));
        client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }
    return client;
}

From source file:org.hammer.santamaria.mapper.dataset.CKAN2DataSetInput.java

@SuppressWarnings("unchecked")
public BSONObject getDataSet(String url, String datasource, String id, BSONObject c) {
    BSONObject dataset = new BasicBSONObject();
    dataset.put("datasource", datasource);
    dataset.put("id", id);

    String sId = EncodeURIComponent(id);
    LOG.info("---> id " + id + " - " + sId);

    HttpClient client = new HttpClient();
    client.getHttpConnectionManager().getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, false);

    LOG.info(/*from   w ww .  jav a2  s.  co m*/
            "******************************************************************************************************");
    LOG.info(" ");
    LOG.info(url + PACKAGE_GET + sId);
    LOG.info(" ");
    LOG.info(
            "******************************************************************************************************");

    GetMethod method = new GetMethod(url + PACKAGE_GET + sId);

    method.setRequestHeader("User-Agent", "Hammer Project - SantaMaria crawler");
    method.getParams().setParameter(HttpMethodParams.USER_AGENT, "Hammer Project - SantaMaria crawler");
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    try {
        int statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            throw new Exception("Method failed: " + method.getStatusLine());
        }
        byte[] responseBody = method.getResponseBody();
        LOG.debug(new String(responseBody));
        Document result = Document.parse(new String(responseBody));

        if (result != null) {
            dataset.put("title", result.get("title"));
            dataset.put("author", result.get("author"));
            dataset.put("author_email", result.get("author_email"));
            dataset.put("license_id", result.get("license"));
        }

        ArrayList<String> tags = new ArrayList<String>();
        ArrayList<String> meta = new ArrayList<String>();
        ArrayList<String> other_tags = new ArrayList<String>();

        if (result.containsKey("author"))
            other_tags.add(result.get("author").toString());
        if (result.containsKey("title"))
            other_tags.addAll(DSSUtils.GetKeyWordsFromText(result.get("title").toString()));
        if (result.containsKey("description"))
            other_tags.addAll(DSSUtils.GetKeyWordsFromText(result.get("description").toString()));

        ArrayList<Document> resources = new ArrayList<Document>();
        if (result != null && result.containsKey("resources")) {
            resources = (ArrayList<Document>) result.get("resources");
            for (Document resource : resources) {
                if (resource.getString("format").toUpperCase().equals("JSON")) {
                    dataset.put("dataset-type", "JSON");
                    dataset.put("url", resource.get("url"));
                    dataset.put("created", resource.get("created"));
                    dataset.put("description", resource.get("description"));
                    dataset.put("revision_timestamp", resource.get("revision_timestamp"));
                    meta = DSSUtils.GetMetaByResource(resource.get("url").toString());

                }
            }
        }

        if (result != null && result.containsKey("tags")) {
            ArrayList<Document> tagsFromCKAN = (ArrayList<Document>) result.get("tags");
            for (Document tag : tagsFromCKAN) {
                if (tag.containsKey("state") && tag.getString("state").toUpperCase().equals("ACTIVE")) {
                    tags.add(tag.getString("display_name").trim().toLowerCase());
                } else if (tag.containsKey("display_name")) {
                    tags.add(tag.getString("display_name").trim().toLowerCase());
                }
            }

        }

        dataset.put("tags", tags);
        dataset.put("meta", meta);
        dataset.put("resources", resources);
        dataset.put("other_tags", other_tags);

    } catch (Exception e) {
        e.printStackTrace();
        LOG.error(e);
    } finally {
        method.releaseConnection();
    }
    return dataset;
}