Example usage for org.apache.http.impl.client BasicCookieStore BasicCookieStore

List of usage examples for org.apache.http.impl.client BasicCookieStore BasicCookieStore

Introduction

In this page you can find the example usage for org.apache.http.impl.client BasicCookieStore BasicCookieStore.

Prototype

public BasicCookieStore() 

Source Link

Usage

From source file:cl.nic.dte.net.ConexionSii.java

private RECEPCIONDTEDocument uploadEnvio(String rutEnvia, String rutCompania, File archivoEnviarSII,
        String token, String urlEnvio, String hostEnvio)
        throws ClientProtocolException, IOException, org.apache.http.ParseException, XmlException {

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(urlEnvio);

    MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

    reqEntity.addPart("rutSender", new StringBody(rutEnvia.substring(0, rutEnvia.length() - 2)));
    reqEntity.addPart("dvSender", new StringBody(rutEnvia.substring(rutEnvia.length() - 1, rutEnvia.length())));
    reqEntity.addPart("rutCompany", new StringBody(rutCompania.substring(0, (rutCompania).length() - 2)));
    reqEntity.addPart("dvCompany",
            new StringBody(rutCompania.substring(rutCompania.length() - 1, rutCompania.length())));

    FileBody bin = new FileBody(archivoEnviarSII);
    reqEntity.addPart("archivo", bin);

    httppost.setEntity(reqEntity);/*  ww w .  j  a  va  2  s  . c  o m*/

    BasicClientCookie cookie = new BasicClientCookie("TOKEN", token);
    cookie.setPath("/");
    cookie.setDomain(hostEnvio);
    cookie.setSecure(true);
    cookie.setVersion(1);

    CookieStore cookieStore = new BasicCookieStore();
    cookieStore.addCookie(cookie);

    httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
    httppost.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);

    HttpContext localContext = new BasicHttpContext();
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

    httppost.addHeader(new BasicHeader("User-Agent", Utilities.netLabels.getString("UPLOAD_SII_HEADER_VALUE")));

    HttpResponse response = httpclient.execute(httppost, localContext);

    HttpEntity resEntity = response.getEntity();

    RECEPCIONDTEDocument resp = null;

    HashMap<String, String> namespaces = new HashMap<String, String>();
    namespaces.put("", "http://www.sii.cl/SiiDte");
    XmlOptions opts = new XmlOptions();
    opts.setLoadSubstituteNamespaces(namespaces);

    resp = RECEPCIONDTEDocument.Factory.parse(EntityUtils.toString(resEntity), opts);

    return resp;
}

From source file:sand.actionhandler.weibo.UdaClient.java

public static String syn(String method, String machineno, String params) {
    String content = "";

    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {/* w  w  w  . ja  va  2 s  . c  o m*/
        httpclient = createHttpClient();
        //      HttpGet httpget = new HttpGet("http://www.broken-server.com/");
        // 

        HttpGet httpget = new HttpGet(SERVER + method + "?no=" + machineno + "&" + params);

        httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2965);

        httpget.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);

        //           httpget.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
        // Execute HTTP request
        //System.out.println("executing request " + httpget.getURI());

        // DefaultHttpClient httpclient = new DefaultHttpClient();
        // cookie store
        CookieStore cookieStore = new BasicCookieStore();

        addCookie(cookieStore);
        // 
        httpclient.setCookieStore(cookieStore);

        logger.info("executing request " + httpget.getURI());
        HttpResponse response = httpclient.execute(httpget);

        //            System.out.println("----------------------------------------");
        //            System.out.println(response.getStatusLine());
        //            System.out.println(response.getLastHeader("Content-Encoding"));
        //            System.out.println(response.getLastHeader("Content-Length"));
        //            System.out.println("----------------------------------------");

        HttpEntity entity = response.getEntity();
        //System.out.println(entity.getContentType());

        if (entity != null) {
            content = EntityUtils.toString(entity);
            //                System.out.println(content);
            //                System.out.println("----------------------------------------");
            //                System.out.println("Uncompressed size: "+content.length());
        }

    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }

    return content;
}

From source file:com.ibm.sbt.services.endpoints.AbstractEndpoint.java

@Override
public CookieStore getCookies() {
    return enableCookies ? this.cookieStore : new BasicCookieStore();
}

From source file:com.hp.mqm.client.AbstractMqmRestClient.java

