Java Code Examples for org.springframework.web.context.request.RequestAttributes#setAttribute()

The following examples show how to use org.springframework.web.context.request.RequestAttributes#setAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PostBackManager.java    From sinavi-jfw with Apache License 2.0 6 votes vote down vote up
/**
 * <p>
 * 現在のリクエストに対してポストバック機構を開始します。
 * </p>
 * @param request リクエスト
 * @param handlerMethod ハンドラ
 */
public static void begin(HttpServletRequest request, HandlerMethod handlerMethod) {
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    PostBackManager instance = new PostBackManager(request , handlerMethod);
    requestAttributes.setAttribute(STORE_KEY_TO_REQUEST, instance, RequestAttributes.SCOPE_REQUEST);
    MessageContext messageContext = (MessageContext) requestAttributes.getAttribute(MessageContext.MESSAGE_CONTEXT_ATTRIBUTE_KEY, RequestAttributes.SCOPE_REQUEST);
    if (messageContext == null) {
        requestAttributes.setAttribute(MessageContext.MESSAGE_CONTEXT_ATTRIBUTE_KEY, new MessageContext(request), RequestAttributes.SCOPE_REQUEST);
    }
    instance.targetControllerType = handlerMethod.getBeanType();
    for (MethodParameter methodParameter : handlerMethod.getMethodParameters()) {
        ModelAttribute attr = methodParameter.getParameterAnnotation(ModelAttribute.class);
        if (attr != null) {
            instance.modelAttributeType = methodParameter.getParameterType();
        }
    }
}
 
Example 2
Source File: PostBackManager.java    From sinavi-jfw with Apache License 2.0 6 votes vote down vote up
/**
 * {@link PostBack} インスタンスを保存します。
 * @param postBack {@link PostBack} インスタンス
 */
public static void save(PostBack postBack) {
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    DispatchType dispatchType = getDispatchType(postBack.getException());
    switch (dispatchType) {
    case JSP:
        requestAttributes.setAttribute(PostBack.POST_BACK_ATTRIBUTE_KEY, postBack, RequestAttributes.SCOPE_REQUEST);
        break;
    case FORWARD:
        requestAttributes.setAttribute(PostBack.POST_BACK_ATTRIBUTE_KEY, postBack, RequestAttributes.SCOPE_REQUEST);
        break;
    case REDIRECT:
        PostBackManager instance = (PostBackManager) requestAttributes.getAttribute(STORE_KEY_TO_REQUEST, RequestAttributes.SCOPE_REQUEST);
        FlashMap flashMap = RequestContextUtils.getOutputFlashMap(instance.request);
        flashMap.put(PostBack.POST_BACK_ATTRIBUTE_KEY, postBack);
        break;
    default:
        throw new InternalException(PostBackManager.class, "E-POSTBACK#0001");
    }
}
 
Example 3
Source File: LogonControllerBasic.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
private String complete(final ComAdmin admin, final String webStorageJson, final Popups popups) {
    final ComAdminPreferences preferences = logonService.getPreferences(admin);
    final RequestAttributes attributes = RequestContextHolder.getRequestAttributes();

    attributes.setAttribute(AgnUtils.SESSION_CONTEXT_KEYNAME_ADMIN, admin, RequestAttributes.SCOPE_SESSION);
    attributes.setAttribute(AgnUtils.SESSION_CONTEXT_KEYNAME_ADMINPREFERENCES, preferences, RequestAttributes.SCOPE_SESSION);
    attributes.setAttribute(Globals.LOCALE_KEY, admin.getLocale(), RequestAttributes.SCOPE_SESSION);  // To be removed when Struts message tags are not in use anymore.
    attributes.setAttribute("emmLayoutBase", logonService.getEmmLayoutBase(admin), RequestAttributes.SCOPE_SESSION);
    attributes.setAttribute("helplanguage", logonService.getHelpLanguage(admin), RequestAttributes.SCOPE_SESSION);

    attributes.setAttribute("userName", StringUtils.defaultString(admin.getUsername()), RequestAttributes.SCOPE_SESSION);
    attributes.setAttribute("firstName", StringUtils.defaultString(admin.getFirstName()), RequestAttributes.SCOPE_SESSION);
    attributes.setAttribute("fullName", admin.getFullname(), RequestAttributes.SCOPE_SESSION);
    attributes.setAttribute("companyShortName", admin.getCompany().getShortname(), RequestAttributes.SCOPE_SESSION);
    attributes.setAttribute("companyID", admin.getCompany().getId(), RequestAttributes.SCOPE_SESSION);
    attributes.setAttribute("adminTimezone", admin.getAdminTimezone(), RequestAttributes.SCOPE_SESSION);

    // Setup web-storage using client's data represented as JSON.
    webStorage.setup(webStorageJson);

    // Skip last successful login, because that's the current login.
    final int times = loginTrackService.countFailedLoginsSinceLastSuccess(admin.getUsername(), true);
    if (times > 0) {
        if (times > 1) {
            popups.alert("warning.failed_logins.more", times);
        } else {
            popups.alert("warning.failed_logins.1", times);
        }
    }
    
    return getStartPageRedirection(admin, preferences);
}
 
