mirror of
https://github.com/zyphlar/LanternPowerMonitor.git
synced 2024-03-08 14:07:47 +00:00
Add a rules engine so I can be notified when I forget to close my garage door.
This commit is contained in:
@@ -532,7 +532,6 @@ public class CollectionUtils {
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
public static <T> void edit(Iterable<T> _coll, IEditor<T> _editor) {
|
||||
if ((_coll == null) || (_editor == null))
|
||||
return;
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package com.lanternsoftware.util;
|
||||
|
||||
public abstract class LanternFiles {
|
||||
public static final String SOURCE_PATH = "C:\\lantern\\wc\\opensource\\LanternPowerMonitor\\";
|
||||
public static final String OPS_PATH = "D:\\zwave\\";
|
||||
// public static final String OPS_PATH = "D:\\zwave\\linux\\";
|
||||
// public static final String OPS_PATH = "/opt/tomcat/";
|
||||
public static final String SOURCE_PATH = "C:\\lantern\\LanternPowerMonitor\\";
|
||||
// public static final String OPS_PATH = "D:\\zwave\\";
|
||||
// public static final String OPS_PATH = "D:\\zwave\\localhost\\";
|
||||
// public static final String OPS_PATH = "D:\\zwave\\mark4770\\";
|
||||
// public static final String OPS_PATH = "D:\\zwave\\prod\\";
|
||||
// public static final String OPS_PATH = "D:\\zwave\\prodremote\\";
|
||||
public static final String OPS_PATH = "/opt/tomcat/";
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@ import javax.crypto.spec.IvParameterSpec;
|
||||
import javax.crypto.spec.PBEKeySpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
import com.lanternsoftware.util.LanternFiles;
|
||||
import com.lanternsoftware.util.ResourceLoader;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -76,6 +78,10 @@ public class AESTool {
|
||||
System.out.println(builder.toString());
|
||||
}
|
||||
|
||||
public static AESTool authTool() {
|
||||
return new AESTool(ResourceLoader.loadFile(LanternFiles.OPS_PATH + "authKey.dat"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param _btKey the encoded form of a {@link SecretKey} object. See the {@link SecretKey#getEncoded()} method.
|
||||
*/
|
||||
|
||||
@@ -71,6 +71,8 @@ public class MongoProxy extends AbstractDaoProxy {
|
||||
|
||||
public MongoProxy(List<String> _hosts, String _userName, String _password, String _clientKeystorePath, String _clientKeystorePassword, String _caKeystorePath, String _caKeystorePassword, String _dbName, String _authDbName) {
|
||||
List<ServerAddress> listAddresses = new LinkedList<>();
|
||||
if (CollectionUtils.isEmpty(_hosts))
|
||||
_hosts = CollectionUtils.asArrayList("localhost");
|
||||
for (String addr : _hosts) {
|
||||
int portIdx = addr.indexOf(":");
|
||||
if (portIdx > 0)
|
||||
@@ -108,7 +110,7 @@ public class MongoProxy extends AbstractDaoProxy {
|
||||
options = MongoClientOptions.builder().sslEnabled(false).build();
|
||||
}
|
||||
}
|
||||
client = new MongoClient(listAddresses, MongoCredential.createCredential(_userName, NullUtils.isNotEmpty(_authDbName) ? _authDbName : "admin", _password.toCharArray()), options);
|
||||
client = NullUtils.isEmpty(_userName) ? new MongoClient(listAddresses, options) : new MongoClient(listAddresses, MongoCredential.createCredential(_userName, NullUtils.isNotEmpty(_authDbName) ? _authDbName : "admin", _password.toCharArray()), options);
|
||||
dbName = _dbName;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package com.lanternsoftware.util.dao.mongo.dao;
|
||||
|
||||
import com.lanternsoftware.util.CollectionUtils;
|
||||
import com.lanternsoftware.util.NullUtils;
|
||||
import com.lanternsoftware.util.dao.AbstractDaoSerializer;
|
||||
import com.lanternsoftware.util.dao.DaoEntity;
|
||||
import com.lanternsoftware.util.dao.DaoProxyType;
|
||||
import com.lanternsoftware.util.dao.DaoSerializer;
|
||||
|
||||
import com.lanternsoftware.util.dao.mongo.MongoConfig;
|
||||
|
||||
import java.lang.String;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class MongoConfigSerializer extends AbstractDaoSerializer<MongoConfig>
|
||||
{
|
||||
@@ -17,11 +17,16 @@ public class MongoConfigSerializer extends AbstractDaoSerializer<MongoConfig>
|
||||
return MongoConfig.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DaoProxyType> getSupportedProxies() {
|
||||
return Collections.singletonList(DaoProxyType.MONGO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DaoEntity toDaoEntity(MongoConfig _o)
|
||||
{
|
||||
DaoEntity d = new DaoEntity();
|
||||
d.put("hosts", CollectionUtils.commaSeparated(_o.getHosts()));
|
||||
d.put("hosts", _o.getHosts());
|
||||
d.put("username", _o.getUsername());
|
||||
d.put("password", _o.getPassword());
|
||||
d.put("client_keystore_path", _o.getClientKeystorePath());
|
||||
@@ -37,7 +42,7 @@ public class MongoConfigSerializer extends AbstractDaoSerializer<MongoConfig>
|
||||
public MongoConfig fromDaoEntity(DaoEntity _d)
|
||||
{
|
||||
MongoConfig o = new MongoConfig();
|
||||
o.setHosts(CollectionUtils.asArrayList(NullUtils.cleanSplit(DaoSerializer.getString(_d, "hosts"), ",")));
|
||||
o.setHosts(DaoSerializer.getList(_d, "hosts", String.class));
|
||||
o.setUsername(DaoSerializer.getString(_d, "username"));
|
||||
o.setPassword(DaoSerializer.getString(_d, "password"));
|
||||
o.setClientKeystorePath(DaoSerializer.getString(_d, "client_keystore_path"));
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.util.Map;
|
||||
import java.util.ServiceLoader;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.bson.BsonBinaryReader;
|
||||
import org.bson.BsonBinaryWriter;
|
||||
import org.bson.Document;
|
||||
@@ -725,6 +726,14 @@ public class DaoSerializer {
|
||||
return ZipUtils.zip(toBson(_entity, true));
|
||||
}
|
||||
|
||||
public static String toBase64ZipBson(Object _o) {
|
||||
return toBase64ZipBson(toDaoEntity(_o));
|
||||
}
|
||||
|
||||
public static String toBase64ZipBson(DaoEntity _entity) {
|
||||
return Base64.encodeBase64String(toZipBson(_entity));
|
||||
}
|
||||
|
||||
public static <T> T fromZipBson(byte[] _btZipBson, Class<T> _class) {
|
||||
return DaoSerializer.fromDaoEntity(fromZipBson(_btZipBson), _class);
|
||||
}
|
||||
@@ -733,6 +742,14 @@ public class DaoSerializer {
|
||||
return fromBson(ZipUtils.unzip(_btZipBson));
|
||||
}
|
||||
|
||||
public static <T> T fromBase64ZipBson(String _zipBson, Class<T> _class) {
|
||||
return DaoSerializer.fromDaoEntity(fromBase64ZipBson(_zipBson), _class);
|
||||
}
|
||||
|
||||
public static DaoEntity fromBase64ZipBson(String _zipBson) {
|
||||
return fromBson(ZipUtils.unzip(Base64.decodeBase64(_zipBson)));
|
||||
}
|
||||
|
||||
public static <T> T fromBson(byte[] _btBson, Class<T> _class) {
|
||||
return fromDaoEntity(fromBson(_btBson), _class);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.lanternsoftware.util.dao.auth;
|
||||
|
||||
import com.lanternsoftware.util.CollectionUtils;
|
||||
import com.lanternsoftware.util.dao.annotations.DBSerializable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@DBSerializable(autogen = false)
|
||||
public class AuthCode {
|
||||
private int accountId;
|
||||
private List<Integer> auxiliaryAccountIds;
|
||||
|
||||
public AuthCode() {
|
||||
}
|
||||
|
||||
public AuthCode(int _accountId, List<Integer> _auxiliaryAccountIds) {
|
||||
accountId = _accountId;
|
||||
auxiliaryAccountIds = _auxiliaryAccountIds;
|
||||
}
|
||||
|
||||
public int getAccountId() {
|
||||
return accountId;
|
||||
}
|
||||
|
||||
public void setAccountId(int _accountId) {
|
||||
accountId = _accountId;
|
||||
}
|
||||
|
||||
public List<Integer> getAuxiliaryAccountIds() {
|
||||
return auxiliaryAccountIds;
|
||||
}
|
||||
|
||||
public void setAuxiliaryAccountIds(List<Integer> _auxiliaryAccountIds) {
|
||||
auxiliaryAccountIds = _auxiliaryAccountIds;
|
||||
}
|
||||
|
||||
public List<Integer> getAllAccountIds() {
|
||||
List<Integer> ids = new ArrayList<>();
|
||||
ids.add(accountId);
|
||||
if (auxiliaryAccountIds != null)
|
||||
ids.addAll(auxiliaryAccountIds);
|
||||
return ids;
|
||||
}
|
||||
|
||||
public boolean isAuthorized(int _accountId) {
|
||||
return accountId == _accountId || CollectionUtils.contains(auxiliaryAccountIds, _accountId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.lanternsoftware.util.dao.auth.dao;
|
||||
|
||||
import com.lanternsoftware.util.dao.auth.AuthCode;
|
||||
import com.lanternsoftware.util.CollectionUtils;
|
||||
import com.lanternsoftware.util.dao.AbstractDaoSerializer;
|
||||
import com.lanternsoftware.util.dao.DaoEntity;
|
||||
import com.lanternsoftware.util.dao.DaoProxyType;
|
||||
import com.lanternsoftware.util.dao.DaoSerializer;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class AuthCodeSerializer extends AbstractDaoSerializer<AuthCode>
|
||||
{
|
||||
@Override
|
||||
public Class<AuthCode> getSupportedClass()
|
||||
{
|
||||
return AuthCode.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DaoProxyType> getSupportedProxies() {
|
||||
return Collections.singletonList(DaoProxyType.MONGO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DaoEntity toDaoEntity(AuthCode _o)
|
||||
{
|
||||
DaoEntity d = new DaoEntity();
|
||||
d.put("account_id", _o.getAccountId());
|
||||
if (CollectionUtils.isNotEmpty(_o.getAuxiliaryAccountIds()))
|
||||
d.put("aux_account_ids", CollectionUtils.toByteArray(_o.getAuxiliaryAccountIds()));
|
||||
return d;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthCode fromDaoEntity(DaoEntity _d)
|
||||
{
|
||||
AuthCode o = new AuthCode();
|
||||
o.setAccountId(DaoSerializer.getInteger(_d, "account_id"));
|
||||
o.setAuxiliaryAccountIds(CollectionUtils.fromByteArrayOfIntegers(DaoSerializer.getByteArray(_d, "aux_account_ids")));
|
||||
return o;
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,6 @@ import com.lanternsoftware.util.dao.DaoProxyType;
|
||||
import com.lanternsoftware.util.dao.DaoSerializer;
|
||||
import com.lanternsoftware.util.dao.jdbc.DatabaseType;
|
||||
import com.lanternsoftware.util.dao.jdbc.JdbcConfig;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
com.lanternsoftware.util.dao.auth.dao.AuthCodeSerializer
|
||||
com.lanternsoftware.util.dao.jdbc.dao.JdbcConfigSerializer
|
||||
|
||||
@@ -3,172 +3,79 @@ package com.lanternsoftware.util.servlet;
|
||||
import com.lanternsoftware.util.CollectionUtils;
|
||||
import com.lanternsoftware.util.NullUtils;
|
||||
import com.lanternsoftware.util.dao.DaoEntity;
|
||||
import com.lanternsoftware.util.dao.DaoSerializer;
|
||||
import freemarker.template.Configuration;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class FreemarkerServlet extends HttpServlet {
|
||||
protected abstract Configuration getFreemarkerConfig();
|
||||
|
||||
public static String[] getPath(HttpServletRequest _request) {
|
||||
String sPath = _request.getRequestURI().substring(_request.getContextPath().length());
|
||||
if (sPath.startsWith("/"))
|
||||
sPath = sPath.substring(1);
|
||||
String[] path = sPath.split("/");
|
||||
if ((path.length == 0) || (path[0].length() == 0))
|
||||
return new String[] { "index" };
|
||||
int iExtPos = CollectionUtils.last(path).lastIndexOf(".");
|
||||
if (iExtPos > -1) {
|
||||
path[path.length - 1] = CollectionUtils.last(path).substring(0, iExtPos);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
public static void redirect(HttpServletResponse _response, String _sURL) throws IOException {
|
||||
_response.sendRedirect(_response.encodeRedirectURL(_sURL));
|
||||
}
|
||||
|
||||
public void render(HttpServletResponse _rep, String _sHtmlResourceKey, Map<String, Object> _mapModel) {
|
||||
String html = FreemarkerUtil.render(getFreemarkerConfig(), _sHtmlResourceKey, _mapModel);
|
||||
if (html == null)
|
||||
_rep.setStatus(500);
|
||||
else
|
||||
setResponseHtml(_rep, html);
|
||||
}
|
||||
public abstract class FreemarkerServlet extends LanternServlet {
|
||||
protected abstract Configuration getFreemarkerConfig();
|
||||
|
||||
public static DaoEntity model(HttpServletRequest _req, String _name, Object _value) {
|
||||
DaoEntity model = model(_req);
|
||||
model.put(_name, _value);
|
||||
return model;
|
||||
}
|
||||
public static String[] getPath(HttpServletRequest _request) {
|
||||
String sPath = _request.getRequestURI().substring(_request.getContextPath().length());
|
||||
if (sPath.startsWith("/"))
|
||||
sPath = sPath.substring(1);
|
||||
String[] path = sPath.split("/");
|
||||
if ((path.length == 0) || (path[0].length() == 0))
|
||||
return new String[]{"index"};
|
||||
int iExtPos = CollectionUtils.last(path).lastIndexOf(".");
|
||||
if (iExtPos > -1) {
|
||||
path[path.length - 1] = CollectionUtils.last(path).substring(0, iExtPos);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
protected static DaoEntity model(HttpServletRequest _req) {
|
||||
DaoEntity model = new DaoEntity("context", _req.getContextPath());
|
||||
model.put("css_version", "1.0.0");
|
||||
return model;
|
||||
}
|
||||
public static void redirect(HttpServletResponse _response, String _sURL) throws IOException {
|
||||
_response.sendRedirect(_response.encodeRedirectURL(_sURL));
|
||||
}
|
||||
|
||||
public static <T> T getSessionVar(HttpServletRequest _req, String _name) {
|
||||
return (T) _req.getSession().getAttribute(_name);
|
||||
}
|
||||
public void render(HttpServletResponse _rep, String _sHtmlResourceKey, Map<String, Object> _mapModel) {
|
||||
String html = FreemarkerUtil.render(getFreemarkerConfig(), _sHtmlResourceKey, _mapModel);
|
||||
if (html == null)
|
||||
_rep.setStatus(500);
|
||||
else
|
||||
setResponseHtml(_rep, html);
|
||||
}
|
||||
|
||||
public static void putSessionVar(HttpServletRequest _req, String _name, Object _var) {
|
||||
_req.getSession().setAttribute(_name, _var);
|
||||
}
|
||||
public static DaoEntity model(HttpServletRequest _req, String _name, Object _value) {
|
||||
DaoEntity model = model(_req);
|
||||
model.put(_name, _value);
|
||||
return model;
|
||||
}
|
||||
|
||||
protected String relativeOffset(HttpServletRequest _req) {
|
||||
String[] path = getPath(_req);
|
||||
StringBuilder offset = new StringBuilder();
|
||||
for (int i = 1; i < CollectionUtils.size(path); i++) {
|
||||
offset.append("../");
|
||||
}
|
||||
return offset.toString();
|
||||
}
|
||||
|
||||
protected Cookie getCookie(HttpServletRequest _req, String _name) {
|
||||
if (_req.getCookies() != null) {
|
||||
for (Cookie c : _req.getCookies()) {
|
||||
if (NullUtils.isEqual(c.getName(), _name))
|
||||
return c;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
protected static DaoEntity model(HttpServletRequest _req) {
|
||||
DaoEntity model = new DaoEntity("context", _req.getContextPath());
|
||||
model.put("css_version", "1.0.0");
|
||||
return model;
|
||||
}
|
||||
|
||||
public static void setResponseHtml(HttpServletResponse _response, String _sHtml) {
|
||||
setResponseEntity(_response, MediaType.TEXT_HTML, _sHtml);
|
||||
}
|
||||
public static <T> T getSessionVar(HttpServletRequest _req, String _name) {
|
||||
return (T) _req.getSession().getAttribute(_name);
|
||||
}
|
||||
|
||||
public static void setResponseEntity(HttpServletResponse _response, String _sContentType, String _sEntity) {
|
||||
setResponseEntity(_response, 200, _sContentType, _sEntity);
|
||||
}
|
||||
public static void putSessionVar(HttpServletRequest _req, String _name, Object _var) {
|
||||
_req.getSession().setAttribute(_name, _var);
|
||||
}
|
||||
|
||||
public static void setResponseEntity(HttpServletResponse _response, String _sContentType, byte[] _btData) {
|
||||
setResponseEntity(_response, 200, _sContentType, _btData);
|
||||
}
|
||||
|
||||
public static void setResponseEntity(HttpServletResponse _response, int _iStatus, String _sContentType, String _sEntity) {
|
||||
setResponseEntity(_response, _iStatus, _sContentType, NullUtils.toByteArray(_sEntity));
|
||||
}
|
||||
|
||||
public static void setResponseEntity(HttpServletResponse _response, int _iStatus, String _sContentType, byte[] _btData) {
|
||||
OutputStream os = null;
|
||||
try {
|
||||
_response.setStatus(_iStatus);
|
||||
_response.setCharacterEncoding("UTF-8");
|
||||
_response.setContentType(_sContentType);
|
||||
if ((_btData != null) && (_btData.length > 0)) {
|
||||
_response.setContentLength(_btData.length);
|
||||
os = _response.getOutputStream();
|
||||
os.write(_btData);
|
||||
} else
|
||||
_response.setContentLength(0);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
IOUtils.closeQuietly(os);
|
||||
}
|
||||
}
|
||||
|
||||
protected void zipBsonResponse(HttpServletResponse _response, Object _object)
|
||||
{
|
||||
setResponseEntity(_response, 200, MediaType.APPLICATION_OCTET_STREAM, DaoSerializer.toZipBson(_object));
|
||||
}
|
||||
|
||||
protected void jsonResponse(HttpServletResponse _response, Object _object)
|
||||
{
|
||||
setResponseEntity(_response, 200, MediaType.APPLICATION_JSON, DaoSerializer.toJson(_object));
|
||||
}
|
||||
|
||||
protected void jsonResponse(HttpServletResponse _response, String _json)
|
||||
{
|
||||
setResponseEntity(_response, 200, MediaType.APPLICATION_JSON, _json);
|
||||
}
|
||||
|
||||
protected String getRequestPayloadAsString(HttpServletRequest _req) {
|
||||
return NullUtils.toString(getRequestPayload(_req));
|
||||
}
|
||||
|
||||
protected byte[] getRequestPayload(HttpServletRequest _req) {
|
||||
InputStream is = null;
|
||||
try {
|
||||
is = _req.getInputStream();
|
||||
return IOUtils.toByteArray(is);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
finally {
|
||||
IOUtils.closeQuietly(is);
|
||||
}
|
||||
}
|
||||
|
||||
protected DaoEntity getRequestZipBson(HttpServletRequest _req) {
|
||||
return DaoSerializer.fromZipBson(getRequestPayload(_req));
|
||||
}
|
||||
|
||||
protected <T> T getRequestPayload(HttpServletRequest _req, Class<T> _retClass) {
|
||||
return DaoSerializer.fromZipBson(getRequestPayload(_req), _retClass);
|
||||
}
|
||||
|
||||
protected String[] path(HttpServletRequest _req) {
|
||||
return NullUtils.cleanSplit(NullUtils.makeNotNull(_req.getPathInfo()), "/");
|
||||
}
|
||||
|
||||
protected boolean isPath(HttpServletRequest _req, int _index, String _path) {
|
||||
return NullUtils.isEqual(_path, CollectionUtils.get(path(_req), _index));
|
||||
}
|
||||
protected String relativeOffset(HttpServletRequest _req) {
|
||||
String[] path = getPath(_req);
|
||||
StringBuilder offset = new StringBuilder();
|
||||
for (int i = 1; i < CollectionUtils.size(path); i++) {
|
||||
offset.append("../");
|
||||
}
|
||||
return offset.toString();
|
||||
}
|
||||
|
||||
protected Cookie getCookie(HttpServletRequest _req, String _name) {
|
||||
if (_req.getCookies() != null) {
|
||||
for (Cookie c : _req.getCookies()) {
|
||||
if (NullUtils.isEqual(c.getName(), _name))
|
||||
return c;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
package com.lanternsoftware.util.servlet;
|
||||
|
||||
import com.lanternsoftware.util.CollectionUtils;
|
||||
import com.lanternsoftware.util.NullUtils;
|
||||
import com.lanternsoftware.util.dao.DaoEntity;
|
||||
import com.lanternsoftware.util.dao.DaoSerializer;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public abstract class LanternServlet extends HttpServlet {
|
||||
public static void setResponseHtml(HttpServletResponse _response, String _sHtml) {
|
||||
setResponseEntity(_response, MediaType.TEXT_HTML, _sHtml);
|
||||
}
|
||||
|
||||
public static void setResponseEntity(HttpServletResponse _response, String _sContentType, String _sEntity) {
|
||||
setResponseEntity(_response, 200, _sContentType, _sEntity);
|
||||
}
|
||||
|
||||
public static void setResponseEntity(HttpServletResponse _response, String _sContentType, byte[] _btData) {
|
||||
setResponseEntity(_response, 200, _sContentType, _btData);
|
||||
}
|
||||
|
||||
public static void setResponseEntity(HttpServletResponse _response, int _iStatus, String _sContentType, String _sEntity) {
|
||||
setResponseEntity(_response, _iStatus, _sContentType, NullUtils.toByteArray(_sEntity));
|
||||
}
|
||||
|
||||
public static void setResponseEntity(HttpServletResponse _response, int _iStatus, String _sContentType, byte[] _btData) {
|
||||
OutputStream os = null;
|
||||
try {
|
||||
_response.setStatus(_iStatus);
|
||||
_response.setCharacterEncoding("UTF-8");
|
||||
_response.setContentType(_sContentType);
|
||||
if ((_btData != null) && (_btData.length > 0)) {
|
||||
_response.setContentLength(_btData.length);
|
||||
os = _response.getOutputStream();
|
||||
os.write(_btData);
|
||||
} else
|
||||
_response.setContentLength(0);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
IOUtils.closeQuietly(os);
|
||||
}
|
||||
}
|
||||
|
||||
protected void zipBsonResponse(HttpServletResponse _response, Object _object)
|
||||
{
|
||||
setResponseEntity(_response, 200, MediaType.APPLICATION_OCTET_STREAM, DaoSerializer.toZipBson(_object));
|
||||
}
|
||||
|
||||
protected void jsonResponse(HttpServletResponse _response, Object _object)
|
||||
{
|
||||
setResponseEntity(_response, 200, MediaType.APPLICATION_JSON, DaoSerializer.toJson(_object));
|
||||
}
|
||||
|
||||
protected void jsonResponse(HttpServletResponse _response, String _json)
|
||||
{
|
||||
setResponseEntity(_response, 200, MediaType.APPLICATION_JSON, _json);
|
||||
}
|
||||
|
||||
protected String getRequestPayloadAsString(HttpServletRequest _req) {
|
||||
return NullUtils.toString(getRequestPayload(_req));
|
||||
}
|
||||
|
||||
protected byte[] getRequestPayload(HttpServletRequest _req) {
|
||||
InputStream is = null;
|
||||
try {
|
||||
is = _req.getInputStream();
|
||||
return IOUtils.toByteArray(is);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
finally {
|
||||
IOUtils.closeQuietly(is);
|
||||
}
|
||||
}
|
||||
|
||||
protected DaoEntity getRequestZipBson(HttpServletRequest _req) {
|
||||
return DaoSerializer.fromZipBson(getRequestPayload(_req));
|
||||
}
|
||||
|
||||
protected <T> T getRequestPayload(HttpServletRequest _req, Class<T> _retClass) {
|
||||
return DaoSerializer.fromZipBson(getRequestPayload(_req), _retClass);
|
||||
}
|
||||
|
||||
protected String[] path(HttpServletRequest _req) {
|
||||
return NullUtils.cleanSplit(NullUtils.makeNotNull(_req.getPathInfo()), "/");
|
||||
}
|
||||
|
||||
protected boolean isPath(HttpServletRequest _req, int _index, String _path) {
|
||||
return NullUtils.isEqual(_path, CollectionUtils.get(path(_req), _index));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user