diff --git a/src/main/java/com/nis/restful/RestBusinessCode.java b/src/main/java/com/nis/restful/RestBusinessCode.java index 3f2c39e..fb41663 100644 --- a/src/main/java/com/nis/restful/RestBusinessCode.java +++ b/src/main/java/com/nis/restful/RestBusinessCode.java @@ -620,6 +620,15 @@ public enum RestBusinessCode { * Redis关联关系库中未找到配置对应的redisdb信息 */ RedisDBRelationNotExistsInRedis(5003005,"Redis关联关系库中未找到配置对应的redisdb信息"), + /** + * 保存文件到redis发生了异常 + */ + SaveFileToRedisError(5003006,"保存文件到redis发生了异常"), + + + + + /** * Map中缺少编译配置信息 diff --git a/src/main/java/com/nis/util/File2Redis.java b/src/main/java/com/nis/util/File2Redis.java new file mode 100644 index 0000000..d528ee4 --- /dev/null +++ b/src/main/java/com/nis/util/File2Redis.java @@ -0,0 +1,50 @@ +package com.nis.util; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; + +import com.nis.restful.RestBusinessCode; +import com.nis.restful.ServiceRuntimeException; + +import redis.clients.jedis.Transaction; + +public class File2Redis { + + public static String file2Redis(String url, Transaction transaction) { + try { + byte[] fileByte = downLoadFromUrl(url); + String md5 = MD5Utils.getMd5ByIS(new ByteArrayInputStream(fileByte)); + String key = "redis://_FILE_" + md5; + transaction.set(key.getBytes(), fileByte); + return key; + } catch (Exception e) { + throw new ServiceRuntimeException("保存文件到redis发生了异常:" + e.getMessage(), + RestBusinessCode.SaveFileToRedisError.getValue()); + } + } + + public static byte[] downLoadFromUrl(String urlStr) throws IOException { + URL url = new URL(urlStr); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + // 设置超时间为30秒 + conn.setConnectTimeout(3 * 10000); + // 防止屏蔽程序抓取而返回403错误 + conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); + // 得到输入流 + InputStream inputStream = conn.getInputStream(); + ByteArrayOutputStream baosOutputStream = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + int len; + while ((len = inputStream.read(buffer)) > -1) { + baosOutputStream.write(buffer, 0, len); + } + baosOutputStream.flush(); + return baosOutputStream.toByteArray(); + } + + +} diff --git a/src/main/java/com/nis/util/MD5Utils.java b/src/main/java/com/nis/util/MD5Utils.java index ce925d9..9a58bb4 100644 --- a/src/main/java/com/nis/util/MD5Utils.java +++ b/src/main/java/com/nis/util/MD5Utils.java @@ -3,81 +3,95 @@ */ package com.nis.util; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; -import java.util.List; -import java.util.Locale; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import javax.servlet.http.HttpServletRequest; - -import org.apache.commons.lang3.StringEscapeUtils; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.context.request.ServletRequestAttributes; -import org.springframework.web.servlet.LocaleResolver; - -import com.ckfinder.connector.ServletContextFactory; -import com.google.common.collect.Lists; -import com.nis.web.service.SpringContextHolder; +import org.apache.commons.codec.digest.DigestUtils; /** * 字符串工具类, 继承org.apache.commons.lang3.StringUtils类 * @author ThinkGem * @version 2013-05-22 */ -public class MD5Utils{ - +public class MD5Utils { + /** * 字符串转为MD532位小写 * @param plain * @return */ - public static String md5LowerCase(String plain) throws Exception{ + public static String md5LowerCase(String plain) throws Exception { String re_md5 = new String(); - MessageDigest md = MessageDigest.getInstance("MD5"); - md.update(plain.getBytes()); - byte b[] = md.digest(); - - int i; - - StringBuffer buf = new StringBuffer(""); - for (int offset = 0; offset < b.length; offset++) { + MessageDigest md = MessageDigest.getInstance("MD5"); + md.update(plain.getBytes()); + byte b[] = md.digest(); + + int i; + + StringBuffer buf = new StringBuffer(""); + for (int offset = 0; offset < b.length; offset++) { i = b[offset]; if (i < 0) - i += 256; + i += 256; if (i < 16) - buf.append("0"); + buf.append("0"); buf.append(Integer.toHexString(i)); - } - - re_md5 = buf.toString(); - return re_md5; } - public static byte[] createChecksum(String filename) throws IOException, NoSuchAlgorithmException{ - InputStream fis=new FileInputStream(filename); - byte[] buffer=new byte[1024]; - MessageDigest complete=MessageDigest.getInstance("MD5"); + + re_md5 = buf.toString(); + return re_md5; + } + + public static byte[] createChecksum(String filename) throws IOException, NoSuchAlgorithmException { + InputStream fis = new FileInputStream(filename); + byte[] buffer = new byte[1024]; + MessageDigest complete = MessageDigest.getInstance("MD5"); int numRead; - do{ - numRead=fis.read(buffer); - if(numRead>0){ - complete.update(buffer,0,numRead); + do { + numRead = fis.read(buffer); + if (numRead > 0) { + complete.update(buffer, 0, numRead); } - }while(numRead!=-1); + } while (numRead != -1); fis.close(); return complete.digest(); } - public static String getMD5Checksum(String filename) throws NoSuchAlgorithmException, IOException{ - byte[] b=createChecksum(filename); - StringBuffer result=new StringBuffer(); - for(int i=0;i urlList = new ArrayList(); for (String valStr : valSplit) { if (!StringUtils.isEmpty(valStr) && valStr.trim().startsWith("[")) { valStr = valStr.trim().replace("[", "").replace("]", ""); - valBF.append(map.get(valStr)); +// if (service == 520) { +// if (valStr.equals("private_key_file") +// || valStr.equals("public_key_file")) { +// String fileUrl = map.get(valStr); +// urlList.add(fileUrl); +// } +// } +// if(valStr.equals("op_time")) { +// if (urlList.size() > 0) { +// for (String fileUrl : urlList) { +// String key = File2Redis.file2Redis(fileUrl, transaction); +// logger.info("向{}号redis数据库添加了一条文件配置,key是{}",redisDBIndex,key); +// valBF.append(key); +// valBF.append("\t"); +// } +// } +// } + + if(valStr.contains("redisFile_")) { + String key = File2Redis.file2Redis(map.get(valStr), transaction); + logger.info("向{}号redis数据库添加了一条文件配置,key是{}",redisDBIndex,key); + valBF.append(key); + //valBF.append("\t"); + }else { + valBF.append(map.get(valStr)); + } } else if (valStr.equals(" ")) { valBF.append(" "); } else if (valStr.equals("\\t")) {// xml中是字符串的\t这里判断的时候需要转义为\\t,但是添加的时候需要添加\t不是\\t @@ -270,7 +297,7 @@ public class ConfigJedisServiceimpl implements ConfigRedisService { && redisStatisticsRealDBIndex < Configurations.getIntProperty("maxRedisDBIndex", 16)) { List list = configMap.get(redisDBIndex); if (list != null && list.size() > 0) { - transaction.select(redisStatisticsRealDBIndex);//选择实时统计库 + transaction.select(redisStatisticsRealDBIndex);// 选择实时统计库 String redisStatisticsReal = Configurations.getStringProperty("redis-statisticsReal", "[COMPILE_ID];\t;[SERVICE];\t;[ACTION];\t;[CONT_TYPE];\t;[ATTR_TYPE];\t;[CONT_LABEL];\t;[TASK_ID];\t;[AFFAIR_ID];\t;[DO_BLACKLIST];\t;[DO_LOG];\t;[EFFECTIVE_RANGE];\t;[START_TIME];\t;[END_TIME];\t;[USER_REGION];\t;[IS_VALID];\t;[GROUP_NUM];\t;[FATHER_CFG_ID];\t;[OP_TIME]"); String[] redisStatisticsRealArr = redisStatisticsReal.split(";"); @@ -461,11 +488,13 @@ public class ConfigJedisServiceimpl implements ConfigRedisService { int service = maatConfig.getService(); if (ServiceAndRDBIndexReal.isAddASU(service)) { if (!keySet.contains(TAPREDISDB)) { - throw new ServiceRuntimeException("业务类型:" + service + ",需要向阀门" + TAPREDISDB + "号库分发,但是当前只往" - + keySet + "库下发,请检查阀门编号或者当前业务对应的配置文件是否正确",RestBusinessCode.PropertiesIsError.getValue()); + throw new ServiceRuntimeException( + "业务类型:" + service + ",需要向阀门" + TAPREDISDB + "号库分发,但是当前只往" + keySet + + "库下发,请检查阀门编号或者当前业务对应的配置文件是否正确", + RestBusinessCode.PropertiesIsError.getValue()); } } - + MaatXmlConfig maatXmlConfig = ReadMaatXmlUtil.getMaatConfigByService(service); setConfig(maatConfig, maatXmlConfig, maatVersion, service, transaction, redisDBIndex); @@ -695,7 +724,7 @@ public class ConfigJedisServiceimpl implements ConfigRedisService { } } } - } else if (isReuse && (type == 12 || type == 13 || type == 14|| type == 15|| type == 18)) {// 将数据往临时库复制一份,无论临时库里有没有都可以直接写,有则覆盖,不影响 + } else if (isReuse && (type == 12 || type == 13 || type == 14 || type == 15 || type == 18)) {// 将数据往临时库复制一份,无论临时库里有没有都可以直接写,有则覆盖,不影响 int tmpStorageReuseRegionDB = Configurations.getIntProperty("tmpStorageReuseRegionDB", 15); transaction.select(tmpStorageReuseRegionDB); transaction.set(maatKey.toUpperCase(), valBF.toString()); diff --git a/src/main/resources/maatXml/applicationConfig-maatRedis.xml b/src/main/resources/maatXml/applicationConfig-maatRedis.xml index f70c50e..54853f9 100644 --- a/src/main/resources/maatXml/applicationConfig-maatRedis.xml +++ b/src/main/resources/maatXml/applicationConfig-maatRedis.xml @@ -139,7 +139,7 @@ EFFECTIVE_RULE;:;{un_maat_table_name};,;[cfg_id] - [cfg_id];\t;[service];\t;[keyring_name];\t;[keyring_type];\t;[private_key_file];\t;[public_key_file];\t;[expire_after];\t;[public_key_algo];\t;[crl];\t;[is_valid];\t;[op_time];&nbsp;0;\n + [cfg_id];\t;[service];\t;[keyring_name];\t;[keyring_type];\t;[private_key_file];\t;[public_key_file];\t;[expire_after];\t;[public_key_algo];\t;[crl];\t;[is_valid];\t;[redisFile_private_key_file];\t;[redisFile_public_key_file];\t;[op_time];&nbsp;0;\n 1