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:net.groupbuy.controller.admin.BaseController.java

/**
 * ??/*from www  .  j  a  v a 2 s  . c om*/
 * 
 * @param target
 *            ?
 * @param groups
 *            ?
 * @return ?
 */
protected boolean isValid(Object target, Class<?>... groups) {
    Set<ConstraintViolation<Object>> constraintViolations = validator.validate(target, groups);
    if (constraintViolations.isEmpty()) {
        return true;
    } else {
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        requestAttributes.setAttribute(CONSTRAINT_VIOLATIONS_ATTRIBUTE_NAME, constraintViolations,
                RequestAttributes.SCOPE_REQUEST);
        return false;
    }
}

From source file:io.jmnarloch.spring.boot.hystrix.Demo.java

@Test
public void shouldPropagateRequestAttributes() {

    // given/*from  w  w w . ja v  a  2  s  . c o  m*/
    RequestContextHolder.currentRequestAttributes().setAttribute(REQUEST_ID, requestId, SCOPE_REQUEST);

    // when
    final Object result = new HystrixCommand<Object>(commandGroup("TestGroup")) {
        @Override
        protected Object run() throws Exception {
            return RequestContextHolder.currentRequestAttributes().getAttribute(REQUEST_ID, SCOPE_REQUEST);
        }
    }.execute();

    // then
    assertEquals(requestId, result);
}

From source file:com.xpeppers.phonedirectory.controller.HomeController.java

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

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

@RequestMapping(value = "/default", method = RequestMethod.GET)
public final ModelAndView showDefaultPage() {
    final String methodName = ServicesController.CNAME + "#showDefaultPage()";

    if (DEBUG) {/*from w w  w  .j  a  va 2s.  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.defaultPage);

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

    return mView;
}

From source file:uk.ac.ebi.intact.editor.util.HybridSessionThreadScope.java

public void registerDestructionCallback(String name, Runnable callback) {
    if (RequestContextHolder.getRequestAttributes() != null) {
        RequestAttributes attributes = RequestContextHolder.currentRequestAttributes();
        attributes.registerDestructionCallback(name, callback, RequestAttributes.SCOPE_SESSION);
    }/*from   w ww. j a v  a  2  s. c  om*/
}

From source file:de.uni_koeln.spinfo.maalr.login.UserInfoBackend.java

/**
 * Returns the user infos of the user currently logged in. If no user infos
 * exist for this user, a new user will be stored. <br>
 * <strong>Security:</strong> Any user may call this method.
 * //w  ww  .  ja v  a  2s  .  c o m
 * @return
 */
// @Secured( { Constants.Roles.GUEST_1, Constants.Roles.OPENID_2,
// Constants.Roles.TRUSTED_EXTERNAL_3, Constants.Roles.TRUSTED_INTERNAL_4,
// Constants.Roles.ADMIN_5 })
public MaalrUserInfo getOrCreateCurrentUser() {

    UserInfoDB userInfos = new UserInfoDB();
    // String name = SecurityContextHolder.getContext().getAuthentication().getName();
    String name = null;
    if (SecurityContextHolder.getContext().getAuthentication().isAuthenticated()) {
        name = SecurityContextHolder.getContext().getAuthentication().getName();
    } else {
        name = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest()
                .getRemoteAddr();
    }
    try {
        return userInfos.getOrCreate(name);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.ngrinder.common.util.HttpContainerContext.java

/**
 * Check the user has unix user agent.//  ww w  . ja v a 2s .  com
 *
 * @return true if unix.
 */
public boolean isUnixUser() {
    SecurityContextHolderAwareRequestWrapper request = cast(
            RequestContextHolder.currentRequestAttributes().resolveReference("request"));
    return !StringUtils.containsIgnoreCase(request.getHeader("User-Agent"), "Win");
}

From source file:it.f2informatica.webapp.utils.HttpRequest.java

private RequestAttributes currentRequestAttributes() {
    return RequestContextHolder.currentRequestAttributes();
}

From source file:org.cloudfoundry.identity.uaa.util.UaaHttpRequestUtils.java

public static boolean isAcceptedInvitationAuthentication() {
    try {/*from  w w  w  .j a  v a 2 s . c o  m*/
        RequestAttributes attr = RequestContextHolder.currentRequestAttributes();
        if (attr != null) {
            Boolean result = (Boolean) attr.getAttribute("IS_INVITE_ACCEPTANCE",
                    RequestAttributes.SCOPE_SESSION);
            if (result != null) {
                return result.booleanValue();
            }
        }
    } catch (IllegalStateException x) {
        //nothing bound on thread.
        logger.debug("Unable to retrieve request attributes looking for invitation.");

    }
    return false;
}