org.springframework.web.method.support.ModelAndViewContainer Java Examples

The following examples show how to use org.springframework.web.method.support.ModelAndViewContainer. 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: ClientAppUserAccessTokenArgumentResolver.java    From kaif with Apache License 2.0 6 votes vote down vote up
@Override
public ClientAppUserAccessToken resolveArgument(MethodParameter parameter,
    ModelAndViewContainer mavContainer,
    NativeWebRequest webRequest,
    WebDataBinderFactory binderFactory) throws Exception {
  String tokenStr = Strings.trimToEmpty(webRequest.getHeader(HttpHeaders.AUTHORIZATION));
  if (!tokenStr.startsWith("Bearer ")) {
    throw new MissingBearerTokenException();
  }
  String token = tokenStr.substring("Bearer ".length(), tokenStr.length()).trim();
  Optional<ClientAppUserAccessToken> accessToken = clientAppService.verifyAccessToken(token);
  if (!accessToken.isPresent()) {
    throw new InvalidTokenException();
  }
  RequiredScope requiredScope = parameter.getMethodAnnotation(RequiredScope.class);
  if (!accessToken.get().containsScope(requiredScope.value())) {
    throw new InsufficientScopeException(requiredScope.value());
  }
  return accessToken.get();
}
 
Example #2
Source File: ClientResolver.java    From nakadi with MIT License 6 votes vote down vote up
@Override
public Client resolveArgument(final MethodParameter parameter,
                              final ModelAndViewContainer mavContainer,
                              final NativeWebRequest request,
                              final WebDataBinderFactory binderFactory) {

    final String clientId = authorizationService.getSubject().map(Subject::getName)
            .orElse(SecuritySettings.UNAUTHENTICATED_CLIENT_ID);

    if (settings.getAuthMode() == SecuritySettings.AuthMode.OFF) {
        return new FullAccessClient(clientId);
    } else {
        if (clientId.equals(settings.getAdminClientId())) {
            return new FullAccessClient(FULL_ACCESS_CLIENT_ID);
        } else if (!authorizationService.getSubject().isPresent()) {
            throw new UnauthorizedUserException("Client unauthorized");
        } else {
            return new NakadiClient(clientId, getRealm());
        }
    }
}
 
Example #3
Source File: AsyncResponseEntityReturnHandler.java    From searchbox-core with Apache License 2.0 6 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

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

    final AsyncResponseEntity<?> asyncResponseEntity = AsyncResponseEntity.class.cast(returnValue);
    
    Observable<?> observable = asyncResponseEntity.getObservable();
    Single<?> single = asyncResponseEntity.getSingle();
    MultiValueMap<String, String> headers =  asyncResponseEntity.getHeaders();
    HttpStatus status = asyncResponseEntity.getStatus();
    
    if(observable != null)
    	WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(new ObservableDeferredResult<>(observable, headers, status), mavContainer);
    else if(single != null)
    	WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(new SingleDeferredResult<>(single, headers, status), mavContainer);
    
}
 
Example #4
Source File: RaptorHandlerMethodProcessor.java    From raptor with Apache License 2.0 6 votes vote down vote up
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
    ServletServerHttpRequest inputMessage = createInputMessage(webRequest);
    HttpMethod httpMethod = inputMessage.getMethod();
    switch (httpMethod) {
        case GET:
        case HEAD:
            return bindData(parameter, mavContainer, webRequest, binderFactory);
        case POST:
        case PUT:
        case PATCH:
        case DELETE:
        case OPTIONS:
            return convertBody(parameter, mavContainer, webRequest, binderFactory);
        default:
            return null;
    }
}
 
Example #5
Source File: RequestMappingHandlerAdapter.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Nullable
private ModelAndView getModelAndView(ModelAndViewContainer mavContainer,
		ModelFactory modelFactory, NativeWebRequest webRequest) throws Exception {

	modelFactory.updateModel(webRequest, mavContainer);
	if (mavContainer.isRequestHandled()) {
		return null;
	}
	ModelMap model = mavContainer.getModel();
	ModelAndView mav = new ModelAndView(mavContainer.getViewName(), model, mavContainer.getStatus());
	if (!mavContainer.isViewReference()) {
		mav.setView((View) mavContainer.getView());
	}
	if (model instanceof RedirectAttributes) {
		Map<String, ?> flashAttributes = ((RedirectAttributes) model).getFlashAttributes();
		HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
		if (request != null) {
			RequestContextUtils.getOutputFlashMap(request).putAll(flashAttributes);
		}
	}
	return mav;
}
 
