Example usage for java.net HttpCookie getPath

List of usage examples for java.net HttpCookie getPath

Introduction

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

Prototype

public String getPath() 

Source Link

Document

Returns the path on the server to which the browser returns this cookie.

Usage

From source file:org.jwebsocket.util.Tools.java

/**
 * Indicates if a cookie is valid for a given URI
 *
 * @param aURI//from   w  w w .j av  a2 s. c o m
 * @param aCookie
 * @return TRUE if the cookie is valid, FALSE otherwise
 */
public static boolean isCookieValid(URI aURI, HttpCookie aCookie) {
    return !aCookie.hasExpired()
            && (null == aCookie.getDomain() || HttpCookie.domainMatches(aCookie.getDomain(), aURI.getHost()))
            && (null == aCookie.getPath()
                    || (null != aURI.getPath() && aURI.getPath().startsWith(aCookie.getPath())))
            && (aCookie.getSecure() == (aURI.getScheme().equals("wss")));
}

From source file:org.openhab.binding.amazonechocontrol.internal.Connection.java

public String serializeLoginData() {
    Date loginTime = this.loginTime;
    if (refreshToken == null || loginTime == null) {
        return "";
    }/*from w  w w.java  2s.  c om*/
    StringBuilder builder = new StringBuilder();
    builder.append("6\n"); // version
    builder.append(frc);
    builder.append("\n");
    builder.append(serial);
    builder.append("\n");
    builder.append(deviceId);
    builder.append("\n");
    builder.append(refreshToken);
    builder.append("\n");
    builder.append(amazonSite);
    builder.append("\n");
    builder.append(deviceName);
    builder.append("\n");
    builder.append(accountCustomerId);
    builder.append("\n");
    builder.append(loginTime.getTime());
    builder.append("\n");
    List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
    builder.append(cookies.size());
    builder.append("\n");
    for (HttpCookie cookie : cookies) {
        writeValue(builder, cookie.getName());
        writeValue(builder, cookie.getValue());
        writeValue(builder, cookie.getComment());
        writeValue(builder, cookie.getCommentURL());
        writeValue(builder, cookie.getDomain());
        writeValue(builder, cookie.getMaxAge());
        writeValue(builder, cookie.getPath());
        writeValue(builder, cookie.getPortlist());
        writeValue(builder, cookie.getVersion());
        writeValue(builder, cookie.getSecure());
        writeValue(builder, cookie.getDiscard());
    }
    return builder.toString();
}

From source file:org.piwik.ResponseData.java

public List<Cookie> getCookies() {
    List<Cookie> cookies = new ArrayList<Cookie>();

    for (String key : headerData.keySet()) {
        List<String> headerParts = headerData.get(key);

        StringBuilder cookieInfo = new StringBuilder();
        for (String part : headerParts) {
            cookieInfo.append(part);//  w w  w  .  j  a  va  2s. c o m
        }

        if (key == null && cookieInfo.toString().equals("")) {
            LOGGER.debug("No more headers, not proceeding");
            return null;
        }

        if (key == null) {
            LOGGER.debug("The header value contains the server's HTTP version, not proceeding");
        } else if (key.equals("Set-Cookie")) {
            List<HttpCookie> httpCookies = HttpCookie.parse(cookieInfo.toString());
            for (HttpCookie h : httpCookies) {
                Cookie c = new Cookie(h.getName(), h.getValue());
                c.setComment(h.getComment());
                if (h.getDomain() != null) {
                    c.setDomain(h.getDomain());
                }
                c.setMaxAge(Long.valueOf(h.getMaxAge()).intValue());
                c.setPath(h.getPath());
                c.setSecure(h.getSecure());
                c.setVersion(h.getVersion());
                cookies.add(c);
            }
        } else {
            LOGGER.debug("The provided key (" + key + ") with value (" + cookieInfo
                    + ") were not processed because the key is unknown");
        }
    }
    return cookies;
}

From source file:org.zaproxy.zap.extension.httpsessions.HttpSessionsSite.java

/**
 * Process the http response message received after a request.
 * //  w  ww  . j a  v a 2 s. c o m
 * @param message the message
 */
