diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/download/FineMarketDownloadManager.java b/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/download/FineMarketDownloadManager.java index 88ef8dc4a6..a51c4b4ccc 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/download/FineMarketDownloadManager.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/download/FineMarketDownloadManager.java @@ -23,6 +23,12 @@ import java.io.File; import java.io.IOException; import java.util.HashMap; + +/** + * 在这里统一管理帆软市场的下载 + * 下载的流程控制尽量都在这个类内部完成 + * 通过Process类来实现下载流程控制 + * */ public class FineMarketDownloadManager { private static final FineMarketDownloadManager INSTANCE = new FineMarketDownloadManager(); @@ -38,9 +44,10 @@ public class FineMarketDownloadManager { private static final String OPENING_PLEASE_WAIT = Toolkit.i18nText("Fine-Design_Report_AlphaFine_Template_Resource_Opening"); private static final String DOWNLOAD_FAILED = Toolkit.i18nText("Fine-Design_Report_AlphaFine_Template_Resource_Download_Failed_Check_Network"); private static final String REPORTLETS = "/reportlets"; - - private HashMap processMap = new HashMap<>(); - + private static final String ZIP = ".zip"; + private static final String RAR = ".rar"; + private static final String CPT = ".cpt"; + private static final String FRM = ".frm"; /** * 下载资源并解压, @@ -77,10 +84,10 @@ public class FineMarketDownloadManager { return; } File fileNeedOpen = new File(fileName); - if (fileName.endsWith(".zip")) { + if (fileName.endsWith(ZIP)) { File[] files = unzip(fileName); fileNeedOpen = getFirstCptOrFrm(files); - } else if (fileName.endsWith(".rar")) { + } else if (fileName.endsWith(RAR)) { // rar直接打开系统文件夹 File parentDir = new File(fileName).getParentFile(); Desktop.getDesktop().open(parentDir); @@ -91,7 +98,7 @@ public class FineMarketDownloadManager { private File getFirstCptOrFrm(File[] files) { for (File f : files) { - if (f.getName().endsWith(".cpt") || f.getName().endsWith(".frm")) { + if (f.getName().endsWith(CPT) || f.getName().endsWith(FRM)) { return f; } } diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/search/helper/FineMarketClientHelper.java b/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/search/helper/FineMarketClientHelper.java index f261d97ba4..194f3dfc8f 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/search/helper/FineMarketClientHelper.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/search/helper/FineMarketClientHelper.java @@ -4,12 +4,12 @@ import com.fr.design.DesignerEnvManager; import com.fr.design.extra.PluginConstants; import com.fr.design.mainframe.alphafine.download.FineMarketDownloadManager; import com.fr.design.mainframe.alphafine.model.TemplateResource; -import com.fr.ftp.util.Base64; import com.fr.general.CloudCenter; import com.fr.general.http.HttpToolbox; import com.fr.json.JSONArray; import com.fr.json.JSONObject; import com.fr.log.FineLoggerFactory; +import com.fr.security.SecurityToolbox; import com.fr.stable.StableUtils; import com.fr.third.org.apache.http.HttpEntity; import com.fr.third.org.apache.http.HttpException; @@ -24,15 +24,10 @@ import com.fr.third.org.apache.http.impl.client.CloseableHttpClient; import com.fr.third.org.apache.http.impl.client.HttpClients; import org.jetbrains.annotations.Nullable; -import javax.crypto.Cipher; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import java.security.KeyFactory; -import java.security.interfaces.RSAPublicKey; -import java.security.spec.X509EncodedKeySpec; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -102,26 +97,36 @@ public class FineMarketClientHelper { File destFile = createDestFile(destDir, resource); StableUtils.makesureFileExist(destFile); - FileOutputStream writer = new FileOutputStream(destFile.getAbsolutePath()); - HttpEntity entity = fileRes.getEntity(); - long totalSize = entity.getContentLength(); - InputStream content = entity.getContent(); + InputStream content = null; + FileOutputStream writer = null; + try { + writer = new FileOutputStream(destFile.getAbsolutePath()); + HttpEntity entity = fileRes.getEntity(); + long totalSize = entity.getContentLength(); + content = entity.getContent(); - byte[] data = new byte[PluginConstants.BYTES_NUM]; - int bytesRead; - int totalBytesRead = 0; - while ((bytesRead = content.read(data)) > 0) { - writer.write(data, 0, bytesRead); - data = new byte[PluginConstants.BYTES_NUM]; - totalBytesRead += bytesRead; - process.process(totalBytesRead / (double) totalSize); + byte[] data = new byte[PluginConstants.BYTES_NUM]; + int bytesRead; + int totalBytesRead = 0; + + while ((bytesRead = content.read(data)) > 0) { + writer.write(data, 0, bytesRead); + data = new byte[PluginConstants.BYTES_NUM]; + totalBytesRead += bytesRead; + process.process(totalBytesRead / (double) totalSize); + } + } catch (Exception e) { + FineLoggerFactory.getLogger().error(e, e.getMessage()); + } finally { + content.close(); + writer.flush(); + writer.close(); } - content.close(); - writer.flush(); - writer.close(); + + FineLoggerFactory.getLogger().info("download resource{} success", resourceId); process.process(FineMarketDownloadManager.PROCESS_SUCCESS); @@ -132,9 +137,9 @@ public class FineMarketClientHelper { throw new HttpException(); } - } + private CloseableHttpResponse getFileResponse(TemplateResource resource, String resourceId) throws Exception { CloseableHttpResponse fileRes = postDownloadHttpResponse(getResourceDownloadUrl(resource), resourceId); if (fileRes.getStatusLine().getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) { @@ -206,7 +211,7 @@ public class FineMarketClientHelper { RequestBuilder builder = RequestBuilder.post() .setHeader("User-Agent", "Mozilla/5.0") .setUri(url) - .addParameter("id", encrypt(id)) + .addParameter("id", SecurityToolbox.encrypt(id, CERTIFICATE_PUBLIC_KEY)) .addParameter("userId", String.valueOf(DesignerEnvManager.getEnvManager().getDesignerLoginUid())); if (params != null) { @@ -318,15 +323,4 @@ public class FineMarketClientHelper { } } - - private static String encrypt(String str) throws Exception { - //base64编码的公钥 - byte[] decoded = Base64.decodeBase64(CERTIFICATE_PUBLIC_KEY); - RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded)); - //RSA加密 - Cipher cipher = Cipher.getInstance("RSA"); - cipher.init(Cipher.ENCRYPT_MODE, pubKey); - String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes(StandardCharsets.UTF_8))); - return outStr; - } } \ No newline at end of file