Refine search
@Test public void requestContextListenerWithSameThread() { RequestContextListener listener = new RequestContextListener(); MockServletContext context = new MockServletContext(); MockHttpServletRequest request = new MockHttpServletRequest(context); request.setAttribute("test", "value"); assertNull(RequestContextHolder.getRequestAttributes()); listener.requestInitialized(new ServletRequestEvent(context, request)); assertNotNull(RequestContextHolder.getRequestAttributes()); assertEquals("value", RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST)); MockRunnable runnable = new MockRunnable(); RequestContextHolder.getRequestAttributes().registerDestructionCallback( "test", runnable, RequestAttributes.SCOPE_REQUEST); listener.requestDestroyed(new ServletRequestEvent(context, request)); assertNull(RequestContextHolder.getRequestAttributes()); assertTrue(runnable.wasExecuted()); }
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { RequestContextHolder.getRequestAttributes().setAttribute(FROM_REQUEST_ATTRIBUTES_FILTER, FROM_REQUEST_ATTRIBUTES_FILTER, RequestAttributes.SCOPE_REQUEST); chain.doFilter(request, response); } }
@Override @Nullable public Object remove(String name) { RequestAttributes attributes = RequestContextHolder.currentRequestAttributes(); Object scopedObject = attributes.getAttribute(name, getScope()); if (scopedObject != null) { attributes.removeAttribute(name, getScope()); return scopedObject; } else { return null; } }
@Override public Object get(String name, ObjectFactory<?> objectFactory) { RequestAttributes attributes = RequestContextHolder.currentRequestAttributes(); Object scopedObject = attributes.getAttribute(name, getScope()); if (scopedObject == null) { scopedObject = objectFactory.getObject(); attributes.setAttribute(name, scopedObject, getScope()); // Retrieve object again, registering it for implicit session attribute updates. // As a bonus, we also allow for potential decoration at the getAttribute level. Object retrievedObject = attributes.getAttribute(name, getScope()); if (retrievedObject != null) { // Only proceed with retrieved object if still present (the expected case). // If it disappeared concurrently, we return our locally created instance. scopedObject = retrievedObject; } } return scopedObject; }
public static WxRequest getWxRequestFromRequest() { RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); if (requestAttributes != null) { return (WxRequest) requestAttributes.getAttribute(WX_REQUEST_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST); } return null; }
final RequestAttributes attributes = RequestContextHolder.getRequestAttributes(); if (attributes != null) { attributes.removeAttribute(this.attributeName, RequestAttributes.SCOPE_REQUEST); RequestContextHolder.currentRequestAttributes().registerDestructionCallback(this.attributeName, this, RequestAttributes.SCOPE_REQUEST);
@Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { RequestContextHolder.getRequestAttributes().setAttribute("exception", ex, SCOPE_REQUEST); Long startTime = (Long) RequestContextHolder.getRequestAttributes().getAttribute( TIMING_REQUEST_ATTRIBUTE, SCOPE_REQUEST); if (startTime != null) recordMetric(request, response, handler, startTime); super.afterCompletion(request, response, handler, ex); }
/** * Attaches assertion from current session to the {@link SecureRequest}. * @param request * @return {@link SecureRequest} with attached assertion */ protected SecureRequest attachId(SecureRequest request) { String sessionId = RequestContextHolder.getRequestAttributes()!=null? RequestContextHolder.getRequestAttributes().getSessionId():null; if (sessionId != null) { request.addHeaders(new HeaderField(HeaderFieldTypes.TYPE_SESSION_ID, sessionId)); } return request; }
public static boolean isAcceptedInvitationAuthentication() { try { 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; } }
@Override @Nullable public Object resolveContextualObject(String key) { RequestAttributes attributes = RequestContextHolder.currentRequestAttributes(); return attributes.resolveReference(key); }
public void configureRelayRedirect(String relayState) { //configure relay state if (UaaUrlUtils.isUrl(relayState)) { RequestContextHolder.currentRequestAttributes() .setAttribute( UaaSavedRequestAwareAuthenticationSuccessHandler.URI_OVERRIDE_ATTRIBUTE, relayState, RequestAttributes.SCOPE_REQUEST ); } }
@Override public String getConversationId() { return RequestContextHolder.currentRequestAttributes().getSessionId(); }
private ScimUser getInvitedUser() { ScimUser invitedUser = new ScimUser(null, "marissa.invited@test.org", "Marissa", "Bloggs"); invitedUser.setPassword("a"); invitedUser.setVerified(false); invitedUser.setPrimaryEmail("marissa.invited@test.org"); invitedUser.setOrigin(OriginKeys.UAA); ScimUser scimUser = userProvisioning.create(invitedUser, IdentityZoneHolder.get().getId()); RequestAttributes attributes = new ServletRequestAttributes(new MockHttpServletRequest()); attributes.setAttribute("IS_INVITE_ACCEPTANCE", true, RequestAttributes.SCOPE_SESSION); attributes.setAttribute("user_id", scimUser.getId(), RequestAttributes.SCOPE_SESSION); RequestContextHolder.setRequestAttributes(attributes); return scimUser; }
/** * Notify about leaving this context. * @param webAppContext The current web application context. * @param handle The returned handle from {@link #enteringContext(WebApplicationContext)}. */ public static void leavingContext(WebApplicationContext webAppContext, Object handle) { if (!(handle instanceof ContextInfo)) { throw new IllegalArgumentException("Handle must be an instance of ContextInfo and not " + handle); } final ContextInfo info = (ContextInfo) handle; // get request attributes final RequestAttributes attributes = RequestContextHolder.currentRequestAttributes(); // restore class loader and previous web application context Thread.currentThread().setContextClassLoader(info.classLoader); if (info.webAppContext == null) { attributes.removeAttribute(CONTAINER_REQUEST_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST); } else { attributes.setAttribute(CONTAINER_REQUEST_ATTRIBUTE, info.webAppContext, RequestAttributes.SCOPE_REQUEST); } }
@SuppressWarnings("unchecked") private <T> T getAttribute(RequestAttributes requestAttributes, String name) { return (T) requestAttributes.getAttribute(name, RequestAttributes.SCOPE_REQUEST); }
@Override public void clearDefaultLocationForCurrentUser() { _currentUserService.clearDefaultLocation(); RequestAttributes attributes = RequestContextHolder.currentRequestAttributes(); attributes.removeAttribute(KEY_DEFAULT_SEARCH_LOCATION_FOR_SESSSION, RequestAttributes.SCOPE_SESSION); } }
RequestAttributes a = RequestContextHolder.currentRequestAttributes(); String name = ScopedProxyUtils.getTargetBeanName("...your bean name...") synchronized (a.getSessionMutex()) { Object o = a.getAttribute(name, RequestAttributes.SCOPE_SESSION); a.setAttribute(name, o, RequestAttributes.SCOPE_SESSION); }
RequestAttributes attributes = RequestContextHolder.currentRequestAttributes(); attributes.getAttribute("some key", NativeWebRequest.SCOPE_SESSION); attributes.setAttribute("some key", YouObject, NativeWebRequest.SCOPE_SESSION);
@Nullable private View getBestView(List<View> candidateViews, List<MediaType> requestedMediaTypes, RequestAttributes attrs) { for (View candidateView : candidateViews) { if (candidateView instanceof SmartView) { SmartView smartView = (SmartView) candidateView; if (smartView.isRedirectView()) { return candidateView; } } } for (MediaType mediaType : requestedMediaTypes) { for (View candidateView : candidateViews) { if (StringUtils.hasText(candidateView.getContentType())) { MediaType candidateContentType = MediaType.parseMediaType(candidateView.getContentType()); if (mediaType.isCompatibleWith(candidateContentType)) { if (logger.isDebugEnabled()) { logger.debug("Selected '" + mediaType + "' given " + requestedMediaTypes); } attrs.setAttribute(View.SELECTED_CONTENT_TYPE, mediaType, RequestAttributes.SCOPE_REQUEST); return candidateView; } } } } return null; }
public void removeAttribute(RequestAttributes request, String name) { request.removeAttribute(name, RequestAttributes.SCOPE_SESSION); } }