Example usage for javax.servlet.http Cookie getPath

List of usage examples for javax.servlet.http Cookie getPath

Introduction

In this page you can find the example usage for javax.servlet.http Cookie 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.jasig.portal.portlet.container.services.SessionOnlyPortletCookieImpl.java

SessionOnlyPortletCookieImpl(Cookie cookie) {
    this.name = cookie.getName();
    this.value = cookie.getValue();
    this.comment = cookie.getComment();
    this.domain = cookie.getDomain();
    this.path = cookie.getPath();
    this.version = cookie.getVersion();
    this.secure = cookie.getSecure();

    setMaxAge(cookie.getMaxAge());/* w  w w  .j  a  v a  2s  .c om*/
}

From source file:org.jasig.portal.portlet.container.services.SessionOnlyPortletCookieImpl.java

@Override
public void updateFromCookie(Cookie cookie) {
    this.setComment(cookie.getComment());
    this.setDomain(cookie.getDomain());
    this.setExpires(DateUtils.addSeconds(new Date(), cookie.getMaxAge()));
    this.setPath(cookie.getPath());
    this.setSecure(cookie.getSecure());
    this.setValue(cookie.getValue());
}

From source file:org.jboss.web.loadbalancer.Loadbalancer.java

protected HttpClient prepareServerRequest(HttpServletRequest request, HttpServletResponse response,
        HttpMethod method) {// w  ww .ja v a  2s  . c  om
    // clear state
    HttpClient client = new HttpClient(connectionManager);
    client.setStrictMode(false);
    client.setTimeout(connectionTimeout);
    method.setFollowRedirects(false);
    method.setDoAuthentication(false);
    client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);

    Enumeration reqHeaders = request.getHeaderNames();

    while (reqHeaders.hasMoreElements()) {
        String headerName = (String) reqHeaders.nextElement();
        String headerValue = request.getHeader(headerName);

        if (!ignorableHeader.contains(headerName.toLowerCase())) {
            method.setRequestHeader(headerName, headerValue);
        }
    }

    //Cookies
    Cookie[] cookies = request.getCookies();
    HttpState state = client.getState();

    for (int i = 0; cookies != null && i < cookies.length; ++i) {
        Cookie cookie = cookies[i];

        org.apache.commons.httpclient.Cookie reqCookie = new org.apache.commons.httpclient.Cookie();

        reqCookie.setName(cookie.getName());
        reqCookie.setValue(cookie.getValue());

        if (cookie.getPath() != null) {
            reqCookie.setPath(cookie.getPath());
        } else {
            reqCookie.setPath("/");
        }

        reqCookie.setSecure(cookie.getSecure());

        reqCookie.setDomain(method.getHostConfiguration().getHost());
        state.addCookie(reqCookie);
    }
    return client;
}

From source file:org.mule.transport.http.servlet.MuleHttpServletResponse.java

private org.apache.commons.httpclient.Cookie toHttpClientCookie(Cookie cookie) {
    org.apache.commons.httpclient.Cookie internal = new org.apache.commons.httpclient.Cookie();

    internal.setName(cookie.getName());//from  w w w  . j  av  a 2s.c  o  m
    internal.setValue(cookie.getValue());
    internal.setComment(cookie.getComment());
    internal.setDomain(cookie.getDomain());
    //        internal.setExpiryDate(toExpiry(cookie.getMaxAge()));
    internal.setPath(cookie.getPath());
    internal.setVersion(cookie.getVersion());

    return internal;
}

From source file:org.nunux.poc.portal.ProxyServlet.java

/**
 * Retrieves all of the cookies from the servlet request and sets them on
 * the proxy request//  w  ww  .  jav a 2  s. c  o m
 *
 * @param httpServletRequest The request object representing the client's
 * request to the servlet engine
 * @param httpMethodProxyRequest The request that we are about to send to
 * the proxy host
 */
@SuppressWarnings("unchecked")
private void setProxyRequestCookies(HttpServletRequest httpServletRequest, HttpMethod httpMethodProxyRequest) {
    // Get an array of all of all the cookies sent by the client
    Cookie[] cookies = httpServletRequest.getCookies();
    if (cookies == null) {
        return;
    }

    if (httpServletRequest.getSession().getAttribute("jsessionid" + this.getProxyHostAndPort()) != null) {
        String jsessionid = (String) httpServletRequest.getSession()
                .getAttribute("jsessionid" + this.getProxyHostAndPort());
        httpMethodProxyRequest.setRequestHeader("Cookie", "JSESSIONID=" + jsessionid);
        debug("redirecting: setting jsessionid: " + jsessionid);
    }

    for (Cookie cookie : cookies) {
        if (!cookie.getName().equalsIgnoreCase("jsessionid")) {
            cookie.setDomain(stringProxyHost);
            cookie.setPath(httpServletRequest.getServletPath());
            httpMethodProxyRequest.setRequestHeader("Cookie",
                    cookie.getName() + "=" + cookie.getValue() + "; Path=" + cookie.getPath());
        }
    }
}

From source file:org.nuxeo.ecm.platform.ui.web.auth.cleartrust.ClearTrustAuthenticator.java

protected void displayCookieInformation(Cookie[] cookies) {
    log.debug(">>>>>>>>>>>>> Here are the cookies: ");
    for (Cookie cookie : cookies) {
        log.debug("displayCookieInformation cookie name: [" + cookie.getName() + "] path: [" + cookie.getPath()
                + "] domain: " + cookie.getDomain() + " max age: " + cookie.getMaxAge() + " value: ["
                + cookie.getValue() + "]");
    }/*w ww  . j  ava  2 s  .  co  m*/
}

From source file:org.opencms.flex.CmsFlexResponse.java

/**
 * Method overloaded from the standard HttpServletRequest API.<p>
 *
 * Cookies must be set directly as a header, otherwise they might not be set
 * in the super class.<p>/*from   www . j  ava 2s  . c  o  m*/
 *
 * @see javax.servlet.http.HttpServletResponseWrapper#addCookie(javax.servlet.http.Cookie)
 */
@Override
public void addCookie(Cookie cookie) {

    if (cookie == null) {
        throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_ADD_COOKIE_0));
    }

    StringBuffer header = new StringBuffer(128);

    // name and value
    header.append(cookie.getName());
    header.append('=');
    header.append(cookie.getValue());

    // add version 1 / RFC 2109 specific information
    if (cookie.getVersion() == 1) {
        header.append("; Version=1");

        // comment
        if (cookie.getComment() != null) {
            header.append("; Comment=");
            header.append(cookie.getComment());
        }
    }

    // domain
    if (cookie.getDomain() != null) {
        header.append("; Domain=");
        header.append(cookie.getDomain());
    }

    // max-age / expires
    if (cookie.getMaxAge() >= 0) {
        if (cookie.getVersion() == 0) {
            // old Netscape format
            header.append("; Expires=");
            long time;
            if (cookie.getMaxAge() == 0) {
                time = 10000L;
            } else {
                time = System.currentTimeMillis() + (cookie.getMaxAge() * 1000L);
            }
            header.append(CmsDateUtil.getOldCookieDate(time));
        } else {
            // new RFC 2109 format 
            header.append("; Max-Age=");
            header.append(cookie.getMaxAge());
        }
    }

    // path
    if (cookie.getPath() != null) {
        header.append("; Path=");
        header.append(cookie.getPath());
    }

    // secure
    if (cookie.getSecure()) {
        header.append("; Secure");
    }

    addHeader("Set-Cookie", header.toString());
}

From source file:org.opensubsystems.core.util.servlet.WebUtils.java

/**
 * Create debug string containing all parameter names and their values from
 * the request, all attributes, all cookies and other data characterizing the
 * request.//from   ww w. ja  va 2 s . c o  m
 *
 * @param  hsrqRequest - the servlet request.
 * @return String - debug string containing all parameter names and their
 *                  values from the request
 */
public static String debug(HttpServletRequest hsrqRequest) {
    Enumeration enumNames;
    Enumeration enumValues;
    Iterator iterValues;
    String strName;
    String[] arValues;
    Cookie[] arCookies;
    int iIndex;
    Map<String, String[]> mpParamMap;
    StringBuilder sbfReturn = new StringBuilder();

    sbfReturn.append("HttpServletRequest=[");
    sbfReturn.append("\nRemoteAddress=");
    sbfReturn.append(StringUtils.valueIfNotNull(hsrqRequest.getRemoteAddr()));
    sbfReturn.append(";");
    sbfReturn.append("\nRemotePort=");
    sbfReturn.append(hsrqRequest.getRemotePort());
    sbfReturn.append(";");
    sbfReturn.append("\nRemoteHost=");
    sbfReturn.append(StringUtils.valueIfNotNull(hsrqRequest.getRemoteHost()));
    sbfReturn.append(";");
    sbfReturn.append("\nRemoteUser=");
    sbfReturn.append(StringUtils.valueIfNotNull(hsrqRequest.getRemoteUser()));
    sbfReturn.append(";");
    sbfReturn.append("\nFullURL=");
    sbfReturn.append(getFullRequestURL(hsrqRequest));
    sbfReturn.append(";");
    sbfReturn.append("\nContextPath=");
    sbfReturn.append(hsrqRequest.getContextPath());
    sbfReturn.append(";");
    sbfReturn.append("\nServletPath=");
    sbfReturn.append(hsrqRequest.getServletPath());
    sbfReturn.append(";");
    sbfReturn.append("\nPathInfo =");
    sbfReturn.append(hsrqRequest.getPathInfo());
    sbfReturn.append(";");
    sbfReturn.append("\nRequestURI=");
    sbfReturn.append(hsrqRequest.getRequestURI());
    sbfReturn.append(";");
    sbfReturn.append("\nRequestURL=");
    sbfReturn.append(hsrqRequest.getRequestURL());
    sbfReturn.append(";");
    sbfReturn.append("\nMethod=");
    sbfReturn.append(hsrqRequest.getMethod());
    sbfReturn.append(";");
    sbfReturn.append("\nAuthenticationType=");
    sbfReturn.append(StringUtils.valueIfNotNull(hsrqRequest.getAuthType()));
    sbfReturn.append(";");
    sbfReturn.append("\nCharacterEncoding=");
    sbfReturn.append(StringUtils.valueIfNotNull(hsrqRequest.getCharacterEncoding()));
    sbfReturn.append(";");
    sbfReturn.append("\nContentType=");
    sbfReturn.append(StringUtils.valueIfNotNull(hsrqRequest.getContentType()));
    sbfReturn.append(";");
    sbfReturn.append("\nMultiPart=");
    sbfReturn.append(ServletFileUpload.isMultipartContent(hsrqRequest));
    sbfReturn.append(";");

    // Parameters ////////////////////////////////////////////////////////////

    try {
        Map.Entry<String, String[]> entry;

        // Use getParameterMap rather than request.getParameterNames since it 
        // correctly handles multipart requests
        mpParamMap = WebParamUtils.getParameterMap("WebUtils: ", hsrqRequest);
        for (iterValues = mpParamMap.entrySet().iterator(); iterValues.hasNext();) {
            entry = (Map.Entry<String, String[]>) iterValues.next();
            strName = entry.getKey();
            arValues = entry.getValue();
            sbfReturn.append("\nParam=");
            sbfReturn.append(strName);
            sbfReturn.append(" values=");
            for (iIndex = 0; iIndex < arValues.length; iIndex++) {
                sbfReturn.append(arValues[iIndex]);
                if (iIndex < (arValues.length - 1)) {
                    sbfReturn.append(";");
                }
            }
            if (iterValues.hasNext()) {
                sbfReturn.append(";");
            }
        }
    } catch (OSSInvalidDataException ex) {
        sbfReturn.append("<Cannot access parameter map of the request>");
        s_logger.log(Level.SEVERE, "Cannot access parameter map of the request", ex);
    }

    // Uploaded files ////////////////////////////////////////////////////////

    if (ServletFileUpload.isMultipartContent(hsrqRequest)) {
        try {
            FileItem item;
            Map<String, FileItem> mpFiles;
            TwoElementStruct<Map<String, Object>, Map<String, FileItem>> params;

            params = WebParamUtils.getMultipartParameters("WebUtils: ", hsrqRequest);
            mpFiles = params.getSecond();

            for (iterValues = mpFiles.values().iterator(); iterValues.hasNext();) {
                item = (FileItem) iterValues.next();
                sbfReturn.append("\nUpload=");
                sbfReturn.append(item.getName());
                sbfReturn.append(" field=");
                sbfReturn.append(item.getFieldName());
                sbfReturn.append(" contentType=");
                sbfReturn.append(item.getContentType());
                sbfReturn.append(" isInMemory=");
                sbfReturn.append(item.isInMemory());
                sbfReturn.append(" sizeInBytes=");
                sbfReturn.append(item.getSize());
                if (iterValues.hasNext()) {
                    sbfReturn.append(";");
                }
            }
        } catch (OSSInvalidDataException ex) {
            sbfReturn.append("<Cannot access list of multipart parameters>");
            s_logger.log(Level.SEVERE, "Cannot access list of multipart parameters", ex);
        }
    }

    // Headers ///////////////////////////////////////////////////////////////

    for (enumNames = hsrqRequest.getHeaderNames(); enumNames.hasMoreElements();) {
        strName = (String) enumNames.nextElement();
        sbfReturn.append("\nHeader=");
        sbfReturn.append(strName);
        sbfReturn.append(" values=");
        for (enumValues = hsrqRequest.getHeaders(strName); enumValues.hasMoreElements();) {
            sbfReturn.append(enumValues.nextElement());
            if (enumValues.hasMoreElements()) {
                sbfReturn.append(";");
            }
        }
        if (enumNames.hasMoreElements()) {
            sbfReturn.append(";");
        }
    }

    // Cookies ///////////////////////////////////////////////////////////////

    arCookies = hsrqRequest.getCookies();
    if (arCookies != null) {
        Cookie cookie;

        for (iIndex = 0; iIndex < arCookies.length; iIndex++) {
            cookie = arCookies[iIndex];
            sbfReturn.append("\nCookie=");
            sbfReturn.append(cookie.getName());
            sbfReturn.append(" path=");
            sbfReturn.append(cookie.getPath());
            sbfReturn.append(" path=");
            sbfReturn.append(cookie.getDomain());
            sbfReturn.append(" maxage=");
            sbfReturn.append(cookie.getMaxAge());
            sbfReturn.append(" version=");
            sbfReturn.append(cookie.getVersion());
            sbfReturn.append(" secure=");
            sbfReturn.append(cookie.getSecure());
            sbfReturn.append(" value=");
            sbfReturn.append(cookie.getValue());
            sbfReturn.append(" comment=");
            sbfReturn.append(StringUtils.valueIfNotNull(cookie.getComment()));
            if (iIndex < (arCookies.length - 1)) {
                sbfReturn.append(";");
            }
        }
    }
    if (enumNames.hasMoreElements()) {
        sbfReturn.append(";");
    }

    // Attributes ////////////////////////////////////////////////////////////

    for (enumNames = hsrqRequest.getAttributeNames(); enumNames.hasMoreElements();) {
        strName = (String) enumNames.nextElement();
        sbfReturn.append("\nAttribute=");
        sbfReturn.append(strName);
        sbfReturn.append(" value=");
        sbfReturn.append(hsrqRequest.getAttribute(strName));
        if (enumNames.hasMoreElements()) {
            sbfReturn.append(";");
        }
    }

    // Content ///////////////////////////////////////////////////////////////

    sbfReturn.append("\nContent=");
    try {
        sbfReturn.append(StringUtils.convertStreamToString(hsrqRequest.getInputStream(), true));
    } catch (IOException ex) {
        sbfReturn.append("<Cannot access input stream of the request>");
        s_logger.log(Level.SEVERE, "Cannot access input stream of the request", ex);
    }
    sbfReturn.append(";");

    return sbfReturn.toString();
}

