Example usage for java.net HttpCookie setPath

List of usage examples for java.net HttpCookie setPath

Introduction

In this page you can find the example usage for java.net HttpCookie setPath.

Prototype

public void setPath(String uri) 

Source Link

Document

Specifies a path for the cookie to which the client should return the cookie.

Usage

From source file:keywhiz.cli.JsonCookie.java

public static HttpCookie toHttpCookie(JsonCookie cookieContents) {
    HttpCookie cookie = new HttpCookie(cookieContents.name(), cookieContents.value());
    cookie.setDomain(cookieContents.domain());
    cookie.setPath(cookieContents.path());
    cookie.setSecure(cookieContents.isSecure());
    cookie.setHttpOnly(cookieContents.isHttpOnly());
    cookie.setVersion(1); // Always set version to 1 or important fields will be dropped
    return cookie;
}

From source file:com.github.parisoft.resty.utils.CookieUtils.java

public static String toString(Cookie... cookies) {
    if (isEmpty(cookies)) {
        return null;
    }/*from w  ww  .  ja v a  2s  .co  m*/

    final HttpCookie[] httpCookies = new HttpCookie[cookies.length];

    for (int i = 0; i < cookies.length; i++) {
        final Cookie srcCookie = cookies[i];
        final HttpCookie httpCookie = new HttpCookie(srcCookie.getName(), srcCookie.getValue());
        httpCookie.setDomain(srcCookie.getDomain());
        httpCookie.setPath(srcCookie.getPath());
        httpCookie.setVersion(srcCookie.getVersion());

        httpCookies[i] = httpCookie;
    }

    return toString(httpCookies);
}

From source file:Main.java

@Deprecated
// Deprecated because this uses org.apache.http, which is itself deprecated
public static HttpCookie servletCookieFromApacheCookie(org.apache.http.cookie.Cookie apacheCookie) {
    if (apacheCookie == null) {
        return null;
    }/* w  w  w . j av  a  2 s .co m*/

    String name = apacheCookie.getName();
    String value = apacheCookie.getValue();

    HttpCookie cookie = new HttpCookie(name, value);

    value = apacheCookie.getDomain();
    if (value != null) {
        cookie.setDomain(value);
    }
    value = apacheCookie.getPath();
    if (value != null) {
        cookie.setPath(value);
    }
    cookie.setSecure(apacheCookie.isSecure());

    value = apacheCookie.getComment();
    if (value != null) {
        cookie.setComment(value);
    }

    // version
    cookie.setVersion(apacheCookie.getVersion());

    // From the Apache source code, maxAge is converted to expiry date using the following formula
    // if (maxAge >= 0) {
    //     setExpiryDate(new Date(System.currentTimeMillis() + maxAge * 1000L));
    // }
    // Reverse this to get the actual max age

    Date expiryDate = apacheCookie.getExpiryDate();
    if (expiryDate != null) {
        long maxAge = (expiryDate.getTime() - System.currentTimeMillis()) / 1000;
        // we have to lower down, no other option
        cookie.setMaxAge((int) maxAge);
    }

    // return the servlet cookie
    return cookie;
}

From source file:com.github.parisoft.resty.utils.CookieUtils.java

public static String toString(org.apache.http.cookie.Cookie... cookies) {
    if (isEmpty(cookies)) {
        return null;
    }/* ww w  .  j a  v a  2  s  .  c o  m*/

    final HttpCookie[] httpCookies = new HttpCookie[cookies.length];

    for (int i = 0; i < cookies.length; i++) {
        final org.apache.http.cookie.Cookie srcCookie = cookies[i];
        final HttpCookie httpCookie = new HttpCookie(srcCookie.getName(), srcCookie.getValue());
        httpCookie.setComment(srcCookie.getComment());
        httpCookie.setDomain(srcCookie.getDomain());
        httpCookie.setPath(srcCookie.getPath());
        httpCookie.setSecure(srcCookie.isSecure());
        httpCookie.setVersion(srcCookie.getVersion());

        final Date now = new Date();

        if (srcCookie.isExpired(now)) {
            httpCookie.setMaxAge(0);
        } else {
            httpCookie.setMaxAge(
                    TimeUnit.MILLISECONDS.toSeconds(srcCookie.getExpiryDate().getTime() - now.getTime()));
        }

        httpCookies[i] = httpCookie;
    }

    return toString(httpCookies);
}

From source file:interactivespaces.service.web.server.internal.netty.NettyHttpRequest.java

public static HttpCookie convertFromNettyCookie(Cookie cookie) {
    HttpCookie httpCookie = new HttpCookie(cookie.getName(), cookie.getValue());
    httpCookie.setComment(cookie.getComment());
    httpCookie.setDomain(cookie.getDomain());
    httpCookie.setMaxAge(cookie.getMaxAge());
    httpCookie.setPath(cookie.getPath());
    httpCookie.setPortlist(createPortString(cookie.getPorts()));
    httpCookie.setVersion(cookie.getVersion());
    httpCookie.setSecure(cookie.isSecure());
    httpCookie.setDiscard(cookie.isDiscard());

    return httpCookie;
}

From source file:cn.ttyhuo.common.MyApplication.java

public static void getJavaCookieStore(java.net.CookieStore jCookieStore) {
    if (cookieStore == null)
        return;/*w ww  . j a  v  a 2  s .  c  om*/

    for (Cookie h : cookieStore.getCookies()) {
        HttpCookie newCookie = new HttpCookie(h.getName(), h.getValue());
        newCookie.setVersion(h.getVersion());
        newCookie.setDomain(h.getDomain());
        newCookie.setPath(h.getPath());
        newCookie.setSecure(h.isSecure());
        newCookie.setComment(h.getComment());
        jCookieStore.add(URI.create("http://" + h.getDomain()), newCookie);
    }
}