Example #6
Source File: ModelMethodProcessor.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

	if (returnValue == null) {
		return;
	}
	else if (returnValue instanceof Model) {
		mavContainer.addAllAttributes(((Model) returnValue).asMap());
	}
	else {
		// should not happen
		throw new UnsupportedOperationException("Unexpected return type: " +
				returnType.getParameterType().getName() + " in method: " + returnType.getMethod());
	}
}
 
Example #7
Source File: RequestResponseBodyMethodProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {

	Method method = getClass().getDeclaredMethod("handle", List.class,
			SimpleBean.class, MultiValueMap.class, String.class);

	paramGenericList = new MethodParameter(method, 0);
	paramSimpleBean = new MethodParameter(method, 1);
	paramMultiValueMap = new MethodParameter(method, 2);
	paramString = new MethodParameter(method, 3);
	returnTypeString = new MethodParameter(method, -1);

	mavContainer = new ModelAndViewContainer();

	servletRequest = new MockHttpServletRequest();
	servletRequest.setMethod("POST");
	servletResponse = new MockHttpServletResponse();
	webRequest = new ServletWebRequest(servletRequest, servletResponse);

	this.binderFactory = new ValidatingBinderFactory();
}
 
Example #8
Source File: AbstractWebArgumentResolverAdapter.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Delegate to the {@link WebArgumentResolver} instance.
 * @throws IllegalStateException if the resolved value is not assignable
 * to the method parameter.
 */
@Override
@Nullable
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
		NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {

	Class<?> paramType = parameter.getParameterType();
	Object result = this.adaptee.resolveArgument(parameter, webRequest);
	if (result == WebArgumentResolver.UNRESOLVED || !ClassUtils.isAssignableValue(paramType, result)) {
		throw new IllegalStateException(
				"Standard argument type [" + paramType.getName() + "] in method " + parameter.getMethod() +
				"resolved to incompatible value of type [" + (result != null ? result.getClass() : null) +
				"]. Consider declaring the argument type in a less specific fashion.");
	}
	return result;
}
 
Example #9
Source File: ModelFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void updateModelBindingResult() throws Exception {
	String commandName = "attr1";
	Object command = new Object();
	ModelAndViewContainer container = new ModelAndViewContainer();
	container.addAttribute(commandName, command);

	WebDataBinder dataBinder = new WebDataBinder(command, commandName);
	WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
	given(binderFactory.createBinder(this.webRequest, command, commandName)).willReturn(dataBinder);

	ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.attributeHandler);
	modelFactory.updateModel(this.webRequest, container);

	assertEquals(command, container.getModel().get(commandName));
	String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + commandName;
	assertSame(dataBinder.getBindingResult(), container.getModel().get(bindingResultKey));
	assertEquals(2, container.getModel().size());
}
 
Example #10
Source File: ErrorsMethodArgumentResolver.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
		NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {

	ModelMap model = mavContainer.getModel();
	if (model.size() > 0) {
		int lastIndex = model.size()-1;
		String lastKey = new ArrayList<String>(model.keySet()).get(lastIndex);
		if (lastKey.startsWith(BindingResult.MODEL_KEY_PREFIX)) {
			return model.get(lastKey);
		}
	}

	throw new IllegalStateException(
			"An Errors/BindingResult argument is expected to be declared immediately after the model attribute, " +
			"the @RequestBody or the @RequestPart arguments to which they apply: " + parameter.getMethod());
}
 
Example #11
Source File: ModelFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void updateModelSessionAttributesSaved() throws Exception {
	String attributeName = "sessionAttr";
	String attribute = "value";
	ModelAndViewContainer container = new ModelAndViewContainer();
	container.addAttribute(attributeName, attribute);

	WebDataBinder dataBinder = new WebDataBinder(attribute, attributeName);
	WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
	given(binderFactory.createBinder(this.webRequest, attribute, attributeName)).willReturn(dataBinder);

	ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.attributeHandler);
	modelFactory.updateModel(this.webRequest, container);

	assertEquals(attribute, container.getModel().get(attributeName));
	assertEquals(attribute, this.attributeStore.retrieveAttribute(this.webRequest, attributeName));
}
 
Example #12
Source File: AbstractRender.java    From Aooms with Apache License 2.0 6 votes vote down vote up
/**
 * springmvc提供的render方法
 *
 */
public void springMvcRender(Object returnValue){
    try{
        // modelAndViewContainer init
        ModelAndViewContainer modelAndViewContainer = new ModelAndViewContainer();
        ServletWebRequest webRequest = new ServletWebRequest(AoomsContext.getRequest(), AoomsContext.getResponse());

        // get ApplicationContext
        ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(AoomsContext.getServletContext());

        // get RequestMappingHandlerAdapter Bean
        RequestMappingHandlerAdapter requestMappingHandlerAdapter = (RequestMappingHandlerAdapter)ac1.getBean("requestMappingHandlerAdapter");

        // init handlerMethodReturnValueHandlerComposite
        HandlerMethodReturnValueHandlerComposite handlerMethodReturnValueHandlerComposite = new HandlerMethodReturnValueHandlerComposite();
        handlerMethodReturnValueHandlerComposite.addHandlers(requestMappingHandlerAdapter.getReturnValueHandlers());

        // render
        HandlerMethod method = AoomsContext.getMethodHandler();
        handlerMethodReturnValueHandlerComposite.handleReturnValue(returnValue, method.getReturnType(), modelAndViewContainer, webRequest);

    } catch (Exception ex){
        throw AoomsExceptions.create("springmvc render error",ex);
    }
}
 
Example #13
Source File: RequestResponseBodyMethodProcessorMockTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
	messageConverter = mock(HttpMessageConverter.class);
	given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));

	processor = new RequestResponseBodyMethodProcessor(Collections.<HttpMessageConverter<?>>singletonList(messageConverter));

	Method methodHandle1 = getClass().getMethod("handle1", String.class, Integer.TYPE);
	paramRequestBodyString = new MethodParameter(methodHandle1, 0);
	paramInt = new MethodParameter(methodHandle1, 1);
	returnTypeString = new MethodParameter(methodHandle1, -1);
	returnTypeInt = new MethodParameter(getClass().getMethod("handle2"), -1);
	returnTypeStringProduces = new MethodParameter(getClass().getMethod("handle3"), -1);
	paramValidBean = new MethodParameter(getClass().getMethod("handle4", SimpleBean.class), 0);
	paramStringNotRequired = new MethodParameter(getClass().getMethod("handle5", String.class), 0);

	mavContainer = new ModelAndViewContainer();

	servletRequest = new MockHttpServletRequest();
	servletRequest.setMethod("POST");
	webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse());
}
 
Example #14
Source File: ModelFactory.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Populate the model in the following order:
 * <ol>
 * 	<li>Retrieve "known" session attributes listed as {@code @SessionAttributes}.
 * 	<li>Invoke {@code @ModelAttribute} methods
 * 	<li>Find {@code @ModelAttribute} method arguments also listed as
 * 	{@code @SessionAttributes} and ensure they're present in the model raising
 * 	an exception if necessary.
 * </ol>
 * @param request the current request
 * @param mavContainer a container with the model to be initialized
 * @param handlerMethod the method for which the model is initialized
 * @throws Exception may arise from {@code @ModelAttribute} methods
 */
public void initModel(NativeWebRequest request, ModelAndViewContainer mavContainer, HandlerMethod handlerMethod)
		throws Exception {

	Map<String, ?> sessionAttributes = this.sessionAttributesHandler.retrieveAttributes(request);
	mavContainer.mergeAttributes(sessionAttributes);

	invokeModelAttributeMethods(request, mavContainer);

	for (String name : findSessionAttributeArguments(handlerMethod)) {
		if (!mavContainer.containsAttribute(name)) {
			Object value = this.sessionAttributesHandler.retrieveAttribute(request, name);
			if (value == null) {
				throw new HttpSessionRequiredException("Expected session attribute '" + name + "'");
			}
			mavContainer.addAttribute(name, value);
		}
	}
}
 
Example #15
Source File: ViewMethodReturnValueHandler.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

	if (returnValue instanceof View){
		View view = (View) returnValue;
		mavContainer.setView(view);
		if (view instanceof SmartView && ((SmartView) view).isRedirectView()) {
			mavContainer.setRedirectModelScenario(true);
		}
	}
	else if (returnValue != null) {
		// should not happen
		throw new UnsupportedOperationException("Unexpected return type: " +
				returnType.getParameterType().getName() + " in method: " + returnType.getMethod());
	}
}
 
Example #16
Source File: ModelFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void updateModelSessionAttributesRemoved() throws Exception {
	String attributeName = "sessionAttr";
	String attribute = "value";
	ModelAndViewContainer container = new ModelAndViewContainer();
	container.addAttribute(attributeName, attribute);

	// Store and resolve once (to be "remembered")
	this.sessionAttributeStore.storeAttribute(this.webRequest, attributeName, attribute);
	this.sessionAttrsHandler.isHandlerSessionAttribute(attributeName, null);

	WebDataBinder dataBinder = new WebDataBinder(attribute, attributeName);
	WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
	given(binderFactory.createBinder(this.webRequest, attribute, attributeName)).willReturn(dataBinder);

	container.getSessionStatus().setComplete();

	ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.sessionAttrsHandler);
	modelFactory.updateModel(this.webRequest, container);

	assertEquals(attribute, container.getModel().get(attributeName));
	assertNull(this.sessionAttributeStore.retrieveAttribute(this.webRequest, attributeName));
}
 
Example #17
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 #18
Source File: ModelFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void updateModelBindingResult() throws Exception {
	String commandName = "attr1";
	Object command = new Object();
	ModelAndViewContainer container = new ModelAndViewContainer();
	container.addAttribute(commandName, command);

	WebDataBinder dataBinder = new WebDataBinder(command, commandName);
	WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
	given(binderFactory.createBinder(this.webRequest, command, commandName)).willReturn(dataBinder);

	ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.attributeHandler);
	modelFactory.updateModel(this.webRequest, container);

	assertEquals(command, container.getModel().get(commandName));
	String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + commandName;
	assertSame(dataBinder.getBindingResult(), container.getModel().get(bindingResultKey));
	assertEquals(2, container.getModel().size());
}
 
Example #19
Source File: GuavaLFReturnValueHandlerTest.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void handlesSuccess() throws Exception {
    final AtomicReference<Object> value = new AtomicReference<>();

    ListenableFuture<String> future = Futures.immediateFuture("42");

    GuavaLFReturnValueHandler handler = new GuavaLFReturnValueHandler() {
        @Override
        protected void startDeferredResultProcessing(ModelAndViewContainer mavContainer, NativeWebRequest webRequest, DeferredResult<Object> deferredResult) throws Exception {
            value.set(deferredResult.getResult());
        }
    };

    handler.handleReturnValue(future, null, null, null);
    assertThat(value.get()).isEqualTo("42");
}
 
Example #20
Source File: RequestResponseBodyMethodProcessor.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Throws MethodArgumentNotValidException if validation fails.
 * @throws HttpMessageNotReadableException if {@link RequestBody#required()}
 * is {@code true} and there is no body content or if there is no suitable
 * converter to read the content with.
 */
@Override
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
		NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {

	parameter = parameter.nestedIfOptional();
	Object arg = readWithMessageConverters(webRequest, parameter, parameter.getNestedGenericParameterType());
	String name = Conventions.getVariableNameForParameter(parameter);

	if (binderFactory != null) {
		WebDataBinder binder = binderFactory.createBinder(webRequest, arg, name);
		if (arg != null) {
			validateIfApplicable(binder, parameter);
			if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
				throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());
			}
		}
		if (mavContainer != null) {
			mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());
		}
	}

	return adaptArgumentIfNecessary(arg, parameter);
}
 
Example #21
Source File: GitRepositoryArgumentResolver.java    From code-examples with MIT License 6 votes vote down vote up
@Override
public Object resolveArgument(
    MethodParameter parameter,
    ModelAndViewContainer mavContainer,
    NativeWebRequest webRequest,
    WebDataBinderFactory binderFactory) {

  String requestPath = ((ServletWebRequest) webRequest).getRequest().getPathInfo();

  String slug = requestPath
      .substring(0, requestPath.indexOf("/", 1))
      .replaceAll("^/", "");

  return gitRepositoryFinder.findBySlug(slug)
      .orElseThrow(NotFoundException::new);
}
 
Example #22
Source File: ModelFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void updateModelSessionAttributesSaved() throws Exception {
	String attributeName = "sessionAttr";
	String attribute = "value";
	ModelAndViewContainer container = new ModelAndViewContainer();
	container.addAttribute(attributeName, attribute);

	WebDataBinder dataBinder = new WebDataBinder(attribute, attributeName);
	WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
	given(binderFactory.createBinder(this.webRequest, attribute, attributeName)).willReturn(dataBinder);

	ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.sessionAttrsHandler);
	modelFactory.updateModel(this.webRequest, container);

	assertEquals(attribute, container.getModel().get(attributeName));
	assertEquals(attribute, this.sessionAttributeStore.retrieveAttribute(this.webRequest, attributeName));
}
 
Example #23
Source File: MatrixVariablesMethodArgumentResolverTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
	this.resolver = new MatrixVariableMethodArgumentResolver();

	Method method = getClass().getMethod("handle", String.class, List.class, int.class);
	this.paramString = new MethodParameter(method, 0);
	this.paramColors = new MethodParameter(method, 1);
	this.paramYear = new MethodParameter(method, 2);

	this.paramColors.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());

	this.mavContainer = new ModelAndViewContainer();
	this.request = new MockHttpServletRequest();
	this.webRequest = new ServletWebRequest(request, new MockHttpServletResponse());

	Map<String, MultiValueMap<String, String>> params = new LinkedHashMap<String, MultiValueMap<String, String>>();
	this.request.setAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE, params);
}
 
Example #24
Source File: ServletModelAttributeMethodProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
	this.processor = new ServletModelAttributeMethodProcessor(false);

	Method method = getClass().getDeclaredMethod("modelAttribute",
			TestBean.class, TestBeanWithoutStringConstructor.class, Optional.class);

	this.testBeanModelAttr = new MethodParameter(method, 0);
	this.testBeanWithoutStringConstructorModelAttr = new MethodParameter(method, 1);
	this.testBeanWithOptionalModelAttr = new MethodParameter(method, 2);

	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultConversionService());

	this.binderFactory = new ServletRequestDataBinderFactory(null, initializer);
	this.mavContainer = new ModelAndViewContainer();

	this.request = new MockHttpServletRequest();
	this.webRequest = new ServletWebRequest(request);
}
 
Example #25
Source File: ServletModelAttributeMethodProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Before
public void setup() throws Exception {
	processor = new ServletModelAttributeMethodProcessor(false);

	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultConversionService());
	binderFactory = new ServletRequestDataBinderFactory(null, initializer);

	mavContainer = new ModelAndViewContainer();
	request = new MockHttpServletRequest();
	webRequest = new ServletWebRequest(request);

	Method method = getClass().getDeclaredMethod("modelAttribute",
			TestBean.class, TestBeanWithoutStringConstructor.class, Optional.class);
	testBeanModelAttr = new MethodParameter(method, 0);
	testBeanWithoutStringConstructorModelAttr = new MethodParameter(method, 1);
	testBeanWithOptionalModelAttr = new MethodParameter(method, 2);
}
 
Example #26
Source File: PropertiesHandlerMethodArgumentResolver.java    From SpringAll with MIT License 6 votes vote down vote up
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
    ServletWebRequest servletWebRequest = (ServletWebRequest) webRequest;
    HttpServletRequest request = servletWebRequest.getRequest();
    String contentType = request.getHeader("Content-Type");

    MediaType mediaType = MediaType.parseMediaType(contentType);
    // 获取编码
    Charset charset = mediaType.getCharset() == null ? Charset.forName("UTF-8") : mediaType.getCharset();
    // 获取输入流
    InputStream inputStream = request.getInputStream();
    InputStreamReader inputStreamReader = new InputStreamReader(inputStream, charset);

    // 输入流转换为 Properties
    Properties properties = new Properties();
    properties.load(inputStreamReader);
    return properties;
}
 
Example #27
Source File: ViewNameMethodReturnValueHandler.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

	if (returnValue instanceof CharSequence) {
		String viewName = returnValue.toString();
		mavContainer.setViewName(viewName);
		if (isRedirectViewName(viewName)) {
			mavContainer.setRedirectModelScenario(true);
		}
	}
	else if (returnValue != null){
		// should not happen
		throw new UnsupportedOperationException("Unexpected return type: " +
				returnType.getParameterType().getName() + " in method: " + returnType.getMethod());
	}
}
 
Example #28
Source File: ModelFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Promote model attributes listed as {@code @SessionAttributes} to the session.
 * Add {@link BindingResult} attributes where necessary.
 * @param request the current request
 * @param mavContainer contains the model to update
 * @throws Exception if creating BindingResult attributes fails
 */
public void updateModel(NativeWebRequest request, ModelAndViewContainer mavContainer) throws Exception {
	ModelMap defaultModel = mavContainer.getDefaultModel();
	if (mavContainer.getSessionStatus().isComplete()){
		this.sessionAttributesHandler.cleanupAttributes(request);
	}
	else {
		this.sessionAttributesHandler.storeAttributes(request, defaultModel);
	}
	if (!mavContainer.isRequestHandled() && mavContainer.getModel() == defaultModel) {
		updateBindingResult(request, defaultModel);
	}
}
 
Example #29
Source File: StreamingResponseBodyReturnValueHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {
	this.handler = new StreamingResponseBodyReturnValueHandler();
	this.mavContainer = new ModelAndViewContainer();

	this.request = new MockHttpServletRequest("GET", "/path");
	this.response = new MockHttpServletResponse();
	this.webRequest = new ServletWebRequest(this.request, this.response);

	AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
	WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncWebRequest(asyncWebRequest);
	this.request.setAsyncSupported(true);
}
 
Example #30
Source File: SessionStatusMethodArgumentResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
		NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {

	Assert.state(mavContainer != null, "ModelAndViewContainer is required for session status exposure");
	return mavContainer.getSessionStatus();
}