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:org.apache.trafficcontrol.client.RestApiSession.java

public void open() {
    if (httpclient == null) {
        RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD) //User standard instead of default. Default will result in cookie parse exceptions with the Mojolicous cookie
                .setConnectTimeout(5000).build();
        CookieStore cookieStore = new BasicCookieStore();
        HttpClientContext context = HttpClientContext.create();
        context.setCookieStore(cookieStore);

        httpclient = HttpAsyncClients.custom().setDefaultRequestConfig(globalConfig)
                .setDefaultCookieStore(cookieStore).build();
    }//from w w w.jav a 2  s. c  o  m

    if (!httpclient.isRunning()) {
        httpclient.start();
    }
}

From source file:me.vertretungsplan.parser.BaseParser.java

BaseParser(SubstitutionScheduleData scheduleData, CookieProvider cookieProvider) {
    this.scheduleData = scheduleData;
    this.cookieProvider = cookieProvider;
    this.cookieStore = new BasicCookieStore();
    this.colorProvider = new ColorProvider(scheduleData);
    this.encodingDetector = new UniversalDetector(null);

    try {// ww w. j  a  va2s .c om
        KeyStore ks = loadKeyStore();
        MultiTrustManager multiTrustManager = new MultiTrustManager();
        multiTrustManager.addTrustManager(getDefaultTrustManager());
        multiTrustManager.addTrustManager(trustManagerFromKeystore(ks));

        TrustManager[] trustManagers = new TrustManager[] { multiTrustManager };
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustManagers, null);
        final HostnameVerifier hostnameVerifier;

        if (scheduleData.getData() != null && scheduleData.getData().has(PARAM_SSL_HOSTNAME)) {
            hostnameVerifier = new CustomHostnameVerifier(scheduleData.getData().getString(PARAM_SSL_HOSTNAME));
        } else {
            hostnameVerifier = new DefaultHostnameVerifier();
        }
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
                new String[] { "TLSv1", "TLSv1.1", "TLSv1.2" }, null, hostnameVerifier);

        CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf)
                .setRedirectStrategy(new LaxRedirectStrategy())
                .setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build())
                .build();
        this.executor = Executor.newInstance(httpclient).use(cookieStore);
    } catch (GeneralSecurityException | JSONException | IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.phonty.improved.Sms.java

public Sms(String url, Context _context) {
    context = _context;//  w ww.  j a  v  a  2s  . c  o m
    APIURL = url;
    httppost = new HttpPost(APIURL);
    httppost.addHeader("Content-Type", "application/json; charset=\"utf-8\"");

    BasicHttpParams params = new BasicHttpParams();
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
    schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
    client = new PhontyHttpClient(cm, params, context);
    client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Phonty-Android-Client");
    CookieStore cookieStore = new BasicCookieStore();
    cookieStore.addCookie(Login.SESSION_COOKIE);
    client.setCookieStore(cookieStore);
}

From source file:com.cognifide.qa.bb.aem.core.login.AemAuthCookieFactoryImpl.java

/**
 * This method provides browser cookie for authenticating user to AEM instance
 *
 * @param url      URL to AEM instance, like http://localhost:4502
 * @param login    Username to use/*from   w w  w. j  a v  a  2  s  .  co m*/
 * @param password Password to use
 * @return Cookie for selenium WebDriver.
 */
@Override
public Cookie getCookie(String url, String login, String password) {
    if (!cookieJar.containsKey(url)) {
        HttpPost loginPost = new HttpPost(url + "/libs/granite/core/content/login.html/j_security_check");

        List<NameValuePair> nameValuePairs = new ArrayList<>();
        nameValuePairs.add(new BasicNameValuePair("_charset_", "utf-8"));
        nameValuePairs.add(new BasicNameValuePair("j_username", login));
        nameValuePairs.add(new BasicNameValuePair("j_password", password));
        nameValuePairs.add(new BasicNameValuePair("j_validate", "true"));

        CookieStore cookieStore = new BasicCookieStore();
        HttpClientContext context = HttpClientContext.create();
        context.setCookieStore(cookieStore);

        try {
            loginPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            CloseableHttpResponse loginResponse = httpClient.execute(loginPost, context);
            loginResponse.close();
        } catch (IOException e) {
            LOG.error("Can't get AEM authentication cookie", e);
        } finally {
            loginPost.reset();
        }
        Cookie cookie = findAuthenticationCookie(cookieStore.getCookies());
        if (cookie != null) {
            cookieJar.put(url, cookie);
        }
    }
    return cookieJar.get(url);
}

From source file:com.janoz.usenet.processors.impl.WebbasedProcessor.java

public WebbasedProcessor() {
    BasicHttpParams params = new BasicHttpParams();
    params.setParameter(CookieSpecPNames.DATE_PATTERNS,
            Arrays.asList("EEE, dd-MMM-yyyy HH:mm:ss z", "EEE, dd MMM yyyy HH:mm:ss z"));
    httpClient = new DefaultHttpClient(params);
    cookieStore = new BasicCookieStore();
    context = new BasicHttpContext();
    context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
}

From source file:org.wso2.appserver.integration.tests.webapp.spring.SpringScopeTestCase.java

@Test(groups = "wso2.as", description = "Verfiy Spring Request scope")
public void testSpringRequestScope() throws Exception {

    String endpoint = webAppURL + "/" + webAppMode.getWebAppName() + "/scope/request";
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    CookieStore cookieStore = new BasicCookieStore();
    HttpContext httpContext = new BasicHttpContext();
    httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);

    HttpGet httpget = new HttpGet(endpoint);
    HttpResponse response1 = httpClient.execute(httpget, httpContext);
    String responseMsg1 = new BasicResponseHandler().handleResponse(response1);
    HttpResponse response2 = httpClient.execute(httpget, httpContext);
    String responseMsg2 = new BasicResponseHandler().handleResponse(response2);
    httpClient.close();/*from ww w.j  a v a  2 s .c o  m*/

    assertTrue(!responseMsg1.equalsIgnoreCase(responseMsg2), "Failed: Responses should not be the same");
}

From source file:com.phonty.improved.Contacts.java

public Contacts(String url, Context _context) {
    context = _context;/*from w  w  w .ja  v a  2 s . c om*/
    APIURL = url;
    httppost = new HttpPost(APIURL);
    httppost.addHeader("Content-Type", "application/json; charset=\"utf-8\"");

    BasicHttpParams params = new BasicHttpParams();
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
    schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
    client = new PhontyHttpClient(cm, params, context);
    client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Phonty-Android-Client");
    CookieStore cookieStore = new BasicCookieStore();
    cookieStore.addCookie(Login.SESSION_COOKIE);
    client.setCookieStore(cookieStore);
}

From source file:neembuu.release1.httpclient.NHttpClient.java

public static DefaultHttpClient getNewInstance() {
    DefaultHttpClient new_httpClient = null;
    new_httpClient = new DefaultHttpClient();
    GlobalTestSettings.ProxySettings proxySettings = GlobalTestSettings.getGlobalProxySettings();
    HttpContext context = new BasicHttpContext();
    SchemeRegistry schemeRegistry = new SchemeRegistry();

    schemeRegistry.register(new Scheme("http", new PlainSocketFactory(), 80));

    try {/*  w w  w  . j  a  v  a 2  s  .  c  o  m*/
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        schemeRegistry.register(new Scheme("https", new SSLSocketFactory(keyStore), 8080));
    } catch (Exception a) {
        a.printStackTrace(System.err);
    }

    context.setAttribute(ClientContext.SCHEME_REGISTRY, schemeRegistry);
    context.setAttribute(ClientContext.AUTHSCHEME_REGISTRY,
            new BasicScheme()/*file.httpClient.getAuthSchemes()*/);

    context.setAttribute(ClientContext.COOKIESPEC_REGISTRY,
            new_httpClient.getCookieSpecs()/*file.httpClient.getCookieSpecs()*/
    );

    BasicCookieStore basicCookieStore = new BasicCookieStore();

    context.setAttribute(ClientContext.COOKIE_STORE, basicCookieStore/*file.httpClient.getCookieStore()*/);
    context.setAttribute(ClientContext.CREDS_PROVIDER,
            new BasicCredentialsProvider()/*file.httpClient.getCredentialsProvider()*/);

    HttpConnection hc = new DefaultHttpClientConnection();
    context.setAttribute(ExecutionContext.HTTP_CONNECTION, hc);

    //System.out.println(file.httpClient.getParams().getParameter("http.useragent"));
    HttpParams httpParams = new BasicHttpParams();

    if (proxySettings != null) {
        AuthState as = new AuthState();
        as.setCredentials(new UsernamePasswordCredentials(proxySettings.userName, proxySettings.password));
        as.setAuthScope(AuthScope.ANY);
        as.setAuthScheme(new BasicScheme());
        httpParams.setParameter(ClientContext.PROXY_AUTH_STATE, as);
        httpParams.setParameter("http.proxy_host", new HttpHost(proxySettings.host, proxySettings.port));
    }

    new_httpClient = new DefaultHttpClient(
            new SingleClientConnManager(httpParams/*file.httpClient.getParams()*/, schemeRegistry),
            httpParams/*file.httpClient.getParams()*/);

    if (proxySettings != null) {
        new_httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(proxySettings.userName, proxySettings.password));
    }

    return new_httpClient;
}

From source file:com.teslagov.joan.example.ArcPortalApiTest.java

@Before
public void setup() {
    //Setup our ArcConfiguration and Api
    Properties properties = ArcPropertiesFactory.createArcProperties();

    arcConfiguration = arcConfig().arcPortalConfiguration(portalConfig()
            .portalAdminUsername(properties.getString(ArcProperties.PORTAL_ADMIN_USERNAME))
            .portalAdminPassword(properties.getString(ArcProperties.PORTAL_ADMIN_PASSWORD))
            .portalUrl(properties.getString(ArcProperties.PORTAL_URL))
            .portalPort(properties.getInteger(ArcProperties.PORTAL_PORT))
            .portalContextPath(properties.getString(ArcProperties.PORTAL_CONTEXT_PATH))
            .portalIsUsingWebAdaptor(properties.getBoolean(ArcProperties.PORTAL_IS_USING_WEB_ADAPTOR)).build())
            .build();/*from w w  w .j  av  a  2 s  .co  m*/

    ArcPortalConfiguration arcPortalConfiguration = arcConfiguration.getArcPortalConfiguration();

    cookieStore = new BasicCookieStore();

    httpClient = TrustingHttpClientFactory.createVeryUnsafePortalHttpClient(arcConfiguration, cookieStore);

    arcPortalApi = new ArcPortalApi(httpClient, arcPortalConfiguration, ZoneOffset.UTC, new TokenManager(
            new TokenRefresher(new PortalTokenFetcher(httpClient, arcPortalConfiguration), ZoneOffset.UTC)));
}

From source file:org.mobicents.client.slee.resource.http.HttpClientResourceAdaptorSbbInterfaceImpl.java

@Override
public HttpClientActivity createHttpClientActivity(boolean endOnReceivingResponse, HttpContext context)
        throws StartActivityException {

    if (tracer.isFinestEnabled()) {
        tracer.finest("createHttpClientActivity(endOnReceivingResponse=" + endOnReceivingResponse + ",context="
                + context + ")");
    }//w  w w .java2 s .c o m

    if (!this.ra.isActive) {
        throw new IllegalStateException("ra is not in active state");
    }

    // Maintain HttpSession on server side
    if (context == null) {
        context = new BasicHttpContext();

    }
    if (context.getAttribute(ClientContext.COOKIE_STORE) == null) {
        BasicCookieStore cookieStore = new BasicCookieStore();
        context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    }

    HttpClientActivity activity = new HttpClientActivityImpl(this.ra, endOnReceivingResponse, context);

    HttpClientActivityHandle handle = new HttpClientActivityHandle(activity.getSessionId());

    // this happens with a tx context
    this.ra.getResourceAdaptorContext().getSleeEndpoint().startActivitySuspended(handle, activity,
            ACTIVITY_FLAGS);

    this.ra.addActivity(handle, activity);

    if (tracer.isFineEnabled()) {
        tracer.fine("Started activity " + activity.getSessionId() + ", context is " + context
                + ", endOnReceivingResponse is " + endOnReceivingResponse);
    }

    return activity;
}