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

org.visallo.webster.parameterProviders.OptionalParameterProvider Maven / Gradle / Ivy

There is a newer version: 3.2.0
Show newest version
package org.visallo.webster.parameterProviders;

import org.visallo.webster.HandlerChain;
import org.visallo.webster.ParameterValueConverter;
import org.visallo.webster.WebsterException;
import org.visallo.webster.annotations.Optional;
import org.visallo.webster.utils.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class OptionalParameterProvider extends ValueParameterProvider {
    private final String defaultValue;
    private final Optional annotation;

    public OptionalParameterProvider(Class parameterType, Optional annotation, ParameterValueConverter parameterValueConverter, String defaultValue) {
        super(parameterType, annotation.name(), parameterValueConverter);
        this.annotation = annotation;
        this.defaultValue = defaultValue;
    }

    @Override
    public T getParameter(HttpServletRequest request, HttpServletResponse response, HandlerChain chain) {
        String[] value = getParameterOrAttribute(request);
        if (value == null) {
            if (defaultValue == null) {
                value = null;
            } else {
                value = new String[]{defaultValue};
            }
        } else {
            if (!annotation.allowEmpty() && StringUtils.containsAnEmpty(value)) {
                throw new WebsterException(String.format("Parameter: '%s' may not be or contain blanks in the request", getParameterName()));
            }
        }
        return toParameterType(value);
    }

    public String getDefaultValue() {
        return defaultValue;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy