Example usage for org.springframework.web.context.request RequestContextHolder currentRequestAttributes

List of usage examples for org.springframework.web.context.request RequestContextHolder currentRequestAttributes

Introduction

In this page you can find the example usage for org.springframework.web.context.request RequestContextHolder currentRequestAttributes.

Prototype

public static RequestAttributes currentRequestAttributes() throws IllegalStateException 

Source Link

Document

Return the RequestAttributes currently bound to the thread.

Usage

From source file:com.mobileman.moments.core.security.UserSecurityService.java

private HttpServletRequest getHttpServletRequest() {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
            .getRequest();
    return request;
}

From source file:com.cws.us.pws.controllers.AboutController.java

@RequestMapping(value = "/careers", method = RequestMethod.GET)
public final ModelAndView showCareersPage() {
    final String methodName = AboutController.CNAME + "#showCareersPage()";

    if (DEBUG) {/*w  ww  .jav a 2 s. c  om*/
        DEBUGGER.debug(methodName);
    }

    ModelAndView mView = new ModelAndView();

    final ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
            .currentRequestAttributes();
    final HttpServletRequest hRequest = requestAttributes.getRequest();
    final HttpSession hSession = hRequest.getSession();

    if (DEBUG) {
        DEBUGGER.debug("ServletRequestAttributes: {}", requestAttributes);
        DEBUGGER.debug("HttpServletRequest: {}", hRequest);
        DEBUGGER.debug("HttpSession: {}", hSession);

        DEBUGGER.debug("Dumping session content:");
        @SuppressWarnings("unchecked")
        Enumeration<String> sessionEnumeration = hSession.getAttributeNames();

        while (sessionEnumeration.hasMoreElements()) {
            String sessionElement = sessionEnumeration.nextElement();
            Object sessionValue = hSession.getAttribute(sessionElement);

            DEBUGGER.debug("Attribute: " + sessionElement + "; Value: " + sessionValue);
        }

        DEBUGGER.debug("Dumping request content:");
        @SuppressWarnings("unchecked")
        Enumeration<String> requestEnumeration = hRequest.getAttributeNames();

        while (requestEnumeration.hasMoreElements()) {
            String requestElement = requestEnumeration.nextElement();
            Object requestValue = hRequest.getAttribute(requestElement);

            DEBUGGER.debug("Attribute: " + requestElement + "; Value: " + requestValue);
        }

        DEBUGGER.debug("Dumping request parameters:");
        @SuppressWarnings("unchecked")
        Enumeration<String> paramsEnumeration = hRequest.getParameterNames();

        while (paramsEnumeration.hasMoreElements()) {
            String requestElement = paramsEnumeration.nextElement();
            Object requestValue = hRequest.getParameter(requestElement);

            DEBUGGER.debug("Parameter: " + requestElement + "; Value: " + requestValue);
        }
    }

    try {
        CareerRequest request = new CareerRequest();
        request.setLang(
                (StringUtils.isBlank(hRequest.getParameter("lang"))) ? "en" : hRequest.getParameter("lang"));

        if (DEBUG) {
            DEBUGGER.debug("CareerRequest: {}", request);
        }

        CareerResponse response = this.careerRefSvc.getCareerList(request);

        if (DEBUG) {
            DEBUGGER.debug("CareerResponse: {}", response);
        }

        if (response.getRequestStatus() == CoreServicesStatus.SUCCESS) {
            mView.addObject("careerList", response.getCareerList());
        }
    } catch (CareerRequestException crx) {
        ERROR_RECORDER.error(crx.getMessage(), crx);
    }

    mView.setViewName(this.careersPage);

    if (DEBUG) {
        DEBUGGER.debug("ModelAndView: {}", mView);
    }

    return mView;
}

From source file:com.github.dactiv.fear.user.service.account.AccountService.java

/**
 * ?/*from  w w w  .  j  a va2  s  .  c  om*/
 *
 * @param username ??
 * @param captcha ??
 */
public Map<String, Object> forgotPassword(String username, String captcha) throws Exception {

    HttpSession session = (HttpSession) RequestContextHolder.currentRequestAttributes().getSessionMutex();
    captchaManager.setCurrentSession(session);

    ValidResult validResult = captchaManager.valid(session.getId(), captcha);

    if (validResult.getIsValid()) {
        throw new ServiceException(validResult.getMessage());
    }

    Map<String, Object> user = Apis.invoke("accountService", "getUserByUsernameOrEmail", username);

    if (MapUtils.isEmpty(user)) {
        throw new ServiceException("?[" + username + "]");
    }

    sendForgetPasswordMail(user);

    return user;
}

From source file:org.jasig.portlet.newsreader.adapter.RomeAdapter.java

