org.springframework.core.Conventions Java Examples

The following examples show how to use org.springframework.core.Conventions. 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: AbstractDispatcherServletInitializer.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Add the given filter to the ServletContext and map it to the
 * {@code DispatcherServlet} as follows:
 * <ul>
 * <li>a default filter name is chosen based on its concrete type
 * <li>the {@code asyncSupported} flag is set depending on the
 * return value of {@link #isAsyncSupported() asyncSupported}
 * <li>a filter mapping is created with dispatcher types {@code REQUEST},
 * {@code FORWARD}, {@code INCLUDE}, and conditionally {@code ASYNC} depending
 * on the return value of {@link #isAsyncSupported() asyncSupported}
 * </ul>
 * <p>If the above defaults are not suitable or insufficient, override this
 * method and register filters directly with the {@code ServletContext}.
 * @param servletContext the servlet context to register filters with
 * @param filter the filter to be registered
 * @return the filter registration
 */
protected FilterRegistration.Dynamic registerServletFilter(ServletContext servletContext, Filter filter) {
	String filterName = Conventions.getVariableName(filter);
	Dynamic registration = servletContext.addFilter(filterName, filter);

	if (registration == null) {
		int counter = 0;
		while (registration == null) {
			if (counter == 100) {
				throw new IllegalStateException("Failed to register filter with name '" + filterName + "'. " +
						"Check if there is another filter registered under the same name.");
			}
			registration = servletContext.addFilter(filterName + "#" + counter, filter);
			counter++;
		}
	}

	registration.setAsyncSupported(isAsyncSupported());
	registration.addMappingForServletNames(getDispatcherTypes(), false, getServletName());
	return registration;
}
 
Example #2
Source File: ErrorsMethodArgumentResolver.java    From java-technology-stack with MIT License 6 votes vote down vote up
private Object getErrors(MethodParameter parameter, BindingContext context) {
	Assert.isTrue(parameter.getParameterIndex() > 0,
			"Errors argument must be declared immediately after a model attribute argument");

	int index = parameter.getParameterIndex() - 1;
	MethodParameter attributeParam = MethodParameter.forExecutable(parameter.getExecutable(), index);
	ReactiveAdapter adapter = getAdapterRegistry().getAdapter(attributeParam.getParameterType());

	Assert.state(adapter == null, "An @ModelAttribute and an Errors/BindingResult argument " +
			"cannot both be declared with an async type wrapper. " +
			"Either declare the @ModelAttribute without an async wrapper type or " +
			"handle a WebExchangeBindException error signal through the async type.");

	ModelAttribute ann = parameter.getParameterAnnotation(ModelAttribute.class);
	String name = (ann != null && StringUtils.hasText(ann.value()) ?
			ann.value() : Conventions.getVariableNameForParameter(attributeParam));
	Object errors = context.getModel().asMap().get(BindingResult.MODEL_KEY_PREFIX + name);

	Assert.state(errors != null, () -> "An Errors/BindingResult argument is expected " +
			"immediately after the @ModelAttribute argument to which it applies. " +
			"For @RequestBody and @RequestPart arguments, please declare them with a reactive " +
			"type wrapper and use its onError operators to handle WebExchangeBindException: " +
			parameter.getMethod());

	return errors;
}
 
Example #3
Source File: LocalCachedMapOptionsDecorator.java    From redisson with Apache License 2.0 6 votes vote down vote up
private void invokeTimeUnitOptions(Element element, String id, ParserContext parserContext, RedissonNamespaceParserSupport helper, String timeAttrubute, String timeUnitAttribute) {
    if (helper.hasAttribute(element, timeUnitAttribute)) {
        Assert.state(
                helper.hasAttribute(element, timeAttrubute),
                "Missing \"" + timeAttrubute + "\" attribute in \""
                + RedissonNamespaceParserSupport.LOCAL_CACHED_MAP_OPTIONS_ELEMENT
                + "\" element.");
        helper.invoker(id,
                Conventions.attributeNameToPropertyName(timeAttrubute),
                new Object[]{
                    Integer.parseInt(
                            helper.getAttribute(element, timeAttrubute)),
                    helper.getAttribute(element, timeUnitAttribute)},
                parserContext);
    }
}
 
Example #4
Source File: RedissonGenericObjectDefinitionParser.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Override
protected void parseNested(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, BeanDefinition bd) {
    bd.setFactoryBeanName(element.getAttribute(
            RedissonNamespaceParserSupport.REDISSON_REF_ATTRIBUTE));
    String typeName
            = Conventions.attributeNameToPropertyName(element.getLocalName());
    bd.setFactoryMethodName("get" + StringUtils.capitalize(typeName));
    
    helper.addConstructorArgs(element, KEY_ATTRIBUTE,
            String.class, builder);
    helper.addConstructorArgs(element, TOPIC_ATTRIBUTE,
            String.class, builder);
    helper.addConstructorArgs(element, PATTERN_ATTRIBUTE,
            String.class, builder);
    helper.addConstructorArgs(element, SERVICE_ATTRIBUTE,
            String.class, builder);
    helper.addConstructorArgs(element, CODEC_REF_ATTRIBUTE,
            Codec.class, builder);
    if (RDestroyable.class.isAssignableFrom(getBeanClass(element))) {
        ((AbstractBeanDefinition) bd).setDestroyMethodName("destroy");
    }
}
 
Example #5
Source File: SimplePropertyNamespaceHandler.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) {
	if (node instanceof Attr) {
		Attr attr = (Attr) node;
		String propertyName = parserContext.getDelegate().getLocalName(attr);
		String propertyValue = attr.getValue();
		MutablePropertyValues pvs = definition.getBeanDefinition().getPropertyValues();
		if (pvs.contains(propertyName)) {
			parserContext.getReaderContext().error("Property '" + propertyName + "' is already defined using " +
					"both <property> and inline syntax. Only one approach may be used per property.", attr);
		}
		if (propertyName.endsWith(REF_SUFFIX)) {
			propertyName = propertyName.substring(0, propertyName.length() - REF_SUFFIX.length());
			pvs.add(Conventions.attributeNameToPropertyName(propertyName), new RuntimeBeanReference(propertyValue));
		}
		else {
			pvs.add(Conventions.attributeNameToPropertyName(propertyName), propertyValue);
		}
	}
	return definition;
}
 
Example #6
Source File: RequestResponseBodyMethodProcessor.java    From lams with GNU General Public License v2.0 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, ModelAndViewContainer mavContainer,
		NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {

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

	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());
		}
	}
	mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());

	return adaptArgumentIfNecessary(arg, parameter);
}
 
Example #7
Source File: RedissonDefinitionParser.java    From redisson with Apache License 2.0 6 votes vote down vote up
private void parseChildElements(Element element, String parentId, String redissonRef, BeanDefinitionBuilder redissonDef, ParserContext parserContext) {
    if (element.hasChildNodes()) {
        CompositeComponentDefinition compositeDef
                = new CompositeComponentDefinition(parentId,
                        parserContext.extractSource(element));
        parserContext.pushContainingComponent(compositeDef);
        List<Element> childElts = DomUtils.getChildElements(element);
        for (Element elt : childElts) {
            if (BeanDefinitionParserDelegate.QUALIFIER_ELEMENT.equals(elt.getLocalName())) {
                continue; //parsed elsewhere
            }
            String localName = parserContext.getDelegate().getLocalName(elt);
            localName = Conventions.attributeNameToPropertyName(localName);
            if (ConfigType.contains(localName)) {
                parseConfigTypes(elt, localName, redissonDef, parserContext);
            } else if (AddressType.contains(localName)) {
                parseAddressTypes(elt, localName, redissonDef, parserContext);
            } else if (helper.isRedissonNS(elt)) {
                elt.setAttribute(REDISSON_REF, redissonRef);
                parserContext.getDelegate().parseCustomElement(elt);
            }
        }
        parserContext.popContainingComponent();
    }
}
 