/**
 * Invokes {@link org.apache.http.client.HttpClient#execute(org.apache.http.client.methods.HttpUriRequest)}
 * with given request and it does login if it is necessary.
 *
 * Method does not support request with non-repeatable entity (see {@link HttpEntity#isRepeatable()}).
 *
 * @param request which should be executed
 * @return response for given request/*  w  ww  .j  a v a2 s  . co m*/
 * @throws IllegalArgumentException when request entity is not repeatable
 */
protected HttpResponse execute(HttpUriRequest request) throws IOException {
    HttpResponse response;

    if (LWSSO_TOKEN == null) {
        login();
    }
    HttpContext localContext = new BasicHttpContext();
    CookieStore localCookies = new BasicCookieStore();
    localCookies.addCookie(LWSSO_TOKEN);
    localContext.setAttribute(HttpClientContext.COOKIE_STORE, localCookies);

    addRequestHeaders(request);
    response = httpClient.execute(request, localContext);
    if (response.getStatusLine().getStatusCode() == 401) {
        HttpClientUtils.closeQuietly(response);
        login();
        localCookies.clear();
        localCookies.addCookie(LWSSO_TOKEN);
        localContext.setAttribute(HttpClientContext.COOKIE_STORE, localCookies);
        addRequestHeaders(request);
        response = httpClient.execute(request, localContext);
    }
    return response;
}

From source file:org.apache.gobblin.service.modules.orchestration.AzkabanAjaxAPIClient.java

private static CloseableHttpClient getHttpClient() throws IOException {
    try {/* w w  w .j a v  a  2  s  .  c  om*/
        // Self sign SSL
        SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial(null, (TrustStrategy) new TrustSelfSignedStrategy());
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());

        // Create client
        return HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultCookieStore(new BasicCookieStore())
                .build();
    } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
        throw new IOException("Issue with creating http client", e);
    }
}

From source file:crawlercommons.fetcher.SimpleHttpFetcher.java

