Java Code Examples for org.springframework.web.context.request.NativeWebRequest#getAttribute()

The following examples show how to use org.springframework.web.context.request.NativeWebRequest#getAttribute() . 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: PathVariableMapMethodArgumentResolver.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Return a Map with all URI template variables or an empty map.
 */
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
		NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {

	@SuppressWarnings("unchecked")
	Map<String, String> uriTemplateVars =
			(Map<String, String>) webRequest.getAttribute(
					HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);

	if (!CollectionUtils.isEmpty(uriTemplateVars)) {
		return new LinkedHashMap<String, String>(uriTemplateVars);
	}
	else {
		return Collections.emptyMap();
	}
}
 
Example 2
Source File: AppGroupArgResolver.java    From secrets-proxy with Apache License 2.0 6 votes vote down vote up
/**
 * Construct new {@link AppGroup} object from the given Web request. The authenticated user is
 * available in the web request principal.
 */
@Override
public AppGroup resolveArgument(
    MethodParameter parameter,
    ModelAndViewContainer mavContainer,
    NativeWebRequest webReq,
    WebDataBinderFactory binderFactory)
    throws Exception {
  Authentication auth = (Authentication) webReq.getUserPrincipal();
  OneOpsUser user = (OneOpsUser) auth.getPrincipal();

  // Get the path variable.
  @SuppressWarnings("unchecked")
  Map<String, String> pathVars =
      (Map<String, String>) webReq.getAttribute(URI_TEMPLATE_VARIABLES_ATTRIBUTE, SCOPE_REQUEST);
  String group = pathVars != null ? pathVars.get(APP_NAME_PARAM) : "";
  return AppGroup.from(user.getDomain(), group);
}
 
Example 3
Source File: PathVariableMapMethodArgumentResolver.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Return a Map with all URI template variables or an empty map.
 */
@Override
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
		NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {

	@SuppressWarnings("unchecked")
	Map<String, String> uriTemplateVars =
			(Map<String, String>) webRequest.getAttribute(
					HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);

	if (!CollectionUtils.isEmpty(uriTemplateVars)) {
		return new LinkedHashMap<>(uriTemplateVars);
	}
	else {
		return Collections.emptyMap();
	}
}
 
Example 4
Source File: PathVariableMethodArgumentResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
@Nullable
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception {
	Map<String, String> uriTemplateVars = (Map<String, String>) request.getAttribute(
			HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
	return (uriTemplateVars != null ? uriTemplateVars.get(name) : null);
}
 
Example 5
Source File: MatrixVariableMapMethodArgumentResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
		NativeWebRequest request, WebDataBinderFactory binderFactory) throws Exception {

	@SuppressWarnings("unchecked")
	Map<String, MultiValueMap<String, String>> matrixVariables =
			(Map<String, MultiValueMap<String, String>>) request.getAttribute(
					HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);

	if (CollectionUtils.isEmpty(matrixVariables)) {
		return Collections.emptyMap();
	}

	MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
	String pathVariable = parameter.getParameterAnnotation(MatrixVariable.class).pathVar();

	if (!pathVariable.equals(ValueConstants.DEFAULT_NONE)) {
		MultiValueMap<String, String> mapForPathVariable = matrixVariables.get(pathVariable);
		if (mapForPathVariable == null) {
			return Collections.emptyMap();
		}
		map.putAll(mapForPathVariable);
	}
	else {
		for (MultiValueMap<String, String> vars : matrixVariables.values()) {
			for (String name : vars.keySet()) {
				for (String value : vars.get(name)) {
					map.add(name, value);
				}
			}
		}
	}

	return (isSingleValueMap(parameter) ? map.toSingleValueMap() : map);
}
 