public void processHttpResponseMessage(HttpMessage message) {

    // Get the session tokens for this site
    HttpSessionTokensSet siteTokensSet = extension.getHttpSessionTokensSet(getSite());

    // No tokens for this site, so no processing
    if (siteTokensSet == null) {
        log.debug("No session tokens for: " + this.getSite());
        return;
    }
    // Create an auxiliary map of token values and insert keys for every token
    Map<String, Cookie> tokenValues = new HashMap<>();

    // Get new values that were set for tokens (e.g. using SET-COOKIE headers), if any

    List<HttpCookie> cookiesToSet = message.getResponseHeader()
            .getHttpCookies(message.getRequestHeader().getHostName());
    for (HttpCookie cookie : cookiesToSet) {
        String lcCookieName = cookie.getName();
        if (siteTokensSet.isSessionToken(lcCookieName)) {
            Cookie ck = new Cookie(cookie.getDomain(), lcCookieName, cookie.getValue(), cookie.getPath(),
                    (int) cookie.getMaxAge(), cookie.getSecure());
            tokenValues.put(lcCookieName, ck);
        }
    }

    // Get the cookies present in the request
    List<HttpCookie> requestCookies = message.getRequestHeader().getHttpCookies();

    // XXX When an empty HttpSession is set in the message and the response
    // contains session cookies, the empty HttpSession is reused which
    // causes the number of messages matched to be incorrect.

    // Get the session, based on the request header
    HttpSession session = message.getHttpSession();
    if (session == null || !session.isValid()) {
        session = getMatchingHttpSession(requestCookies, siteTokensSet);
        if (log.isDebugEnabled()) {
            log.debug("Matching session for response message (from site " + getSite() + "): " + session);
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Matching cached session for response message (from site " + getSite() + "): " + session);
        }
    }

    // If the session didn't exist, create it now
    if (session == null) {
        session = new HttpSession(generateUniqueSessionName(), extension.getHttpSessionTokensSet(getSite()));
        this.addHttpSession(session);

        // Add all the existing tokens from the request, if they don't replace one in the
        // response
        for (HttpCookie cookie : requestCookies) {
            String cookieName = cookie.getName();
            if (siteTokensSet.isSessionToken(cookieName)) {
                if (!tokenValues.containsKey(cookieName)) {

                    // We must ensure that a cookie as always a valid domain and path in order to be able to reuse it.
                    // HttpClient will discard invalid cookies

                    String domain = cookie.getDomain();
                    if (domain == null) {
                        domain = message.getRequestHeader().getHostName();
                    }

                    String path = cookie.getPath();
                    if (path == null) {
                        path = "/"; // Default path
                    }

                    Cookie ck = new Cookie(domain, cookieName, cookie.getValue(), path,
                            (int) cookie.getMaxAge(), cookie.getSecure());
                    tokenValues.put(cookieName, ck);
                }
            }
        }
        log.info("Created a new session as no match was found: " + session);
    }

    // Update the session
    if (!tokenValues.isEmpty()) {
        for (Entry<String, Cookie> tv : tokenValues.entrySet()) {
            session.setTokenValue(tv.getKey(), tv.getValue());
        }
    }

    // Update the count of messages matched
    session.setMessagesMatched(session.getMessagesMatched() + 1);

    this.model.fireHttpSessionUpdated(session);

    // Store the session in the HttpMessage for caching purpose
    message.setHttpSession(session);
}

From source file:org.zaproxy.zap.utils.HarUtils.java

public static HarResponse createHarResponse(HttpMessage httpMessage) {
    HttpResponseHeader responseHeader = httpMessage.getResponseHeader();
    HarCookies harCookies = new HarCookies();

    long whenCreated = System.currentTimeMillis();
    for (HttpCookie cookie : responseHeader.getHttpCookies()) {
        Date expires;/*w ww . ja v a2 s  . c  o m*/
        if (cookie.getVersion() == 0) {
            expires = new Date(whenCreated + (cookie.getMaxAge() * 1000));
        } else {
            expires = new Date(httpMessage.getTimeSentMillis() + httpMessage.getTimeElapsedMillis()
                    + (cookie.getMaxAge() * 1000));
        }

        harCookies.addCookie(new HarCookie(cookie.getName(), cookie.getValue(), cookie.getPath(),
                cookie.getDomain(), expires, cookie.isHttpOnly(), cookie.getSecure(), null));
    }

    String text = null;
    String encoding = null;
    String contentType = responseHeader.getHeader(HttpHeader.CONTENT_TYPE);
    if (contentType == null) {
        contentType = "";
    } else if (!contentType.isEmpty()) {
        String lcContentType = contentType.toLowerCase(Locale.ROOT);
        final int pos = lcContentType.indexOf(';');
        if (pos != -1) {
            lcContentType = lcContentType.substring(0, pos).trim();
        }

        if (!lcContentType.startsWith("text")) {
            encoding = "base64";
            text = Base64.encodeBytes(httpMessage.getResponseBody().getBytes());
        } else {
            text = httpMessage.getResponseBody().toString();
        }
    }

    HarContent harContent = new HarContent(httpMessage.getResponseBody().length(), 0, contentType, text,
            encoding, null);

    String redirectUrl = responseHeader.getHeader(HttpHeader.LOCATION);

    return new HarResponse(responseHeader.getStatusCode(), responseHeader.getReasonPhrase(),
            responseHeader.getVersion(), harCookies, createHarHeaders(responseHeader), harContent,
            redirectUrl == null ? "" : redirectUrl, responseHeader.toString().length(),
            httpMessage.getResponseBody().length(), null);
}