From source file:org.owasp.esapi.reference.DefaultHTTPUtilities.java

/**
* {@inheritDoc}/*  www.j  av  a 2  s .com*/
 * This implementation uses a custom "set-cookie" header rather than Java's
 * cookie interface which doesn't allow the use of HttpOnly. Configure the
 * HttpOnly and Secure settings in ESAPI.properties.
*/
public void addCookie(HttpServletResponse response, Cookie cookie) {
    String name = cookie.getName();
    String value = cookie.getValue();
    int maxAge = cookie.getMaxAge();
    String domain = cookie.getDomain();
    String path = cookie.getPath();
    boolean secure = cookie.getSecure();

    // validate the name and value
    ValidationErrorList errors = new ValidationErrorList();
    String cookieName = ESAPI.validator().getValidInput("cookie name", name, "HTTPCookieName", 50, false,
            errors);
    String cookieValue = ESAPI.validator().getValidInput("cookie value", value, "HTTPCookieValue", 5000, false,
            errors);

    // if there are no errors, then set the cookie either with a header or normally
    if (errors.size() == 0) {
        if (ESAPI.securityConfiguration().getForceHttpOnlyCookies()) {
            String header = createCookieHeader(cookieName, cookieValue, maxAge, domain, path, secure);
            addHeader(response, "Set-Cookie", header);
        } else {
            // Issue 23 - If the ESAPI Configuration is set to force secure cookies, force the secure flag on the cookie before setting it
            cookie.setSecure(secure || ESAPI.securityConfiguration().getForceSecureCookies());
            response.addCookie(cookie);
        }
        return;
    }
    logger.warning(Logger.SECURITY_FAILURE,
            "Attempt to add unsafe data to cookie (skip mode). Skipping cookie and continuing.");
}

From source file:org.owasp.esapi.reference.DefaultHTTPUtilities.java

/**
 * {@inheritDoc}/*from w w  w  .  jav  a 2  s  . c o m*/
  *
  * @param request
  * @param response
  * @param name
  */
public void killCookie(HttpServletRequest request, HttpServletResponse response, String name) {
    String path = "//";
    String domain = "";
    Cookie cookie = getFirstCookie(request, name);
    if (cookie != null) {
        path = cookie.getPath();
        domain = cookie.getDomain();
    }
    Cookie deleter = new Cookie(name, "deleted");
    deleter.setMaxAge(0);
    if (domain != null)
        deleter.setDomain(domain);
    if (path != null)
        deleter.setPath(path);
    response.addCookie(deleter);
}