Please wait. This can take some minutes ...
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.
com.fastchar.core.FastAction Maven / Gradle / Ivy
package com.fastchar.core;
import com.fastchar.asm.FastParameter;
import com.fastchar.exception.FastFileException;
import com.fastchar.exception.FastReturnException;
import com.fastchar.out.*;
import com.fastchar.utils.*;
import org.w3c.dom.Document;
import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.lang.reflect.Array;
import java.lang.reflect.Type;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@SuppressWarnings("all")
public abstract class FastAction {
HttpServletRequest request;
HttpServletResponse response;
ServletContext servletContext;
List params = new ArrayList<>();
volatile FastAction forwarder;
volatile FastUrl fastUrl;
volatile FastRoute fastRoute;
volatile FastOut fastOut;
volatile FastCheck fastCheck = new FastCheck(this);
private volatile boolean log = true;
private volatile int status = 200;
/**
* 获得路由地址
* Get routing address
*
* @return
*/
protected abstract String getRoute();
public final void invoke() {
if (getFastRoute() != null) {
getFastRoute().invoke();
}
}
private boolean validParam(String paramName, Object value) {
Object validate = fastCheck.validate(paramName, value);
if (validate != null) {
responseParamError(paramName, validate.toString());
return false;
}
return true;
}
/**
* 判断是否是multipart/form-data表单格式
*
* @return
*/
public boolean isMultipart() {
String type = null;
String type1 = request.getHeader("Content-Type");
String type2 = request.getContentType();
if (type1 == null && type2 != null) {
type = type2;
} else if (type2 == null && type1 != null) {
type = type1;
} else if (type1 != null && type2 != null) {
type = type1.length() > type2.length() ? type1 : type2;
}
if (type != null && type.toLowerCase().startsWith("multipart/form-data")) {
return true;
}
return false;
}
public String getProjectHost() {
String[] split = getRequest().getRequestURL().toString().split(FastChar.getConstant().getProjectName());
return FastStringUtils.stripEnd(split[0], "/") + "/" + FastChar.getConstant().getProjectName() + "/";
}
public HttpServletRequest getRequest() {
if (isMultipart()) {
try {
if (!(request instanceof FastMultipartWrapper)) {
request = new FastMultipartWrapper(request,
FastChar.getConstant().getAttachDirectory(),
FastChar.getConstant().getAttachMaxPostSize(),
FastChar.getConstant().getEncoding(),
FastChar.getConstant().isAttachNameMD5());
}
} catch (Exception e) {
e.printStackTrace();
}
}
return request;
}
public final ServletContext getServletContext() {
return servletContext;
}
public boolean isParamEmpty(String paramName) {
return FastStringUtils.isEmpty(getParam(paramName));
}
public boolean isParamNotEmpty(String paramName) {
return FastStringUtils.isNotEmpty(getParam(paramName));
}
public boolean isParamBlank(String paramName) {
return FastStringUtils.isBlank(getParam(paramName));
}
public boolean isParamNotBlank(String paramName) {
return FastStringUtils.isNotBlank(getParam(paramName));
}
public boolean isParamExists(String paramName) {
return getParamNames().contains(paramName);
}
public FastAction addParam(String paramName, String paramValue) {
params.add(new FastRequestParam().setName(paramName).setValue(paramValue));
return this;
}
public Set getParamNames() {
LinkedHashSet strings = new LinkedHashSet<>();
Enumeration parameterNames = getRequest().getParameterNames();
while (parameterNames.hasMoreElements()) {
strings.add(parameterNames.nextElement());
}
for (FastRequestParam param : params) {
strings.add(param.getName());
}
return strings;
}
public String getParam(String paramName, String defaultValue) {
return FastStringUtils.defaultValue(getParam(paramName), defaultValue);
}
public String getParam(String paramName) {
return getParam(paramName, false);
}
public String getParam(String paramName, boolean notNull) {
if (notNull) {
check("@null:" + FastChar.getLocal().getInfo("Param_Error1"));
}
for (FastRequestParam param : params) {
if (param.getName().equals(paramName)) {
if (validParam(paramName, param.getValue())) {
return param.getValue();
}
}
}
String value = getRequest().getParameter(paramName);
if (validParam(paramName, value)) {
return value;
}
return null;
}
public String[] getParamToArray(String paramName) {
return getParamToArray(paramName, false);
}
public String[] getParamToArray(String paramName, boolean notNull) {
String[] parameterValues = getRequest().getParameterValues(paramName);
List arrays = new ArrayList<>();
if (parameterValues != null) {
arrays.addAll(Arrays.asList(parameterValues));
}
for (FastRequestParam param : params) {
if (param.getName().equals(paramName)) {
arrays.add(param.getValue());
}
}
if (notNull) {
check("@null:" + FastChar.getLocal().getInfo("Param_Error1"));
}
String[] strings = arrays.toArray(new String[]{});
if (validParam(paramName, strings)) {
return strings;
}
return null;
}
public Integer[] getParamToIntArray(String paramName) {
return getParamToIntArray(paramName, false);
}
public Integer[] getParamToIntArray(String paramName, boolean notNull) {
List arrays = new ArrayList<>();
String[] parameterValues = getParamToArray(paramName, notNull);
for (String parameterValue : parameterValues) {
arrays.add(FastNumberUtils.formatToInt(parameterValue));
}
return arrays.toArray(new Integer[]{});
}
public Double[] getParamToDoubleArray(String paramName) {
return getParamToDoubleArray(paramName, false);
}
public Double[] getParamToDoubleArray(String paramName, boolean notNull) {
List arrays = new ArrayList<>();
String[] parameterValues = getParamToArray(paramName, notNull);
for (String parameterValue : parameterValues) {
arrays.add(FastNumberUtils.formatToDouble(parameterValue));
}
return arrays.toArray(new Double[]{});
}
public Long[] getParamToLongArray(String paramName) {
return getParamToLongArray(paramName, false);
}
public Long[] getParamToLongArray(String paramName, boolean notNull) {
List arrays = new ArrayList<>();
String[] parameterValues = getParamToArray(paramName, notNull);
for (String parameterValue : parameterValues) {
arrays.add(FastNumberUtils.formatToLong(parameterValue));
}
return arrays.toArray(new Long[]{});
}
public Float[] getParamToFloatArray(String paramName) {
return getParamToFloatArray(paramName, false);
}
public Float[] getParamToFloatArray(String paramName, boolean notNull) {
List arrays = new ArrayList<>();
String[] parameterValues = getParamToArray(paramName, notNull);
for (String parameterValue : parameterValues) {
arrays.add(FastNumberUtils.formatToFloat(parameterValue));
}
return arrays.toArray(new Float[]{});
}
public Short[] getParamToShortArray(String paramName) {
return getParamToShortArray(paramName, false);
}
public Short[] getParamToShortArray(String paramName, boolean notNull) {
List arrays = new ArrayList<>();
String[] parameterValues = getParamToArray(paramName, notNull);
for (String parameterValue : parameterValues) {
arrays.add(FastNumberUtils.formatToShort(parameterValue));
}
return arrays.toArray(new Short[]{});
}
public Boolean[] getParamToBooleanArray(String paramName) {
return getParamToBooleanArray(paramName, false);
}
public Boolean[] getParamToBooleanArray(String paramName, boolean notNull) {
List arrays = new ArrayList<>();
String[] parameterValues = getParamToArray(paramName, notNull);
for (String parameterValue : parameterValues) {
arrays.add(FastBooleanUtils.formatToBoolean(parameterValue));
}
return arrays.toArray(new Boolean[]{});
}
public Date[] getParamToDateArray(String paramName) {
return getParamToDateArray(paramName, false);
}
public Date[] getParamToDateArray(String paramName, boolean notNull) {
return getParamToDateArray(paramName, FastChar.getConstant().getDateFormat(), notNull);
}
public Date[] getParamToDateArray(String paramName, String dateFormat) {
return getParamToDateArray(paramName, dateFormat, false);
}
public Date[] getParamToDateArray(String paramName, String dateFormat, boolean notNull) {
List arrays = new ArrayList<>();
String[] parameterValues = getParamToArray(paramName, notNull);
for (String parameterValue : parameterValues) {
arrays.add(FastDateUtils.parse(parameterValue, dateFormat));
}
return arrays.toArray(new Date[]{});
}
public T[] getParamToEnumArray(String paramName, Class enumClass) {
return getParamToEnumArray(paramName, enumClass, false);
}
public T[] getParamToEnumArray(String paramName, Class enumClass, boolean notNull) {
String[] parameterValues = getParamToArray(paramName, notNull);
Object array = Array.newInstance(enumClass, parameterValues.length);
for (int i = 0; i < parameterValues.length; i++) {
T e = FastEnumUtils.formatToEnum(enumClass, parameterValues[i]);
Array.set(array, i, e);
}
return (T[]) array;
}
public List getParamToList(String prefix) {
return getParamToList(prefix, false);
}
public List getParamToList(String prefix, boolean notNull) {
List list = new ArrayList<>();
Set paramNames = getParamNames();
for (String paramName : paramNames) {
if (paramName.startsWith(prefix)) {
String[] parameterValues = getParamToArray(paramName);
if (parameterValues.length == 0) continue;
if (parameterValues.length > 1) {
list.addAll(Arrays.asList(parameterValues));
} else {
list.add(parameterValues[0]);
}
}
}
if (list.size() == 0 && notNull) {
responseParamError(prefix, FastChar.getLocal().getInfo("Param_Error1", prefix));
}
return list;
}
public List getParamToIntList(String prefix) {
return getParamToIntList(prefix, false);
}
public List getParamToIntList(String prefix, boolean notNull) {
List list = new ArrayList<>();
List paramToList = getParamToList(prefix, notNull);
for (String value : paramToList) {
list.add(FastNumberUtils.formatToInt(value));
}
return list;
}
public List getParamToDoubleList(String prefix) {
return getParamToDoubleList(prefix, false);
}
public List getParamToDoubleList(String prefix, boolean notNull) {
List list = new ArrayList<>();
List paramToList = getParamToList(prefix, notNull);
for (String value : paramToList) {
list.add(FastNumberUtils.formatToDouble(value));
}
return list;
}
public List getParamToFloatList(String prefix) {
return getParamToFloatList(prefix, false);
}
public List getParamToFloatList(String prefix, boolean notNull) {
List list = new ArrayList<>();
List paramToList = getParamToList(prefix, notNull);
for (String value : paramToList) {
list.add(FastNumberUtils.formatToFloat(value));
}
return list;
}
public List getParamToLongList(String prefix) {
return getParamToLongList(prefix, false);
}
public List getParamToLongList(String prefix, boolean notNull) {
List list = new ArrayList<>();
List paramToList = getParamToList(prefix, notNull);
for (String value : paramToList) {
list.add(FastNumberUtils.formatToLong(value));
}
return list;
}
public List getParamToShortList(String prefix) {
return getParamToShortList(prefix, false);
}
public List getParamToShortList(String prefix, boolean notNull) {
List list = new ArrayList<>();
List paramToList = getParamToList(prefix, notNull);
for (String value : paramToList) {
list.add(FastNumberUtils.formatToShort(value));
}
return list;
}
public List getParamToEnumList(String prefix, Class enumClass) {
return getParamToEnumList(prefix, enumClass, false);
}
public List getParamToEnumList(String prefix, Class enumClass, boolean notNull) {
List list = new ArrayList<>();
List paramToList = getParamToList(prefix, notNull);
Object array = Array.newInstance(enumClass, paramToList.size());
for (int i = 0; i < paramToList.size(); i++) {
T e = FastEnumUtils.formatToEnum(enumClass, paramToList.get(i));
Array.set(array, i, e);
}
list.addAll(Arrays.asList((T[]) array));
return list;
}
public int getParamToInt(String paramName) {
return FastNumberUtils.formatToInt(getParam(paramName));
}
public int getParamToInt(String paramName, boolean notNull) {
return FastNumberUtils.formatToInt(getParam(paramName, notNull));
}
public int getParamToInt(String paramName, int defaultValue) {
return FastNumberUtils.formatToInt(getParam(paramName), defaultValue);
}
public short getParamToShort(String paramName) {
return FastNumberUtils.formatToShort(getParam(paramName));
}
public short getParamToShort(String paramName, boolean notNull) {
return FastNumberUtils.formatToShort(getParam(paramName, notNull));
}
public short getParamToShort(String paramName, short defaultValue) {
return FastNumberUtils.formatToShort(getParam(paramName), defaultValue);
}
public double getParamToDouble(String paramName) {
return FastNumberUtils.formatToDouble(getParam(paramName));
}
public double getParamToDouble(String paramName, boolean notNull) {
return FastNumberUtils.formatToDouble(getParam(paramName, notNull));
}
public double getParamToDouble(String paramName, double defaultValue) {
return FastNumberUtils.formatToDouble(getParam(paramName), defaultValue);
}
public double getParamToDouble(String paramName, int digit) {
return FastNumberUtils.formatToDouble(getParam(paramName), digit);
}
public double getParamToDouble(String paramName, double defaultValue, int digit) {
return FastNumberUtils.formatToDouble(getParam(paramName), defaultValue, digit);
}
public float getParamToFloat(String paramName) {
return FastNumberUtils.formatToFloat(getParam(paramName));
}
public float getParamToFloat(String paramName, boolean notNull) {
return FastNumberUtils.formatToFloat(getParam(paramName, notNull));
}
public float getParamToFloat(String paramName, float defaultValue) {
return FastNumberUtils.formatToFloat(getParam(paramName), defaultValue);
}
public float getParamToFloat(String paramName, float defaultValue, int digit) {
return FastNumberUtils.formatToFloat(getParam(paramName), defaultValue, digit);
}
public float getParamToFloat(String paramName, int digit) {
return FastNumberUtils.formatToFloat(getParam(paramName), digit);
}
public boolean getParamToBoolean(String paramName) {
return FastBooleanUtils.formatToBoolean(getParam(paramName));
}
public boolean getParamToBoolean(String paramName, boolean notNull) {
return FastBooleanUtils.formatToBoolean(getParam(paramName, notNull));
}
public boolean getParamToBoolean(String paramName, Boolean defaultValue) {
return FastBooleanUtils.formatToBoolean(getParam(paramName), defaultValue);
}
public long getParamToLong(String paramName) {
return FastNumberUtils.formatToLong(getParam(paramName));
}
public long getParamToLong(String paramName, boolean notNull) {
return FastNumberUtils.formatToLong(getParam(paramName, notNull));
}
public long getParamToLong(String paramName, long defaultValue) {
return FastNumberUtils.formatToLong(getParam(paramName), defaultValue);
}
public Date getParamToDate(String paramName) {
return getParamToDate(paramName, FastChar.getConstant().getDateFormat());
}
public Date getParamToDate(String paramName, boolean notNull) {
return getParamToDate(paramName, FastChar.getConstant().getDateFormat(), notNull);
}
public Date getParamToDate(String paramName, Date defaultValue) {
return getParamToDate(paramName, FastChar.getConstant().getDateFormat(), defaultValue);
}
public Date getParamToDate(String paramName, String dateFormat) {
return getParamToDate(paramName, FastChar.getConstant().getDateFormat(), null);
}
public Date getParamToDate(String paramName, String dateFormat, boolean notNull) {
return FastDateUtils.parse(getParam(paramName, notNull), dateFormat, null);
}
public Date getParamToDate(String paramName, String dateFormat, Date defaultValue) {
return FastDateUtils.parse(getParam(paramName), dateFormat, defaultValue);
}
public T getParamToEnum(String paramName, Class enumClass) {
return getParamToEnum(paramName, enumClass, null);
}
public T getParamToEnum(String paramName, Class enumClass, boolean notNull) {
return getParamToEnum(paramName, enumClass, null, notNull);
}
public T getParamToEnum(String paramName, Class enumClass, Enum defaultValue) {
return getParamToEnum(paramName, enumClass, defaultValue, false);
}
private T getParamToEnum(String paramName, Class enumClass, Enum defaultValue, boolean notNull) {
String param = getParam(paramName, notNull);
if (FastStringUtils.isEmpty(param)) {
return null;
}
return (T) FastEnumUtils.formatToEnum(enumClass, param, defaultValue);
}
public Map getParamToMap() {
Map mapParam = new HashMap<>();
Set paramNames = getParamNames();
for (String paramName : paramNames) {
String[] parameterValues = getParamToArray(paramName);
if (parameterValues.length == 0) continue;
if (parameterValues.length > 1) {
mapParam.put(paramName, parameterValues);
} else {
mapParam.put(paramName, parameterValues[0]);
}
}
return mapParam;
}
//where.attr where['attr']
public Map getParamToMap(String prefix) {
return getParamToMap(prefix, false);
}
public Map getParamToMap(String prefix, boolean notNull) {
Map mapParam = new HashMap<>();
Set paramNames = getParamNames();
for (String paramName : paramNames) {
if (paramName.startsWith(prefix)) {
String attr = getParamNameAttr(paramName);
String[] parameterValues = getParamToArray(paramName);
if (parameterValues.length == 0) continue;
if (parameterValues.length > 1) {
mapParam.put(attr, parameterValues);
} else {
mapParam.put(attr, parameterValues[0]);
}
}
}
if (notNull && mapParam.size() == 0) {
responseParamError(prefix, FastChar.getLocal().getInfo("Param_Error1", prefix));
}
return mapParam;
}
//where[0].name where[0]['name']
public List> getParamToMapList(String prefix) {
return getParamToMapList(prefix, false);
}
//where[0].name where[0]['name']
public List> getParamToMapList(String prefix, boolean notNull) {
List> mapList = new ArrayList<>();
SortedSet mapNames = new TreeSet<>();
Set paramNames = getParamNames();
for (String paramName : paramNames) {
if (paramName.startsWith(prefix)) {
String namePrefix = getParamNamePrefix(paramName);
mapNames.add(namePrefix);
}
}
for (String mapName : mapNames) {
Map paramToMap = getParamToMap(mapName);
if (paramToMap.size() == 0) continue;
mapList.add(paramToMap);
}
if (notNull && mapList.size() == 0) {
responseParamError(prefix, FastChar.getLocal().getInfo("Param_Error1", prefix));
}
return mapList;
}
public > T getParamToEntity(String prefix, Class targetClass) {
return getParamToEntity(prefix, targetClass, false);
}
public > T getParamToEntity(String prefix, Class targetClass,
boolean notNull) {
FastEntity fastEntity = FastChar.getOverrides().newInstance(targetClass);
if (fastEntity == null) {
return null;
}
Map paramToMap = getParamToMap(prefix, notNull);
for (String key : paramToMap.keySet()) {
fastEntity.set(key, paramToMap.get(key));
}
if (fastEntity.size() == 0) {
return null;
}
return (T) fastEntity;
}
public > List getParamToEntityList(String prefix, Class targetClass) {
return getParamToEntityList(prefix, targetClass, true);
}
public > List getParamToEntityList(String prefix, Class targetClass, boolean notNull) {
List list = new ArrayList<>();
List> paramToMapList = getParamToMapList(prefix, notNull);
for (Map paramToMap : paramToMapList) {
if (paramToMap.size() == 0) continue;
FastEntity fastEntity = FastChar.getOverrides().newInstance(targetClass);
if (fastEntity == null) {
return null;
}
for (String key : paramToMap.keySet()) {
fastEntity.set(key, paramToMap.get(key));
}
if (fastEntity.size() == 0) continue;
list.add((T) fastEntity);
}
return list;
}
public > T[] getParamToEntityArray(String prefix, Class targetClass) {
return getParamToEntityArray(prefix, targetClass, true);
}
public > T[] getParamToEntityArray(String prefix, Class targetClass, boolean notNull) {
List paramToEntityList = getParamToEntityList(prefix, targetClass, notNull);
Object array = Array.newInstance(targetClass, paramToEntityList.size());
for (int i = 0; i < paramToEntityList.size(); i++) {
Array.set(array, i, paramToEntityList.get(i));
}
return (T[]) array;
}
public T getParamToClass(String paramName, Class targetClass) throws Exception {
return getParamToClass(paramName, targetClass, null);
}
public T getParamToClass(String paramName, Class targetClass, Type parameterizedType) throws Exception {
Object value = FastChar.getConverters().convertParam(this, new FastParameter().setType(targetClass).setParameterizedType(parameterizedType));
if (value == null) {
return null;
}
return (T) value;
}
public FastAction putParamFile(FastFile fastFile) {
HttpServletRequest request = getRequest();
if (request instanceof FastMultipartWrapper) {
FastMultipartWrapper multipartWrapper = (FastMultipartWrapper) request;
multipartWrapper.putFile(fastFile.getParamName(), fastFile);
}
return this;
}
public void deleteAllParamFiles() {
for (FastFile fastFile : getParamListFile()) {
fastFile.getFile().delete();
}
}
public FastFile getParamFile() {
List> paramToListFile = getParamListFile();
if (paramToListFile.size() > 0) {
return paramToListFile.get(0);
}
return null;
}
public FastFile getParamFile(String paramName) {
HttpServletRequest request = getRequest();
if (request instanceof FastMultipartWrapper) {
FastMultipartWrapper multipartWrapper = (FastMultipartWrapper) request;
return multipartWrapper.getFile(paramName);
}
return null;
}
public FastFile getParamFile(String paramName, String moveToDirectory) throws FastFileException {
FastFile paramToFile = getParamFile(paramName);
if (paramToFile != null) {
return paramToFile.moveFile(moveToDirectory);
}
return null;
}
public List> getParamListFile() {
HttpServletRequest request = getRequest();
if (request instanceof FastMultipartWrapper) {
FastMultipartWrapper multipartWrapper = (FastMultipartWrapper) request;
return multipartWrapper.getFiles();
}
return new ArrayList<>();
}
private String getParamNameAttr(String paramName) {
//where['attr'] where[0]['attr']
String regStr = "(.*)\\[(['\"])(.*)\\2\\]";
Matcher matcher = Pattern.compile(regStr).matcher(paramName);
if (matcher.find()) {
return matcher.group(3);
}
//where.attr where[0].attr
if (paramName.indexOf(".") > 0) {
String[] split = paramName.split("\\.");
return split[split.length - 1];
}
return paramName;
}
private String getParamNamePrefix(String paramName) {
//where['attr'] where[0]['attr'] 前缀:where where[0]
String regStr = "(.*)\\[(['\"])(.*)\\2\\]";
Matcher matcher = Pattern.compile(regStr).matcher(paramName);
if (matcher.find()) {
return matcher.group(1);
}
//where.attr where[0].attr 前缀:where where[0]
return paramName.split("\\.")[0];
}
public Document getParamDataToXml() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(getRequest().getInputStream());
return doc;
}
public String getParamDataToString() throws Exception {
return FastIOUtils.toString(getRequest().getInputStream(), getRequest().getCharacterEncoding());
}
public String getParamDataToString(String encoding) throws Exception {
return FastIOUtils.toString(getRequest().getInputStream(), encoding);
}
public void response(FastOut out) {
this.fastOut = out;
this.fastRoute.response();
throw new FastReturnException();
}
public void response404(String message) {
this.fastOut = FastChar.getOverrides().newInstance(FastOutError.class).setStatus(404).setDescription(message);
this.fastRoute.response();
throw new FastReturnException();
}
public void response500(Throwable throwable) {
StringBuilder stringBuilder = new StringBuilder(throwable.toString());
StackTraceElement[] stackTrace = throwable.getStackTrace();
for (StackTraceElement stackTraceElement : stackTrace) {
stringBuilder.append("\n\tat ").append(stackTraceElement);
}
this.fastOut = FastChar.getOverrides().newInstance(FastOutError.class).setStatus(500)
.setDescription(throwable.toString())
.setData(stringBuilder.toString());
this.fastRoute.response();
throw new FastReturnException();
}
public void response502(String message) {
this.fastOut = FastChar.getOverrides().newInstance(FastOutError.class).setStatus(502).setDescription(message);
this.fastRoute.response();
throw new FastReturnException();
}
public void responseJson(Object data) {
this.fastOut = FastChar.getOverrides().newInstance(FastOutJson.class).setData(data).setStatus(this.status);
this.fastRoute.response();
throw new FastReturnException();
}
public void responseJson(int code, String message, Object... data) {
Map json = new HashMap<>();
json.put("code", code);
json.put("success", code == 0);
json.put("message", message);
for (int i = 0; i < data.length; i++) {
if (i == 0) {
json.put("data", data[i]);
} else {
json.put("data_" + i, data[i]);
}
}
responseJson(json);
}
public void responseText(Object data) {
this.fastOut = FastChar.getOverrides().newInstance(FastOutText.class).setData(data).setStatus(this.status);
this.fastRoute.response();
throw new FastReturnException();
}
public void responseText(int status, Object data) {
this.fastOut = FastChar.getOverrides().newInstance(FastOutText.class).setData(data).setStatus(status);
this.fastRoute.response();
throw new FastReturnException();
}
public void responseText(Object data, String contentType) {
this.fastOut = FastChar.getOverrides().newInstance(FastOutText.class).setData(data).setContentType(contentType).setStatus(this.status);
this.fastRoute.response();
throw new FastReturnException();
}
public void responseText(int status, Object data, String contentType) {
this.fastOut = FastChar.getOverrides().newInstance(FastOutText.class).setData(data).setContentType(contentType).setStatus(status);
this.fastRoute.response();
throw new FastReturnException();
}
public void responseXml(Object data) {
this.fastOut = FastChar.getOverrides().newInstance(FastOutXml.class).setData(data).setStatus(this.status);
this.fastRoute.response();
throw new FastReturnException();
}
public void responseParamError(String paramName) {
this.fastOut = FastChar.getOverrides().newInstance(FastOutParamError.class).setData(paramName).setStatus(this.status);
this.fastRoute.response();
throw new FastReturnException();
}
public void responseParamError(String paramName, String message) {
this.fastOut = FastChar.getOverrides().newInstance(FastOutParamError.class)
.setMessage(message).setData(paramName).setStatus(this.status);
this.fastRoute.response();
throw new FastReturnException();
}
public void responseHtml(String html) {
this.fastOut = FastChar.getOverrides().newInstance(FastOutHtml.class).setData(html).setStatus(this.status);
this.fastRoute.response();
throw new FastReturnException();
}
public void responseFile(File file) {
responseFile(file.getAbsolutePath());
}
public void responseFile(File file, String fileName) {
responseFile(file.getAbsolutePath(), fileName);
}
public void responseFile(File file, boolean disposition) {
responseFile(file.getAbsolutePath(), null, disposition);
}
public void responseFile(String filePath) {
responseFile(filePath, null);
}
public void responseFile(String filePath, boolean disposition) {
responseFile(filePath, null, disposition);
}
public void responseFile(String filePath, String fileName) {
responseFile(filePath, fileName, true);
}
public void responseFile(String filePath, String fileName, boolean disposition) {
this.fastOut = FastChar.getOverrides().newInstance(FastOutFile.class)
.setData(filePath)
.setFileName(fileName)
.setDisposition(disposition)
.setStatus(this.status);
this.fastRoute.response();
throw new FastReturnException();
}
public void responseJsp(String jspPath) {
this.fastOut = FastChar.getOverrides().newInstance(FastOutJsp.class).setData(jspPath).setStatus(this.status);
this.fastRoute.response();
throw new FastReturnException();
}
public void responseFreemarker(String filePath) {
this.fastOut = FastChar.getOverrides().newInstance(FastOutFreemarker.class).setData(filePath).setStatus(this.status);
this.fastRoute.response();
throw new FastReturnException();
}
public void responseFreemarker(String filePath, String contentType) {
this.fastOut = FastChar.getOverrides().newInstance(FastOutFreemarker.class).setContentType(contentType).setData(filePath).setStatus(this.status);
this.fastRoute.response();
throw new FastReturnException();
}
public void responseThymeleaf(String filePath) {
this.fastOut = FastChar.getOverrides().newInstance(FastOutThymeleaf.class).setData(filePath).setStatus(this.status);
this.fastRoute.response();
throw new FastReturnException();
}
public void responseThymeleaf(String filePath, String contentType) {
this.fastOut = FastChar.getOverrides().newInstance(FastOutThymeleaf.class).setContentType(contentType).setData(filePath).setStatus(this.status);
this.fastRoute.response();
throw new FastReturnException();
}
public void responseVelocity(String filePath) {
this.fastOut = FastChar.getOverrides().newInstance(FastOutVelocity.class).setData(filePath).setStatus(this.status);
this.fastRoute.response();
throw new FastReturnException();
}
public void responseVelocity(String filePath, String contentType) {
this.fastOut = FastChar.getOverrides().newInstance(FastOutVelocity.class).setContentType(contentType).setData(filePath).setStatus(this.status);
this.fastRoute.response();
throw new FastReturnException();
}
public void responseCaptcha() {
this.fastOut = FastChar.getOverrides().newInstance(FastOutCaptcha.class).setStatus(this.status);
this.fastRoute.response();
throw new FastReturnException();
}
public boolean validateCaptcha(String code) {
Object captcha = getSession(FastMD5Utils.MD5(FastChar.getConstant().getProjectName()));
if (captcha != null) {
return captcha.toString().equalsIgnoreCase(code);
}
return false;
}
public void redirect(String url) {
this.fastOut = FastChar.getOverrides().newInstance(FastOutRedirect.class).setData(url).setStatus(302);
this.fastRoute.response();
throw new FastReturnException();
}
public void redirect301(String url) {
this.fastOut = FastChar.getOverrides().newInstance(FastOutRedirect.class).setData(url).setStatus(301);
this.fastRoute.response();
throw new FastReturnException();
}
public void forward(String url) {
this.fastOut = FastChar.getOverrides().newInstance(FastOutForward.class).setData(url);
this.fastRoute.response();
throw new FastReturnException();
}
public final String getRequestMethod() {
return request.getMethod();
}
public String getContentType() {
return request.getContentType();
}
public final HttpSession getSession() {
return request.getSession();
}
public T getSession(String attr) {
return (T) request.getSession().getAttribute(attr);
}
public void setSession(String attr, Object value) {
getSession().setAttribute(attr, value);
}
public void removeSession(String attr) {
getSession().removeAttribute(attr);
}
public FastAction setRequestAttr(String attr, Object value) {
request.setAttribute(attr, value);
return this;
}
public void removeAttr(String attr) {
request.removeAttribute(attr);
}
public FastAction setCookie(String name, Object value, int maxAge, String path, String domain) {
return setCookie(name, value, maxAge, path, domain, null);
}
public FastAction setCookie(String name, Object value, int maxAge, String path) {
return setCookie(name, value, maxAge, path, null, null);
}
public FastAction setCookie(String name, Object value, int maxAge, String path, boolean httpOnly) {
return setCookie(name, value, maxAge, path, null, httpOnly);
}
public FastAction setCookie(String name, Object value, int maxAge, boolean httpOnly) {
return setCookie(name, value, maxAge, null, null, httpOnly);
}
public FastAction setCookie(String name, Object value, int maxAge) {
return setCookie(name, value, maxAge, null, null, null);
}
public FastAction setCookie(String name, Object value) {
return setCookie(name, value, Integer.MAX_VALUE, null, null, null);
}
public FastAction setCookie(String name, Object value, boolean httpOnly) {
return setCookie(name, value, Integer.MAX_VALUE, null, null, httpOnly);
}
public FastAction setCookie(String name, Object value, int maxAge, String path, String domain,
Boolean httpOnly) {
Cookie cookie = new Cookie(name, String.valueOf(value));
cookie.setMaxAge(maxAge);
if (path == null) {
path = "/";
}
cookie.setPath(path);
if (domain != null) {
cookie.setDomain(domain);
}
if (httpOnly != null) {
cookie.setHttpOnly(httpOnly);
}
response.addCookie(cookie);
return this;
}
public String getCookie(String name, String defaultValue) {
Cookie cookie = getCookieObject(name);
return cookie != null ? cookie.getValue() : defaultValue;
}
public String getCookie(String name) {
return getCookie(name, null);
}
public int getCookieToInt(String name) {
return getCookieToInt(name, 0);
}
public int getCookieToInt(String name, int defaultValue) {
String result = getCookie(name);
return FastNumberUtils.formatToInt(result, defaultValue);
}
public double getCookieToDouble(String name) {
return getCookieToDouble(name, 0);
}
public double getCookieToDouble(String name, double defaultValue) {
String result = getCookie(name);
return FastNumberUtils.formatToDouble(result, defaultValue);
}
public long getCookieToLong(String name) {
return getCookieToLong(name, 0);
}
public long getCookieToLong(String name, long defaultValue) {
String result = getCookie(name);
return FastNumberUtils.formatToLong(result, defaultValue);
}
public boolean getCookieToBoolean(String name) {
return getCookieToBoolean(name, false);
}
public boolean getCookieToBoolean(String name, boolean defaultValue) {
String result = getCookie(name);
return FastBooleanUtils.formatToBoolean(result, defaultValue);
}
public Cookie getCookieObject(String name) {
Cookie[] cookies = request.getCookies();
if (cookies != null)
for (Cookie cookie : cookies)
if (cookie.getName().equals(name))
return cookie;
return null;
}
public Cookie[] getCookieObjects() {
Cookie[] result = request.getCookies();
return result != null ? result : new Cookie[0];
}
public HttpServletResponse getResponse() {
return response;
}
public List getUrlParams() {
return this.fastUrl.getUrlParams();
}
public String getUrlParam(int index) {
if (index >= this.fastUrl.getUrlParams().size()) {
return null;
}
return this.fastUrl.getUrlParams().get(index);
}
public FastRoute getFastRoute() {
return fastRoute;
}
public FastOut getFastOut() {
return fastOut;
}
public FastAction getForwarder() {
return forwarder;
}
public boolean isLog() {
return log;
}
public FastAction setLog(boolean log) {
this.log = log;
return this;
}
public int getStatus() {
return status;
}
public FastAction setStatus(int status) {
this.status = status;
return this;
}
public String getRemoveIp() {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Real-IP");//nginx 服务器
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
if (ip != null && ip.length() > 15) {
if (ip.indexOf(",") > 0) {
ip = ip.substring(0, ip.indexOf(","));
}
}
return ip;
}
public final String getUserAgent() {
String agent = getRequest().getHeader("User-Agent");
return agent;
}
public FastAction check(String validator) {
return fastCheck.check(validator);
}
}