adjusted redis-data and jedis version compatibility

This commit is contained in:
strawmanbobi
2018-12-30 22:41:37 +08:00
parent 8f2d091f0f
commit de30694680
9 changed files with 492 additions and 92 deletions

5
.idea/compiler.xml generated
View File

@@ -13,4 +13,9 @@
<module name="decode-service" target="1.8" />
</bytecodeTargetLevel>
</component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="decode-service" options="-parameters" />
</option>
</component>
</project>

View File

@@ -25,58 +25,89 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-security:1.5.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:1.5.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:1.5.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:1.5.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-logging:1.5.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.1.11" level="project" />
<orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.1.11" level="project" />
<orderEntry type="library" name="Maven: org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.1.1.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-logging:2.1.1.RELEASE" level="project" />
<orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.2.3" level="project" />
<orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.2.3" level="project" />
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-to-slf4j:2.11.1" level="project" />
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-api:2.11.1" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:jul-to-slf4j:1.7.25" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:log4j-over-slf4j:1.7.25" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-core:4.3.9.RELEASE" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: org.yaml:snakeyaml:1.17" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-aop:4.3.9.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-beans:4.3.9.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.security:spring-security-config:4.2.3.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.security:spring-security-core:4.2.3.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-context:4.3.9.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.security:spring-security-web:4.2.3.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-expression:4.3.9.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.0" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-jdbc:1.5.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat:tomcat-jdbc:8.5.15" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat:tomcat-juli:8.5.15" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-jdbc:4.3.9.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.mybatis.spring.boot:mybatis-spring-boot-autoconfigure:1.3.0" level="project" />
<orderEntry type="library" name="Maven: org.mybatis:mybatis:3.4.4" level="project" />
<orderEntry type="library" name="Maven: org.mybatis:mybatis-spring:1.3.1" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:1.5.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-tomcat:1.5.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:8.5.15" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-el:8.5.15" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-websocket:8.5.15" level="project" />
<orderEntry type="library" name="Maven: org.hibernate:hibernate-validator:5.3.5.Final" level="project" />
<orderEntry type="library" name="Maven: javax.validation:validation-api:1.1.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.jboss.logging:jboss-logging:3.3.1.Final" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml:classmate:1.3.3" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.8" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.8" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-web:4.3.9.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:4.3.9.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-data-redis:1.5.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: redis.clients:jedis:2.9.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-pool2:2.4.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework.session:spring-session:1.3.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: javax.annotation:javax.annotation-api:1.3.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-core:5.1.3.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.1.3.RELEASE" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: org.yaml:snakeyaml:1.23" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-jdbc:2.1.1.RELEASE" level="project" />
<orderEntry type="library" name="Maven: com.zaxxer:HikariCP:3.2.0" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-jdbc:5.1.3.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.mybatis.spring.boot:mybatis-spring-boot-autoconfigure:1.3.2" level="project" />
<orderEntry type="library" name="Maven: org.mybatis:mybatis:3.4.6" level="project" />
<orderEntry type="library" name="Maven: org.mybatis:mybatis-spring:1.3.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:2.1.1.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-json:2.1.1.RELEASE" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.9.7" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.9.0" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.9.7" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.9.7" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.7" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.module:jackson-module-parameter-names:2.9.7" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-tomcat:2.1.1.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:9.0.13" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-el:9.0.13" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-websocket:9.0.13" level="project" />
<orderEntry type="library" name="Maven: org.hibernate.validator:hibernate-validator:6.0.13.Final" level="project" />
<orderEntry type="library" name="Maven: javax.validation:validation-api:2.0.1.Final" level="project" />
<orderEntry type="library" name="Maven: org.jboss.logging:jboss-logging:3.3.2.Final" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml:classmate:1.4.0" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-web:5.1.3.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.1.3.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:5.1.3.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-aop:5.1.3.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-context:5.1.3.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-expression:5.1.3.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-data-redis:2.1.1.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-redis:2.1.3.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-keyvalue:1.2.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-commons:1.13.4.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:jcl-over-slf4j:1.7.25" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-tx:4.3.9.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-oxm:4.3.9.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-context-support:4.3.9.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-keyvalue:2.1.3.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-commons:2.1.3.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-tx:5.1.3.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-oxm:5.1.3.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-context-support:5.1.3.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.25" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: mysql:mysql-connector-java:5.1.42" level="project" />
<orderEntry type="library" name="Maven: io.lettuce:lettuce-core:5.1.3.RELEASE" level="project" />
<orderEntry type="library" name="Maven: io.projectreactor:reactor-core:3.2.3.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.reactivestreams:reactive-streams:1.0.2" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-common:4.1.31.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-transport:4.1.31.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-buffer:4.1.31.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-resolver:4.1.31.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-handler:4.1.31.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-codec:4.1.31.Final" level="project" />
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:8.0.13" level="project" />
<orderEntry type="library" name="Maven: commons-io:commons-io:2.6" level="project" />
<orderEntry type="library" name="Maven: com.aliyun.oss:aliyun-sdk-oss:3.4.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.5.6" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.4.10" level="project" />
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.11" level="project" />
<orderEntry type="library" name="Maven: org.jdom:jdom:1.1" level="project" />
<orderEntry type="library" name="Maven: com.sun.jersey:jersey-json:1.9" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.jettison:jettison:1.1" level="project" />
<orderEntry type="library" name="Maven: stax:stax-api:1.0.1" level="project" />
<orderEntry type="library" name="Maven: com.sun.xml.bind:jaxb-impl:2.2.3-1" level="project" />
<orderEntry type="library" name="Maven: javax.xml.bind:jaxb-api:2.3.1" level="project" />
<orderEntry type="library" name="Maven: javax.activation:javax.activation-api:1.2.0" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-core-asl:1.8.3" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-mapper-asl:1.8.3" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-jaxrs:1.8.3" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-xc:1.8.3" level="project" />
<orderEntry type="library" name="Maven: com.sun.jersey:jersey-core:1.9" level="project" />
<orderEntry type="library" name="Maven: com.aliyun:aliyun-java-sdk-core:3.4.0" level="project" />
<orderEntry type="library" name="Maven: org.json:json:20170516" level="project" />
<orderEntry type="library" name="Maven: com.aliyun:aliyun-java-sdk-ram:3.0.0" level="project" />
<orderEntry type="library" name="Maven: com.aliyun:aliyun-java-sdk-sts:3.0.0" level="project" />
<orderEntry type="library" name="Maven: com.aliyun:aliyun-java-sdk-ecs:4.2.0" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:2.1.1.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.1.1.RELEASE" level="project" />
<orderEntry type="library" name="Maven: redis.clients:jedis:2.9.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-pool2:2.6.0" level="project" />
</component>
</module>

55
pom.xml
View File

@@ -12,10 +12,10 @@
<description>Decoding IR binaries online</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/>
</parent>
<properties>
@@ -25,37 +25,44 @@
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.1.3.RELEASE</version>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>8.0.13</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
</dependencies>

View File

@@ -1,16 +0,0 @@
package net.irext.decoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer;
@Configuration
@EnableRedisHttpSession
public class SessionConfig extends AbstractHttpSessionApplicationInitializer {
@Bean
public JedisConnectionFactory connectionFactory() {
return new JedisConnectionFactory();
}
}

View File

@@ -0,0 +1,94 @@
package net.irext.decoder.alioss;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.Bucket;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
/**
* Filename: OSSHelper.java
* Revised: Date: 2017-05-10
* Revision: Revision: 1.0
* <p>
* Description: Aliyun OSS file operation helper
* <p>
* Revision log:
* 2017-05-10: created by strawmanbobi
*/
public class OSSHelper {
public static String BUCKET_NAME = "irext-debug";
private OSSClient mOSSClient;
public OSSHelper() {
String END_POINT = "oss-cn-hangzhou.aliyuncs.com";
String ACCESS_KEY = "T82nbipHSESmHzd8";
String ACCESS_SECRET = "SOweQ8UVwCwPr2NC8EC89EOeKJc5Um";
mOSSClient = new OSSClient(END_POINT, ACCESS_KEY, ACCESS_SECRET);
}
public boolean createBucket(String bucketName) {
Bucket bucket = mOSSClient.createBucket(bucketName);
return bucketName.equals(bucket.getName());
}
public final void deleteBucket(String bucketName) {
mOSSClient.deleteBucket(bucketName);
}
public final String uploadObject2OSS(File file, String bucketName, String diskName) {
String resultStr = null;
try {
InputStream is = new FileInputStream(file);
String fileName = file.getName();
Long fileSize = file.length();
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(is.available());
metadata.setCacheControl("no-cache");
metadata.setHeader("Pragma", "no-cache");
metadata.setContentEncoding("utf-8");
metadata.setContentType(getContentType(fileName));
metadata.setContentDisposition("filename/filesize=" + fileName + "/" + fileSize + "Byte.");
PutObjectResult putResult = mOSSClient.putObject(bucketName, diskName + fileName, is, metadata);
resultStr = putResult.getETag();
} catch (Exception e) {
e.printStackTrace();
}
return resultStr;
}
public InputStream getOSS2InputStream(String bucketName, String diskName, String key) {
OSSObject ossObj = mOSSClient.getObject(bucketName, diskName + key);
return ossObj.getObjectContent();
}
public void deleteFile(String bucketName, String diskName, String key) {
mOSSClient.deleteObject(bucketName, diskName + key);
}
private String getContentType(String fileName) {
String fileExtension = fileName.substring(fileName.lastIndexOf("."));
if ("bmp".equalsIgnoreCase(fileExtension)) return "image/bmp";
if ("gif".equalsIgnoreCase(fileExtension)) return "image/gif";
if ("jpeg".equalsIgnoreCase(fileExtension) ||
"jpg".equalsIgnoreCase(fileExtension) ||
"png".equalsIgnoreCase(fileExtension))
return "image/jpeg";
if ("html".equalsIgnoreCase(fileExtension)) return "text/html";
if ("txt".equalsIgnoreCase(fileExtension)) return "text/plain";
if ("vsd".equalsIgnoreCase(fileExtension)) return "application/vnd.visio";
if ("ppt".equalsIgnoreCase(fileExtension) || "pptx".equalsIgnoreCase(fileExtension))
return "application/vnd.ms-powerpoint";
if ("doc".equalsIgnoreCase(fileExtension) || "docx".equalsIgnoreCase(fileExtension))
return "application/msword";
if ("xml".equalsIgnoreCase(fileExtension)) return "text/xml";
return "text/html";
}
}

