implemented OSS download and binary fetch
This commit is contained in:
@@ -3,12 +3,11 @@ package net.irext.decoder.businesslogic;
|
||||
import net.irext.decoder.alioss.OSSHelper;
|
||||
import net.irext.decoder.model.IRBinary;
|
||||
import net.irext.decoder.model.RemoteIndex;
|
||||
import net.irext.decoder.redisrepo.IIRBinaryRepository;
|
||||
import net.irext.decoder.cache.IIRBinaryRepository;
|
||||
import net.irext.decoder.utils.FileUtil;
|
||||
import net.irext.decoder.utils.LoggerUtil;
|
||||
import net.irext.decoder.utils.MD5Util;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import java.io.File;
|
||||
@@ -26,17 +25,15 @@ import java.security.MessageDigest;
|
||||
* Revision log:
|
||||
* 2018-12-08: created by strawmanbobi
|
||||
*/
|
||||
@Controller
|
||||
public class DecodeLogic {
|
||||
|
||||
private static final String TAG = DecodeLogic.class.getSimpleName();
|
||||
|
||||
private static final String IR_BIN_FILE_PREFIX = "irda_";
|
||||
private static final String IR_BIN_FILE_SUFFIX = ".bin";
|
||||
|
||||
private static DecodeLogic decodeLogic;
|
||||
|
||||
@Autowired
|
||||
private ServletContext context;
|
||||
|
||||
public static DecodeLogic getInstance() {
|
||||
if (null == decodeLogic) {
|
||||
decodeLogic = new DecodeLogic();
|
||||
@@ -48,7 +45,8 @@ public class DecodeLogic {
|
||||
|
||||
}
|
||||
|
||||
public byte[] openIRBinary(IIRBinaryRepository irBinaryRepository, RemoteIndex remoteIndex) {
|
||||
public byte[] openIRBinary(ServletContext context, IIRBinaryRepository irBinaryRepository,
|
||||
RemoteIndex remoteIndex) {
|
||||
if (null == remoteIndex) {
|
||||
return null;
|
||||
}
|
||||
@@ -56,6 +54,9 @@ public class DecodeLogic {
|
||||
String checksum = remoteIndex.getBinaryMd5().toUpperCase();
|
||||
String remoteMap = remoteIndex.getRemoteMap();
|
||||
|
||||
LoggerUtil.getInstance().trace(TAG, "checksum for remoteIndex " +
|
||||
remoteIndex.getId() + " = " + checksum);
|
||||
|
||||
IRBinary irBinary = irBinaryRepository.find(remoteIndex.getId());
|
||||
if (null != irBinary) {
|
||||
byte[] binaries = irBinary.getBinary();
|
||||
@@ -63,7 +64,8 @@ public class DecodeLogic {
|
||||
System.out.println("binary content fetched from redis : " + binaries.length);
|
||||
// validate binary content
|
||||
String cachedChecksum =
|
||||
MD5Util.byteArrayToHexString(MessageDigest.getInstance("MD5").digest(binaries)).toUpperCase();
|
||||
MD5Util.byteArrayToHexString(MessageDigest.getInstance("MD5")
|
||||
.digest(binaries)).toUpperCase();
|
||||
if (cachedChecksum.equals(checksum)) {
|
||||
return binaries;
|
||||
}
|
||||
@@ -71,17 +73,21 @@ public class DecodeLogic {
|
||||
}
|
||||
|
||||
// otherwise, read from file or OSS
|
||||
String downloadPath = context.getRealPath("") + "bin_cache" + File.separator;
|
||||
String fileName = IR_BIN_FILE_PREFIX + remoteMap + IR_BIN_FILE_SUFFIX;
|
||||
String localFilePath = downloadPath + fileName;
|
||||
if (null != context) {
|
||||
String downloadPath = context.getRealPath("") + "bin_cache" + File.separator;
|
||||
String fileName = IR_BIN_FILE_PREFIX + remoteMap + IR_BIN_FILE_SUFFIX;
|
||||
String localFilePath = downloadPath + fileName;
|
||||
|
||||
File binFile = new File(localFilePath);
|
||||
FileInputStream fin = getFile(binFile, downloadPath, fileName, checksum);
|
||||
if (null != fin) {
|
||||
byte[] binaries = IOUtils.toByteArray(fin);
|
||||
System.out.println("binary content get, save it to redis");
|
||||
irBinaryRepository.add(new IRBinary(remoteIndex.getId(), binaries));
|
||||
return binaries;
|
||||
File binFile = new File(localFilePath);
|
||||
FileInputStream fin = getFile(binFile, downloadPath, fileName, checksum);
|
||||
if (null != fin) {
|
||||
byte[] binaries = IOUtils.toByteArray(fin);
|
||||
System.out.println("binary content get, save it to redis");
|
||||
irBinaryRepository.add(new IRBinary(remoteIndex.getId(), binaries));
|
||||
return binaries;
|
||||
}
|
||||
} else {
|
||||
LoggerUtil.getInstance().trace(TAG, "servlet context is null");
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
|
||||
@@ -4,6 +4,8 @@ import net.irext.decoder.mapper.RemoteIndexMapper;
|
||||
import net.irext.decoder.model.RemoteIndex;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Filename: CollectCodeLogic
|
||||
* Revised: Date: 2018-12-08
|
||||
@@ -33,6 +35,10 @@ public class IndexLogic {
|
||||
}
|
||||
|
||||
public RemoteIndex getRemoteIndex(int indexId) {
|
||||
return remoteIndexMapper.getRemoteIndexById(indexId);
|
||||
List<RemoteIndex> remoteIndexList = remoteIndexMapper.getRemoteIndexById(indexId);
|
||||
if (null != remoteIndexList && remoteIndexList.size() > 0) {
|
||||
return remoteIndexList.get(0);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package net.irext.decoder.redisrepo;
|
||||
package net.irext.decoder.cache;
|
||||
|
||||
import net.irext.decoder.model.DecodeSession;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package net.irext.decoder.redisrepo;
|
||||
package net.irext.decoder.cache;
|
||||
|
||||
import net.irext.decoder.model.IRBinary;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package net.irext.decoder.redisrepo.impl;
|
||||
package net.irext.decoder.cache.impl;
|
||||
|
||||
import net.irext.decoder.model.DecodeSession;
|
||||
import net.irext.decoder.redisrepo.IDecodeSessionRepository;
|
||||
import net.irext.decoder.cache.IDecodeSessionRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.HashOperations;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
@@ -1,13 +1,14 @@
|
||||
package net.irext.decoder.redisrepo.impl;
|
||||
package net.irext.decoder.cache.impl;
|
||||
|
||||
import net.irext.decoder.model.IRBinary;
|
||||
import net.irext.decoder.redisrepo.IIRBinaryRepository;
|
||||
import net.irext.decoder.cache.IIRBinaryRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.HashOperations;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -22,8 +23,9 @@ import java.util.Map;
|
||||
*/
|
||||
@Repository
|
||||
public class IRBinaryRepositoryImpl implements IIRBinaryRepository {
|
||||
private static final String KEY = "SESSION_KEY";
|
||||
private static final String KEY = "BINARY_KEY";
|
||||
|
||||
@Resource(name="redisTemplate")
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
private HashOperations hashOperations;
|
||||
|
||||
@@ -2,8 +2,11 @@ package net.irext.decoder.mapper;
|
||||
|
||||
import net.irext.decoder.model.RemoteIndex;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.ResultMap;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Filename: RemoteIndexMapper.java
|
||||
* Revised: Date: 2018-12-08
|
||||
@@ -18,5 +21,6 @@ import org.apache.ibatis.annotations.Select;
|
||||
public interface RemoteIndexMapper {
|
||||
|
||||
@Select("SELECT * FROM remote_index WHERE id = #{id}")
|
||||
RemoteIndex getRemoteIndexById(int id);
|
||||
@ResultMap("BaseResultMap")
|
||||
List<RemoteIndex> getRemoteIndexById(int id);
|
||||
}
|
||||
|
||||
@@ -4,8 +4,8 @@ import net.irext.decoder.businesslogic.DecodeLogic;
|
||||
import net.irext.decoder.businesslogic.IndexLogic;
|
||||
import net.irext.decoder.mapper.RemoteIndexMapper;
|
||||
import net.irext.decoder.model.RemoteIndex;
|
||||
import net.irext.decoder.redisrepo.IDecodeSessionRepository;
|
||||
import net.irext.decoder.redisrepo.IIRBinaryRepository;
|
||||
import net.irext.decoder.cache.IDecodeSessionRepository;
|
||||
import net.irext.decoder.cache.IIRBinaryRepository;
|
||||
import net.irext.decoder.request.CloseRequest;
|
||||
import net.irext.decoder.request.DecodeRequest;
|
||||
import net.irext.decoder.request.OpenRequest;
|
||||
@@ -16,11 +16,12 @@ import net.irext.decoder.service.base.AbstractBaseService;
|
||||
import net.irext.decoder.utils.LoggerUtil;
|
||||
import net.irext.decodesdk.bean.ACStatus;
|
||||
import net.irext.decodesdk.utils.Constants;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
/**
|
||||
* Filename: IRDecodeService.java
|
||||
* Revised: Date: 2018-12-16
|
||||
@@ -33,12 +34,16 @@ import org.springframework.web.bind.annotation.*;
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/irext")
|
||||
@Service("IRDecodeService")
|
||||
public class IRDecodeService extends AbstractBaseService {
|
||||
|
||||
private static final String TAG = IRDecodeService.class.getSimpleName();
|
||||
|
||||
private RemoteIndexMapper remoteIndexMapper;
|
||||
|
||||
@Autowired
|
||||
private ServletContext context;
|
||||
|
||||
@Autowired
|
||||
private IIRBinaryRepository irBinaryRepository;
|
||||
|
||||
@@ -61,10 +66,16 @@ public class IRDecodeService extends AbstractBaseService {
|
||||
if (null == remoteIndex) {
|
||||
response.setStatus(new Status(Constants.ERROR_CODE_NETWORK_ERROR, ""));
|
||||
return response;
|
||||
} else {
|
||||
LoggerUtil.getInstance().trace(TAG, "remoteIndex get : " + remoteIndex.getId() + ", " +
|
||||
remoteIndex.getRemoteMap());
|
||||
}
|
||||
byte []binaryContent = DecodeLogic.getInstance().openIRBinary(irBinaryRepository, remoteIndex);
|
||||
System.out.println("binary content fetched : " + binaryContent.length);
|
||||
byte []binaryContent = DecodeLogic.getInstance().openIRBinary(context, irBinaryRepository, remoteIndex);
|
||||
|
||||
if (null != binaryContent) {
|
||||
LoggerUtil.getInstance().trace(TAG,"binary content fetched : " + binaryContent.length);
|
||||
}
|
||||
response.setStatus(new Status(Constants.ERROR_CODE_SUCCESS, ""));
|
||||
return response;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="net.irext.decoder.mapper.RemoteIndexMapper">
|
||||
<resultMap id="BaseResultMap" type="net.irext.decoder.model.RemoteIndex">
|
||||
<!--
|
||||
WARNING - @mbggenerated
|
||||
This element is automatically generated by MyBatis Generator, do not modify.
|
||||
This element was generated on Thu May 04 12:06:44 CST 2017.
|
||||
-->
|
||||
<id column="id" property="id" jdbcType="INTEGER"/>
|
||||
<result column="category_id" property="categoryId" jdbcType="INTEGER"/>
|
||||
<result column="category_name" property="categoryName" jdbcType="VARCHAR"/>
|
||||
<result column="brand_id" property="brandId" jdbcType="INTEGER"/>
|
||||
<result column="brand_name" property="brandName" jdbcType="VARCHAR"/>
|
||||
<result column="city_code" property="cityCode" jdbcType="VARCHAR"/>
|
||||
<result column="city_name" property="cityName" jdbcType="VARCHAR"/>
|
||||
<result column="operator_id" property="operatorId" jdbcType="VARCHAR"/>
|
||||
<result column="operator_name" property="operatorName" jdbcType="VARCHAR"/>
|
||||
<result column="protocol" property="protocol" jdbcType="VARCHAR"/>
|
||||
<result column="remote" property="remote" jdbcType="VARCHAR"/>
|
||||
<result column="remote_map" property="remoteMap" jdbcType="VARCHAR"/>
|
||||
<result column="status" property="status" jdbcType="TINYINT"/>
|
||||
<result column="sub_cate" property="subCate" jdbcType="TINYINT"/>
|
||||
<result column="priority" property="priority" jdbcType="INTEGER"/>
|
||||
<result column="remote_number" property="remoteNumber" jdbcType="VARCHAR"/>
|
||||
<result column="operator_name_tw" property="operatorNameTw" jdbcType="VARCHAR"/>
|
||||
<result column="category_name_tw" property="categoryNameTw" jdbcType="VARCHAR"/>
|
||||
<result column="brand_name_tw" property="brandNameTw" jdbcType="VARCHAR"/>
|
||||
<result column="city_name_tw" property="cityNameTw" jdbcType="VARCHAR"/>
|
||||
<result column="binary_md5" property="binaryMd5" jdbcType="CHAR"/>
|
||||
<result column="contributor" property="contributor" jdbcType="VARCHAR"/>
|
||||
<result column="update_time" property="updateTime" jdbcType="CHAR"/>
|
||||
</resultMap>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user