Example usage for org.apache.http.impl.cookie BasicClientCookie getPath

List of usage examples for org.apache.http.impl.cookie BasicClientCookie getPath

Introduction

In this page you can find the example usage for org.apache.http.impl.cookie BasicClientCookie getPath.

Prototype

public String getPath() 

Source Link

Document

Returns the path attribute of the cookie

Usage

From source file:ti.modules.titanium.network.NetworkModule.java

/**
 * Adds a cookie to the system cookie store. Any existing cookie with the same domain, path and name will be replaced with
 * the new cookie. The cookie being set must not have expired, otherwise it will be ignored.
 * @param cookieProxy the cookie to add//from  w  w  w  . j  ava2 s . c o  m
 */
@Kroll.method
public void addSystemCookie(CookieProxy cookieProxy) {
    BasicClientCookie cookie = cookieProxy.getHTTPCookie();
    String cookieString = cookie.getName() + "=" + cookie.getValue();
    String domain = cookie.getDomain();
    if (domain == null) {
        Log.w(TAG, "Unable to add system cookie. Need to provide domain.");
        return;
    }
    cookieString += "; domain=" + domain;

    String path = cookie.getPath();
    Date expiryDate = cookie.getExpiryDate();
    boolean secure = cookie.isSecure();
    boolean httponly = TiConvert.toBoolean(cookieProxy.getProperty(TiC.PROPERTY_HTTP_ONLY), false);
    if (path != null) {
        cookieString += "; path=" + path;
    }
    if (expiryDate != null) {
        cookieString += "; expires=" + CookieProxy.systemExpiryDateFormatter.format(expiryDate);
    }
    if (secure) {
        cookieString += "; secure";
    }
    if (httponly) {
        cookieString += " httponly";
    }
    CookieSyncManager.createInstance(TiApplication.getInstance().getRootOrCurrentActivity());
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setCookie(domain, cookieString);
    CookieSyncManager.getInstance().sync();
}

From source file:br.com.autonomiccs.apacheCloudStack.client.ApacheCloudStackClientTest.java

@Test
public void createCookieForHeaderElementTest() {
    String cookiePath = "/client/api";

    String paramName = "paramName1";
    String paramValue = "paramVale1";
    NameValuePair[] parameters = new NameValuePair[1];
    parameters[0] = new BasicNameValuePair(paramName, paramValue);

    String headerName = "headerElementName";
    String headerValue = "headerElementValue";
    HeaderElement headerElement = new BasicHeaderElement(headerName, headerValue, parameters);

    Mockito.doNothing().when(apacheCloudStackClient)
            .configureDomainForCookie(Mockito.any(BasicClientCookie.class));

    BasicClientCookie cookieForHeaderElement = apacheCloudStackClient
            .createCookieForHeaderElement(headerElement);

    Assert.assertNotNull(cookieForHeaderElement);
    Assert.assertEquals(headerName, cookieForHeaderElement.getName());
    Assert.assertEquals(headerValue, cookieForHeaderElement.getValue());
    Assert.assertEquals(paramValue, cookieForHeaderElement.getAttribute(paramName));
    Assert.assertEquals(cookiePath, cookieForHeaderElement.getPath());

    Mockito.verify(apacheCloudStackClient).configureDomainForCookie(Mockito.eq(cookieForHeaderElement));
}

From source file:nl.esciencecenter.ptk.web.WebClient.java

protected void initJSession(boolean deletePrevious) throws WebException {
    logger.debugPrintf("initJSession(). Using JESSION URI init string:%s\n", config.jsessionInitPart);

    if (deletePrevious) {
        this.jsessionID = null;
    }/*from   ww w  .  j a  va 2s . c  o  m*/

    String uri = null;

    // re-use JSESSIONID:
    if (this.jsessionID != null) {

        BasicClientCookie cookie = new BasicClientCookie(WebConst.COOKIE_JSESSIONID, jsessionID);
        cookie.setPath(config.servicePath);
        cookie.setDomain(config.serverHostname);
        logger.infoPrintf(" - Using JSessionID   = %s\n", jsessionID);
        logger.debugPrintf(" - Cookie Domain/Path = %s/%s\n", cookie.getDomain(), cookie.getPath());

        this.httpClient.getCookieStore().addCookie(cookie);

        return;
    } else {
        logger.debugPrintf("initJSession():NO JSESSIONID\n");
    }

    try {
        uri = getServerURI().toString();

        // put slash between parts:
        if ((uri.endsWith("/") == false) && (config.servicePath.startsWith("/") == false)) {
            uri = uri + "/";
        }

        uri = uri + config.servicePath + "/" + config.jsessionInitPart;

        HttpPost postMethod = new HttpPost(uri);
        // get.setFollowRedirects(true);

        int result = executeAuthenticatedPut(postMethod, null, null);

        List<Cookie> cookies = this.httpClient.getCookieStore().getCookies();

        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(WebConst.COOKIE_JSESSIONID)) {
                this.jsessionID = cookie.getValue();
                logger.infoPrintf(" - new JSessionID     = %s\n", jsessionID);
                logger.debugPrintf(" - Cookie Domain/Path = '%s','%s'\n", cookie.getDomain(), cookie.getPath());
            }
        }

        checkHttpStatus(result, "initJSession(): Couldn't initialize JSessionID.", null, null);

        // all ok here.
    } catch (WebException e) {
        Reason reason = e.getReason();

        if (reason == Reason.FORBIDDEN) {
            throw new WebException(reason, "Failed to authenticate: Forbidden.\n" + e.getMessage(), e);
        } else if (reason == Reason.UNAUTHORIZED) {
            if (this.config.useAuthentication() == false) {
                throw new WebException(reason,
                        "Need proper authentication for this service, but authentication is disabled for:"
                                + this.getServiceURI() + ".\n" + e.getMessage(),
                        e);
            }

            throw new WebException(reason, "Failed to authenticate: User or password wrong for URI:"
                    + this.getServiceURI() + ".\n" + e.getMessage(), e);
        } else {
            throw new WebException(reason,
                    "Failed to initialize JSession to: " + this.getServiceURI() + "\n" + e.getMessage(), e);
        }
    }
}