Example usage for javax.servlet.http Cookie getValue

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

Introduction

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

Prototype

public String getValue() 

Source Link

Document

Gets the current value of this Cookie.

Usage

From source file:hudson.plugins.timestamper.format.TimestampFormatterImpl.java

/**
 * Create a new {@link TimestampFormatterImpl}.
 * /*  w  w  w  . jav a 2 s. co m*/
 * @param systemTimeFormat
 *          the system clock time format
 * @param elapsedTimeFormat
 *          the elapsed time format
 * @param timeZoneId
 *          the configured time zone identifier
 * @param request
 *          the current HTTP request
 */
public TimestampFormatterImpl(String systemTimeFormat, String elapsedTimeFormat, Optional<String> timeZoneId,
        HttpServletRequest request) {

    String cookieValue = null;
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if ("jenkins-timestamper".equals(cookie.getName())) {
                cookieValue = cookie.getValue();
                break;
            }
        }
    }

    if ("elapsed".equalsIgnoreCase(cookieValue)) {
        formatTimestamp = new ElapsedTimeFormatFunction(elapsedTimeFormat);
    } else if ("none".equalsIgnoreCase(cookieValue)) {
        formatTimestamp = new EmptyFormatFunction();
    } else {
        // "system", no cookie, or unrecognised cookie
        TimeZone timeZone = null;
        if (timeZoneId.isPresent()) {
            timeZone = TimeZone.getTimeZone(timeZoneId.get());
        }
        FastDateFormat format = FastDateFormat.getInstance(systemTimeFormat, timeZone);
        formatTimestamp = new SystemTimeFormatFunction(format);
    }
}

From source file:org.obiba.mica.config.locale.AngularCookieLocaleResolver.java

private void parseLocaleCookieIfNecessary(HttpServletRequest request) {
    if (request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME) == null) {
        // Retrieve and parse cookie value.
        Cookie cookie = WebUtils.getCookie(request, getCookieName());
        Locale locale = null;/*from  ww  w.  java  2  s  . com*/
        TimeZone timeZone = null;
        if (cookie != null) {
            String value = cookie.getValue();

            // Remove the double quote
            value = StringUtils.replace(value, "%22", "");

            String localePart = value;
            String timeZonePart = null;
            int spaceIndex = localePart.indexOf(' ');
            if (spaceIndex != -1) {
                localePart = value.substring(0, spaceIndex);
                timeZonePart = value.substring(spaceIndex + 1);
            }
            locale = "-".equals(localePart) ? null : StringUtils.parseLocaleString(localePart);
            if (timeZonePart != null) {
                timeZone = StringUtils.parseTimeZoneString(timeZonePart);
            }
            if (logger.isTraceEnabled()) {
                logger.trace("Parsed cookie value [" + cookie.getValue() + "] into locale '" + locale + "'"
                        + (timeZone != null ? " and time zone '" + timeZone.getID() + "'" : ""));
            }
        }
        request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME,
                locale == null ? determineDefaultLocale(request) : locale);
        request.setAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME,
                timeZone == null ? determineDefaultTimeZone(request) : timeZone);
    }
}

From source file:com.glaf.core.util.RequestUtils.java

public static String getTheme(HttpServletRequest request) {
    String theme = "default";
    Cookie[] cookies = request.getCookies();
    if (cookies != null && cookies.length > 0) {
        for (Cookie cookie : cookies) {
            if (StringUtils.equals(cookie.getName(), Constants.THEME_COOKIE)) {
                if (cookie.getValue() != null) {
                    theme = cookie.getValue();
                }/* w w w.j a va 2s .  co m*/
            }
        }
    }
    if (StringUtils.isNotEmpty(request.getParameter("theme"))) {
        theme = request.getParameter("theme");
    }
    return theme;
}

From source file:hudson.Functions.java

public static boolean isAutoRefresh(HttpServletRequest request) {
    String param = request.getParameter("auto_refresh");
    if (param != null) {
        return Boolean.parseBoolean(param);
    }/*  w  w w  .j  a v a 2 s  .  c  o m*/
    Cookie[] cookies = request.getCookies();
    if (cookies == null)
        return false; // when API design messes it up, we all suffer

    for (Cookie c : cookies) {
        if (c.getName().equals("hudson_auto_refresh")) {
            return Boolean.parseBoolean(c.getValue());
        }
    }
    return false;
}

From source file:il.co.brandis.controller.UserController.java

/**
 * Creating new user before directing to register page
*///from  ww  w. ja va 2s.  c o  m
@RequestMapping(value = "/index")
public String registerForm(ModelMap modelMap, HttpServletRequest req) {
    Cookie cookie = CookiesUtil.getUserCookie(req);
    if (cookie != null) {
        String productsURI = "redirect:/products/showproducts";
        return userService.performUserLogin(cookie.getValue(), modelMap, productsURI);
    }
    modelMap.addAttribute("newUser", new User());
    return "index";
}