Example 4
Source File: WxWebUtils.java    From FastBootWeixin with Apache License 2.0 5 votes vote down vote up
/**
 * 同上面方法,不过request从RequestContextHolder中取
 * @param wxRequest
 */
public static void setWxRequestToRequest(WxRequest wxRequest) {
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    if (requestAttributes != null) {
        requestAttributes.setAttribute(WX_REQUEST_ATTRIBUTE, wxRequest, RequestAttributes.SCOPE_REQUEST);
    }
}
 
Example 5
Source File: AuthEnvs.java    From onetwo with Apache License 2.0 5 votes vote down vote up
public static void setCurrent(AuthEnv env) {
	if (env==null) {
		removeCurrent();
		return ;
	}
	RequestAttributes req = RequestContextHolder.getRequestAttributes();
	if (req!=null) {
		req.setAttribute(AUTH_ENV_KEY, env, RequestAttributes.SCOPE_REQUEST);
	} else {
		CURRENT_ENVS.set(env);
	}
}
 
Example 6
Source File: CatnapViewResolverTest.java    From catnap with Apache License 2.0 5 votes vote down vote up
@Test
public void resolveViewByAcceptHeader() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Accept", "application/json");

    RequestAttributes requestAttributes = new ServletRequestAttributes(request);
    requestAttributes.setAttribute(RequestAttributes.REFERENCE_REQUEST, request, RequestAttributes.SCOPE_REQUEST);
    RequestContextHolder.setRequestAttributes(requestAttributes);

    CatnapWrappingView view = (CatnapWrappingView) viewResolver.resolveViewName("/view", Locale.US);
    assertTrue(view.getWrappedView() instanceof JsonView);
}
 
Example 7
Source File: CatnapViewResolverTest.java    From catnap with Apache License 2.0 5 votes vote down vote up
@Test
public void resolveViewByInvalidHrefSuffixReturnsNull() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    RequestAttributes requestAttributes = new ServletRequestAttributes(request);
    requestAttributes.setAttribute(RequestAttributes.REFERENCE_REQUEST, request, RequestAttributes.SCOPE_REQUEST);
    RequestContextHolder.setRequestAttributes(requestAttributes);

    assertNull(viewResolver.resolveViewName("/view.xml", Locale.US));
}
 
Example 8
Source File: CatnapViewResolverTest.java    From catnap with Apache License 2.0 5 votes vote down vote up
@Test
public void resolveViewByInvalidAcceptHeaderReturnsNull() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Accept", "application/xml");

    RequestAttributes requestAttributes = new ServletRequestAttributes(request);
    requestAttributes.setAttribute(RequestAttributes.REFERENCE_REQUEST, request, RequestAttributes.SCOPE_REQUEST);
    RequestContextHolder.setRequestAttributes(requestAttributes);

    assertNull(viewResolver.resolveViewName("/view", Locale.US));
}
 
Example 9
Source File: CatnapViewResolverTest.java    From catnap with Apache License 2.0 5 votes vote down vote up
@Test
public void noConfiguredViewsReturnsNull() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    RequestAttributes requestAttributes = new ServletRequestAttributes(request);
    requestAttributes.setAttribute(RequestAttributes.REFERENCE_REQUEST, request, RequestAttributes.SCOPE_REQUEST);
    RequestContextHolder.setRequestAttributes(requestAttributes);

    assertNull(new CatnapViewResolver().resolveViewName("/view.json", Locale.US));
}
 
Example 10
Source File: WxWebUtils.java    From FastBootWeixin with Apache License 2.0 4 votes vote down vote up
public static void setWxWebUserToSession(WxWebUser wxWebUser) {
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    if (requestAttributes != null) {
        requestAttributes.setAttribute(WX_SESSION_USER, wxWebUser, RequestAttributes.SCOPE_SESSION);
    }
}
 
Example 11
Source File: PostBackManager.java    From sinavi-jfw with Apache License 2.0 4 votes vote down vote up
/**
 * {@link PostBack}インスタンスをリクエスト・スコープに保存します。
 * @param postBack {@link PostBack}インスタンス
 */
public static void saveToRequest(PostBack postBack) {
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    requestAttributes.setAttribute(PostBack.POST_BACK_ATTRIBUTE_KEY, postBack, RequestAttributes.SCOPE_REQUEST);
}
 
Example 12
Source File: PostBackManager.java    From sinavi-jfw with Apache License 2.0 4 votes vote down vote up
/**
 * {@link BindingResult}インスタンスをリクエスト・スコープに保存します。
 * @param bindingResult {@link BindingResult}インスタンス
 */
public static void saveBindingResult(BindingResult bindingResult) {
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    requestAttributes.setAttribute(BINDING_RESULT_KEY, bindingResult, RequestAttributes.SCOPE_REQUEST);
}