Java Code Examples for org.springframework.core.MethodParameter#nested()

The following examples show how to use org.springframework.core.MethodParameter#nested() . 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: MappingJackson2MessageConverter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private JavaType getJavaType(Class<?> targetClass, @Nullable Object conversionHint) {
	if (conversionHint instanceof MethodParameter) {
		MethodParameter param = (MethodParameter) conversionHint;
		param = param.nestedIfOptional();
		if (Message.class.isAssignableFrom(param.getParameterType())) {
			param = param.nested();
		}
		Type genericParameterType = param.getNestedGenericParameterType();
		Class<?> contextClass = param.getContainingClass();
		Type type = GenericTypeResolver.resolveType(genericParameterType, contextClass);
		return this.objectMapper.getTypeFactory().constructType(type);
	}
	return this.objectMapper.constructType(targetClass);
}
 
Example 2
Source File: HandlerMethodArgumentResolverSupport.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Evaluate the {@code Predicate} on the method parameter type if it has the
 * given annotation, nesting within {@link java.util.Optional} if necessary,
 * but raise an {@code IllegalStateException} if the same matches the generic
 * type within a reactive type wrapper.
 */
protected <A extends Annotation> boolean checkAnnotatedParamNoReactiveWrapper(
		MethodParameter parameter, Class<A> annotationType, BiPredicate<A, Class<?>> typePredicate) {

	A annotation = parameter.getParameterAnnotation(annotationType);
	if (annotation == null) {
		return false;
	}

	parameter = parameter.nestedIfOptional();
	Class<?> type = parameter.getNestedParameterType();

	ReactiveAdapter adapter = getAdapterRegistry().getAdapter(type);
	if (adapter != null) {
		assertHasValues(adapter, parameter);
		parameter = parameter.nested();
		type = parameter.getNestedParameterType();
	}

	if (typePredicate.test(annotation, type)) {
		if (adapter == null) {
			return true;
		}
		throw buildReactiveWrapperException(parameter);
	}

	return false;
}
 
Example 3
Source File: MappingJackson2MessageConverter.java    From java-technology-stack with MIT License 5 votes vote down vote up
private JavaType getJavaType(Class<?> targetClass, @Nullable Object conversionHint) {
	if (conversionHint instanceof MethodParameter) {
		MethodParameter param = (MethodParameter) conversionHint;
		param = param.nestedIfOptional();
		if (Message.class.isAssignableFrom(param.getParameterType())) {
			param = param.nested();
		}
		Type genericParameterType = param.getNestedGenericParameterType();
		Class<?> contextClass = param.getContainingClass();
		Type type = GenericTypeResolver.resolveType(genericParameterType, contextClass);
		return this.objectMapper.getTypeFactory().constructType(type);
	}
	return this.objectMapper.constructType(targetClass);
}
 
Example 4
Source File: HandlerMethodArgumentResolverSupport.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Evaluate the {@code Predicate} on the method parameter type if it has the
 * given annotation, nesting within {@link java.util.Optional} if necessary,
 * but raise an {@code IllegalStateException} if the same matches the generic
 * type within a reactive type wrapper.
 */
protected <A extends Annotation> boolean checkAnnotatedParamNoReactiveWrapper(
		MethodParameter parameter, Class<A> annotationType, BiPredicate<A, Class<?>> typePredicate) {

	A annotation = parameter.getParameterAnnotation(annotationType);
	if (annotation == null) {
		return false;
	}

	parameter = parameter.nestedIfOptional();
	Class<?> type = parameter.getNestedParameterType();

	ReactiveAdapter adapter = getAdapterRegistry().getAdapter(type);
	if (adapter != null) {
		assertHasValues(adapter, parameter);
		parameter = parameter.nested();
		type = parameter.getNestedParameterType();
	}

	if (typePredicate.test(annotation, type)) {
		if (adapter == null) {
			return true;
		}
		throw buildReactiveWrapperException(parameter);
	}

	return false;
}
 
Example 5
Source File: ResponseEntityResultHandler.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {

	Mono<?> returnValueMono;
	MethodParameter bodyParameter;
	ReactiveAdapter adapter = getAdapter(result);
	MethodParameter actualParameter = result.getReturnTypeSource();

	if (adapter != null) {
		Assert.isTrue(!adapter.isMultiValue(), "Only a single ResponseEntity supported");
		returnValueMono = Mono.from(adapter.toPublisher(result.getReturnValue()));
		bodyParameter = actualParameter.nested().nested();
	}
	else {
		returnValueMono = Mono.justOrEmpty(result.getReturnValue());
		bodyParameter = actualParameter.nested();
	}

	return returnValueMono.flatMap(returnValue -> {
		HttpEntity<?> httpEntity;
		if (returnValue instanceof HttpEntity) {
			httpEntity = (HttpEntity<?>) returnValue;
		}
		else if (returnValue instanceof HttpHeaders) {
			httpEntity = new ResponseEntity<>((HttpHeaders) returnValue, HttpStatus.OK);
		}
		else {
			throw new IllegalArgumentException(
					"HttpEntity or HttpHeaders expected but got: " + returnValue.getClass());
		}

		if (httpEntity instanceof ResponseEntity) {
			ResponseEntity<?> responseEntity = (ResponseEntity<?>) httpEntity;
			ServerHttpResponse response = exchange.getResponse();
			if (response instanceof AbstractServerHttpResponse) {
				((AbstractServerHttpResponse) response).setStatusCodeValue(responseEntity.getStatusCodeValue());
			}
			else {
				response.setStatusCode(responseEntity.getStatusCode());
			}
		}

		HttpHeaders entityHeaders = httpEntity.getHeaders();
		HttpHeaders responseHeaders = exchange.getResponse().getHeaders();
		if (!entityHeaders.isEmpty()) {
			entityHeaders.entrySet().stream()
					.forEach(entry -> responseHeaders.put(entry.getKey(), entry.getValue()));
		}

		if (httpEntity.getBody() == null || returnValue instanceof HttpHeaders) {
			return exchange.getResponse().setComplete();
		}

		String etag = entityHeaders.getETag();
		Instant lastModified = Instant.ofEpochMilli(entityHeaders.getLastModified());
		HttpMethod httpMethod = exchange.getRequest().getMethod();
		if (SAFE_METHODS.contains(httpMethod) && exchange.checkNotModified(etag, lastModified)) {
			return exchange.getResponse().setComplete();
		}

		return writeBody(httpEntity.getBody(), bodyParameter, actualParameter, exchange);
	});
}
 
Example 6
Source File: ResponseBodyEmitterReturnValueHandler.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
@SuppressWarnings("resource")
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

	if (returnValue == null) {
		mavContainer.setRequestHandled(true);
		return;
	}

	HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
	Assert.state(response != null, "No HttpServletResponse");
	ServerHttpResponse outputMessage = new ServletServerHttpResponse(response);

	if (returnValue instanceof ResponseEntity) {
		ResponseEntity<?> responseEntity = (ResponseEntity<?>) returnValue;
		response.setStatus(responseEntity.getStatusCodeValue());
		outputMessage.getHeaders().putAll(responseEntity.getHeaders());
		returnValue = responseEntity.getBody();
		returnType = returnType.nested();
		if (returnValue == null) {
			mavContainer.setRequestHandled(true);
			outputMessage.flush();
			return;
		}
	}

	ServletRequest request = webRequest.getNativeRequest(ServletRequest.class);
	Assert.state(request != null, "No ServletRequest");

	ResponseBodyEmitter emitter;
	if (returnValue instanceof ResponseBodyEmitter) {
		emitter = (ResponseBodyEmitter) returnValue;
	}
	else {
		emitter = this.reactiveHandler.handleValue(returnValue, returnType, mavContainer, webRequest);
		if (emitter == null) {
			// Not streaming: write headers without committing response..
			outputMessage.getHeaders().forEach((headerName, headerValues) -> {
				for (String headerValue : headerValues) {
					response.addHeader(headerName, headerValue);
				}
			});
			return;
		}
	}
	emitter.extendResponse(outputMessage);

	// At this point we know we're streaming..
	ShallowEtagHeaderFilter.disableContentCaching(request);

	// Wrap the response to ignore further header changes
	// Headers will be flushed at the first write
	outputMessage = new StreamingServletServerHttpResponse(outputMessage);

	DeferredResult<?> deferredResult = new DeferredResult<>(emitter.getTimeout());
	WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(deferredResult, mavContainer);

	HttpMessageConvertingHandler handler = new HttpMessageConvertingHandler(outputMessage, deferredResult);
	emitter.initialize(handler);
}
 
