@ -1,9 +1,10 @@
package com.fr.design.mainframe.share.util ;
import com.fr.config.MarketConfig ;
import com.fr.design.DesignerEnvManager ;
import com.fr.design.extra.PluginConstants ;
import com.fr.form.share.base.CancelCheck ;
import com.fr.form.share.constants.ShareComponentConstants ;
import com.fr.ftp.util.Base64 ;
import com.fr.general.CloudCenter ;
import com.fr.log.FineLoggerFactory ;
import com.fr.stable.ProductConstants ;
@ -21,31 +22,27 @@ import com.fr.third.org.apache.http.impl.client.CloseableHttpClient;
import com.fr.third.org.apache.http.impl.client.HttpClients ;
import org.jetbrains.annotations.NotNull ;
import javax.crypto.Cipher ;
import java.io.File ;
import java.io.FileOutputStream ;
import java.io.InputStream ;
import java.nio.charset.StandardCharsets ;
import java.security.KeyFactory ;
import java.security.interfaces.RSAPublicKey ;
import java.security.spec.X509EncodedKeySpec ;
/ * *
* created by Harrison on 2020 / 05 / 27
* * /
public class DownloadUtils {
private static final String MARKET_LOGIN_URL = CloudCenter . getInstance ( ) . acquireUrlByKind ( "market.login" ) ;
private static final String REUSES_URL = CloudCenter . getInstance ( ) . acquireUrlByKind ( "af.reuseInfo" ) + "file/" ;
private static final String PACKAGE_REUSES_URL = CloudCenter . getInstance ( ) . acquireUrlByKind ( "af.reuseInfo" ) + "package/download/" ;
private static final String CERTIFICATE_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCtsz62CPSWXZE/IYZRiAuTSZkw\n" +
"1WOwer8+JFktK0uKLAUuQoBr+UjAMFtRA8W7JgKMDwZy/2liEAiXEOSPU/hrdV8D\n" +
"tT541LnGi1X/hXiRwuttPWYN3L2GYm/d5blU+FBNwghBIrdAxXTzYBc6P4KL/oYX\n" +
"nMdTIrkz8tYkG3QoFQIDAQAB" ;
public static boolean login ( CloseableHttpClient client ) throws Exception {
HttpUriRequest login = RequestBuilder . post ( )
. setUri ( MARKET_LOGIN_URL )
. setHeader ( "User-Agent" , "Mozilla/5.0" )
. addParameter ( "username" , MarketConfig . getInstance ( ) . getBbsUsername ( ) )
. addParameter ( "password" , MarketConfig . getInstance ( ) . getBbsPassword ( ) )
. build ( ) ;
CloseableHttpResponse loginResponse = client . execute ( login ) ;
return loginResponse . getStatusLine ( ) . getStatusCode ( ) = = HttpStatus . SC_OK ;
}
private static CloseableHttpClient createClient ( ) {
BasicCookieStore cookieStore = new BasicCookieStore ( ) ;
@ -59,8 +56,10 @@ public class DownloadUtils {
@NotNull
public static String download ( String id , String fileName , com . fr . design . extra . Process < Double > process ) throws Exception {
CloseableHttpResponse fileRes = getHttpResponse ( REUSES_URL , id ) ;
if ( fileRes . getStatusLine ( ) . getStatusCode ( ) = = HttpStatus . SC_MOVED_TEMPORARILY ) {
fileRes = getHttpResponse ( fileRes . getHeaders ( "Location" ) [ 0 ] . getValue ( ) ) ;
}
if ( fileRes . getStatusLine ( ) . getStatusCode ( ) = = HttpStatus . SC_OK ) {
String realPath = StableUtils . pathJoin ( ProductConstants . getEnvHome ( ) , ShareComponentConstants . PLUGIN_CACHE , ShareComponentConstants . DOWNLOAD_SHARE ) ;
String filePath ;
@ -95,7 +94,9 @@ public class DownloadUtils {
public static String downloadPackage ( String id , String fileName , CancelCheck cancelCheck ) throws Exception {
CloseableHttpResponse fileRes = getHttpResponse ( PACKAGE_REUSES_URL , id ) ;
if ( fileRes . getStatusLine ( ) . getStatusCode ( ) = = HttpStatus . SC_MOVED_TEMPORARILY ) {
fileRes = getHttpResponse ( fileRes . getHeaders ( "Location" ) [ 0 ] . getValue ( ) ) ;
}
String realPath = StableUtils . pathJoin ( ProductConstants . getEnvHome ( ) , ShareComponentConstants . PLUGIN_CACHE , ShareComponentConstants . DOWNLOAD_PACKAGE_SHARE ) ;
String filePath ;
if ( fileRes . getStatusLine ( ) . getStatusCode ( ) = = HttpStatus . SC_OK ) {
@ -133,11 +134,36 @@ public class DownloadUtils {
//先登录一下。不然可能失败
CloseableHttpClient client = createClient ( ) ;
FineLoggerFactory . getLogger ( ) . info ( "login fr-market" ) ;
login ( client ) ;
FineLoggerFactory . getLogger ( ) . info ( "start download widget {}" , id ) ;
HttpUriRequest file = RequestBuilder . post ( )
. setHeader ( "User-Agent" , "Mozilla/5.0" )
. setUri ( url ) . addParameter ( "id" , encrypt ( id ) ) . addParameter ( "userId" ,
String . valueOf ( DesignerEnvManager . getEnvManager ( ) . getDesignerLoginUid ( ) ) )
. build ( ) ;
return client . execute ( file ) ;
}
private static CloseableHttpResponse getHttpResponse ( String url ) throws Exception {
//先登录一下。不然可能失败
CloseableHttpClient client = createClient ( ) ;
HttpUriRequest file = RequestBuilder . get ( )
. setUri ( url + id )
. setUri ( url )
. build ( ) ;
return client . execute ( file ) ;
}
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 ;
}
}