com.silentgo.servlet.http.Request Maven / Gradle / Ivy
package com.silentgo.servlet.http;
import com.silentgo.core.SilentGo;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.HashMap;
import java.util.Map;
/**
* Project : silentgo
* com.silentgo.servlet.web
*
* @author teddyzhu
*
* Created by on 16/7/18.
*/
public class Request extends HttpServletRequestWrapper {
private Map paramsMap = new HashMap<>();
private Map resolvedMap;
private int pathSize = 0;
private String[] pathParameters = new String[0];
private Map pathNamedParameters = new HashMap<>();
public Request(javax.servlet.http.HttpServletRequest request) {
super(request);
this.paramsMap.putAll(request.getParameterMap());
resolvedMap = RequestKit.parse(paramsMap);
}
@Override
public String getParameter(String name) {
String[] values = paramsMap.get(name);
if (values == null || values.length == 0) {
return null;
}
return values[0];
}
public String[] getParameterValues(String name) {
return paramsMap.get(name);
}
public void addAllParameters(Map otherParams) {
for (Map.Entry entry : otherParams.entrySet()) {
addParameter(entry.getKey(), entry.getValue());
}
}
public void addParameter(String name, Object value) {
if (value != null) {
if (value instanceof String[]) {
paramsMap.put(name, (String[]) value);
} else if (value instanceof String) {
paramsMap.put(name, new String[]{(String) value});
} else {
paramsMap.put(name, new String[]{String.valueOf(value)});
}
}
}
public Map getParameterMap() {
return paramsMap;
}
public void setPathParameters(String[] pathParameters) {
pathSize = pathParameters.length;
this.pathParameters = pathParameters;
}
public void setPathNamedParameters(Map pathNamedParameters) {
this.pathNamedParameters = pathNamedParameters;
}
public String getPathParameter(int index) {
return index > pathSize ? null : pathParameters[index];
}
public String getPathParameter(String name) {
return pathNamedParameters.get(name);
}
public Map getResolvedMap() {
return resolvedMap;
}
/**
* forward to another controller
*
* @param controllerName
* @throws Throwable
*/
public void forward(String controllerName) throws Throwable {
SilentGo instance = SilentGo.me();
instance.getConfig().getRouteAction().doAction(SilentGo.me().getConfig().getCtx().get().getActionParam());
}
public void rebuildResovedMap() {
resolvedMap = RequestKit.parse(paramsMap);
}
}