Example 7
Source File: ResponseEntityResultHandler.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {

	Mono<?> returnValueMono;
	MethodParameter bodyParameter;
	ReactiveAdapter adapter = getAdapter(result);
	MethodParameter actualParameter = result.getReturnTypeSource();

	if (adapter != null) {
		Assert.isTrue(!adapter.isMultiValue(), "Only a single ResponseEntity supported");
		returnValueMono = Mono.from(adapter.toPublisher(result.getReturnValue()));
		bodyParameter = actualParameter.nested().nested();
	}
	else {
		returnValueMono = Mono.justOrEmpty(result.getReturnValue());
		bodyParameter = actualParameter.nested();
	}

	return returnValueMono.flatMap(returnValue -> {
		HttpEntity<?> httpEntity;
		if (returnValue instanceof HttpEntity) {
			httpEntity = (HttpEntity<?>) returnValue;
		}
		else if (returnValue instanceof HttpHeaders) {
			httpEntity = new ResponseEntity<>((HttpHeaders) returnValue, HttpStatus.OK);
		}
		else {
			throw new IllegalArgumentException(
					"HttpEntity or HttpHeaders expected but got: " + returnValue.getClass());
		}

		if (httpEntity instanceof ResponseEntity) {
			ResponseEntity<?> responseEntity = (ResponseEntity<?>) httpEntity;
			ServerHttpResponse response = exchange.getResponse();
			if (response instanceof AbstractServerHttpResponse) {
				((AbstractServerHttpResponse) response).setStatusCodeValue(responseEntity.getStatusCodeValue());
			}
			else {
				response.setStatusCode(responseEntity.getStatusCode());
			}
		}

		HttpHeaders entityHeaders = httpEntity.getHeaders();
		HttpHeaders responseHeaders = exchange.getResponse().getHeaders();
		if (!entityHeaders.isEmpty()) {
			entityHeaders.entrySet().stream()
					.forEach(entry -> responseHeaders.put(entry.getKey(), entry.getValue()));
		}

		if (httpEntity.getBody() == null || returnValue instanceof HttpHeaders) {
			return exchange.getResponse().setComplete();
		}

		String etag = entityHeaders.getETag();
		Instant lastModified = Instant.ofEpochMilli(entityHeaders.getLastModified());
		HttpMethod httpMethod = exchange.getRequest().getMethod();
		if (SAFE_METHODS.contains(httpMethod) && exchange.checkNotModified(etag, lastModified)) {
			return exchange.getResponse().setComplete();
		}

		return writeBody(httpEntity.getBody(), bodyParameter, actualParameter, exchange);
	});
}
 
Example 8
Source File: ResponseBodyEmitterReturnValueHandler.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
@SuppressWarnings("resource")
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

	if (returnValue == null) {
		mavContainer.setRequestHandled(true);
		return;
	}

	HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
	Assert.state(response != null, "No HttpServletResponse");
	ServerHttpResponse outputMessage = new ServletServerHttpResponse(response);

	if (returnValue instanceof ResponseEntity) {
		ResponseEntity<?> responseEntity = (ResponseEntity<?>) returnValue;
		response.setStatus(responseEntity.getStatusCodeValue());
		outputMessage.getHeaders().putAll(responseEntity.getHeaders());
		returnValue = responseEntity.getBody();
		returnType = returnType.nested();
		if (returnValue == null) {
			mavContainer.setRequestHandled(true);
			outputMessage.flush();
			return;
		}
	}

	ServletRequest request = webRequest.getNativeRequest(ServletRequest.class);
	Assert.state(request != null, "No ServletRequest");

	ResponseBodyEmitter emitter;
	if (returnValue instanceof ResponseBodyEmitter) {
		emitter = (ResponseBodyEmitter) returnValue;
	}
	else {
		emitter = this.reactiveHandler.handleValue(returnValue, returnType, mavContainer, webRequest);
		if (emitter == null) {
			// Not streaming: write headers without committing response..
			outputMessage.getHeaders().forEach((headerName, headerValues) -> {
				for (String headerValue : headerValues) {
					response.addHeader(headerName, headerValue);
				}
			});
			return;
		}
	}
	emitter.extendResponse(outputMessage);

	// At this point we know we're streaming..
	ShallowEtagHeaderFilter.disableContentCaching(request);

	// Commit the response and wrap to ignore further header changes
	outputMessage.getBody();
	outputMessage.flush();
	outputMessage = new StreamingServletServerHttpResponse(outputMessage);

	DeferredResult<?> deferredResult = new DeferredResult<>(emitter.getTimeout());
	WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(deferredResult, mavContainer);

	HttpMessageConvertingHandler handler = new HttpMessageConvertingHandler(outputMessage, deferredResult);
	emitter.initialize(handler);
}