Example usage for org.apache.http.client.config CookieSpecs IGNORE_COOKIES

List of usage examples for org.apache.http.client.config CookieSpecs IGNORE_COOKIES

Introduction

In this page you can find the example usage for org.apache.http.client.config CookieSpecs IGNORE_COOKIES.

Prototype

String IGNORE_COOKIES

To view the source code for org.apache.http.client.config CookieSpecs IGNORE_COOKIES.

Click Source Link

Document

The policy that ignores cookies.

Usage

From source file:org.apache.jmeter.protocol.http.control.TestHC4CookieManager.java

@Test
public void testCookiePolicyIgnore() throws Exception {
    man.setCookiePolicy(CookieSpecs.IGNORE_COOKIES);
    man.testStarted(); // ensure policy is picked up
    URL url = new URL("http://order.now/sub1/moo.html");
    man.addCookieFromHeader("test1=moo1;", url);
    man.addCookieFromHeader("test2=moo2;path=/sub1", url);
    man.addCookieFromHeader("test2=moo3;path=/", url);
    assertEquals(0, man.getCookieCount());// Cookies are ignored
    Cookie cc;/*from ww  w  .ja va  2 s.c  om*/
    cc = new Cookie("test1", "moo1", null, "/sub1", false, 0, false, false);
    man.add(cc);
    cc = new Cookie("test2", "moo2", null, "/sub1", false, 0, true, false);
    man.add(cc);
    cc = new Cookie("test3", "moo3", null, "/", false, 0, false, false);
    man.add(cc);
    assertEquals(3, man.getCookieCount());
    assertEquals("/sub1", man.get(0).getPath());
    assertEquals("/sub1", man.get(1).getPath());
    assertEquals("/", man.get(2).getPath());
    String s = man.getCookieHeaderForURL(url);

    // With ignore policy, s is null as no match will occur, 
    // getCookieHeaderForURL will return null 
    assertNull(s);

    HC4CookieHandler cookieHandler = (HC4CookieHandler) man.getCookieHandler();
    List<org.apache.http.cookie.Cookie> c = cookieHandler.getCookiesForUrl(man.getCookies(), url,
            CookieManager.ALLOW_VARIABLE_COOKIES);
    assertNotNull(c); // Make sure variable is used
    // see https://issues.apache.org/jira/browse/HTTPCLIENT-1704
    assertEquals(0, c.size()); // Cookies again ignored
}

From source file:org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.java

private HttpClient setupClient(URL url, SampleResult res) {

    Map<HttpClientKey, HttpClient> mapHttpClientPerHttpClientKey = HTTPCLIENTS_CACHE_PER_THREAD_AND_HTTPCLIENTKEY
            .get();/*from  w  ww  .  j a va  2  s. c  om*/

    final String host = url.getHost();
    String proxyHost = getProxyHost();
    int proxyPort = getProxyPortInt();
    String proxyPass = getProxyPass();
    String proxyUser = getProxyUser();

    // static proxy is the globally define proxy eg command line or properties
    boolean useStaticProxy = isStaticProxy(host);
    // dynamic proxy is the proxy defined for this sampler
    boolean useDynamicProxy = isDynamicProxy(proxyHost, proxyPort);
    boolean useProxy = useStaticProxy || useDynamicProxy;

    // if both dynamic and static are used, the dynamic proxy has priority over static
    if (!useDynamicProxy) {
        proxyHost = PROXY_HOST;
        proxyPort = PROXY_PORT;
        proxyUser = PROXY_USER;
        proxyPass = PROXY_PASS;
    }

    // Lookup key - must agree with all the values used to create the HttpClient.
    HttpClientKey key = new HttpClientKey(url, useProxy, proxyHost, proxyPort, proxyUser, proxyPass);

    HttpClient httpClient = null;
    if (this.testElement.isConcurrentDwn()) {
        httpClient = (HttpClient) JMeterContextService.getContext().getSamplerContext().get(HTTPCLIENT_TOKEN);
    }

    if (httpClient == null) {
        httpClient = mapHttpClientPerHttpClientKey.get(key);
    }

    if (httpClient != null && resetSSLContext
            && HTTPConstants.PROTOCOL_HTTPS.equalsIgnoreCase(url.getProtocol())) {
        ((AbstractHttpClient) httpClient).clearRequestInterceptors();
        ((AbstractHttpClient) httpClient).clearResponseInterceptors();
        httpClient.getConnectionManager().closeIdleConnections(1L, TimeUnit.MICROSECONDS);
        httpClient = null;
        JsseSSLManager sslMgr = (JsseSSLManager) SSLManager.getInstance();
        sslMgr.resetContext();
        resetSSLContext = false;
    }

    if (httpClient == null) { // One-time init for this client

        HttpParams clientParams = new DefaultedHttpParams(new BasicHttpParams(), DEFAULT_HTTP_PARAMS);

        DnsResolver resolver = this.testElement.getDNSResolver();
        if (resolver == null) {
            resolver = SystemDefaultDnsResolver.INSTANCE;
        }
        MeasuringConnectionManager connManager = new MeasuringConnectionManager(createSchemeRegistry(),
                resolver, TIME_TO_LIVE, VALIDITY_AFTER_INACTIVITY_TIMEOUT);

        // Modern browsers use more connections per host than the current httpclient default (2)
        // when using parallel download the httpclient and connection manager are shared by the downloads threads
        // to be realistic JMeter must set an higher value to DefaultMaxPerRoute
        if (this.testElement.isConcurrentDwn()) {
            try {
                int maxConcurrentDownloads = Integer.parseInt(this.testElement.getConcurrentPool());
                connManager.setDefaultMaxPerRoute(
                        Math.max(maxConcurrentDownloads, connManager.getDefaultMaxPerRoute()));
            } catch (NumberFormatException nfe) {
                // no need to log -> will be done by the sampler
            }
        }

        httpClient = new DefaultHttpClient(connManager, clientParams) {
            @Override
            protected HttpRequestRetryHandler createHttpRequestRetryHandler() {
                return new DefaultHttpRequestRetryHandler(RETRY_COUNT, false); // set retry count
            }
        };

        if (IDLE_TIMEOUT > 0) {
            ((AbstractHttpClient) httpClient).setKeepAliveStrategy(IDLE_STRATEGY);
        }
        // see https://issues.apache.org/jira/browse/HTTPCORE-397
        ((AbstractHttpClient) httpClient).setReuseStrategy(DefaultClientConnectionReuseStrategy.INSTANCE);
        ((AbstractHttpClient) httpClient).addResponseInterceptor(RESPONSE_CONTENT_ENCODING);
        ((AbstractHttpClient) httpClient).addResponseInterceptor(METRICS_SAVER); // HACK
        ((AbstractHttpClient) httpClient).addRequestInterceptor(METRICS_RESETTER);

        // Override the default schemes as necessary
        SchemeRegistry schemeRegistry = httpClient.getConnectionManager().getSchemeRegistry();

        if (SLOW_HTTP != null) {
            schemeRegistry.register(SLOW_HTTP);
        }

        // Set up proxy details
        if (useProxy) {

            HttpHost proxy = new HttpHost(proxyHost, proxyPort);
            clientParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

            if (proxyUser.length() > 0) {
                ((AbstractHttpClient) httpClient).getCredentialsProvider().setCredentials(
                        new AuthScope(proxyHost, proxyPort),
                        new NTCredentials(proxyUser, proxyPass, localHost, PROXY_DOMAIN));
            }
        }

        // Bug 52126 - we do our own cookie handling
        clientParams.setParameter(ClientPNames.COOKIE_POLICY, CookieSpecs.IGNORE_COOKIES);

        if (log.isDebugEnabled()) {
            log.debug("Created new HttpClient: @" + System.identityHashCode(httpClient) + " " + key.toString());
        }

        mapHttpClientPerHttpClientKey.put(key, httpClient); // save the agent for next time round
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Reusing the HttpClient: @" + System.identityHashCode(httpClient) + " " + key.toString());
        }
    }

    if (this.testElement.isConcurrentDwn()) {
        JMeterContextService.getContext().getSamplerContext().put(HTTPCLIENT_TOKEN, httpClient);
    }

    // TODO - should this be done when the client is created?
    // If so, then the details need to be added as part of HttpClientKey
    setConnectionAuthorization(httpClient, url, getAuthManager(), key);

    return httpClient;
}

From source file:org.apache.nifi.processors.standard.GetHTTP.java

@Override
public void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory)
        throws ProcessException {
    final ComponentLog logger = getLogger();

    final ProcessSession session = sessionFactory.createSession();
    final FlowFile incomingFlowFile = session.get();
    if (incomingFlowFile != null) {
        session.transfer(incomingFlowFile, REL_SUCCESS);
        logger.warn("found FlowFile {} in input queue; transferring to success",
                new Object[] { incomingFlowFile });
    }/*from   w  w  w. ja v a 2 s. com*/

    // get the URL
    final String url = context.getProperty(URL).evaluateAttributeExpressions().getValue();
    final URI uri;
    String source = url;
    try {
        uri = new URI(url);
        source = uri.getHost();
    } catch (final URISyntaxException swallow) {
        // this won't happen as the url has already been validated
    }

    // get the ssl context service
    final SSLContextService sslContextService = context.getProperty(SSL_CONTEXT_SERVICE)
            .asControllerService(SSLContextService.class);

    // create the connection manager
    final HttpClientConnectionManager conMan;
    if (sslContextService == null) {
        conMan = new BasicHttpClientConnectionManager();
    } else {
        final SSLContext sslContext;
        try {
            sslContext = createSSLContext(sslContextService);
        } catch (final Exception e) {
            throw new ProcessException(e);
        }

        final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);

        // Also include a plain socket factory for regular http connections (especially proxies)
        final Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
                .<ConnectionSocketFactory>create().register("https", sslsf)
                .register("http", PlainConnectionSocketFactory.getSocketFactory()).build();

        conMan = new BasicHttpClientConnectionManager(socketFactoryRegistry);
    }

    try {
        // build the request configuration
        final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
        requestConfigBuilder.setConnectionRequestTimeout(
                context.getProperty(DATA_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue());
        requestConfigBuilder.setConnectTimeout(
                context.getProperty(CONNECTION_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue());
        requestConfigBuilder.setSocketTimeout(
                context.getProperty(DATA_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue());
        requestConfigBuilder.setRedirectsEnabled(context.getProperty(FOLLOW_REDIRECTS).asBoolean());
        switch (context.getProperty(REDIRECT_COOKIE_POLICY).getValue()) {
        case STANDARD_COOKIE_POLICY_STR:
            requestConfigBuilder.setCookieSpec(CookieSpecs.STANDARD);
            break;
        case STRICT_COOKIE_POLICY_STR:
            requestConfigBuilder.setCookieSpec(CookieSpecs.STANDARD_STRICT);
            break;
        case NETSCAPE_COOKIE_POLICY_STR:
            requestConfigBuilder.setCookieSpec(CookieSpecs.NETSCAPE);
            break;
        case IGNORE_COOKIE_POLICY_STR:
            requestConfigBuilder.setCookieSpec(CookieSpecs.IGNORE_COOKIES);
            break;
        case DEFAULT_COOKIE_POLICY_STR:
        default:
            requestConfigBuilder.setCookieSpec(CookieSpecs.DEFAULT);
        }

        // build the http client
        final HttpClientBuilder clientBuilder = HttpClientBuilder.create();
        clientBuilder.setConnectionManager(conMan);

        // include the user agent
        final String userAgent = context.getProperty(USER_AGENT).getValue();
        if (userAgent != null) {
            clientBuilder.setUserAgent(userAgent);
        }

        // set the ssl context if necessary
        if (sslContextService != null) {
            clientBuilder.setSslcontext(sslContextService.createSSLContext(ClientAuth.REQUIRED));
        }

        final String username = context.getProperty(USERNAME).getValue();
        final String password = context.getProperty(PASSWORD).getValue();

        // set the credentials if appropriate
        if (username != null) {
            final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            if (password == null) {
                credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username));
            } else {
                credentialsProvider.setCredentials(AuthScope.ANY,
                        new UsernamePasswordCredentials(username, password));
            }
            clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
        }

        // Set the proxy if specified
        if (context.getProperty(PROXY_HOST).isSet() && context.getProperty(PROXY_PORT).isSet()) {
            final String host = context.getProperty(PROXY_HOST).getValue();
            final int port = context.getProperty(PROXY_PORT).asInteger();
            clientBuilder.setProxy(new HttpHost(host, port));
        }

        // create request
        final HttpGet get = new HttpGet(url);
        get.setConfig(requestConfigBuilder.build());

        final StateMap beforeStateMap;

        try {
            beforeStateMap = context.getStateManager().getState(Scope.LOCAL);
            final String lastModified = beforeStateMap.get(LAST_MODIFIED + ":" + url);
            if (lastModified != null) {
                get.addHeader(HEADER_IF_MODIFIED_SINCE, parseStateValue(lastModified).getValue());
            }

            final String etag = beforeStateMap.get(ETAG + ":" + url);
            if (etag != null) {
                get.addHeader(HEADER_IF_NONE_MATCH, parseStateValue(etag).getValue());
            }
        } catch (final IOException ioe) {
            throw new ProcessException(ioe);
        }

        final String accept = context.getProperty(ACCEPT_CONTENT_TYPE).getValue();
        if (accept != null) {
            get.addHeader(HEADER_ACCEPT, accept);
        }

        // Add dynamic headers

        PropertyValue customHeaderValue;
        for (PropertyDescriptor customProperty : customHeaders) {
            customHeaderValue = context.getProperty(customProperty).evaluateAttributeExpressions();
            if (StringUtils.isNotBlank(customHeaderValue.getValue())) {
                get.addHeader(customProperty.getName(), customHeaderValue.getValue());
            }
        }

        // create the http client
        try (final CloseableHttpClient client = clientBuilder.build()) {
            // NOTE: including this inner try in order to swallow exceptions on close
            try {
                final StopWatch stopWatch = new StopWatch(true);
                final HttpResponse response = client.execute(get);
                final int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == NOT_MODIFIED) {
                    logger.info(
                            "content not retrieved because server returned HTTP Status Code {}: Not Modified",
                            new Object[] { NOT_MODIFIED });
                    context.yield();
                    // doing a commit in case there were flow files in the input queue
                    session.commit();
                    return;
                }
                final String statusExplanation = response.getStatusLine().getReasonPhrase();

                if ((statusCode >= 300) || (statusCode == 204)) {
                    logger.error("received status code {}:{} from {}",
                            new Object[] { statusCode, statusExplanation, url });
                    // doing a commit in case there were flow files in the input queue
                    session.commit();
                    return;
                }

                FlowFile flowFile = session.create();
                flowFile = session.putAttribute(flowFile, CoreAttributes.FILENAME.key(),
                        context.getProperty(FILENAME).evaluateAttributeExpressions().getValue());
                flowFile = session.putAttribute(flowFile,
                        this.getClass().getSimpleName().toLowerCase() + ".remote.source", source);
                flowFile = session.importFrom(response.getEntity().getContent(), flowFile);

                final Header contentTypeHeader = response.getFirstHeader("Content-Type");
                if (contentTypeHeader != null) {
                    final String contentType = contentTypeHeader.getValue();
                    if (!contentType.trim().isEmpty()) {
                        flowFile = session.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(),
                                contentType.trim());
                    }
                }

                final long flowFileSize = flowFile.getSize();
                stopWatch.stop();
                final String dataRate = stopWatch.calculateDataRate(flowFileSize);
                session.getProvenanceReporter().receive(flowFile, url,
                        stopWatch.getDuration(TimeUnit.MILLISECONDS));
                session.transfer(flowFile, REL_SUCCESS);
                logger.info("Successfully received {} from {} at a rate of {}; transferred to success",
                        new Object[] { flowFile, url, dataRate });
                session.commit();

                updateStateMap(context, response, beforeStateMap, url);

            } catch (final IOException e) {
                context.yield();
                session.rollback();
                logger.error("Failed to retrieve file from {} due to {}; rolling back session",
                        new Object[] { url, e.getMessage() }, e);
                throw new ProcessException(e);
            } catch (final Throwable t) {
                context.yield();
                session.rollback();
                logger.error("Failed to process due to {}; rolling back session",
                        new Object[] { t.getMessage() }, t);
                throw t;
            }
        } catch (final IOException e) {
            logger.debug("Error closing client due to {}, continuing.", new Object[] { e.getMessage() });
        }
    } finally {
        conMan.shutdown();
    }
}

From source file:org.apache.zeppelin.submarine.hadoop.YarnClient.java

private static HttpClient buildSpengoHttpClient() {
    HttpClientBuilder builder = HttpClientBuilder.create();
    Lookup<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
            .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true)).build();
    builder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
    BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(null, -1, null), new Credentials() {
        @Override/*from ww  w .j  ava 2 s .  c om*/
        public Principal getUserPrincipal() {
            return null;
        }

        @Override
        public String getPassword() {
            return null;
        }
    });
    builder.setDefaultCredentialsProvider(credentialsProvider);

    // Avoid output WARN: Cookie rejected
    RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
    builder.setDefaultRequestConfig(globalConfig);

    CloseableHttpClient httpClient = builder.build();

    return httpClient;
}