From source file:com.googlesource.gerrit.plugins.github.oauth.GitHubLogin.java

private String getScopesKeyFromCookie(HttpServletRequest request) {
    Cookie[] cookies = request.getCookies();
    if (cookies == null) {
        return null;
    }/*w w w.  j ava 2  s . c  o  m*/

    for (Cookie cookie : cookies) {
        if (cookie.getName().equalsIgnoreCase("scope")) {
            return cookie.getValue();
        }
    }
    return null;
}

From source file:org.moserp.infrastructure.gateway.config.OAuthConfiguration.java

/**
 * Spring security offers in-built protection for cross site request forgery
 * (CSRF) by needing a custom token in the header for any requests that are
 * NOT safe i.e. modify the resources from the server e.g. POST, PUT & PATCH
 * etc.<br>// www. j a  va  2s.  c o m
 * <br>
 *
 * This protection is achieved using cookies that send a custom value (would
 * remain same for the session) in the first request and then the front-end
 * would send back the value as a custom header.<br>
 * <br>
 *
 * In this method we create a filter that is applied to the web security as
 * follows:
 * <ol>
 * <li>Spring security provides the CSRF token value as a request attribute;
 * so we extract it from there.</li>
 * <li>If we have the token, Angular wants the cookie name to be
 * "XSRF-TOKEN". So we add the cookie if it's not there and set the path for
 * the cookie to be "/" which is root. In more complicated cases, this might
 * have to be the context root of the api gateway.</li>
 * <li>We forward the request to the next filter in the chain</li>
 * </ol>
 *
 * The request-to-cookie filter that we add needs to be after the
 * <code>csrf()</code> filter so that the request attribute for CsrfToken
 * has been already added before we start to process it.
 *
 * @return
 */
private Filter createCSRFHeaderFilter() {
    return new OncePerRequestFilter() {
        @Override
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
                FilterChain filterChain) throws ServletException, IOException {
            CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
            if (csrf != null) {
                Cookie cookie = WebUtils.getCookie(request, CSRF_COOKIE_NAME);
                String token = csrf.getToken();
                if (cookie == null || token != null && !token.equals(cookie.getValue())) {
                    cookie = new Cookie(CSRF_COOKIE_NAME, token);
                    cookie.setPath("/");
                    response.addCookie(cookie);
                }
            }
            filterChain.doFilter(request, response);
        }
    };
}

From source file:hudson.Functions.java

/**
 * If we know the user's screen resolution, return it. Otherwise null.
 * @since 1.213//from  w w  w  .jav a2 s .c  om
 */
public static Area getScreenResolution() {
    Cookie res = Functions.getCookie(Stapler.getCurrentRequest(), "screenResolution");
    if (res != null)
        return Area.parse(res.getValue());
    return null;
}

From source file:com.gradecak.alfresco.mvc.aop.AuthenticationAdvice.java

private String getTicket() {

    String ticket = "";
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
            .getRequest();//from  ww  w . j  ava  2  s .  c  o m

    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (int i = 0; i < cookies.length; i++) {
            Cookie cookie = cookies[i];
            if (cookie != null && "TICKET".equals(cookie.getName().toUpperCase())) {
                ticket = cookie.getValue();
            }
        }
    }

    @SuppressWarnings("unchecked")
    Map<String, Object> parameterMap = request.getParameterMap();

    if (parameterMap != null) {
        for (Object parameter : parameterMap.keySet()) {
            if (parameter != null && "TICKET".equals(((String) parameter).toUpperCase())) {
                ticket = (String) parameterMap.get(parameter);
            }
        }
    }

    // HttpSession session = request.getSession();
    // if (session != null) {
    // // TODO dgradecak: FIX THIS
    // User user = (User)session.getAttribute("_alfAuthTicket");
    // ticket = user.getTicket();
    // }

    return ticket;
}

From source file:eu.semlibproject.annotationserver.managers.CookiesManager.java

/**
 * Check if the current user is authenticated/authorized verifying
 * the annotation server cookie//w w w. j  av a  2  s .  c  o m
 * 
 * @param httpRequest   the HTTP servlet request
 * @return              if the user is authorized/authenticaed, return the accessToken
 *                      otherwise return <code>null</code>
 */
public String isUserAuthorizedFromCookie(HttpServletRequest httpRequest, HttpServletResponse response) {

    boolean authorized = false;
    Cookie[] cookies = httpRequest.getCookies();
    if (cookies != null) {
        for (int i = 0; i < cookies.length; i++) {
            Cookie cCookie = cookies[i];
            if (SemlibConstants.COOCKIE_NAME.equalsIgnoreCase(cCookie.getName())) {
                String cookieValue = cCookie.getValue();
                authorized = TokenManager.getInstance().isTokenValid(cookieValue);
                if (authorized) {
                    return cookieValue;
                } else {
                    removeASCookie(response, cCookie);
                }
            }
        }
    }

    return null;
}