Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2014. Escalon System-Entwicklung, Dietrich Schulten
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for
* the specific language governing permissions and limitations under the License.
*/
package com.github.hateoas.forms.spring;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.github.hateoas.forms.action.Input;
import com.github.hateoas.forms.action.Options;
import com.github.hateoas.forms.action.Select;
import com.github.hateoas.forms.action.Type;
import com.github.hateoas.forms.affordance.ActionDescriptor;
import com.github.hateoas.forms.affordance.ActionInputParameter;
import com.github.hateoas.forms.affordance.DataType;
import com.github.hateoas.forms.affordance.ParameterType;
import com.github.hateoas.forms.affordance.Suggest;
import com.github.hateoas.forms.affordance.SuggestType;
/**
* Describes a Spring MVC rest services method parameter value with recorded sample call value and input constraints.
*
* @author Dietrich Schulten
*/
public abstract class SpringActionInputParameter implements ActionInputParameter {
private static final String[] EMPTY = new String[0];
private static final List> EMPTY_SUGGEST = Collections.emptyList();
TypeDescriptor typeDescriptor;
private final Object value;
private Boolean arrayOrCollection = null;
private final Map inputConstraints = new HashMap();
Suggest>[] possibleValues;
String[] excluded = EMPTY;
String[] readOnly = EMPTY;
String[] hidden = EMPTY;
String[] include = EMPTY;
boolean editable = true;
ParameterType type = ParameterType.UNKNOWN;
@SuppressWarnings({ "unchecked", "rawtypes" }) PossibleValuesResolver> resolver = new FixedPossibleValuesResolver(
EMPTY_SUGGEST, SuggestType.INTERNAL);
protected static ConversionService DEFAULT_CONVERSION_SERVICE = new DefaultFormattingConversionService();
private final ConversionService conversionService;
Type fieldType;
private final String name;
protected SpringActionInputParameter(final String name, final Object value,
final ConversionService conversionService) {
this.name = name;
this.conversionService = conversionService;
this.value = value;
}
public static void setDefaultConversionService(final ConversionService conversionService) {
DEFAULT_CONVERSION_SERVICE = conversionService;
}
protected void putInputConstraint(final String key, final Object defaultValue, final Object value) {
if (!value.equals(defaultValue)) {
inputConstraints.put(key, value);
}
}
/**
* The value of the parameter at sample invocation time.
*
* @return value, may be null
*/
@Override
public Object getValue() {
return value;
}
/**
* The value of the parameter at sample invocation time, formatted according to conversion configuration.
*
* @return value, may be null
*/
@Override
public String getValueFormatted() {
String ret;
if (value == null) {
ret = null;
} else {
ret = (String) conversionService.convert(value, typeDescriptor, TypeDescriptor.valueOf(String.class));
}
return ret;
}
/**
* Gets HTML5 parameter type for input field according to {@link Type} annotation.
*
* @return the type
*/
@Override
public Type getHtmlInputFieldType() {
return fieldType;
}
@Override
public void setHtmlInputFieldType(final Type type) {
fieldType = type;
}
/**
* Has constraints defined via @Input annotation. Note that there might also be other kinds of
* constraints, e.g. @Select may define values for {@link #getPossibleValues}.
*
* @return true if parameter is constrained
*/
@Override
public boolean hasInputConstraints() {
return !inputConstraints.isEmpty();
}
/**
* Determines if request body input parameter has a hidden input property.
*
* @param property name or property path
* @return true if hidden
*/
boolean isHidden(final String property) {
return arrayContains(hidden, property);
}
boolean isReadOnly(final String property) {
return !editable || arrayContains(readOnly, property);
}
@Override
public void setReadOnly(final boolean readOnly) {
editable = !readOnly;
putInputConstraint(ActionInputParameter.EDITABLE, "", editable);
}
@Override
public void setRequired(final boolean required) {
putInputConstraint(ActionInputParameter.REQUIRED, "", required);
}
boolean isIncluded(final String property) {
if (isExcluded(property)) {
return false;
}
if (include == null || include.length == 0) {
return true;
}
return containsPropertyIncludeValue(property);
}
/**
* Find out if property is included by searching through all annotations.
*
* @param property
* @return
*/
private boolean containsPropertyIncludeValue(final String property) {
return arrayContains(readOnly, property) || arrayContains(hidden, property) || arrayContains(include, property);
}
/**
* Determines if request body input parameter should be excluded, considering {@link Input#exclude}.
*
* @param property name or property path
* @return true if excluded, false if no include statement found or not excluded
*/
private boolean isExcluded(final String property) {
return excluded != null && arrayContains(excluded, property);
}
private boolean arrayContains(final String[] array, final String toFind) {
if (array == null || array.length == 0) {
return false;
}
for (String item : array) {
if (toFind.equals(item)) {
return true;
}
}
return false;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public List> getPossibleValues(final ActionDescriptor actionDescriptor) {
List