Example 6
Source File: PathVariableMethodArgumentResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
@Nullable
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception {
	Map<String, String> uriTemplateVars = (Map<String, String>) request.getAttribute(
			HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
	return (uriTemplateVars != null ? uriTemplateVars.get(name) : null);
}
 
Example 7
Source File: BaseMethodArgumentResolver.java    From es with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected final Map<String, String> getUriTemplateVariables(NativeWebRequest request) {
    Map<String, String> variables =
            (Map<String, String>) request.getAttribute(
                    HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
    return (variables != null) ? variables : Collections.<String, String>emptyMap();
}
 
Example 8
Source File: ReactiveTypeHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
private Collection<MediaType> getMediaTypes(NativeWebRequest request)
		throws HttpMediaTypeNotAcceptableException {

	Collection<MediaType> mediaTypes = (Collection<MediaType>) request.getAttribute(
			HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);

	return CollectionUtils.isEmpty(mediaTypes) ?
			this.contentNegotiationManager.resolveMediaTypes(request) : mediaTypes;
}
 
Example 9
Source File: PathVariableMethodArgumentResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception {
	Map<String, String> uriTemplateVars =
		(Map<String, String>) request.getAttribute(
				HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
	return (uriTemplateVars != null) ? uriTemplateVars.get(name) : null;
}
 
Example 10
Source File: PathVariableMethodArgumentResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected void handleResolvedValue(Object arg, String name, MethodParameter parameter,
		ModelAndViewContainer mavContainer, NativeWebRequest request) {

	String key = View.PATH_VARIABLES;
	int scope = RequestAttributes.SCOPE_REQUEST;
	Map<String, Object> pathVars = (Map<String, Object>) request.getAttribute(key, scope);
	if (pathVars == null) {
		pathVars = new HashMap<String, Object>();
		request.setAttribute(key, pathVars, scope);
	}
	pathVars.put(name, arg);
}
 
Example 11
Source File: SmsVerificationClaimsHandlerMethodArgumentResolver.java    From daming with Apache License 2.0 5 votes vote down vote up
@Override
public Object resolveArgument(MethodParameter parameter,
                              ModelAndViewContainer mavContainer,
                              NativeWebRequest webRequest,
                              WebDataBinderFactory binderFactory) throws Exception {
    return webRequest.getAttribute("smsVerificationClaims", SCOPE_REQUEST);
}
 
Example 12
Source File: CurrentUserMethodArgumentResolver.java    From das with Apache License 2.0 5 votes vote down vote up
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
    LoginUser user = (LoginUser) webRequest.getAttribute("currentUser", RequestAttributes.SCOPE_SESSION);
    if (user == null) {
        return LoginUser.builder()
                .id(UNKNOWNID)
                .userNo(UNKNOWN)
                .userName(UNKNOWN)
                .userEmail(UNKNOWN)
                .build();
    }
    return user;
    //throw new MissingServletRequestPartException("currentUser");
}
 
Example 13
Source File: PathVariableMethodArgumentResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception {
	Map<String, String> uriTemplateVars = (Map<String, String>) request.getAttribute(
			HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
	return (uriTemplateVars != null ? uriTemplateVars.get(name) : null);
}
 
Example 14
Source File: MatrixVariableMapMethodArgumentResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
@Nullable
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
		NativeWebRequest request, @Nullable WebDataBinderFactory binderFactory) throws Exception {

	@SuppressWarnings("unchecked")
	Map<String, MultiValueMap<String, String>> matrixVariables =
			(Map<String, MultiValueMap<String, String>>) request.getAttribute(
					HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);

	if (CollectionUtils.isEmpty(matrixVariables)) {
		return Collections.emptyMap();
	}

	MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
	MatrixVariable ann = parameter.getParameterAnnotation(MatrixVariable.class);
	Assert.state(ann != null, "No MatrixVariable annotation");
	String pathVariable = ann.pathVar();

	if (!pathVariable.equals(ValueConstants.DEFAULT_NONE)) {
		MultiValueMap<String, String> mapForPathVariable = matrixVariables.get(pathVariable);
		if (mapForPathVariable == null) {
			return Collections.emptyMap();
		}
		map.putAll(mapForPathVariable);
	}
	else {
		for (MultiValueMap<String, String> vars : matrixVariables.values()) {
			vars.forEach((name, values) -> {
				for (String value : values) {
					map.add(name, value);
				}
			});
		}
	}

	return (isSingleValueMap(parameter) ? map.toSingleValueMap() : map);
}
 
Example 15
Source File: WebRequestQueryContext.java    From specification-arg-resolver with Apache License 2.0 5 votes vote down vote up
public WebRequestQueryContext(NativeWebRequest request) {
	this.contextMap = (HashMap<String, Object>) request.getAttribute(ATTRIBUTE_KEY, NativeWebRequest.SCOPE_REQUEST);
	if (this.contextMap == null) {
		this.contextMap = new HashMap<>();
		request.setAttribute(ATTRIBUTE_KEY, contextMap, NativeWebRequest.SCOPE_REQUEST);
	}
}
 
Example 16
Source File: HyenaExceptionHandler.java    From hyena with Apache License 2.0 5 votes vote down vote up
private String getSeq(NativeWebRequest request) {
    try {
        return (String) request.getAttribute(HyenaConstants.REQ_IDEMPOTENT_SEQ_KEY, 0);
    } catch (Exception e) {
        return "";
    }
}
 
Example 17
Source File: CurrentUserMethodArgumentResolver.java    From EasyReport with Apache License 2.0 4 votes vote down vote up
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
                              NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
    CurrentUser currentUserAnnotation = parameter.getParameterAnnotation(CurrentUser.class);
    return webRequest.getAttribute(currentUserAnnotation.value(), NativeWebRequest.SCOPE_REQUEST);
}
 
Example 18
Source File: ServletModelAttributeMethodProcessor.java    From java-technology-stack with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
protected final Map<String, String> getUriTemplateVariables(NativeWebRequest request) {
	Map<String, String> variables = (Map<String, String>) request.getAttribute(
			HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
	return (variables != null ? variables : Collections.emptyMap());
}
 
Example 19
Source File: ServletModelAttributeMethodProcessor.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
protected final Map<String, String> getUriTemplateVariables(NativeWebRequest request) {
	Map<String, String> variables = (Map<String, String>) request.getAttribute(
			HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
	return (variables != null ? variables : Collections.emptyMap());
}
 
Example 20
Source File: CustomArgumentResolver.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
	NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
	RequestAttribute attr = parameter.getParameterAnnotation(RequestAttribute.class);
	return webRequest.getAttribute(attr.value(), WebRequest.SCOPE_REQUEST);
}