diff --git a/decode-service.iml b/decode-service.iml index c75063c..18f7f8d 100644 --- a/decode-service.iml +++ b/decode-service.iml @@ -82,27 +82,6 @@ - - - - - - - - - - - - - - - - - - - - - @@ -113,5 +92,10 @@ + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 32efee5..8d4a298 100644 --- a/pom.xml +++ b/pom.xml @@ -56,11 +56,6 @@ commons-io 2.6 - - com.aliyun.oss - aliyun-sdk-oss - 3.4.0 - org.springframework.boot spring-boot-autoconfigure @@ -75,6 +70,16 @@ spring-boot-starter-log4j2 2.1.1.RELEASE + + com.squareup.okhttp + okhttp + 2.7.5 + + + com.squareup.okio + okio + 2.2.2 + diff --git a/src/main/java/net/irext/decode/service/alioss/OSSHelper.java b/src/main/java/net/irext/decode/service/alioss/OSSHelper.java deleted file mode 100644 index b013edc..0000000 --- a/src/main/java/net/irext/decode/service/alioss/OSSHelper.java +++ /dev/null @@ -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 - *

- * Description: Aliyun OSS file operation helper - *

- * 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"; - } -} diff --git a/src/main/java/net/irext/decode/service/businesslogic/DecodeLogic.java b/src/main/java/net/irext/decode/service/businesslogic/DecodeLogic.java index a0e8336..1065c32 100644 --- a/src/main/java/net/irext/decode/service/businesslogic/DecodeLogic.java +++ b/src/main/java/net/irext/decode/service/businesslogic/DecodeLogic.java @@ -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(); + } } diff --git a/src/main/java/net/irext/decode/service/request/DownloadBinaryRequest.java b/src/main/java/net/irext/decode/service/request/DownloadBinaryRequest.java new file mode 100755 index 0000000..85403bc --- /dev/null +++ b/src/main/java/net/irext/decode/service/request/DownloadBinaryRequest.java @@ -0,0 +1,32 @@ +package net.irext.decode.service.request; + +/** + * Filename: DownloadBinaryRequest.java + * Revised: Date: 2017-04-14 + * Revision: Revision: 1.0 + *

+ * Description: HTTP download IR binary + *

+ * 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; + } +}