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

The following examples show how to use org.springframework.web.context.request.NativeWebRequest#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: AbstractNotificationMessageHandlerMethodArgumentResolver.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Object resolveArgument(MethodParameter parameter,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
		WebDataBinderFactory binderFactory) throws Exception {
	Assert.notNull(parameter, "Parameter must not be null");
	if (webRequest.getAttribute(NOTIFICATION_REQUEST_ATTRIBUTE_NAME,
			RequestAttributes.SCOPE_REQUEST) == null) {
		webRequest.setAttribute(NOTIFICATION_REQUEST_ATTRIBUTE_NAME,
				this.messageConverter.read(JsonNode.class,
						createInputMessage(webRequest)),
				RequestAttributes.SCOPE_REQUEST);
	}

	JsonNode content = (JsonNode) webRequest.getAttribute(
			NOTIFICATION_REQUEST_ATTRIBUTE_NAME, RequestAttributes.SCOPE_REQUEST);
	return doResolveArgumentFromNotificationMessage(content,
			createInputMessage(webRequest), parameter.getParameterType());
}
 
Example 2
Source File: PathVariableMethodArgumentResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected void handleResolvedValue(@Nullable Object arg, String name, MethodParameter parameter,
		@Nullable 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<>();
		request.setAttribute(key, pathVars, scope);
	}
	pathVars.put(name, arg);
}
 
Example 3
Source File: PathVariableMethodArgumentResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected void handleResolvedValue(@Nullable Object arg, String name, MethodParameter parameter,
		@Nullable 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<>();
		request.setAttribute(key, pathVars, scope);
	}
	pathVars.put(name, arg);
}
 
Example 4
Source File: JsonArgumentResolver.java    From springboot_security_restful_api with Apache License 2.0 5 votes vote down vote up
private String getRequestBody(NativeWebRequest webRequest) {
    HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);

    String jsonBody = (String) webRequest.getAttribute(JSON_BODY_ATTRIBUTE, NativeWebRequest.SCOPE_REQUEST);
    if (jsonBody == null) {
        try {
            jsonBody = IOUtils.toString(servletRequest.getInputStream(), "UTF-8");
            webRequest.setAttribute(JSON_BODY_ATTRIBUTE, jsonBody, NativeWebRequest.SCOPE_REQUEST);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    return jsonBody;
}
 
Example 5
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 6
Source File: PathVariableMethodArgumentResolver.java    From spring4-understanding with Apache License 2.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 7
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 8
Source File: AdviceTrait.java    From problem-spring-web with MIT License 4 votes vote down vote up
default ResponseEntity<Problem> create(final Throwable throwable, final Problem problem,
        final NativeWebRequest request, final HttpHeaders headers) {

    final HttpStatus status = HttpStatus.valueOf(Optional.ofNullable(problem.getStatus())
            .orElse(Status.INTERNAL_SERVER_ERROR)
            .getStatusCode());

    log(throwable, problem, request, status);

    if (status == HttpStatus.INTERNAL_SERVER_ERROR) {
        request.setAttribute(ERROR_EXCEPTION, throwable, SCOPE_REQUEST);
    }

    return process(negotiate(request).map(contentType ->
            ResponseEntity
                    .status(status)
                    .headers(headers)
                    .contentType(contentType)
                    .body(problem))
            .orElseGet(throwingSupplier(() -> {
                final ResponseEntity<Problem> fallback = fallback(throwable, problem, request, headers);

                if (fallback.getBody() == null) {
                    /*
                     * Ugly hack to workaround an issue with Tomcat and Spring as described in
                     * https://github.com/zalando/problem-spring-web/issues/84.
                     *
                     * The default fallback in case content negotiation failed is a 406 Not Acceptable without
                     * a body. Tomcat will then display its error page since no body was written and the response
                     * was not committed. In order to force Spring to flush/commit one would need to provide a
                     * body but that in turn would fail because Spring would then fail to negotiate the correct
                     * content type.
                     *
                     * Writing the status code, headers and flushing the body manually is a dirty way to bypass
                     * both parties, Tomcat and Spring, at the same time.
                     */
                    final ServerHttpResponse response = new ServletServerHttpResponse(
                            request.getNativeResponse(HttpServletResponse.class));

                    response.setStatusCode(fallback.getStatusCode());
                    response.getHeaders().putAll(fallback.getHeaders());
                    response.getBody(); // just so we're actually flushing the body...
                    response.flush();
                }

                return fallback;
            })), request);
}