org.springframework.web.context.request.RequestAttributes类的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(9.9k)|赞(0)|评价(0)|浏览(518)

本文整理了Java中org.springframework.web.context.request.RequestAttributes类的一些代码示例,展示了RequestAttributes类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。RequestAttributes类的具体详情如下:
包路径:org.springframework.web.context.request.RequestAttributes
类名称:RequestAttributes

RequestAttributes介绍

[英]Abstraction for accessing attribute objects associated with a request. Supports access to request-scoped attributes as well as to session-scoped attributes, with the optional notion of a "global session".

Can be implemented for any kind of request/session mechanism, in particular for servlet requests.
[中]用于访问与请求关联的属性对象的抽象。通过可选的“全局会话”概念,支持访问请求范围的属性以及会话范围的属性。
可以实现任何类型的请求/会话机制,尤其是servlet请求。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@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());
}

代码示例来源:origin: spring-projects/spring-framework

@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);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@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;
  }
}

代码示例来源:origin: spring-projects/spring-framework

@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;
}

代码示例来源:origin: FastBootWeixin/FastBootWeixin

public static WxRequest getWxRequestFromRequest() {
  RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
  if (requestAttributes != null) {
    return (WxRequest) requestAttributes.getAttribute(WX_REQUEST_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
  }
  return null;
}

代码示例来源:origin: org.apache.cocoon/cocoon-sitemap-impl

final RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
if (attributes != null) {
  attributes.removeAttribute(this.attributeName, RequestAttributes.SCOPE_REQUEST);
RequestContextHolder.currentRequestAttributes().registerDestructionCallback(this.attributeName, this, RequestAttributes.SCOPE_REQUEST);

代码示例来源:origin: org.springframework.metrics/spring-metrics

@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);
}

代码示例来源:origin: pl.edu.icm.yadda/yadda-aas2-common

/**
 * 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;
}

代码示例来源:origin: cloudfoundry/uaa

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;
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public Object resolveContextualObject(String key) {
  RequestAttributes attributes = RequestContextHolder.currentRequestAttributes();
  return attributes.resolveReference(key);
}

代码示例来源:origin: cloudfoundry/uaa

public void configureRelayRedirect(String relayState) {
  //configure relay state
  if (UaaUrlUtils.isUrl(relayState)) {
    RequestContextHolder.currentRequestAttributes()
      .setAttribute(
        UaaSavedRequestAwareAuthenticationSuccessHandler.URI_OVERRIDE_ATTRIBUTE,
        relayState,
        RequestAttributes.SCOPE_REQUEST
      );
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public String getConversationId() {
  return RequestContextHolder.currentRequestAttributes().getSessionId();
}

代码示例来源:origin: cloudfoundry/uaa

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;
}

代码示例来源:origin: org.apache.cocoon/cocoon-spring-configurator

/**
 * 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);
  }
}

代码示例来源:origin: org.springframework.boot/spring-boot

@SuppressWarnings("unchecked")
private <T> T getAttribute(RequestAttributes requestAttributes, String name) {
  return (T) requestAttributes.getAttribute(name, RequestAttributes.SCOPE_REQUEST);
}

代码示例来源:origin: org.onebusaway/onebusaway-presentation

@Override
 public void clearDefaultLocationForCurrentUser() {

  _currentUserService.clearDefaultLocation();

  RequestAttributes attributes = RequestContextHolder.currentRequestAttributes();
  attributes.removeAttribute(KEY_DEFAULT_SEARCH_LOCATION_FOR_SESSSION,
    RequestAttributes.SCOPE_SESSION);
 }
}

代码示例来源:origin: stackoverflow.com

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);
}

代码示例来源:origin: stackoverflow.com

RequestAttributes attributes = RequestContextHolder.currentRequestAttributes();
attributes.getAttribute("some key", NativeWebRequest.SCOPE_SESSION);
attributes.setAttribute("some key", YouObject, NativeWebRequest.SCOPE_SESSION);

代码示例来源:origin: spring-projects/spring-framework

@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;
}

代码示例来源:origin: org.springframework.social/spring-social-web

public void removeAttribute(RequestAttributes request, String name) {
    request.removeAttribute(name, RequestAttributes.SCOPE_SESSION);
  }
}

相关文章