From source file:in.rab.ordboken.CookieSerializer.java

public void loadFromString(String json) {
    try {/*from  ww w.ja v a2  s .  com*/
        JSONObject obj = new JSONObject(json);
        JSONArray cookieStrings = obj.getJSONArray("cookies");
        URI uri = new URI("http://" + mDomain);

        for (int i = 0; i < cookieStrings.length(); i++) {
            String cookieString = cookieStrings.getString(i);
            HttpCookie cookie = HttpCookie.parse(cookieString).get(0);

            cookie.setPath("/");
            cookie.setDomain(mDomain);

            mCookieStore.add(uri, cookie);
        }
    } catch (Exception e) {
        // No big deal if this fails.
    }
}

From source file:org.exoplatform.utils.image.CookieAwarePicassoDownloader.java

/**
 * Syncs all cookies from ExoConnectionUtils cookieStore from Apache's
 * HttpClient to HttpURLConnection./*  w  w  w  . j a va  2 s  . co  m*/
 * 
 * @param manager the CookieManager in which to store the retrieved cookies
 */
private void syncCookies(CookieManager manager) {
    CookieStore store = ExoConnectionUtils.cookiesStore;
    if (store == null)
        return;

    for (Cookie cookie : store.getCookies()) {
        HttpCookie c = new HttpCookie(cookie.getName(), cookie.getValue());
        c.setDomain(cookie.getDomain());
        c.setPath(cookie.getPath());
        c.setVersion(cookie.getVersion());
        String url = AccountSetting.getInstance().getDomainName() + "/" + cookie.getPath();
        try {
            manager.getCookieStore().add(new URI(url), c);
        } catch (URISyntaxException e) {
            Log.e(TAG, e.getMessage(), e);
        }
    }
}

From source file:at.becast.youploader.youtube.data.Cookie.java

public HttpCookie getCookie() {
    final HttpCookie cookie = new HttpCookie(name, value);
    cookie.setComment(comment);//from   w  w  w.j a  v  a 2 s.  c  om
    cookie.setCommentURL(commentUrl);
    cookie.setDiscard(discard);
    cookie.setDomain(domain);
    cookie.setPath(path);
    cookie.setPortlist(portList);
    cookie.setMaxAge(maxAge);
    cookie.setSecure(secure);
    cookie.setVersion(version);
    return cookie;
}

From source file:com.muk.services.security.DefaultUaaLoginService.java

@SuppressWarnings("unchecked")
@Override/*  w  w  w  .ja  v a  2 s .  com*/
public Map<String, Object> loginForClient(String username, String password, String clientId,
        UriComponents inUrlComponents) {
    final Map<String, Object> responsePayload = new HashMap<String, Object>();

    final HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON_UTF8));

    final UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(cfgService.getOauthServer());

    // login for csrf
    final UriComponents loginUri = uriBuilder.cloneBuilder().pathSegment("login").build();

    ResponseEntity<String> response = exchangeForType(loginUri.toUriString(), HttpMethod.GET, null, headers,
            String.class);

    final List<String> cookies = new ArrayList<String>();
    cookies.addAll(response.getHeaders().get(HttpHeaders.SET_COOKIE));

    final MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
    formData.add("username", username);
    formData.add("password", password);
    formData.add(CSRF, getCsrf(cookies));

    headers.put(HttpHeaders.COOKIE, translateInToOutCookies(cookies));
    headers.add(HttpHeaders.REFERER, loginUri.toUriString());

    // login.do
    response = exchangeForType(uriBuilder.cloneBuilder().pathSegment("login.do").build().toUriString(),
            HttpMethod.POST, formData, headers, String.class);

    if (response.getStatusCode() != HttpStatus.FOUND
            || response.getHeaders().getFirst(HttpHeaders.LOCATION).contains("login")) {
        responsePayload.put("error", "bad credentials");
        return responsePayload;
    }

    removeCookie(cookies, "X-Uaa-Csrf");
    cookies.addAll(response.getHeaders().get(HttpHeaders.SET_COOKIE));
    removeExpiredCookies(cookies);
    headers.remove(HttpHeaders.REFERER);
    headers.put(HttpHeaders.COOKIE, translateInToOutCookies(cookies));

    // authorize
    final ResponseEntity<JsonNode> authResponse = exchangeForType(
            uriBuilder.cloneBuilder().pathSegment("oauth").pathSegment("authorize")
                    .queryParam("response_type", "code").queryParam("client_id", clientId)
                    .queryParam("redirect_uri", inUrlComponents.toUriString()).build().toUriString(),
            HttpMethod.GET, null, headers, JsonNode.class);

    if (authResponse.getStatusCode() == HttpStatus.OK) {
        removeCookie(cookies, "X-Uaa-Csrf");
        cookies.addAll(authResponse.getHeaders().get(HttpHeaders.SET_COOKIE));
        // return approval data
        final List<HttpCookie> parsedCookies = new ArrayList<HttpCookie>();

        for (final String cookie : cookies) {
            parsedCookies.add(HttpCookie.parse(cookie).get(0));
        }

        responsePayload.put(HttpHeaders.SET_COOKIE, new ArrayList<String>());

        for (final HttpCookie parsedCookie : parsedCookies) {
            if (!parsedCookie.getName().startsWith("Saved-Account")) {
                parsedCookie.setPath(inUrlComponents.getPath());
                ((List<String>) responsePayload.get(HttpHeaders.SET_COOKIE))
                        .add(httpCookieToString(parsedCookie));
            }
        }

        responsePayload.put("json", authResponse.getBody());
    } else {
        // get auth_code from Location Header
        responsePayload.put("code", authResponse.getHeaders().getLocation().getQuery().split("=")[1]);
    }

    return responsePayload;
}