protected String[] getPolicyPref(String... policyKey) {
    String[] values = new String[policyKey.length];
    try {//ww  w . jav  a 2s.com
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        if (requestAttributes instanceof PortletRequestAttributes) {
            PortletRequest request = ((PortletRequestAttributes) requestAttributes).getRequest();
            PortletPreferences prefs = request.getPreferences();
            List<String> valueList = new ArrayList<String>();
            for (String key : policyKey) {
                valueList.add(prefs.getValue(key, DEFAULT_ANTISAMY_POLICY));
            }
            return valueList.toArray(values);
        }
    } catch (IllegalStateException ex) {
        log.warn("Call to RomeAdapter.getPolicyPref that was not within a request", ex);
    }
    /* In other words, if we're not in a PortletRequest you get
     * the DEFAULT_ANTISAMY_POLICY for every value requested.
     */
    Arrays.fill(values, DEFAULT_ANTISAMY_POLICY);
    return values;
}

From source file:com.cws.us.pws.controllers.ProductController.java

@RequestMapping(value = "/product/{product}", method = RequestMethod.GET)
public final ModelAndView getProductInfo(@PathVariable("product") final String product) {
    final String methodName = ProductController.CNAME
            + "#getProductInfo(@PathVariable(\"product\") final String product)";

    if (DEBUG) {//from  w w  w  .ja  v  a  2 s.co  m
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("Product: {}", product);
    }

    ModelAndView mView = new ModelAndView();

    final ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
            .currentRequestAttributes();
    final HttpServletRequest hRequest = requestAttributes.getRequest();
    final HttpSession hSession = hRequest.getSession();
    final String lang = hRequest.getParameter(Constants.PARAMETER_LANG);

    if (DEBUG) {
        DEBUGGER.debug("ServletRequestAttributes: {}", requestAttributes);
        DEBUGGER.debug("HttpServletRequest: {}", hRequest);
        DEBUGGER.debug("HttpSession: {}", hSession);
        DEBUGGER.debug("lang: {}", lang);

        DEBUGGER.debug("Dumping session content:");
        @SuppressWarnings("unchecked")
        Enumeration<String> sessionEnumeration = hSession.getAttributeNames();

        while (sessionEnumeration.hasMoreElements()) {
            String sessionElement = sessionEnumeration.nextElement();
            Object sessionValue = hSession.getAttribute(sessionElement);

            DEBUGGER.debug("Attribute: " + sessionElement + "; Value: " + sessionValue);
        }

        DEBUGGER.debug("Dumping request content:");
        @SuppressWarnings("unchecked")
        Enumeration<String> requestEnumeration = hRequest.getAttributeNames();

        while (requestEnumeration.hasMoreElements()) {
            String requestElement = requestEnumeration.nextElement();
            Object requestValue = hRequest.getAttribute(requestElement);

            DEBUGGER.debug("Attribute: " + requestElement + "; Value: " + requestValue);
        }

        DEBUGGER.debug("Dumping request parameters:");
        @SuppressWarnings("unchecked")
        Enumeration<String> paramsEnumeration = hRequest.getParameterNames();

        while (paramsEnumeration.hasMoreElements()) {
            String requestElement = paramsEnumeration.nextElement();
            Object requestValue = hRequest.getParameter(requestElement);

            DEBUGGER.debug("Parameter: " + requestElement + "; Value: " + requestValue);
        }
    }

    try {
        Product reqProduct = new Product();
        reqProduct.setProductId(product);

        if (DEBUG) {
            DEBUGGER.debug("Product: {}", reqProduct);
        }

        ProductRequest productRequest = new ProductRequest();
        productRequest.setProduct(reqProduct);
        productRequest.setLang((StringUtils.isBlank(lang)) ? "en" : lang);

        if (DEBUG) {
            DEBUGGER.debug("ProductRequest: {}", productRequest);
        }

        ProductResponse productResponse = this.productRefSvc.getProductData(productRequest);

        if (DEBUG) {
            DEBUGGER.debug("ProductResponse: {}", productResponse);
        }

        if (productResponse.getRequestStatus() == CoreServicesStatus.SUCCESS) {
            Product resProduct = productResponse.getProduct();

            if (DEBUG) {
                DEBUGGER.debug("Product: {}", resProduct);
            }

            mView.addObject("product", resProduct);
            mView.setViewName(this.showProduct);
        } else {
            mView = new ModelAndView(new RedirectView());
            mView.addObject(Constants.ERROR_MESSAGE, productResponse.getResponse());
            mView.setViewName(this.defaultPage);
        }
    } catch (ProductRequestException prx) {
        ERROR_RECORDER.error(prx.getMessage(), prx);

        mView.setViewName(this.appConfig.getErrorResponsePage());
    }

    if (DEBUG) {
        DEBUGGER.debug("ModelAndView: {}", mView);
    }

    return mView;
}

From source file:com.bosch.iot.things.example.historian.Controller.java

/**
 * Check access on specific property by doing a callback to the Things service.
 *//*from  www .j  a  va2  s.c o m*/