Example #8
Source File: RedissonNamespaceParserSupport.java    From redisson with Apache License 2.0 6 votes vote down vote up
public void parseAttributes(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
    NamedNodeMap attributes = element.getAttributes();
    for (int x = 0; x < attributes.getLength(); x++) {
        Attr attribute = (Attr) attributes.item(x);
        if (isEligibleAttribute(attribute)) {
            String propertyName = attribute.getLocalName();
            if (propertyName.endsWith(REF_SUFFIX)) {
                propertyName = propertyName.substring(0, attribute.getLocalName().length() - REF_SUFFIX.length());
            }
            propertyName = Conventions
                    .attributeNameToPropertyName(propertyName);
            Assert.state(StringUtils.hasText(propertyName),
                    "Illegal property name returned from"
                            + " 'extractPropertyName(String)': cannot be"
                            + " null or empty.");
            if (attribute.getLocalName().endsWith(REF_SUFFIX)) {
                builder.addPropertyReference(propertyName,
                        attribute.getValue());
            } else {
                builder.addPropertyValue(propertyName, attribute.getValue());
            }
        }
    }
}
 
Example #9
Source File: AbstractDispatcherServletInitializer.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Add the given filter to the ServletContext and map it to the
 * {@code DispatcherServlet} as follows:
 * <ul>
 * <li>a default filter name is chosen based on its concrete type
 * <li>the {@code asyncSupported} flag is set depending on the
 * return value of {@link #isAsyncSupported() asyncSupported}
 * <li>a filter mapping is created with dispatcher types {@code REQUEST},
 * {@code FORWARD}, {@code INCLUDE}, and conditionally {@code ASYNC} depending
 * on the return value of {@link #isAsyncSupported() asyncSupported}
 * </ul>
 * <p>If the above defaults are not suitable or insufficient, override this
 * method and register filters directly with the {@code ServletContext}.
 * @param servletContext the servlet context to register filters with
 * @param filter the filter to be registered
 * @return the filter registration
 */
protected FilterRegistration.Dynamic registerServletFilter(ServletContext servletContext, Filter filter) {
	String filterName = Conventions.getVariableName(filter);
	Dynamic registration = servletContext.addFilter(filterName, filter);
	if (registration == null) {
		int counter = -1;
		while (counter == -1 || registration == null) {
			counter++;
			registration = servletContext.addFilter(filterName + "#" + counter, filter);
			Assert.isTrue(counter < 100,
					"Failed to register filter '" + filter + "'." +
					"Could the same Filter instance have been registered already?");
		}
	}
	registration.setAsyncSupported(isAsyncSupported());
	registration.addMappingForServletNames(getDispatcherTypes(), false, getServletName());
	return registration;
}
 
Example #10
Source File: SimplePropertyNamespaceHandler.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) {
	if (node instanceof Attr) {
		Attr attr = (Attr) node;
		String propertyName = parserContext.getDelegate().getLocalName(attr);
		String propertyValue = attr.getValue();
		MutablePropertyValues pvs = definition.getBeanDefinition().getPropertyValues();
		if (pvs.contains(propertyName)) {
			parserContext.getReaderContext().error("Property '" + propertyName + "' is already defined using " +
					"both <property> and inline syntax. Only one approach may be used per property.", attr);
		}
		if (propertyName.endsWith(REF_SUFFIX)) {
			propertyName = propertyName.substring(0, propertyName.length() - REF_SUFFIX.length());
			pvs.add(Conventions.attributeNameToPropertyName(propertyName), new RuntimeBeanReference(propertyValue));
		}
		else {
			pvs.add(Conventions.attributeNameToPropertyName(propertyName), propertyValue);
		}
	}
	return definition;
}
 
Example #11
Source File: RequestResponseBodyMethodProcessor.java    From java-technology-stack 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 #12
Source File: HandlerMethodInvoker.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private WebDataBinder resolveModelAttribute(String attrName, MethodParameter methodParam,
		ExtendedModelMap implicitModel, NativeWebRequest webRequest, Object handler) throws Exception {

	// Bind request parameter onto object...
	String name = attrName;
	if ("".equals(name)) {
		name = Conventions.getVariableNameForParameter(methodParam);
	}
	Class<?> paramType = methodParam.getParameterType();
	Object bindObject;
	if (implicitModel.containsKey(name)) {
		bindObject = implicitModel.get(name);
	}
	else if (this.methodResolver.isSessionAttribute(name, paramType)) {
		bindObject = this.sessionAttributeStore.retrieveAttribute(webRequest, name);
		if (bindObject == null) {
			raiseSessionRequiredException("Session attribute '" + name + "' required - not found in session");
		}
	}
	else {
		bindObject = BeanUtils.instantiateClass(paramType);
	}
	WebDataBinder binder = createBinder(webRequest, bindObject, name);
	initBinder(handler, name, binder, webRequest);
	return binder;
}
 
Example #13
Source File: AbstractDispatcherServletInitializer.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Add the given filter to the ServletContext and map it to the
 * {@code DispatcherServlet} as follows:
 * <ul>
 * <li>a default filter name is chosen based on its concrete type
 * <li>the {@code asyncSupported} flag is set depending on the
 * return value of {@link #isAsyncSupported() asyncSupported}
 * <li>a filter mapping is created with dispatcher types {@code REQUEST},
 * {@code FORWARD}, {@code INCLUDE}, and conditionally {@code ASYNC} depending
 * on the return value of {@link #isAsyncSupported() asyncSupported}
 * </ul>
 * <p>If the above defaults are not suitable or insufficient, override this
 * method and register filters directly with the {@code ServletContext}.
 * @param servletContext the servlet context to register filters with
 * @param filter the filter to be registered
 * @return the filter registration
 */
protected FilterRegistration.Dynamic registerServletFilter(ServletContext servletContext, Filter filter) {
	String filterName = Conventions.getVariableName(filter);
	Dynamic registration = servletContext.addFilter(filterName, filter);

	if (registration == null) {
		int counter = 0;
		while (registration == null) {
			if (counter == 100) {
				throw new IllegalStateException("Failed to register filter with name '" + filterName + "'. " +
						"Check if there is another filter registered under the same name.");
			}
			registration = servletContext.addFilter(filterName + "#" + counter, filter);
			counter++;
		}
	}

	registration.setAsyncSupported(isAsyncSupported());
	registration.addMappingForServletNames(getDispatcherTypes(), false, getServletName());
	return registration;
}
 
Example #14
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 #15
Source File: SimplePropertyNamespaceHandler.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) {
	if (node instanceof Attr) {
		Attr attr = (Attr) node;
		String propertyName = parserContext.getDelegate().getLocalName(attr);
		String propertyValue = attr.getValue();
		MutablePropertyValues pvs = definition.getBeanDefinition().getPropertyValues();
		if (pvs.contains(propertyName)) {
			parserContext.getReaderContext().error("Property '" + propertyName + "' is already defined using " +
					"both <property> and inline syntax. Only one approach may be used per property.", attr);
		}
		if (propertyName.endsWith(REF_SUFFIX)) {
			propertyName = propertyName.substring(0, propertyName.length() - REF_SUFFIX.length());
			pvs.add(Conventions.attributeNameToPropertyName(propertyName), new RuntimeBeanReference(propertyValue));
		}
		else {
			pvs.add(Conventions.attributeNameToPropertyName(propertyName), propertyValue);
		}
	}
	return definition;
}
 
Example #16
Source File: RedissonGenericObjectDefinitionParser.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Override
protected Class<?> getBeanClass(Element element) {
    String elementName
            = Conventions.attributeNameToPropertyName(
                    element.getLocalName());
    try {
        String name = RedissonNamespaceParserSupport.API_CLASS_PATH_PREFIX;
        if (FAIL_LOCK.equals(elementName)) {
            name += StringUtils.capitalize("lock");
        } else {
            name += StringUtils.capitalize(elementName);
        }
        
        return Class.forName(name);
    } catch (ClassNotFoundException ex) {
        throw new IllegalArgumentException(ex);
    }
}
 
Example #17
Source File: ErrorsMethodArgumentResolver.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private Object getErrors(MethodParameter parameter, BindingContext context) {
	Assert.isTrue(parameter.getParameterIndex() > 0,
			"Errors argument must be declared immediately after a model attribute argument");

	int index = parameter.getParameterIndex() - 1;
	MethodParameter attributeParam = MethodParameter.forExecutable(parameter.getExecutable(), index);
	ReactiveAdapter adapter = getAdapterRegistry().getAdapter(attributeParam.getParameterType());

	Assert.state(adapter == null, "An @ModelAttribute and an Errors/BindingResult argument " +
			"cannot both be declared with an async type wrapper. " +
			"Either declare the @ModelAttribute without an async wrapper type or " +
			"handle a WebExchangeBindException error signal through the async type.");

	ModelAttribute ann = parameter.getParameterAnnotation(ModelAttribute.class);
	String name = (ann != null && StringUtils.hasText(ann.value()) ?
			ann.value() : Conventions.getVariableNameForParameter(attributeParam));
	Object errors = context.getModel().asMap().get(BindingResult.MODEL_KEY_PREFIX + name);

	Assert.state(errors != null, () -> "An Errors/BindingResult argument is expected " +
			"immediately after the @ModelAttribute argument to which it applies. " +
			"For @RequestBody and @RequestPart arguments, please declare them with a reactive " +
			"type wrapper and use its onError operators to handle WebExchangeBindException: " +
			parameter.getMethod());

	return errors;
}
 
Example #18
Source File: SimplePropertyNamespaceHandler.java    From blog_demos with Apache License 2.0 6 votes vote down vote up
@Override
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) {
	if (node instanceof Attr) {
		Attr attr = (Attr) node;
		String propertyName = parserContext.getDelegate().getLocalName(attr);
		String propertyValue = attr.getValue();
		MutablePropertyValues pvs = definition.getBeanDefinition().getPropertyValues();
		if (pvs.contains(propertyName)) {
			parserContext.getReaderContext().error("Property '" + propertyName + "' is already defined using " +
					"both <property> and inline syntax. Only one approach may be used per property.", attr);
		}
		if (propertyName.endsWith(REF_SUFFIX)) {
			propertyName = propertyName.substring(0, propertyName.length() - REF_SUFFIX.length());
			pvs.add(Conventions.attributeNameToPropertyName(propertyName), new RuntimeBeanReference(propertyValue));
		}
		else {
			pvs.add(Conventions.attributeNameToPropertyName(propertyName), propertyValue);
		}
	}
	return definition;
}
 
Example #19
Source File: SimplePropertyNamespaceHandler.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) {
	if (node instanceof Attr) {
		Attr attr = (Attr) node;
		String propertyName = parserContext.getDelegate().getLocalName(attr);
		String propertyValue = attr.getValue();
		MutablePropertyValues pvs = definition.getBeanDefinition().getPropertyValues();
		if (pvs.contains(propertyName)) {
			parserContext.getReaderContext().error("Property '" + propertyName + "' is already defined using " +
					"both <property> and inline syntax. Only one approach may be used per property.", attr);
		}
		if (propertyName.endsWith(REF_SUFFIX)) {
			propertyName = propertyName.substring(0, propertyName.length() - REF_SUFFIX.length());
			pvs.add(Conventions.attributeNameToPropertyName(propertyName), new RuntimeBeanReference(propertyValue));
		}
		else {
			pvs.add(Conventions.attributeNameToPropertyName(propertyName), propertyValue);
		}
	}
	return definition;
}
 
Example #20
Source File: RequestResponseBodyMethodProcessor.java    From spring4-understanding with Apache License 2.0 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, ModelAndViewContainer mavContainer,
		NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {

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

	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());
		}
	}
	mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());

	return arg;
}
 
Example #21
Source File: AbstractDispatcherServletInitializer.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Add the given filter to the ServletContext and map it to the
 * {@code DispatcherServlet} as follows:
 * <ul>
 * <li>a default filter name is chosen based on its concrete type
 * <li>the {@code asyncSupported} flag is set depending on the
 * return value of {@link #isAsyncSupported() asyncSupported}
 * <li>a filter mapping is created with dispatcher types {@code REQUEST},
 * {@code FORWARD}, {@code INCLUDE}, and conditionally {@code ASYNC} depending
 * on the return value of {@link #isAsyncSupported() asyncSupported}
 * </ul>
 * <p>If the above defaults are not suitable or insufficient, override this
 * method and register filters directly with the {@code ServletContext}.
 * @param servletContext the servlet context to register filters with
 * @param filter the filter to be registered
 * @return the filter registration
 */
protected FilterRegistration.Dynamic registerServletFilter(ServletContext servletContext, Filter filter) {
	String filterName = Conventions.getVariableName(filter);
	Dynamic registration = servletContext.addFilter(filterName, filter);
	if (registration == null) {
		int counter = -1;
		while (counter == -1 || registration == null) {
			counter++;
			registration = servletContext.addFilter(filterName + "#" + counter, filter);
			Assert.isTrue(counter < 100,
					"Failed to register filter '" + filter + "'." +
					"Could the same Filter instance have been registered already?");
		}
	}
	registration.setAsyncSupported(isAsyncSupported());
	registration.addMappingForServletNames(getDispatcherTypes(), false, getServletName());
	return registration;
}
 
Example #22
Source File: HandlerMethodInvoker.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private WebDataBinder resolveModelAttribute(String attrName, MethodParameter methodParam,
		ExtendedModelMap implicitModel, NativeWebRequest webRequest, Object handler) throws Exception {

	// Bind request parameter onto object...
	String name = attrName;
	if ("".equals(name)) {
		name = Conventions.getVariableNameForParameter(methodParam);
	}
	Class<?> paramType = methodParam.getParameterType();
	Object bindObject;
	if (implicitModel.containsKey(name)) {
		bindObject = implicitModel.get(name);
	}
	else if (this.methodResolver.isSessionAttribute(name, paramType)) {
		bindObject = this.sessionAttributeStore.retrieveAttribute(webRequest, name);
		if (bindObject == null) {
			raiseSessionRequiredException("Session attribute '" + name + "' required - not found in session");
		}
	}
	else {
		bindObject = BeanUtils.instantiateClass(paramType);
	}
	WebDataBinder binder = createBinder(webRequest, bindObject, name);
	initBinder(handler, name, binder, webRequest);
	return binder;
}
 
Example #23
Source File: RedissonReadAndWriteLockDefinitionParser.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
protected void parseNested(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, BeanDefinition bd) {
    bd.setFactoryBeanName(element.getAttribute(
            RedissonNamespaceParserSupport.READ_WRITE_LOCK_REF_ATTRIBUTE));
    String typeName
            = Conventions.attributeNameToPropertyName(element.getLocalName());
    bd.setFactoryMethodName(typeName);
}
 
Example #24
Source File: ModelFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Derive the model attribute name for the given return value. Results will be
 * based on:
 * <ol>
 * <li>the method {@code ModelAttribute} annotation value
 * <li>the declared return type if it is more specific than {@code Object}
 * <li>the actual return value type
 * </ol>
 * @param returnValue the value returned from a method invocation
 * @param returnType a descriptor for the return type of the method
 * @return the derived name (never {@code null} or empty String)
 */
public static String getNameForReturnValue(@Nullable Object returnValue, MethodParameter returnType) {
	ModelAttribute ann = returnType.getMethodAnnotation(ModelAttribute.class);
	if (ann != null && StringUtils.hasText(ann.value())) {
		return ann.value();
	}
	else {
		Method method = returnType.getMethod();
		Assert.state(method != null, "No handler method");
		Class<?> containingClass = returnType.getContainingClass();
		Class<?> resolvedType = GenericTypeResolver.resolveReturnType(method, containingClass);
		return Conventions.getVariableNameForReturnType(method, resolvedType, returnValue);
	}
}
 
Example #25
Source File: HandlerMethodInvoker.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected final void addReturnValueAsModelAttribute(Method handlerMethod, Class<?> handlerType,
		Object returnValue, ExtendedModelMap implicitModel) {

	ModelAttribute attr = AnnotationUtils.findAnnotation(handlerMethod, ModelAttribute.class);
	String attrName = (attr != null ? attr.value() : "");
	if ("".equals(attrName)) {
		Class<?> resolvedType = GenericTypeResolver.resolveReturnType(handlerMethod, handlerType);
		attrName = Conventions.getVariableNameForReturnType(handlerMethod, resolvedType, returnValue);
	}
	implicitModel.addAttribute(attrName, returnValue);
}
 
Example #26
Source File: RedissonMultiLockDefinitionParser.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
protected String getBeanClassName(Element element) {
    String elementName
            = Conventions.attributeNameToPropertyName(
                    element.getLocalName());
    return RedissonNamespaceParserSupport.IMPL_CLASS_PATH_PREFIX
            + StringUtils.capitalize(elementName);
}
 
Example #27
Source File: ModelFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Derive the model attribute name for the given return value based on:
 * <ol>
 * <li>the method {@code ModelAttribute} annotation value
 * <li>the declared return type if it is more specific than {@code Object}
 * <li>the actual return value type
 * </ol>
 * @param returnValue the value returned from a method invocation
 * @param returnType a descriptor for the return type of the method
 * @return the derived name (never {@code null} or empty String)
 */
public static String getNameForReturnValue(Object returnValue, MethodParameter returnType) {
	ModelAttribute ann = returnType.getMethodAnnotation(ModelAttribute.class);
	if (ann != null && StringUtils.hasText(ann.value())) {
		return ann.value();
	}
	else {
		Method method = returnType.getMethod();
		Class<?> containingClass = returnType.getContainingClass();
		Class<?> resolvedType = GenericTypeResolver.resolveReturnType(method, containingClass);
		return Conventions.getVariableNameForReturnType(method, resolvedType, returnValue);
	}
}
 
Example #28
Source File: HandlerMethodInvoker.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected final void addReturnValueAsModelAttribute(Method handlerMethod, Class<?> handlerType,
		Object returnValue, ExtendedModelMap implicitModel) {

	ModelAttribute attr = AnnotationUtils.findAnnotation(handlerMethod, ModelAttribute.class);
	String attrName = (attr != null ? attr.value() : "");
	if ("".equals(attrName)) {
		Class<?> resolvedType = GenericTypeResolver.resolveReturnType(handlerMethod, handlerType);
		attrName = Conventions.getVariableNameForReturnType(handlerMethod, resolvedType, returnValue);
	}
	implicitModel.addAttribute(attrName, returnValue);
}
 
Example #29
Source File: TeapotMappingHandler.java    From example-restful-project with MIT License 5 votes vote down vote up
/**
 * Converts {@link TeapotMapping} to {@link Teapot} domain object. Apply
 * binding on {@link Teapot}.
 */
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
        NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {

    /* Read mapping from request */
    TeapotMapping mapping = (TeapotMapping) readWithMessageConverters(
            webRequest, parameter, TeapotMapping.class);
    String name = Conventions.getVariableNameForParameter(parameter);

    /* Convert mapping to domain object */
    Teapot teapot = mapper.fromMapping(mapping);

    WebDataBinder binder = binderFactory
            .createBinder(webRequest, teapot, name);

    if (teapot != null) {
        validateIfApplicable(binder, parameter);
        if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
            throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());
        }
    }

    mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());

    return teapot;
}
 
Example #30
Source File: ModelFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Derive the model attribute name for the given return value using one of:
 * <ol>
 * 	<li>The method {@code ModelAttribute} annotation value
 * 	<li>The declared return type if it is more specific than {@code Object}
 * 	<li>The actual return value type
 * </ol>
 * @param returnValue the value returned from a method invocation
 * @param returnType the return type of the method
 * @return the model name, never {@code null} nor empty
 */
public static String getNameForReturnValue(Object returnValue, MethodParameter returnType) {
	ModelAttribute annotation = returnType.getMethodAnnotation(ModelAttribute.class);
	if (annotation != null && StringUtils.hasText(annotation.value())) {
		return annotation.value();
	}
	else {
		Method method = returnType.getMethod();
		Class<?> resolvedType = GenericTypeResolver.resolveReturnType(method, returnType.getContainingClass());
		return Conventions.getVariableNameForReturnType(method, resolvedType, returnValue);
	}
}