View File

@@ -1,7 +1,23 @@
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.utils.FileUtils;
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;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* Filename: CollectCodeLogic
* Revised: Date: 2018-12-08
@@ -15,8 +31,14 @@ import org.springframework.stereotype.Controller;
@Controller
public class DecodeLogic {
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();
@@ -28,7 +50,86 @@ public class DecodeLogic {
}
public byte[] openIRBinary(IIRBinaryRepository irBinaryRepository, RemoteIndex remoteIndex) {
if (null == remoteIndex) {
return null;
}
try {
String checksum = remoteIndex.getBinaryMd5().toUpperCase();
String remoteMap = remoteIndex.getRemoteMap();
IRBinary irBinary = irBinaryRepository.find(remoteIndex.getId());
if (null != irBinary) {
byte[] binaries = irBinary.getBinary();
if (null != binaries) {
System.out.println("binary content fetched from redis : " + binaries.length);
// validate binary content
String cachedChecksum =
MD5Util.byteArrayToHexString(MessageDigest.getInstance("MD5").digest(binaries)).toUpperCase();
if (cachedChecksum.equals(checksum)) {
return binaries;
}
}
}
// 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;
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;
}
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
return null;
}
public int[] decode() {
return null;
}
// helper methods
private FileInputStream getFile(File binFile, String downloadPath, String fileName, String checksum) {
try {
if (binFile.exists()) {
FileInputStream fileInputStream = new FileInputStream(binFile);
// validate binary content
byte []binaries = IOUtils.toByteArray(fileInputStream);
String fileChecksum =
MD5Util.byteArrayToHexString(MessageDigest.getInstance("MD5").digest(binaries)).toUpperCase();
if (fileChecksum.equals(checksum)) {
return new FileInputStream(binFile);
}
}
OSSHelper ossHelper = new OSSHelper();
InputStream inputStream = ossHelper.getOSS2InputStream(OSSHelper.BUCKET_NAME, "", fileName);
// validate binary content
byte []binaries = IOUtils.toByteArray(inputStream);
String ossChecksum =
MD5Util.byteArrayToHexString(MessageDigest.getInstance("MD5").digest(binaries)).toUpperCase();
if (ossChecksum.equals(checksum)) {
FileUtils.createDirs(downloadPath);
if (FileUtils.write(binFile, binaries)) {
return new FileInputStream(binFile);
} else {
System.out.println("fatal : write file to local path failed");
}
} else {
System.out.println("fatal : checksum does not match even downloaded from OSS," +
" please contact the admin");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

View File

@@ -4,6 +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.request.CloseRequest;
import net.irext.decoder.request.DecodeRequest;
import net.irext.decoder.request.OpenRequest;
@@ -13,6 +15,7 @@ import net.irext.decoder.response.Status;
import net.irext.decoder.service.base.AbstractBaseService;
import net.irext.decodesdk.bean.ACStatus;
import net.irext.decodesdk.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
@@ -31,6 +34,12 @@ public class IRDecodeService extends AbstractBaseService {
private RemoteIndexMapper remoteIndexMapper;
@Autowired
private IIRBinaryRepository irBinaryRepository;
@Autowired
private IDecodeSessionRepository decodeSessionRepository;
public IRDecodeService(RemoteIndexMapper remoteIndexMapper) {
this.remoteIndexMapper = remoteIndexMapper;
}
@@ -41,11 +50,13 @@ public class IRDecodeService extends AbstractBaseService {
int indexId = openRequest.getIndexId();
ServiceResponse response = new ServiceResponse();
RemoteIndex index = IndexLogic.getInstance(remoteIndexMapper).getRemoteIndex(indexId);
if (null == index) {
RemoteIndex remoteIndex = IndexLogic.getInstance(remoteIndexMapper).getRemoteIndex(indexId);
if (null == remoteIndex) {
response.setStatus(new Status(Constants.ERROR_CODE_NETWORK_ERROR, ""));
return response;
}
byte []binaryContent = DecodeLogic.getInstance().openIRBinary(irBinaryRepository, remoteIndex);
System.out.println("binary content fetched : " + binaryContent.length);
return response;
} catch (Exception e) {

View File

@@ -0,0 +1,114 @@
package net.irext.decoder.utils;
import java.io.*;
/**
* Filename: FileUtils.java
* Revised: Date: 2017-04-14
* Revision: Revision: 1.0
* <p>
* Description: File operations
* <p>
* Revision log:
* 2017-04-14: created by strawmanbobi
*/
public class FileUtils {
public static boolean write(File file, byte[] binaries) {
if (null == file) {
return false;
}
if (null == binaries) {
System.out.println("fatal : content to write is null");
return false;
}
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
outputStream.write(binaries, 0, binaries.length);
outputStream.flush();
return true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
public static boolean createDirs(String path) {
File file = new File(path);
return file.exists() || file.mkdir();
}
public static byte[] getByteArrayFromFile(String fileName) {
File file;
try {
file = new File(fileName);
} catch (Exception e) {
e.printStackTrace();
return null;
}
if (!file.exists() || !file.isFile() || !file.canRead()) {
return null;
}
byte[] byteArray = null;
try {
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int count;
byte buffer[] = new byte[512];
while ((count = fis.read(buffer)) > 0) {
baos.write(buffer, 0, count);
}
byteArray = baos.toByteArray();
fis.close();
baos.flush();
baos.close();
} catch (Exception e) {
e.printStackTrace();
}
return byteArray;
}
private static void deleteAllFiles(File root) {
File files[] = root.listFiles();
if (files != null) {
for (File f : files) {
if (f.isDirectory()) {
deleteAllFiles(f);
try {
if(!f.delete()) {
System.out.println("failed to delete file");
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
if (f.exists()) {
deleteAllFiles(f);
try {
if(!f.delete()) {
System.out.println("failed to delete file");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
}

View File

@@ -0,0 +1,53 @@
package net.irext.decoder.utils;
import java.security.MessageDigest;
/**
* Filename: MD5Util.java
* Revised: Date: 2017-04-30
* Revision: Revision: 1.0
* <p>
* Description: Crypto MD5 utility
* <p>
* Revision log:
* 2017-04-30: created by strawmanbobi
*/
public class MD5Util {
public static String byteArrayToHexString(byte b[]) {
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; i++)
resultSb.append(byteToHexString(b[i]));
return resultSb.toString();
}
private static String byteToHexString(byte b) {
int n = b;
if (n < 0)
n += 256;
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}
public static String MD5Encode(String origin, String charsetname) {
String resultString = null;
try {
resultString = new String(origin);
MessageDigest md = MessageDigest.getInstance("MD5");
if (charsetname == null || "".equals(charsetname))
resultString = byteArrayToHexString(md.digest(resultString
.getBytes()));
else
resultString = byteArrayToHexString(md.digest(resultString
.getBytes(charsetname)));
} catch (Exception exception) {
}
return resultString;
}
private static final String hexDigits[] = {"0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
}