private boolean checkAccess(String thingId, String feature, String path)
        throws UnsupportedEncodingException, IOException {
    HttpServletRequest httpReq = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
            .getRequest();
    HttpServletResponse httpRes = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
            .getResponse();

    // enforce BASIC auth
    String auth = httpReq.getHeader("Authorization");
    if (auth == null) {
        httpRes.setHeader("WWW-Authenticate", "BASIC realm=\"Proxy for Bosch IoT Things\"");
        httpRes.sendError(HttpServletResponse.SC_UNAUTHORIZED);
        return false;
    }

    String httpid = URLEncoder.encode(thingId, "UTF-8") + "/features/" + URLEncoder.encode(feature, "UTF-8")
            + "/properties/" + path;
    HttpGet thingsRequest = new HttpGet(
            getConfig().getProperty("thingsServiceEndpointUrl") + "/cr/1/things/" + httpid);

    // fill in apiToken if not provided
    String apiToken = getConfig().getProperty("apiToken");
    if (apiToken != null && httpReq.getHeader("x-cr-api-token") == null) {
        thingsRequest.addHeader("x-cr-api-token", apiToken);
    }

    // forward all other Headers to Things service
    Enumeration<String> headerNames = httpReq.getHeaderNames();
    if (headerNames != null) {
        final Set<String> headersToIgnore = new HashSet(Arrays.asList(new String[] { "host" }));
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            if (!headersToIgnore.contains(name)) {
                thingsRequest.addHeader(name, httpReq.getHeader(name));
            }
        }
    }

    LOGGER.debug("Callback to Things service: {}", thingsRequest);

    try (CloseableHttpResponse response = getHttpClient().execute(thingsRequest)) {
        LOGGER.debug("... retured {}", response.getStatusLine());

        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode < 200 || statusCode > 299) {
            httpRes.setStatus(statusCode);
            return false;
        }
    }

    return true;
}

From source file:com.cws.us.pws.controllers.CommonController.java

@RequestMapping(value = "/sitemap", method = RequestMethod.GET)
public final ModelAndView showSiteMap() {
    final String methodName = CommonController.CNAME + "#showSiteMap()";

    if (DEBUG) {/*from w  w  w .  j  ava2 s  .c o m*/
        DEBUGGER.debug(methodName);
    }

    ModelAndView mView = new ModelAndView();

    final ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
            .currentRequestAttributes();
    final HttpServletRequest hRequest = requestAttributes.getRequest();
    final HttpSession hSession = hRequest.getSession();

    if (DEBUG) {
        DEBUGGER.debug("ServletRequestAttributes: {}", requestAttributes);
        DEBUGGER.debug("HttpServletRequest: {}", hRequest);
        DEBUGGER.debug("HttpSession: {}", hSession);

        DEBUGGER.debug("Dumping session content:");
        @SuppressWarnings("unchecked")
        Enumeration<String> sessionEnumeration = hSession.getAttributeNames();

        while (sessionEnumeration.hasMoreElements()) {
            String sessionElement = sessionEnumeration.nextElement();
            Object sessionValue = hSession.getAttribute(sessionElement);

            DEBUGGER.debug("Attribute: " + sessionElement + "; Value: " + sessionValue);
        }

        DEBUGGER.debug("Dumping request content:");
        @SuppressWarnings("unchecked")
        Enumeration<String> requestEnumeration = hRequest.getAttributeNames();

        while (requestEnumeration.hasMoreElements()) {
            String requestElement = requestEnumeration.nextElement();
            Object requestValue = hRequest.getAttribute(requestElement);

            DEBUGGER.debug("Attribute: " + requestElement + "; Value: " + requestValue);
        }

        DEBUGGER.debug("Dumping request parameters:");
        @SuppressWarnings("unchecked")
        Enumeration<String> paramsEnumeration = hRequest.getParameterNames();

        while (paramsEnumeration.hasMoreElements()) {
            String requestElement = paramsEnumeration.nextElement();
            Object requestValue = hRequest.getParameter(requestElement);

            DEBUGGER.debug("Parameter: " + requestElement + "; Value: " + requestValue);
        }
    }

    mView.setViewName(this.appConfig.getSiteMap());

    if (DEBUG) {
        DEBUGGER.debug("ModelAndView: {}", mView);
    }

    return mView;
}

From source file:com.acc.storefront.tags.Functions.java

protected static HttpServletRequest getCurrentRequest() {
    return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
}

From source file:org.mitre.openid.connect.token.TofuUserApprovalHandler.java

/**
 * Get the auth time out of the current session and add it to the
 * auth request in the extensions map./*from  www . j av a 2s  .c  o  m*/
 * 
 * @param authorizationRequest
 */
private void setAuthTime(AuthorizationRequest authorizationRequest) {
    // Get the session auth time, if we have it, and store it in the request
    ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
    if (attr != null) {
        HttpSession session = attr.getRequest().getSession();
        if (session != null) {
            Date authTime = (Date) session.getAttribute(AuthenticationTimeStamper.AUTH_TIMESTAMP);
            if (authTime != null) {
                String authTimeString = Long.toString(authTime.getTime());
                authorizationRequest.getExtensions().put(AuthenticationTimeStamper.AUTH_TIMESTAMP,
                        authTimeString);
            }
        }
    }
}