private FetchedResult doRequest(HttpRequestBase request, String url, Payload payload)
        throws BaseFetchException {
    LOGGER.trace("Fetching " + url);

    HttpResponse response;//from   w w w .  ja v  a2s . c  om
    long readStartTime;
    Metadata headerMap = new Metadata();
    String redirectedUrl = null;
    String newBaseUrl = null;
    int numRedirects = 0;
    boolean needAbort = true;
    String contentType = "";
    String mimeType = "";
    String hostAddress = null;

    // Create a local instance of cookie store, and bind to local context
    // Without this we get killed w/lots of threads, due to sync() on single cookie store.
    HttpContext localContext = new BasicHttpContext();
    CookieStore cookieStore = new BasicCookieStore();
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

    StringBuilder fetchTrace = null;
    if (LOGGER.isTraceEnabled()) {
        fetchTrace = new StringBuilder("Fetched url: " + url);
    }

    try {
        request.setURI(new URI(url));

        readStartTime = System.currentTimeMillis();
        response = _httpClient.execute(request, localContext);

        Header[] headers = response.getAllHeaders();
        for (Header header : headers) {
            headerMap.add(header.getName(), header.getValue());
        }

        int httpStatus = response.getStatusLine().getStatusCode();
        if (LOGGER.isTraceEnabled()) {
            fetchTrace.append("; status code: " + httpStatus);
            if (headerMap.get(HttpHeaders.CONTENT_LENGTH) != null) {
                fetchTrace.append("; Content-Length: " + headerMap.get(HttpHeaders.CONTENT_LENGTH));
            }

            if (headerMap.get(HttpHeaders.LOCATION) != null) {
                fetchTrace.append("; Location: " + headerMap.get(HttpHeaders.LOCATION));
            }
        }

        if ((httpStatus < 200) || (httpStatus >= 300)) {
            // We can't just check against SC_OK, as some wackos return 201, 202, etc
            throw new HttpFetchException(url, "Error fetching " + url, httpStatus, headerMap);
        }

        redirectedUrl = extractRedirectedUrl(url, localContext);

        URI permRedirectUri = (URI) localContext.getAttribute(PERM_REDIRECT_CONTEXT_KEY);
        if (permRedirectUri != null) {
            newBaseUrl = permRedirectUri.toURL().toExternalForm();
        }

        Integer redirects = (Integer) localContext.getAttribute(REDIRECT_COUNT_CONTEXT_KEY);
        if (redirects != null) {
            numRedirects = redirects.intValue();
        }

        hostAddress = (String) (localContext.getAttribute(HOST_ADDRESS));
        if (hostAddress == null) {
            throw new UrlFetchException(url, "Host address not saved in context");
        }

        Header cth = response.getFirstHeader(HttpHeaders.CONTENT_TYPE);
        if (cth != null) {
            contentType = cth.getValue();
        }

        // Check if we should abort due to mime-type filtering. Note that this will fail if the server
        // doesn't report a mime-type, but that's how we want it as this configuration is typically
        // used when only a subset of parsers are installed/enabled, so we don't want the auto-detect
        // code in Tika to get triggered & try to process an unsupported type. If you want unknown
        // mime-types from the server to be processed, set "" as one of the valid mime-types in FetcherPolicy.
        mimeType = getMimeTypeFromContentType(contentType);
        Set<String> mimeTypes = getValidMimeTypes();
        if ((mimeTypes != null) && (mimeTypes.size() > 0)) {
            if (!mimeTypes.contains(mimeType)) {
                throw new AbortedFetchException(url, "Invalid mime-type: " + mimeType,
                        AbortedFetchReason.INVALID_MIMETYPE);
            }
        }

        needAbort = false;
    } catch (ClientProtocolException e) {
        // Oleg guarantees that no abort is needed in the case of an IOException (which is is a subclass of)
        needAbort = false;

        // If the root case was a "too many redirects" error, we want to map this to a specific
        // exception that contains the final redirect.
        if (e.getCause() instanceof MyRedirectException) {
            MyRedirectException mre = (MyRedirectException) e.getCause();
            String redirectUrl = url;

            try {
                redirectUrl = mre.getUri().toURL().toExternalForm();
            } catch (MalformedURLException e2) {
                LOGGER.warn("Invalid URI saved during redirect handling: " + mre.getUri());
            }

            throw new RedirectFetchException(url, redirectUrl, mre.getReason());
        } else if (e.getCause() instanceof RedirectException) {
            throw new RedirectFetchException(url, extractRedirectedUrl(url, localContext),
                    RedirectExceptionReason.TOO_MANY_REDIRECTS);
        } else {
            throw new IOFetchException(url, e);
        }
    } catch (IOException e) {
        // Oleg guarantees that no abort is needed in the case of an IOException
        needAbort = false;

        if (e instanceof ConnectionPoolTimeoutException) {
            // Should never happen, so let's dump some info about the connection pool.
            ThreadSafeClientConnManager cm = (ThreadSafeClientConnManager) _httpClient.getConnectionManager();
            int numConnections = cm.getConnectionsInPool();
            cm.closeIdleConnections(0, TimeUnit.MILLISECONDS);
            LOGGER.error(String.format(
                    "Got ConnectionPoolTimeoutException: %d connections before, %d after idle close",
                    numConnections, cm.getConnectionsInPool()));
        }

        throw new IOFetchException(url, e);
    } catch (URISyntaxException e) {
        throw new UrlFetchException(url, e.getMessage());
    } catch (IllegalStateException e) {
        throw new UrlFetchException(url, e.getMessage());
    } catch (BaseFetchException e) {
        throw e;
    } catch (Exception e) {
        // Map anything else to a generic IOFetchException
        // TODO KKr - create generic fetch exception
        throw new IOFetchException(url, new IOException(e));
    } finally {
        safeAbort(needAbort, request);
    }

    // Figure out how much data we want to try to fetch.
    int maxContentSize = getMaxContentSize(mimeType);
    int targetLength = maxContentSize;
    boolean truncated = false;
    String contentLengthStr = headerMap.get(HttpHeaders.CONTENT_LENGTH);
    if (contentLengthStr != null) {
        try {
            int contentLength = Integer.parseInt(contentLengthStr);
            if (contentLength > targetLength) {
                truncated = true;
            } else {
                targetLength = contentLength;
            }
        } catch (NumberFormatException e) {
            // Ignore (and log) invalid content length values.
            LOGGER.warn("Invalid content length in header: " + contentLengthStr);
        }
    }

    // Now finally read in response body, up to targetLength bytes.
    // Note that entity might be null, for zero length responses.
    byte[] content = new byte[0];
    long readRate = 0;
    HttpEntity entity = response.getEntity();
    needAbort = true;

    if (entity != null) {
        InputStream in = null;

        try {
            in = entity.getContent();
            byte[] buffer = new byte[BUFFER_SIZE];
            int bytesRead = 0;
            int totalRead = 0;
            ByteArrayOutputStream out = new ByteArrayOutputStream(DEFAULT_BYTEARRAY_SIZE);

            int readRequests = 0;
            int minResponseRate = getMinResponseRate();
            // TODO KKr - we need to monitor the rate while reading a
            // single block. Look at HttpClient
            // metrics support for how to do this. Once we fix this, fix
            // the test to read a smaller (< 20K)
            // chuck of data.
            while ((totalRead < targetLength) && ((bytesRead = in.read(buffer, 0,
                    Math.min(buffer.length, targetLength - totalRead))) != -1)) {
                readRequests += 1;
                totalRead += bytesRead;
                out.write(buffer, 0, bytesRead);

                // Assume read time is at least one millisecond, to avoid DBZ exception.
                long totalReadTime = Math.max(1, System.currentTimeMillis() - readStartTime);
                readRate = (totalRead * 1000L) / totalReadTime;

                // Don't bail on the first read cycle, as we can get a hiccup starting out.
                // Also don't bail if we've read everything we need.
                if ((readRequests > 1) && (totalRead < targetLength) && (readRate < minResponseRate)) {
                    throw new AbortedFetchException(url, "Slow response rate of " + readRate + " bytes/sec",
                            AbortedFetchReason.SLOW_RESPONSE_RATE);
                }

                // Check to see if we got interrupted.
                if (Thread.interrupted()) {
                    throw new AbortedFetchException(url, AbortedFetchReason.INTERRUPTED);
                }
            }

            content = out.toByteArray();
            needAbort = truncated || (in.available() > 0);
        } catch (IOException e) {
            // We don't need to abort if there's an IOException
            throw new IOFetchException(url, e);
        } finally {
            safeAbort(needAbort, request);
            safeClose(in);
        }
    }

    // Toss truncated image content.
    if ((truncated) && (!isTextMimeType(mimeType))) {
        throw new AbortedFetchException(url, "Truncated image", AbortedFetchReason.CONTENT_SIZE);
    }

    // Now see if we need to uncompress the content.
    String contentEncoding = headerMap.get(HttpHeaders.CONTENT_ENCODING);
    if (contentEncoding != null) {
        if (LOGGER.isTraceEnabled()) {
            fetchTrace.append("; Content-Encoding: " + contentEncoding);
        }

        // TODO KKr We might want to just decompress a truncated gzip
        // containing text (since we have a max content size to save us
        // from any gzip corruption). We might want to break the following 
        // out into a separate method, by the way (if not refactor this 
        // entire monolithic method).
        //
        try {
            if ("gzip".equals(contentEncoding) || "x-gzip".equals(contentEncoding)) {
                if (truncated) {
                    throw new AbortedFetchException(url, "Truncated compressed data",
                            AbortedFetchReason.CONTENT_SIZE);
                } else {
                    ExpandedResult expandedResult = EncodingUtils.processGzipEncoded(content, maxContentSize);
                    truncated = expandedResult.isTruncated();
                    if ((truncated) && (!isTextMimeType(mimeType))) {
                        throw new AbortedFetchException(url, "Truncated decompressed image",
                                AbortedFetchReason.CONTENT_SIZE);
                    } else {
                        content = expandedResult.getExpanded();
                        if (LOGGER.isTraceEnabled()) {
                            fetchTrace.append("; unzipped to " + content.length + " bytes");
                        }
                    }
                    //                    } else if ("deflate".equals(contentEncoding)) {
                    //                        content = EncodingUtils.processDeflateEncoded(content);
                    //                        if (LOGGER.isTraceEnabled()) {
                    //                            fetchTrace.append("; inflated to " + content.length + " bytes");
                    //                        }
                }
            }
        } catch (IOException e) {
            throw new IOFetchException(url, e);
        }
    }

    // Finally dump out the trace msg we've been building.
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace(fetchTrace.toString());
    }

    // TODO KKr - Save truncated flag in FetchedResult/FetchedDatum.
    return new FetchedResult(url, redirectedUrl, System.currentTimeMillis(), headerMap, content, contentType,
            (int) readRate, payload, newBaseUrl, numRedirects, hostAddress);
}

