Example usage for javax.servlet.http Cookie setValue

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

Introduction

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

Prototype

public void setValue(String newValue) 

Source Link

Document

Assigns a new value to this Cookie.

Usage

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

protected HttpClient prepareServerRequest(HttpServletRequest request, HttpServletResponse response,
        HttpMethod method) {//ww w. java2s .  c  o m
    // 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.alfresco.web.app.servlet.AuthenticationHelper.java

/**
 * Setup the Alfresco auth cookie value.
 * /*from ww w .j a v  a2 s.  c o m*/
 * @param httpRequest
 * @param httpResponse
 * @param username
 */
public static void setUsernameCookie(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
        String username) {
    if (logger.isDebugEnabled())
        logger.debug("Setting up the Alfresco auth cookie for " + username);
    Cookie authCookie = getAuthCookie(httpRequest);
    // Let's Base 64 encode the username so it is a legal cookie value
    String encodedUsername;
    try {
        encodedUsername = Base64.encodeBytes(username.getBytes("UTF-8"));
        if (logger.isDebugEnabled())
            logger.debug("Base 64 encode the username: " + encodedUsername);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
    if (authCookie == null) {
        if (logger.isDebugEnabled())
            logger.debug("No Alfresco auth cookie wa found, creating new one.");
        authCookie = new Cookie(COOKIE_ALFUSER, encodedUsername);
    } else {
        if (logger.isDebugEnabled())
            logger.debug("Updating the previous Alfresco auth cookie value.");
        authCookie.setValue(encodedUsername);
    }
    authCookie.setPath(httpRequest.getContextPath());
    // TODO: make this configurable - currently 7 days (value in seconds)
    authCookie.setMaxAge(60 * 60 * 24 * 7);
    httpResponse.addCookie(authCookie);
}

From source file:org.nuxeo.ecm.platform.ui.web.auth.cas2.Cas2Authenticator.java

protected void removeCookie(HttpServletRequest httpRequest, HttpServletResponse httpResponse, Cookie cookie) {
    log.debug(String.format("Removing cookie %s.", cookie.getName()));
    cookie.setMaxAge(0);/*  w  ww .  ja  va2s. c  o m*/
    cookie.setValue("");
    cookie.setPath(httpRequest.getContextPath());
    httpResponse.addCookie(cookie);
}

From source file:custom.application.login.java

public void logout() {
    HttpServletRequest request = (HttpServletRequest) this.context.getAttribute("HTTP_REQUEST");
    HttpServletResponse response = (HttpServletResponse) this.context.getAttribute("HTTP_RESPONSE");

    try {/*w w  w  .  ja  v  a2s . c o m*/
        this.passport = new passport(request, response, "waslogined");
        this.passport.logout();

        if (request.getCookies() != null) {
            Cookie[] cookies = request.getCookies();
            int i = 0;
            Cookie cookie;
            while (cookies.length > i) {
                cookie = cookies[i];
                cookie.setMaxAge(0);
                cookie.setValue("");
                response.addCookie(cookie);
                i++;
            }
        }

        Reforward reforward = new Reforward(request, response);

        reforward.setDefault(this.getLink(this.context.getAttribute("default.login.page").toString()));
        reforward.forward();
    } catch (ApplicationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:org.apache.sling.auth.xing.login.impl.XingLoginAuthenticationHandler.java

protected void deleteCookies(final HttpServletRequest request, final HttpServletResponse response) {
    final Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (final Cookie cookie : cookies) {
            final String name = cookie.getName();
            logger.debug("cookie found: '{}'", name);
            if (name.equals(xingCookie) || name.equals(userCookie) || name.equals(userIdCookie)) {
                logger.debug("deleting cookie '{}' with value '{}'", cookie.getName(), cookie.getValue());
                cookie.setValue(null);
                cookie.setMaxAge(0);//from w w  w  .j  a v  a  2  s. c  o m
                response.addCookie(cookie);
            }
        }
    }
}

From source file:de.innovationgate.wga.server.api.Call.java

/**
 * Creates a new completely initialized HTTP cookie, which is not yet assigned to the call.
 * Use {@link #addCookie(Cookie)} to do so and send it to the client.
 * The cookie is initialized with path (the OpenWGA context path), type/maxage (transient),
 * domain (either request host or host from configured server base url) and security
 * flag (true if the current call is HTTPS).
 * @param name Name of the cookie/* ww w.j a v  a  2s  . c o m*/
 * @param value Value of the cookie
 * @return
 * @throws WGException
 */
public Cookie createCookie(String name, String value) throws WGException {

    URLBuilder baseURL = _wga.urlBuilder(_wga.server().getBaseURL());
    URLBuilder requestURL = _wga.urlBuilder(getURL());

    Cookie c = new Cookie();
    c.setName(name);
    c.setValue(value);
    c.setMaxAge(-1);
    c.setPath(baseURL.build(false));
    if (_wga.isRequestAvailable()) {
        c.setDomain(requestURL.getHost());
    } else {
        c.setDomain(baseURL.getHost());
    }
    c.setSecure(requestURL.getProtocol().equals("https"));

    return c;

}

From source file:org.craftercms.cstudio.share.servlet.CookieManagerImpl.java

public void destroyCookie(HttpServletRequest request, HttpServletResponse response, String key, String path) {
    Cookie[] cookieArray = request.getCookies();
    if (cookieArray != null) {
        for (Cookie cookie : cookieArray) {
            String name = cookie.getName();
            if (name != null && name.equals(key)) {
                if (!StringUtils.isEmpty(path)) {
                    cookie.setPath(path);
                } else {
                    cookie.setPath("/");
                }// www . j  av  a  2s .  c  o  m
                cookie.setMaxAge(0);
                cookie.setValue(null);

                if (_cookieDomain != null) {
                    cookie.setDomain(_cookieDomain);
                }

                response.addCookie(cookie);
            }
        }
    }
}

From source file:com.haulmont.cuba.web.sys.CubaApplicationServlet.java

protected void redirectToApp(HttpServletRequest request, HttpServletResponse response, String contextName,
        String[] uriParts, String action) throws IOException {
    StringBuilder redirectAddress = new StringBuilder();
    for (int i = 0; i < uriParts.length; i++) {
        redirectAddress.append(uriParts[i]);
        if (uriParts[i].equals(contextName)) {
            break;
        }/*from   w  w w .  ja  v a  2s . c  om*/
        if (i < uriParts.length - 1) {
            redirectAddress.append("/");
        }
    }

    // redirect to ROOT context
    if (redirectAddress.length() == 0) {
        redirectAddress.append("/");
    }

    HttpSession httpSession = request.getSession();
    if (action != null) {
        httpSession.setAttribute(AppUI.LAST_REQUEST_ACTION_ATTR, action);
    }
    if (request.getParameterNames().hasMoreElements()) {
        Map<String, String> params = new HashMap<>();
        Enumeration parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String name = (String) parameterNames.nextElement();
            if (!FROM_HTML_REDIRECT_PARAM.equals(name)) {
                params.put(name, request.getParameter(name));
            }
        }
        httpSession.setAttribute(AppUI.LAST_REQUEST_PARAMS_ATTR, params);
    }

    statisticsCounter.incWebRequestsCount();
    String httpSessionId = httpSession.getId();
    log.debug("Redirect to application {}", httpSessionId);

    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if ("JSESSIONID".equals(cookie.getName()) && !httpSessionId.equals(cookie.getValue())) {
                cookie.setValue(httpSessionId);
                break;
            }
        }
    }
    response.sendRedirect(redirectAddress.toString());
}

From source file:org.nuxeo.ecm.platform.auth.saml.SAMLAuthenticationProvider.java

private void removeCookie(HttpServletResponse httpResponse, Cookie cookie) {
    log.debug(String.format("Removing cookie %s.", cookie.getName()));
    cookie.setMaxAge(0);//from   w ww  .  jav a  2  s.co m
    cookie.setValue("");
    httpResponse.addCookie(cookie);
}

From source file:org.syncope.console.commons.PreferenceManager.java

public void set(final Request request, final Response response, final String key, final String value) {

    Cookie prefCookie = ((WebRequest) request).getCookie(Constants.PREFS_COOKIE_NAME);

    final Map<String, String> prefs = new HashMap<String, String>();

    if (prefCookie == null || !StringUtils.hasText(prefCookie.getValue())) {
        prefCookie = new Cookie(Constants.PREFS_COOKIE_NAME, null);
    } else {/*from  w  w w . j a  v a  2 s  .  c  o m*/
        prefs.putAll(getPrefs(new String(Base64.decodeBase64(prefCookie.getValue().getBytes()))));
    }

    // after retrieved previous setting in order to overwrite the key ...
    prefs.put(key, value);

    try {
        prefCookie.setValue(new String(Base64.encodeBase64(setPrefs(prefs).getBytes())));
    } catch (IOException e) {
        LOG.error("Could not set preferences " + prefs, e);
    }

    prefCookie.setMaxAge(ONE_YEAR_TIME);
    ((WebResponse) response).addCookie(prefCookie);
}