implemented OSS download and binary fetch

This commit is contained in:
strawmanbobi
2019-01-02 21:47:04 +08:00
parent b72b8e80c6
commit 83536a6857
9 changed files with 97 additions and 34 deletions

View File

@@ -3,12 +3,11 @@ package net.irext.decoder.businesslogic;
import net.irext.decoder.alioss.OSSHelper; import net.irext.decoder.alioss.OSSHelper;
import net.irext.decoder.model.IRBinary; import net.irext.decoder.model.IRBinary;
import net.irext.decoder.model.RemoteIndex; 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.FileUtil;
import net.irext.decoder.utils.LoggerUtil;
import net.irext.decoder.utils.MD5Util; import net.irext.decoder.utils.MD5Util;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import java.io.File; import java.io.File;
@@ -26,17 +25,15 @@ import java.security.MessageDigest;
* Revision log: * Revision log:
* 2018-12-08: created by strawmanbobi * 2018-12-08: created by strawmanbobi
*/ */
@Controller
public class DecodeLogic { 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_PREFIX = "irda_";
private static final String IR_BIN_FILE_SUFFIX = ".bin"; private static final String IR_BIN_FILE_SUFFIX = ".bin";
private static DecodeLogic decodeLogic; private static DecodeLogic decodeLogic;
@Autowired
private ServletContext context;
public static DecodeLogic getInstance() { public static DecodeLogic getInstance() {
if (null == decodeLogic) { if (null == decodeLogic) {
decodeLogic = new 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) { if (null == remoteIndex) {
return null; return null;
} }
@@ -56,6 +54,9 @@ public class DecodeLogic {
String checksum = remoteIndex.getBinaryMd5().toUpperCase(); String checksum = remoteIndex.getBinaryMd5().toUpperCase();
String remoteMap = remoteIndex.getRemoteMap(); String remoteMap = remoteIndex.getRemoteMap();
LoggerUtil.getInstance().trace(TAG, "checksum for remoteIndex " +
remoteIndex.getId() + " = " + checksum);
IRBinary irBinary = irBinaryRepository.find(remoteIndex.getId()); IRBinary irBinary = irBinaryRepository.find(remoteIndex.getId());
if (null != irBinary) { if (null != irBinary) {
byte[] binaries = irBinary.getBinary(); byte[] binaries = irBinary.getBinary();
@@ -63,7 +64,8 @@ public class DecodeLogic {
System.out.println("binary content fetched from redis : " + binaries.length); System.out.println("binary content fetched from redis : " + binaries.length);
// validate binary content // validate binary content
String cachedChecksum = String cachedChecksum =
MD5Util.byteArrayToHexString(MessageDigest.getInstance("MD5").digest(binaries)).toUpperCase(); MD5Util.byteArrayToHexString(MessageDigest.getInstance("MD5")
.digest(binaries)).toUpperCase();
if (cachedChecksum.equals(checksum)) { if (cachedChecksum.equals(checksum)) {
return binaries; return binaries;
} }
@@ -71,17 +73,21 @@ public class DecodeLogic {
} }
// otherwise, read from file or OSS // otherwise, read from file or OSS
String downloadPath = context.getRealPath("") + "bin_cache" + File.separator; if (null != context) {
String fileName = IR_BIN_FILE_PREFIX + remoteMap + IR_BIN_FILE_SUFFIX; String downloadPath = context.getRealPath("") + "bin_cache" + File.separator;
String localFilePath = downloadPath + fileName; String fileName = IR_BIN_FILE_PREFIX + remoteMap + IR_BIN_FILE_SUFFIX;
String localFilePath = downloadPath + fileName;
File binFile = new File(localFilePath); File binFile = new File(localFilePath);
FileInputStream fin = getFile(binFile, downloadPath, fileName, checksum); FileInputStream fin = getFile(binFile, downloadPath, fileName, checksum);
if (null != fin) { if (null != fin) {
byte[] binaries = IOUtils.toByteArray(fin); byte[] binaries = IOUtils.toByteArray(fin);
System.out.println("binary content get, save it to redis"); System.out.println("binary content get, save it to redis");
irBinaryRepository.add(new IRBinary(remoteIndex.getId(), binaries)); irBinaryRepository.add(new IRBinary(remoteIndex.getId(), binaries));
return binaries; return binaries;
}
} else {
LoggerUtil.getInstance().trace(TAG, "servlet context is null");
} }
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();

View File

@@ -4,6 +4,8 @@ import net.irext.decoder.mapper.RemoteIndexMapper;
import net.irext.decoder.model.RemoteIndex; import net.irext.decoder.model.RemoteIndex;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import java.util.List;
/** /**
* Filename: CollectCodeLogic * Filename: CollectCodeLogic
* Revised: Date: 2018-12-08 * Revised: Date: 2018-12-08
@@ -33,6 +35,10 @@ public class IndexLogic {
} }
public RemoteIndex getRemoteIndex(int indexId) { 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;
} }
} }

View File

@@ -1,4 +1,4 @@
package net.irext.decoder.redisrepo; package net.irext.decoder.cache;
import net.irext.decoder.model.DecodeSession; import net.irext.decoder.model.DecodeSession;

View File

@@ -1,4 +1,4 @@
package net.irext.decoder.redisrepo; package net.irext.decoder.cache;
import net.irext.decoder.model.IRBinary; import net.irext.decoder.model.IRBinary;

View File

@@ -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.model.DecodeSession;
import net.irext.decoder.redisrepo.IDecodeSessionRepository; import net.irext.decoder.cache.IDecodeSessionRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations; import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;

View File

@@ -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.model.IRBinary;
import net.irext.decoder.redisrepo.IIRBinaryRepository; import net.irext.decoder.cache.IIRBinaryRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations; import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Map; import java.util.Map;
/** /**
@@ -22,8 +23,9 @@ import java.util.Map;
*/ */
@Repository @Repository
public class IRBinaryRepositoryImpl implements IIRBinaryRepository { 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 RedisTemplate<String, Object> redisTemplate;
private HashOperations hashOperations; private HashOperations hashOperations;

View File

@@ -2,8 +2,11 @@ package net.irext.decoder.mapper;
import net.irext.decoder.model.RemoteIndex; import net.irext.decoder.model.RemoteIndex;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Select;
import java.util.List;
/** /**
* Filename: RemoteIndexMapper.java * Filename: RemoteIndexMapper.java
* Revised: Date: 2018-12-08 * Revised: Date: 2018-12-08
@@ -18,5 +21,6 @@ import org.apache.ibatis.annotations.Select;
public interface RemoteIndexMapper { public interface RemoteIndexMapper {
@Select("SELECT * FROM remote_index WHERE id = #{id}") @Select("SELECT * FROM remote_index WHERE id = #{id}")
RemoteIndex getRemoteIndexById(int id); @ResultMap("BaseResultMap")
List<RemoteIndex> getRemoteIndexById(int id);
} }

View File

@@ -4,8 +4,8 @@ import net.irext.decoder.businesslogic.DecodeLogic;
import net.irext.decoder.businesslogic.IndexLogic; import net.irext.decoder.businesslogic.IndexLogic;
import net.irext.decoder.mapper.RemoteIndexMapper; import net.irext.decoder.mapper.RemoteIndexMapper;
import net.irext.decoder.model.RemoteIndex; import net.irext.decoder.model.RemoteIndex;
import net.irext.decoder.redisrepo.IDecodeSessionRepository; import net.irext.decoder.cache.IDecodeSessionRepository;
import net.irext.decoder.redisrepo.IIRBinaryRepository; import net.irext.decoder.cache.IIRBinaryRepository;
import net.irext.decoder.request.CloseRequest; import net.irext.decoder.request.CloseRequest;
import net.irext.decoder.request.DecodeRequest; import net.irext.decoder.request.DecodeRequest;
import net.irext.decoder.request.OpenRequest; 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.decoder.utils.LoggerUtil;
import net.irext.decodesdk.bean.ACStatus; import net.irext.decodesdk.bean.ACStatus;
import net.irext.decodesdk.utils.Constants; 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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.ServletContext;
/** /**
* Filename: IRDecodeService.java * Filename: IRDecodeService.java
* Revised: Date: 2018-12-16 * Revised: Date: 2018-12-16
@@ -33,12 +34,16 @@ import org.springframework.web.bind.annotation.*;
*/ */
@RestController @RestController
@RequestMapping("/irext") @RequestMapping("/irext")
@Service("IRDecodeService")
public class IRDecodeService extends AbstractBaseService { public class IRDecodeService extends AbstractBaseService {
private static final String TAG = IRDecodeService.class.getSimpleName(); private static final String TAG = IRDecodeService.class.getSimpleName();
private RemoteIndexMapper remoteIndexMapper; private RemoteIndexMapper remoteIndexMapper;
@Autowired
private ServletContext context;
@Autowired @Autowired
private IIRBinaryRepository irBinaryRepository; private IIRBinaryRepository irBinaryRepository;
@@ -61,10 +66,16 @@ public class IRDecodeService extends AbstractBaseService {
if (null == remoteIndex) { if (null == remoteIndex) {
response.setStatus(new Status(Constants.ERROR_CODE_NETWORK_ERROR, "")); response.setStatus(new Status(Constants.ERROR_CODE_NETWORK_ERROR, ""));
return response; return response;
} else {
LoggerUtil.getInstance().trace(TAG, "remoteIndex get : " + remoteIndex.getId() + ", " +
remoteIndex.getRemoteMap());
} }
byte []binaryContent = DecodeLogic.getInstance().openIRBinary(irBinaryRepository, remoteIndex); byte []binaryContent = DecodeLogic.getInstance().openIRBinary(context, irBinaryRepository, remoteIndex);
System.out.println("binary content fetched : " + binaryContent.length);
if (null != binaryContent) {
LoggerUtil.getInstance().trace(TAG,"binary content fetched : " + binaryContent.length);
}
response.setStatus(new Status(Constants.ERROR_CODE_SUCCESS, ""));
return response; return response;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

@@ -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>