From source file:fr.eolya.utils.http.HttpLoader.java

/**
 * @param /* w w w .  j a  v a 2s . co m*/
 * @return
 */
private HttpClient getHttpClient(String url) {
    try {
        // ClientConnectionManager
        SSLSocketFactory sf = new SSLSocketFactory(new TrustStrategy() {
            public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                return true;
            }
        }, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
        registry.register(new Scheme("https", 443, sf));

        ClientConnectionManager ccm = new PoolingClientConnectionManager(registry);

        // Params
        HttpParams httpParams = getHttpParams();

        // DefaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient(ccm, httpParams);

        // Proxy
        setProxy(httpClient, url, proxyHost, proxyPort, proxyExclude, proxyUserName, proxyPassword);

        //         if (StringUtils.isNotEmpty(proxyHost)) {
        //            if (StringUtils.isNotEmpty(proxyUserName) && StringUtils.isNotEmpty(proxyPassword)) {
        //               httpClient.getCredentialsProvider().setCredentials(
        //                      new AuthScope(proxyHost,Integer.valueOf(proxyPort)),
        //                      new UsernamePasswordCredentials(proxyUserName, proxyPassword));
        //            }
        //            HttpHost proxy = new HttpHost(proxyHost,Integer.valueOf(proxyPort));
        //            httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
        //         } else {
        //            httpClient.getParams().removeParameter(ConnRoutePNames.DEFAULT_PROXY);
        //         }

        // Cookies
        if (cookies != null) {
            CookieStore cookieStore = new BasicCookieStore();
            Iterator<Entry<String, String>> it = cookies.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, String> pairs = (Map.Entry<String, String>) it.next();
                BasicClientCookie cookie = new BasicClientCookie(pairs.getKey(), pairs.getValue());
                //cookie.setDomain("your domain");
                cookie.setPath("/");
                cookieStore.addCookie(cookie);
            }
            httpClient.setCookieStore(cookieStore);
        }

        return new DecompressingHttpClient(httpClient);
    } catch (Exception e) {
        return new DecompressingHttpClient(new DefaultHttpClient());
    }
}

From source file:edu.mit.scratch.ScratchUser.java

public List<ScratchProject> getFavoriteProjects(final int limit, final int offset) throws ScratchUserException {
    final List<ScratchProject> ids = new ArrayList<>();

    try {//from  w w w.ja  v  a 2  s.  c  om
        final RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).build();

        final CookieStore cookieStore = new BasicCookieStore();
        final BasicClientCookie lang = new BasicClientCookie("scratchlanguage", "en");
        final BasicClientCookie debug = new BasicClientCookie("DEBUG", "true");
        debug.setDomain(".scratch.mit.edu");
        debug.setPath("/");
        lang.setPath("/");
        lang.setDomain(".scratch.mit.edu");
        cookieStore.addCookie(lang);
        cookieStore.addCookie(debug);

        final CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig)
                .setUserAgent(Scratch.USER_AGENT).setDefaultCookieStore(cookieStore).build();
        CloseableHttpResponse resp;

        final HttpUriRequest update = RequestBuilder.get()
                .setUri("https://api.scratch.mit.edu/users/" + this.getUsername() + "/favorites?limit=" + limit
                        + "&offset=" + offset)
                .addHeader("Accept",
                        "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
                .addHeader("Referer", "https://scratch.mit.edu/users/" + this.getUsername() + "/")
                .addHeader("Origin", "https://scratch.mit.edu")
                .addHeader("Accept-Encoding", "gzip, deflate, sdch")
                .addHeader("Accept-Language", "en-US,en;q=0.8").addHeader("Content-Type", "application/json")
                .addHeader("X-Requested-With", "XMLHttpRequest").build();
        try {
            resp = httpClient.execute(update);
        } catch (final IOException e) {
            e.printStackTrace();
            throw new ScratchUserException();
        }

        BufferedReader rd;
        try {
            rd = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
        } catch (UnsupportedOperationException | IOException e) {
            e.printStackTrace();
            throw new ScratchUserException();
        }

        final StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null)
            result.append(line);
        final JSONArray jsonOBJ2 = new JSONArray(result.toString().trim());

        for (int i = 0; i < jsonOBJ2.length(); i++) {
            final JSONObject jsonOBJ = jsonOBJ2.getJSONObject(i);

            final Iterator<?> keys = jsonOBJ.keys();

            while (keys.hasNext()) {
                final String key = "" + keys.next();
                final Object o = jsonOBJ.get(key);
                final String val = "" + o;

                if (key.equalsIgnoreCase("id"))
                    ids.add(new ScratchProject(Integer.parseInt(val)));
            }
        }

        return ids;
    } catch (final UnsupportedEncodingException e) {
        e.printStackTrace();
        throw new ScratchUserException();
    } catch (final Exception e) {
        e.printStackTrace();
        throw new ScratchUserException();
    }
}