Commit 738ddd38 authored by Nguyen Ha's avatar Nguyen Ha

add Redis

parent 35578fa2
package com.viettel.campaign.filter;
import com.viettel.campaign.utils.RedisUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
......@@ -14,20 +15,40 @@ public class CorsFilter implements Filter {
private Logger logger = Logger.getLogger(CorsFilter.class);
@Override
public void init(FilterConfig filterConfig) throws ServletException {
public void init(FilterConfig filterConfig){
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Origin, Authorization, X-Requested-With, Content-Type, Accept, token1, X-Auth-Token");
try {
HttpServletRequest request = (HttpServletRequest) req;
if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
chain.doFilter(req, resp);
return;
}
logger.info("uri: "+ request.getRequestURI());
if ("/".equals(request.getRequestURI())) {
chain.doFilter(req, resp);
return;
}
String xAuthToken = request.getHeader("X-Auth-Token");
if (xAuthToken == null || "".equals(xAuthToken)) {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "The token is null.");
return;
}
Object obj = RedisUtil.getInstance().get(xAuthToken);
if (obj instanceof UserSession) {
chain.doFilter(req, resp);
} catch (Exception e) {
logger.error(e.getMessage(), e);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
} else {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "The token is invalid.");
}
}
......
package com.viettel.campaign.utils;
import com.viettel.security.PassTranformer;
import org.apache.log4j.Logger;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
/**
* @author hanv_itsol
* @project campaign
*/
public class Config {
public static final String APP_CONF_FILE_PATH = System.getProperty("user.dir") + File.separator + "etc" + File.separator + "app.conf";
public static final String LOG_CONF_FILE_PATH = System.getProperty("user.dir") + File.separator + "etc" + File.separator + "log.conf";
public static String amcd_xmlrpc_url;
public static int num_client_amcd_xmlprc;
// public static final int AGENT_ANSWER_TIMEOUT = 10;
// public static final int AGENT_ACCEPT_TIMEOUT = 10;
// public static final int SMS_ANSWER_TIMEOUT = 24 * 60 * 60;
// public static final int INTERVAL_SCAN_SMS = 100;
// public static final int NUM_SMSGW = 1;
// public static final int NUM_RETRY_SEND_SMSGW = 3;
// public static final int INTERVAL_RETRY_SEND_SMSGW = 10;
// public static final String SMS_SEND_OUT_CHANNEL = "198";
// public static final int INTERVAL_SCAN_FACEBOOK = 100;
// public static final int FACEBOOK_ANSWER_TIMEOUT = 24 * 60 * 60;
// public static final String prefixKeyRedis = "";
// public static final String redisPatternSMS = "";
// public static final String redisPatternFacebook = "";
// public static final String rabbitConnection;
// public static final String fbGatewayUser;
// public static final String fbGatewayPass;
// public static final String virtual_host;
// public static final String rb_queuename_kpi_log;
// public static final String facebook_text_queue_name;
// public static final String app_code;
// public static final String TICKET_SERVICES_URL;
public static final int redisTimeout;
public static final String redisAddress;
// public static String link_services_ticket;
private static final Properties properties = new Properties();
static {
org.apache.log4j.PropertyConfigurator.configure(Config.LOG_CONF_FILE_PATH);
try {
properties.load(new FileInputStream(APP_CONF_FILE_PATH));
} catch (IOException ex) {
Logger.getLogger(Config.class.getName()).error(ex.getMessage(), ex);
}
PassTranformer.setInputKey("Ipcc#987654321#@!");
// rabbitConnection = properties.getProperty("rabbit_connection_string");
// fbGatewayUser = PassTranformer.decrypt(properties.getProperty("rabbit_user", "").trim());
// fbGatewayPass = PassTranformer.decrypt(properties.getProperty("rabbit_pass", "").trim());
// virtual_host = properties.getProperty("virtual_host", "/").trim();
// facebook_text_queue_name = properties.getProperty("facebook_text_queue_name", "mt2facebooktext").trim();
// rb_queuename_kpi_log = properties.getProperty("rb_queuename_kpi_log", "econtact_kpi_log").trim();
// app_code = properties.getProperty("app_code", "ECONTACT").trim();
// TICKET_SERVICES_URL = properties.getProperty("ticket_services_url", "").trim();
redisAddress = properties.getProperty("redis_address", "").trim();
redisTimeout = Integer.valueOf(properties.getProperty("redis_timeout", "3000").trim());
// link_services_ticket = properties.getProperty("link_services_ticket");
}
private static String getString(String key, String defaultValue) {
return properties.getProperty(key, defaultValue).trim();
}
private static int getInt(String key, int defaultValue) {
return Integer.valueOf(properties.getProperty(key, "" + defaultValue).trim());
}
}
package com.viettel.campaign.utils;
import org.apache.log4j.Logger;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.HostAndPort;
import java.io.Serializable;
import java.util.*;
/**
* @author hanv_itsol
* @project campaign
*/
public class RedisUtil {
private static final Logger logger = Logger.getLogger(RedisUtil.class);
private static volatile RedisUtil INSTANCE = null;
private JedisCluster jedisCluster;
private String redisAddress;
private int redisTimeout;
private RedisUtil(JedisCluster jedisCluster) {
this.jedisCluster = jedisCluster;
}
public RedisUtil(String redisAddress, int redisTimeout) {
this.redisAddress = redisAddress;
this.redisTimeout = redisTimeout;
}
public RedisUtil() {
}
public synchronized RedisUtil setup() {
if (INSTANCE == null) {
logger.info("Start connect Redis: " + redisAddress);
Set<HostAndPort> hostAndPortNodes = new HashSet();
String[] hostAndPorts = redisAddress.split(",");
for (String hostAndPort : hostAndPorts) {
String[] parts = hostAndPort.split(":");
String host = parts[0];
int port = Integer.parseInt(parts[1].trim());
hostAndPortNodes.add(new HostAndPort(host, port));
}
INSTANCE = new RedisUtil(new JedisCluster(hostAndPortNodes, redisTimeout));
}
return INSTANCE;
}
public static synchronized RedisUtil getInstance() {
if (INSTANCE == null) {
INSTANCE = new RedisUtil().setup();
}
return INSTANCE;
}
public JedisCluster getRedis() {
return INSTANCE.jedisCluster;
}
public void setRedisAddress(String dress) {
redisAddress = dress;
}
public void setRedisTimeout(int timeout) {
redisTimeout = timeout;
}
public String getRedisAddress() {
return redisAddress;
}
public int getRedisTimeout() {
return redisTimeout;
}
public boolean set(String key, Serializable value) {
if (key == null) {
return false;
}
// Jedis jedis = null;
try {
JedisCluster jedis = getRedis();
jedis.set(key.getBytes(), org.springframework.util.SerializationUtils.serialize(value));
jedis.expire(key.getBytes(), 60); // exprire
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
}
return true;
}
public Object get(String key) {
// Jedis jedis = null;
Object object = null;
try {
// jedis = pool.getResource();
JedisCluster jedis = getRedis();
byte[] value = jedis.get(key.getBytes());
if (value != null && value.length > 0) {
object = (Object) org.springframework.util.SerializationUtils.deserialize(value);
}
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
}
return object;
}
public void del(String key) {
// Jedis jedis = null;
// Object object = null;
try {
// jedis = pool.getResource();
JedisCluster jedis = getRedis();
jedis.del(key);
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment