Example usage for javax.servlet.http Cookie getVersion

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

Introduction

In this page you can find the example usage for javax.servlet.http Cookie getVersion.

Prototype

public int getVersion() 

Source Link

Document

Returns the version of the protocol this cookie complies with.

Usage

From source file:com.anjz.util.CookieUtils.java

private static String getCookieHeaderName(final Cookie cookie) {
    final int version = cookie.getVersion();
    if (version == 1) {
        return "Set-Cookie2";
    } else {/*ww w. java 2  s  .c o  m*/
        return "Set-Cookie";
    }
}

From source file:com.anjz.util.CookieUtils.java

private static void getCookieHeaderValue(final Cookie cookie, final StringBuffer buf, final boolean httpOnly) {
    final int version = cookie.getVersion();

    // this part is the same for all cookies

    String name = cookie.getName(); // Avoid NPE on malformed cookies
    if (name == null) {
        name = "";
    }//from w  ww.  j  a v a2s. c  om
    String value = cookie.getValue();
    if (value == null) {
        value = "";
    }

    buf.append(name);
    buf.append("=");

    maybeQuote(version, buf, value);

    // add version 1 specific information
    if (version == 1) {
        // Version=1 ... required
        buf.append("; Version=1");

        // Comment=comment
        if (cookie.getComment() != null) {
            buf.append("; Comment=");
            maybeQuote(version, buf, cookie.getComment());
        }
    }

    // add domain information, if present

    if (cookie.getDomain() != null) {
        buf.append("; Domain=");
        maybeQuote(version, buf, cookie.getDomain());
    }

    // Max-Age=secs/Discard ... or use old "Expires" format
    if (cookie.getMaxAge() >= 0) {
        if (version == 0) {
            buf.append("; Expires=");
            SimpleDateFormat dateFormat = new SimpleDateFormat(OLD_COOKIE_PATTERN, LOCALE_US);
            dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); //GMT?
            if (cookie.getMaxAge() == 0) {
                dateFormat.format(new Date(10000), buf, new FieldPosition(0));
            } else {
                dateFormat.format(new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000L), buf,
                        new FieldPosition(0));
            }
        } else {
            buf.append("; Max-Age=");
            buf.append(cookie.getMaxAge());
        }
    } else if (version == 1) {
        buf.append("; Discard");
    }

    // Path=path
    if (cookie.getPath() != null) {
        buf.append("; Path=");
        maybeQuote(version, buf, cookie.getPath());
    }

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

    // HttpOnly
    if (httpOnly) {
        buf.append("; HttpOnly");
    }
}

From source file:net.bluehornreader.web.WebUtils.java

public static String cookieAsString(Cookie cookie) {
    StringBuilder bld = new StringBuilder();
    bld.append("Name=").append(cookie.getName()).append(" ");
    bld.append("Value=").append(cookie.getValue()).append(" ");
    bld.append("Domain=").append(cookie.getDomain()).append(" ");
    bld.append("MaxAge=").append(cookie.getMaxAge()).append(" ");
    bld.append("Path=").append(cookie.getPath()).append(" ");
    bld.append("Secure=").append(cookie.getSecure()).append(" ");
    bld.append("Comment=").append(cookie.getComment()).append(" ");
    bld.append("Version=").append(cookie.getVersion()).append(" ");
    return bld.toString().trim();
}

From source file:RequestUtil.java

/**
 * Encode a cookie as per RFC 2109. The resulting string can be used as the
 * value for a <code>Set-Cookie</code> header.
 * /*from w  w  w.  ja  v a2s.  com*/
 * @param cookie
 *            The cookie to encode.
 * @return A string following RFC 2109.
 */
