All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dynamicextensionsalfresco.webscripts.arguments.AttributeArgumentResolver Maven / Gradle / Ivy

Go to download

Adds an OSGi container to alfresco repository supporting dynamic code reloading, classpath isolation and a bunch of other useful features

There is a newer version: 3.1.0
Show newest version
package com.github.dynamicextensionsalfresco.webscripts.arguments;

import java.lang.annotation.Annotation;
import java.util.Map;

import com.github.dynamicextensionsalfresco.webscripts.AnnotationWebScriptRequest;
import com.github.dynamicextensionsalfresco.webscripts.annotations.Attribute;

import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;
import org.springframework.util.StringUtils;

public class AttributeArgumentResolver implements ArgumentResolver {

	@Override
	public boolean supports(final Class parameterType, final Class annotationType) {
		return Attribute.class.equals(annotationType);
	}

	@Override
	public Object resolveArgument(final Class argumentType, final Attribute attribute, String name,
			final WebScriptRequest request, final WebScriptResponse response) {
		Object value;
		final Map attributesByName = ((AnnotationWebScriptRequest) request).getModel();
		if (StringUtils.hasText(attribute.value())) {
			name = attribute.value();
		}
		if (attributesByName.containsKey(name)) {
			value = attributesByName.get(name);
		} else {
			value = resolveByType(argumentType, attributesByName);
		}
		if (value == null && attribute.required()) {
			throw new RuntimeException(String.format("Cannot find attribute for argument '%s'", name));
		}
		return value;
	}

	/* Utility operations */

	protected Object resolveByType(final Class argumentType, final Map attributesByName) {
		Object value = null;
		for (final Object attributeValue : attributesByName.values()) {
			if (argumentType.isInstance(attributeValue)) {
				value = attributeValue;
				break;
			}
		}
		return value;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy