public abstract class AbstractController
{
/** 日志 */
private static final Logger LOG = LoggerFactory.getLogger(AbstractController.class);
/** 加密策略(md5) */
private static final Encry encry = ExtensionLoader.getExtensionLoader(Encry.class).getExtension("md5");
/** request对象 */
private HttpServletRequest request;
/** response对象 */
private HttpServletResponse response;
/**
* 导出类型
*/
public enum ExportType
{
/** 选中的数据 */
checkedData,
/** 当前页数据 */
currPageData,
/** 所有数据 */
allData
}
/**
* 获取request
* @return
*/
public HttpServletRequest getRequest()
{
return request;
}
/**
* 获取response
* @return
*/
public HttpServletResponse getResponse()
{
return response;
}
/**
* 创建session
* @return
*/
public HttpSession getSession()
{
return getRequest().getSession(true);
}
/**
* 下载处理, 大文件下载尽量用流
* @param in
* @param fileName
*/
public void download(InputStream in, String fileName)
{
OutputStream out = null;
try
{
response.reset();
response.setHeader("Content-disposition", "attachment; filename=" + CommonUtil.encodeDownFileName(fileName, request.getHeader("User-Agent")));
// 设置 默认编码
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-download");
out = response.getOutputStream();
byte[] buf = new byte[1024 * 10];
int len = 0;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
out.flush();
} catch (Exception e)
{
LOG.error(e.getMessage(), e);
} finally
{
StreamUtil.closeStream(in, out);
}
}
/**
* 下载, 大文件下载慎用
* @param fileData
* @param fileName
*/
public void download(byte[] fileData, String fileName)
{
download(new ByteArrayInputStream(fileData), fileName);
}
/**
* 获取当前用户
* @return
*/
protected User getCurrentUser()
{
return getLoginedService().getCurrentUser();
}
/**
* 登录业务数据操作
* @return
*/
protected LoginedService getLoginedService()
{
return LoginedFactory.getInstance().getLoginedService();
}
/**
* 设置数据到session
* @param key
* @param value
*/
protected void setSessionAttribute(String key, Object value)
{
getLoginedService().setAttribute(key, value);
}
/**
* 获取值
* @param key
* @return
*/
protected Object getSessionAttribute(String key)
{
return getLoginedService().getAttribute(key);
}
/**
* 获取操作详细信息
* @param opType
* @param detail
* @return
*/
protected OpDetail getOpDetail(String opType, String detail)
{
return new OpDetail(opType, getCurrentUser() == null ? "" : getCurrentUser().getUsername(), detail);
}
/**
* 获取映射文件流
* @param mappingPath
* @return
* @throws FileNotFoundException
*/
protected InputStream getMappingFileStream(String mappingPath) throws FileNotFoundException
{
// 映射路径
String basePath = getRequest().getServletContext().getRealPath("/");
File mappingFile = new File(basePath, mappingPath);
if (!mappingFile.exists())
{
LOG.error("映射路径[" + mappingPath + "]对应的文件信息不存在.");
return null;
}
return new FileInputStream(mappingFile);
}
/**
* 获取文件字节码
* @param mappingPath
* @return
* @throws Exception
*/
protected byte[] getMappingFileBytes(String mappingPath) throws Exception
{
InputStream in = getMappingFileStream(mappingPath);
return StreamUtil.getStreamBytes(in);
}
/**
* 获取基准路径
* @return
*/
protected String getBasePath()
{
return getRequest().getScheme() + "://" + getRequest().getServerName() + ":" + getRequest().getServerPort() + getRequest().getContextPath();
}
/**
* 判断字符串与当前密码是否相等
* @param pass
* @return
*/
protected boolean isEqPwd(String pass)
{
// return true;
if (CommonUtil.isEmpty(pass))
{
return false;
}
return getCurrentUser() != null && getCurrentUser().getPasswd().equals(encry.encode(pass));
}
/**
* 注入Request, Response对象
* @param request
* @param response
*/
@ModelAttribute
public void setReqAndResp(HttpServletRequest request, HttpServletResponse response, HttpSession session)
{
this.request = request;
this.response = response;
}
protected String appendStrList(List<? extends AbstractEntity> entites, String split)
{
if (entites == null || entites.size() == 0)
{
return "";
}
StringBuilder builder = new StringBuilder();
split = (split == null ? "," : split);
for (AbstractEntity entity : entites)
{
builder.append(entity.getId()).append(split);
}
// 删除最后一个分隔符
builder.delete(builder.length() - split.length(), builder.length());
return builder.toString();
}
}