public static String encodeCookie(Cookie cookie) {

    StringBuffer buf = new StringBuffer(cookie.getName());
    buf.append("=");
    buf.append(cookie.getValue());

    if (cookie.getComment() != null) {
        buf.append("; Comment=\"");
        buf.append(cookie.getComment());
        buf.append("\"");
    }

    if (cookie.getDomain() != null) {
        buf.append("; Domain=\"");
        buf.append(cookie.getDomain());
        buf.append("\"");
    }

    if (cookie.getMaxAge() >= 0) {
        buf.append("; Max-Age=\"");
        buf.append(cookie.getMaxAge());
        buf.append("\"");
    }

    if (cookie.getPath() != null) {
        buf.append("; Path=\"");
        buf.append(cookie.getPath());
        buf.append("\"");
    }

    if (cookie.getSecure()) {
        buf.append("; Secure");
    }

    if (cookie.getVersion() > 0) {
        buf.append("; Version=\"");
        buf.append(cookie.getVersion());
        buf.append("\"");
    }

    return (buf.toString());
}

From source file:org.codemucker.testserver.capturing.CapturedCookie.java

public CapturedCookie(Cookie c) {
    domain = c.getDomain();/*from   w w  w .  j  a va  2s.  com*/
    path = c.getPath();
    name = c.getName();
    value = c.getValue();
    secure = c.getSecure();
    maxAge = c.getMaxAge();
    version = c.getVersion();
}

From source file:org.codemucker.testserver.capturing.CapturedCookie.java

public CapturedCookie(org.apache.http.cookie.Cookie c) {
    domain = c.getDomain();/*w w w  .ja v  a2 s .c  o  m*/
    path = c.getPath();
    name = c.getName();
    value = c.getValue();
    secure = c.isSecure();
    maxAge = c.getExpiryDate() == null ? -1 : 0;
    version = c.getVersion();
}

From source file:com.acc.storefront.security.cookie.EnhancedCookieGenerator.java

@Override
public void addCookie(final HttpServletResponse response, final String cookieValue) {
    super.addCookie(new HttpServletResponseWrapper(response) {
        @Override/*from w w w.  j  av a2 s  .c o m*/
        public void addCookie(final Cookie cookie) {
            setEnhancedCookiePath(cookie);

            if (isHttpOnly()) {
                // Custom code to write the cookie including the httpOnly flag
                final StringBuffer headerBuffer = new StringBuffer(100);
                ServerCookie.appendCookieValue(headerBuffer, cookie.getVersion(), cookie.getName(),
                        cookie.getValue(), cookie.getPath(), cookie.getDomain(), cookie.getComment(),
                        cookie.getMaxAge(), cookie.getSecure(), true);
                response.addHeader(HEADER_COOKIE, headerBuffer.toString());
            } else {
                // Write the cookie as normal
                super.addCookie(cookie);
            }
        }
    }, cookieValue);
}

From source file:net.fenyo.mail4hotspot.web.BrowserServlet.java

@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
    // debug informations
    log.debug("doGet");
    log.debug("context path: " + request.getContextPath());
    log.debug("character encoding: " + request.getCharacterEncoding());
    log.debug("content length: " + request.getContentLength());
    log.debug("content type: " + request.getContentType());
    log.debug("local addr: " + request.getLocalAddr());
    log.debug("local name: " + request.getLocalName());
    log.debug("local port: " + request.getLocalPort());
    log.debug("method: " + request.getMethod());
    log.debug("path info: " + request.getPathInfo());
    log.debug("path translated: " + request.getPathTranslated());
    log.debug("protocol: " + request.getProtocol());
    log.debug("query string: " + request.getQueryString());
    log.debug("requested session id: " + request.getRequestedSessionId());
    log.debug("Host header: " + request.getServerName());
    log.debug("servlet path: " + request.getServletPath());
    log.debug("request URI: " + request.getRequestURI());
    @SuppressWarnings("unchecked")
    final Enumeration<String> header_names = request.getHeaderNames();
    while (header_names.hasMoreElements()) {
        final String header_name = header_names.nextElement();
        log.debug("header name: " + header_name);
        @SuppressWarnings("unchecked")
        final Enumeration<String> header_values = request.getHeaders(header_name);
        while (header_values.hasMoreElements())
            log.debug("  " + header_name + " => " + header_values.nextElement());
    }//from  ww w  .  jav  a 2 s.  co m
    if (request.getCookies() != null)
        for (Cookie cookie : request.getCookies()) {
            log.debug("cookie:");
            log.debug("cookie comment: " + cookie.getComment());
            log.debug("cookie domain: " + cookie.getDomain());
            log.debug("cookie max age: " + cookie.getMaxAge());
            log.debug("cookie name: " + cookie.getName());
            log.debug("cookie path: " + cookie.getPath());
            log.debug("cookie value: " + cookie.getValue());
            log.debug("cookie version: " + cookie.getVersion());
            log.debug("cookie secure: " + cookie.getSecure());
        }
    @SuppressWarnings("unchecked")
    final Enumeration<String> parameter_names = request.getParameterNames();
    while (parameter_names.hasMoreElements()) {
        final String parameter_name = parameter_names.nextElement();
        log.debug("parameter name: " + parameter_name);
        final String[] parameter_values = request.getParameterValues(parameter_name);
        for (final String parameter_value : parameter_values)
            log.debug("  " + parameter_name + " => " + parameter_value);
    }

    // parse request

    String target_scheme = null;
    String target_host;
    int target_port;

    // request.getPathInfo() is url decoded
    final String[] path_info_parts = request.getPathInfo().split("/");
    if (path_info_parts.length >= 2)
        target_scheme = path_info_parts[1];
    if (path_info_parts.length >= 3) {
        target_host = path_info_parts[2];
        try {
            if (path_info_parts.length >= 4)
                target_port = new Integer(path_info_parts[3]);
            else
                target_port = 80;
        } catch (final NumberFormatException ex) {
            log.warn(ex);
            target_port = 80;
        }
    } else {
        target_scheme = "http";
        target_host = "www.google.com";
        target_port = 80;
    }

    log.debug("remote URL: " + target_scheme + "://" + target_host + ":" + target_port);

    // create forwarding request

    final URL target_url = new URL(target_scheme + "://" + target_host + ":" + target_port);
    final HttpURLConnection target_connection = (HttpURLConnection) target_url.openConnection();

    // be transparent for accept-language headers
    @SuppressWarnings("unchecked")
    final Enumeration<String> accepted_languages = request.getHeaders("accept-language");
    while (accepted_languages.hasMoreElements())
        target_connection.setRequestProperty("Accept-Language", accepted_languages.nextElement());

    // be transparent for accepted headers
    @SuppressWarnings("unchecked")
    final Enumeration<String> accepted_content = request.getHeaders("accept");
    while (accepted_content.hasMoreElements())
        target_connection.setRequestProperty("Accept", accepted_content.nextElement());

}

From source file:com.meltmedia.cadmium.servlets.jersey.StatusService.java

@GET
@Path("/health")
@Produces("text/plain")
public String health(@Context HttpServletRequest request) {
    StringBuilder builder = new StringBuilder();
    builder.append("Server: " + request.getServerName() + "\n");
    builder.append("Scheme: " + request.getScheme() + "\n");
    builder.append("Port: " + request.getServerPort() + "\n");
    builder.append("ContextPath:  " + request.getContextPath() + "\n");
    builder.append("ServletPath: " + request.getServletPath() + "\n");
    builder.append("Uri: " + request.getRequestURI() + "\n");
    builder.append("Query: " + request.getQueryString() + "\n");
    Enumeration<?> headerNames = request.getHeaderNames();
    builder.append("Headers:\n");
    while (headerNames.hasMoreElements()) {
        String name = (String) headerNames.nextElement();
        Enumeration<?> headers = request.getHeaders(name);
        builder.append("  '" + name + "':\n");
        while (headers.hasMoreElements()) {
            String headerValue = (String) headers.nextElement();
            builder.append("    -" + headerValue + "\n");
        }//from w  ww  .j a  v  a  2s  .co m
    }
    if (request.getCookies() != null) {
        builder.append("Cookies:\n");
        for (Cookie cookie : request.getCookies()) {
            builder.append("  '" + cookie.getName() + "':\n");
            builder.append("    value: " + cookie.getValue() + "\n");
            builder.append("    domain: " + cookie.getDomain() + "\n");
            builder.append("    path: " + cookie.getPath() + "\n");
            builder.append("    maxAge: " + cookie.getMaxAge() + "\n");
            builder.append("    version: " + cookie.getVersion() + "\n");
            builder.append("    comment: " + cookie.getComment() + "\n");
            builder.append("    secure: " + cookie.getSecure() + "\n");
        }
    }
    return builder.toString();
}

From source file:fr.smile.liferay.EsigatePortlet.java

/**
 * Transform request to IncominqRequest//from  ww  w  .  j ava 2  s  .  co  m
 *
 * @param request
 * @param method
 * @return an incoming request
 * @throws IOException
 */
public IncomingRequest create(PortletRequest request, String method) throws IOException {

    HttpServletRequest httpServletRequest = PortalUtil
            .getOriginalServletRequest(PortalUtil.getHttpServletRequest(request));

    StringBuilder uri = new StringBuilder(HTTP_BASE_INCOMING_URL);

    StringBuilder query = new StringBuilder();
    Enumeration<String> parameters = request.getParameterNames();
    String sep = "";
    while (parameters.hasMoreElements()) {
        String name = parameters.nextElement();
        String[] values = request.getParameterValues(name);
        if (!name.equals(ACTION_PARAMETER)) {
            for (String value : values) {
                query.append(sep);
                query.append(name).append("=").append(URLEncoder.encode(value, "UTF-8"));
                sep = "&";
            }
        }
    }

    ProtocolVersion protocolVersion = HttpVersion.HTTP_1_1.forVersion(1, 0);

    if (method.equals("GET")) {
        if (!query.toString().isEmpty()) {
            if (!uri.toString().contains("?")) {
                uri.append("?");
            } else {
                uri.append("&");
            }
            uri.append(query);
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Creating Incoming request with method " + method + ", URI " + uri + ", protocoleVersion "
                + protocolVersion);
    }
    IncomingRequest.Builder builder = IncomingRequest
            .builder(new BasicRequestLine(method, uri.toString(), protocolVersion));

    if (method.equals("POST")) {
        // create entity
        InputStream inputStream = IOUtils.toInputStream(query.toString());

        if (inputStream != null) {
            // Copy entity-related headers
            InputStreamEntity entity = new InputStreamEntity(inputStream, query.length());
            String contentTypeHeader = httpServletRequest.getContentType();
            if (contentTypeHeader != null) {
                entity.setContentType(contentTypeHeader);
            }
            String contentEncodingHeader = httpServletRequest.getCharacterEncoding();
            if (contentEncodingHeader != null) {
                entity.setContentEncoding(contentEncodingHeader);
            }
            builder.setEntity(entity);
        }
    }

    HttpServletRequestContext context = new HttpServletRequestContext(httpServletRequest, null, null);
    builder.setContext(context);
    builder.setRemoteAddr(httpServletRequest.getRemoteAddr());
    builder.setRemoteUser(request.getRemoteUser());
    HttpSession session = httpServletRequest.getSession(false);
    if (session != null) {
        builder.setSessionId(session.getId());
    }
    builder.setUserPrincipal(request.getUserPrincipal());
    // Copy cookies
    javax.servlet.http.Cookie[] src = request.getCookies();

    if (src != null) {
        LOG.debug("Copying " + src.length + " cookie(s) to response.");
        for (int i = 0; i < src.length; i++) {
            javax.servlet.http.Cookie c = src[i];
            BasicClientCookie dest = new BasicClientCookie(c.getName(), c.getValue());
            dest.setSecure(c.getSecure());
            dest.setDomain(c.getDomain());
            dest.setPath(c.getPath());
            dest.setComment(c.getComment());
            dest.setVersion(c.getVersion());
            builder.addCookie(dest);
        }
    }

    builder.setSession(new HttpServletSession(httpServletRequest));

    IncomingRequest incomingRequest = builder.build();
    return incomingRequest;

}