get rid of OSS dependency

This commit is contained in:
strawmanbobi
2019-02-09 15:37:57 +08:00
parent ed9143bf9a
commit 32c4673e12
5 changed files with 90 additions and 134 deletions

View File

@@ -82,27 +82,6 @@
<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" />
@@ -113,5 +92,10 @@
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-core:2.11.1" level="project" />
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-jul: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: com.squareup.okhttp:okhttp:2.7.5" level="project" />
<orderEntry type="library" name="Maven: com.squareup.okio:okio:2.2.2" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: org.jetbrains.kotlin:kotlin-stdlib:1.2.71" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: org.jetbrains.kotlin:kotlin-stdlib-common:1.2.71" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: org.jetbrains:annotations:13.0" level="project" />
</component>
</module>

15
pom.xml
View File

@@ -56,11 +56,6 @@
<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>
@@ -75,6 +70,16 @@
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
<build>

View File

@@ -1,94 +0,0 @@
package net.irext.decode.service.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,6 +1,6 @@
package net.irext.decode.service.businesslogic;
import net.irext.decode.service.alioss.OSSHelper;
import com.squareup.okhttp.*;
import net.irext.decode.service.cache.IDecodeSessionRepository;
import net.irext.decode.service.cache.IIRBinaryRepository;
import net.irext.decode.service.model.RemoteIndex;
@@ -14,6 +14,7 @@ import org.apache.commons.io.IOUtils;
import javax.servlet.ServletContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
@@ -34,6 +35,8 @@ public class DecodeLogic {
private static final String IR_BIN_FILE_PREFIX = "irda_";
private static final String IR_BIN_FILE_SUFFIX = ".bin";
private static final String IR_BIN_DOWNLOAD_PREFIX = "http://irext-debug.oss-cn-hangzhou.aliyuncs.com/";
private static DecodeLogic decodeLogic;
public static DecodeLogic getInstance() {
@@ -128,26 +131,52 @@ public class DecodeLogic {
return new FileInputStream(binFile);
}
}
OSSHelper ossHelper = new OSSHelper();
InputStream inputStream = ossHelper.getOSS2InputStream(OSSHelper.BUCKET_NAME, "", fileName);
InputStream inputStream = getBinInputStream(fileName);
// validate binary content
byte[] binaries = IOUtils.toByteArray(inputStream);
String ossChecksum =
MD5Util.byteArrayToHexString(MessageDigest.getInstance("MD5").digest(binaries)).toUpperCase();
if (ossChecksum.equals(checksum)) {
FileUtil.createDirs(downloadPath);
if (FileUtil.write(binFile, binaries)) {
return new FileInputStream(binFile);
if (null != inputStream) {
byte[] binaries = IOUtils.toByteArray(inputStream);
String ossChecksum =
MD5Util.byteArrayToHexString(MessageDigest.getInstance("MD5").digest(binaries)).toUpperCase();
if (ossChecksum.equals(checksum)) {
FileUtil.createDirs(downloadPath);
if (FileUtil.write(binFile, binaries)) {
LoggerUtil.getInstance().trace(TAG,"fatal : download file successfully");
return new FileInputStream(binFile);
} else {
LoggerUtil.getInstance().trace(TAG,"fatal : write file to local path failed");
}
} else {
System.out.println("fatal : write file to local path failed");
LoggerUtil.getInstance().trace(TAG,"fatal : checksum does not match even downloaded from OSS," +
" please contact the admin");
}
} else {
System.out.println("fatal : checksum does not match even downloaded from OSS," +
" please contact the admin");
} else{
LoggerUtil.getInstance().trace(TAG,"fatal : download file failed");
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private InputStream getBinInputStream(String fileName) {
String downloadURL = IR_BIN_DOWNLOAD_PREFIX + fileName;
try {
LoggerUtil.getInstance().trace(TAG,"download file from OSS : " + downloadURL);
return getFileByteStreamByURL(downloadURL);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private InputStream getFileByteStreamByURL(String url) throws IOException {
Request request = new Request.Builder()
.url(url)
.get()
.build();
Response response = new OkHttpClient().newCall(request).execute();
return response.body().byteStream();
}
}

View File

@@ -0,0 +1,32 @@
package net.irext.decode.service.request;
/**
* Filename: DownloadBinaryRequest.java
* Revised: Date: 2017-04-14
* Revision: Revision: 1.0
* <p>
* Description: HTTP download IR binary
* <p>
* Revision log:
* 2017-04-14: created by strawmanbobi
*/
public class DownloadBinaryRequest {
private int indexId;
public DownloadBinaryRequest(int indexId) {
this.indexId = indexId;
}
public DownloadBinaryRequest() {
}
public int getIndexId() {
return indexId;
}
public void setIndexId(int indexId) {
this.indexId = indexId;
}
}