com.github.andyshaox.servlet.mapping.BindingParameter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of GearEE Show documentation
Show all versions of GearEE Show documentation
Enhance and formating the coding of JDK
package com.github.andyshaox.servlet.mapping;
import java.io.IOException;
import java.util.function.Function;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.github.andyshaox.servlet.mapping.annotation.Variables;
/**
*
* Title:
* Descript:
* Copyright: Copryright(c) Jan 25, 2016
* Encoding:UNIX UTF-8
*
* @author Andy.Shao
*
*/
public class BindingParameter implements MappingProcess {
private MappingProcess mappingProcess;
private ParameterFormat parameterFormat = ParameterFormat.DO_NOTHING;
private Function variableFactory = (mapping) -> Variables.analyzeParameters(mapping);
@Override
public View doProcess(ServletConfig config , HttpServletRequest request , HttpServletResponse response , Mapping mapping , ProcessType processType)
throws ServletException , IOException , MappingProcessException {
final Variable[] variables = this.variableFactory.apply(mapping);
final Class>[] parameterType = mapping.getProcessMethod().getParameterTypes();
final Object[] args = new Object[parameterType.length];
for (int i = 0 ; i < parameterType.length ; i++) {
Class> type = parameterType[i];
if (type.isInstance(request)) args[i] = request;
else if (type.isInstance(response)) args[i] = response;
else {
Variable variable = variables[i];
Object value = this.parameterFormat.covert(config , request , response , variable , type);
if (variable.getRequired() && value == null) throw new NullPointerException(variable.getParamName() + " is null");
args[i] = value == null ? variable.getDefaultValue() : value;
}
}
processType.args = args;
return this.mappingProcess.doProcess(config , request , response , mapping , processType);
}
public void setMappingProcess(MappingProcess mappingProcess) {
this.mappingProcess = mappingProcess;
}
public void setParameterFormat(ParameterFormat parameterFormat) {
this.parameterFormat = parameterFormat;
}
public void setVariableFactory(Function variableFactory) {
this.variableFactory = variableFactory;
}
}