mirror of
				https://gitee.com/BDWare/ypk-deploy-tool
				synced 2025-10-31 12:42:16 +00:00 
			
		
		
		
	fix http tool bugs
This commit is contained in:
		
							parent
							
								
									23f367f398
								
							
						
					
					
						commit
						ceb3709d7b
					
				| @ -8,7 +8,7 @@ plugins { | |||||||
| apply from: '../spotless.gradle' | apply from: '../spotless.gradle' | ||||||
| 
 | 
 | ||||||
| group "org.bdware.bdcontract" | group "org.bdware.bdcontract" | ||||||
| version "0.7.4" | version "0.7.5" | ||||||
| sourceCompatibility = 1.8 | sourceCompatibility = 1.8 | ||||||
| repositories { | repositories { | ||||||
|     mavenCentral() |     mavenCentral() | ||||||
| @ -23,7 +23,7 @@ dependencies { | |||||||
|     implementation 'org.bouncycastle:bcprov-jdk15on:1.69' |     implementation 'org.bouncycastle:bcprov-jdk15on:1.69' | ||||||
|     implementation 'org.apache.httpcomponents:httpclient:4.5.13' |     implementation 'org.apache.httpcomponents:httpclient:4.5.13' | ||||||
|     implementation 'org.apache.httpcomponents:httpmime:4.5.13' |     implementation 'org.apache.httpcomponents:httpmime:4.5.13' | ||||||
|     implementation 'org.bdware.doip:bdosclient:0.0.7' |     implementation 'org.bdware.doip:bdosclient:0.0.8' | ||||||
|    // implementation 'org.bdware.doip:doip-audit-tool:1.2.4' |    // implementation 'org.bdware.doip:doip-audit-tool:1.2.4' | ||||||
|     testImplementation 'junit:junit:4.13.2' |     testImplementation 'junit:junit:4.13.2' | ||||||
| } | } | ||||||
|  | |||||||
| @ -35,6 +35,7 @@ public class HTTPTool { | |||||||
|         String publicKey; |         String publicKey; | ||||||
|         String ypkPath; |         String ypkPath; | ||||||
|         int remoteDebugPort; |         int remoteDebugPort; | ||||||
|  |         int chunkSize; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public static void deployWithYpk(String deployConfigPath, String ypkPath) { |     public static void deployWithYpk(String deployConfigPath, String ypkPath) { | ||||||
| @ -56,8 +57,7 @@ public class HTTPTool { | |||||||
|         try { |         try { | ||||||
|             if (f.isDirectory()) { |             if (f.isDirectory()) { | ||||||
|                 File[] files = f.listFiles((f2) -> f2.getName().endsWith(".json")); |                 File[] files = f.listFiles((f2) -> f2.getName().endsWith(".json")); | ||||||
|                 if (files != null) |                 if (files != null) for (File file : files) | ||||||
|                     for (File file : files) |  | ||||||
|                     deploy(file.getAbsolutePath()); |                     deploy(file.getAbsolutePath()); | ||||||
|                 return; |                 return; | ||||||
|             } else { |             } else { | ||||||
| @ -74,24 +74,23 @@ public class HTTPTool { | |||||||
| 
 | 
 | ||||||
|     private static void restart(DeployConfig config) { |     private static void restart(DeployConfig config) { | ||||||
|         SM2KeyPair keyPair = SM2KeyPair.fromJson(new Gson().toJson(config)); |         SM2KeyPair keyPair = SM2KeyPair.fromJson(new Gson().toJson(config)); | ||||||
|         SmartContractClientExt ext = new SmartContractClientExt( |         SmartContractClientExt ext = new SmartContractClientExt(String.format("ws://%s/SCIDE/SCExecutor", config.agentAddress), keyPair); | ||||||
|                 String.format("ws://%s/SCIDE/SCExecutor", config.agentAddress), keyPair); |  | ||||||
|         ext.waitForConnect(); |         ext.waitForConnect(); | ||||||
|         ext.login(); |         ext.login(); | ||||||
|         for (; !ext.isLoggedIn;) |         for (; !ext.isLoggedIn; ) | ||||||
|             Thread.yield(); |             Thread.yield(); | ||||||
|         AtomicInteger counter = new AtomicInteger(0); |         AtomicInteger counter = new AtomicInteger(0); | ||||||
|         try { |         try { | ||||||
|             if (config.killBeforeStart != null) |             if (config.killBeforeStart != null) ext.kill(config.killBeforeStart, new ResultCallback() { | ||||||
|                 ext.kill(config.killBeforeStart, new ResultCallback() { |  | ||||||
|                 @Override |                 @Override | ||||||
|                 public void onResult(JsonObject r) { |                 public void onResult(JsonObject r) { | ||||||
|                     System.out.println(TAG + r); |                     System.out.println(TAG + r); | ||||||
|                     counter.incrementAndGet(); |                     counter.incrementAndGet(); | ||||||
|                 } |                 } | ||||||
|             }); |             }); | ||||||
|             for (; counter.get() == 0;) |             for (; counter.get() == 0; ) | ||||||
|                 Thread.yield();; |                 Thread.yield(); | ||||||
|  |             ; | ||||||
|             Thread.sleep(200); |             Thread.sleep(200); | ||||||
|         } catch (Exception e) { |         } catch (Exception e) { | ||||||
|             e.printStackTrace(); |             e.printStackTrace(); | ||||||
| @ -102,17 +101,15 @@ public class HTTPTool { | |||||||
|         ext.listProjects(true, new ResultCallback() { |         ext.listProjects(true, new ResultCallback() { | ||||||
|             @Override |             @Override | ||||||
|             public void onResult(JsonObject r) { |             public void onResult(JsonObject r) { | ||||||
|                 JsonArray array = |                 JsonArray array = JsonParser.parseString(r.get("data").getAsString()).getAsJsonArray(); | ||||||
|                         JsonParser.parseString(r.get("data").getAsString()).getAsJsonArray(); |  | ||||||
|                 for (JsonElement je : array) { |                 for (JsonElement je : array) { | ||||||
|                     String fileName = je.getAsString(); |                     String fileName = je.getAsString(); | ||||||
|                     if (fileName.startsWith(contractName)) |                     if (fileName.startsWith(contractName)) fileNames.add(fileName); | ||||||
|                         fileNames.add(fileName); |  | ||||||
|                 } |                 } | ||||||
|                 counter.incrementAndGet(); |                 counter.incrementAndGet(); | ||||||
|             } |             } | ||||||
|         }); |         }); | ||||||
|         for (; counter.get() == 1;) |         for (; counter.get() == 1; ) | ||||||
|             Thread.yield(); |             Thread.yield(); | ||||||
|         Collections.sort(fileNames, new Comparator<String>() { |         Collections.sort(fileNames, new Comparator<String>() { | ||||||
|             @Override |             @Override | ||||||
| @ -123,8 +120,7 @@ public class HTTPTool { | |||||||
| 
 | 
 | ||||||
|         System.out.println(TAG + "startContract:" + fileNames.get(0)); |         System.out.println(TAG + "startContract:" + fileNames.get(0)); | ||||||
|         if (config.asDebug) { |         if (config.asDebug) { | ||||||
|             ext.startContractAsDebug(fileNames.get(0), config.createParam, config.remoteDebugPort, |             ext.startContractAsDebug(fileNames.get(0), config.createParam, config.remoteDebugPort, new ResultCallback() { | ||||||
|                     new ResultCallback() { |  | ||||||
|                 @Override |                 @Override | ||||||
|                 public void onResult(JsonObject r) { |                 public void onResult(JsonObject r) { | ||||||
|                     System.out.println(TAG + r); |                     System.out.println(TAG + r); | ||||||
| @ -132,17 +128,17 @@ public class HTTPTool { | |||||||
|                 } |                 } | ||||||
|             }); |             }); | ||||||
|         } else { |         } else { | ||||||
|             ext.startContract(fileNames.get(0), config.createParam, config.remoteDebugPort, |             ext.startContract(fileNames.get(0), config.createParam, config.remoteDebugPort, new ResultCallback() { | ||||||
|                     new ResultCallback() { |  | ||||||
|                 @Override |                 @Override | ||||||
|                 public void onResult(JsonObject r) { |                 public void onResult(JsonObject r) { | ||||||
|                     System.out.println(TAG + r); |                     System.out.println(TAG + r); | ||||||
|  |                     System.out.println(TAG + "access: " + config.agentAddress); | ||||||
|                     counter.incrementAndGet(); |                     counter.incrementAndGet(); | ||||||
|                 } |                 } | ||||||
|             }); |             }); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         for (; counter.get() == 2;) |         for (; counter.get() == 2; ) | ||||||
|             Thread.yield(); |             Thread.yield(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -150,8 +146,7 @@ public class HTTPTool { | |||||||
|         String sub = dirName.substring(contractName.length()); |         String sub = dirName.substring(contractName.length()); | ||||||
|         if (sub.startsWith("_")) { |         if (sub.startsWith("_")) { | ||||||
|             return Integer.valueOf(sub.substring(1)); |             return Integer.valueOf(sub.substring(1)); | ||||||
|         } else |         } else return -1; | ||||||
|             return -1; |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public static void deployUseHttp(DeployConfig config) { |     public static void deployUseHttp(DeployConfig config) { | ||||||
| @ -160,41 +155,34 @@ public class HTTPTool { | |||||||
|         String argWithoutSig = "path=%s&fileName=%s&isPrivate=true&order=%d&count=%d&pubKey=%s"; |         String argWithoutSig = "path=%s&fileName=%s&isPrivate=true&order=%d&count=%d&pubKey=%s"; | ||||||
|         SM2KeyPair keyPair; |         SM2KeyPair keyPair; | ||||||
|         try { |         try { | ||||||
|             keyPair = SM2KeyPair |             keyPair = SM2KeyPair.fromJson(String.format("{\"publicKey\":\"%s\",\"privateKey\":\"%s\"}", config.publicKey, config.privateKey)); | ||||||
|                     .fromJson(String.format("{\"publicKey\":\"%s\",\"privateKey\":\"%s\"}", |  | ||||||
|                             config.publicKey, config.privateKey)); |  | ||||||
|         } catch (Exception e) { |         } catch (Exception e) { | ||||||
|             System.out.println("[HttpTool] parse key error:\n" |             System.out.println("[HttpTool] parse key error:\n" + String.format("{\"publicKey\":\"%s\",\"privateKey\":\"%s\"}", config.publicKey, config.privateKey)); | ||||||
|                     + String.format("{\"publicKey\":\"%s\",\"privateKey\":\"%s\"}", |  | ||||||
|                             config.publicKey, config.privateKey)); |  | ||||||
|             e.printStackTrace(); |             e.printStackTrace(); | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
|         String pubKey = keyPair.getPublicKeyStr(); |         String pubKey = keyPair.getPublicKeyStr(); | ||||||
|         int trunc = 300 * 1024; |         int trunc = 1000 * 1024; | ||||||
|  |         if (config.chunkSize > 10) | ||||||
|  |             trunc = config.chunkSize; | ||||||
|         byte[] buff = new byte[trunc]; |         byte[] buff = new byte[trunc]; | ||||||
|         int order = 0; |         int order = 0; | ||||||
|         int count = (int) ((file.length() - 1) / (trunc)) + 1; |         int count = (int) ((file.length() - 1) / (trunc)) + 1; | ||||||
|  |         CloseableHttpClient client = HttpClients.createDefault(); | ||||||
|         try { |         try { | ||||||
|             FileInputStream fin = new FileInputStream(file); |             FileInputStream fin = new FileInputStream(file); | ||||||
|             for (int len = 0; (len = fin.read(buff)) > 0; order++) { |             for (int len = 0; (len = fin.read(buff)) > 0; order++) { | ||||||
|                 CloseableHttpClient client = HttpClients.createDefault(); |                 String arg = String.format(argWithoutSig, "./", file.getName(), order, count, pubKey); | ||||||
|                 String arg = |                 String sign = ByteUtils.toHexString(SM2Util.sign(keyPair.getPrivateKeyParameter(), arg.getBytes(StandardCharsets.UTF_8))); | ||||||
|                         String.format(argWithoutSig, "./", file.getName(), order, count, pubKey); |  | ||||||
|                 String sign = ByteUtils.toHexString(SM2Util.sign(keyPair.getPrivateKeyParameter(), |  | ||||||
|                         arg.getBytes(StandardCharsets.UTF_8))); |  | ||||||
|                 String urlStr = String.format(url, config.agentAddress, arg, sign); |                 String urlStr = String.format(url, config.agentAddress, arg, sign); | ||||||
|                 HttpPost httpPost = new HttpPost(urlStr); |                 HttpPost httpPost = new HttpPost(urlStr); | ||||||
|                 MultipartEntityBuilder builder = MultipartEntityBuilder.create(); |                 MultipartEntityBuilder builder = MultipartEntityBuilder.create(); | ||||||
|                 if (len == trunc) |                 if (len == trunc) | ||||||
|                     builder.addBinaryBody("file", buff, ContentType.APPLICATION_OCTET_STREAM, |                     builder.addBinaryBody("file", buff, ContentType.APPLICATION_OCTET_STREAM, file.getName()); | ||||||
|                             file.getName()); |  | ||||||
|                 else { |                 else { | ||||||
|                     byte[] bu = new byte[len]; |                     byte[] bu = new byte[len]; | ||||||
|                     System.arraycopy(buff, 0, bu, 0, len); |                     System.arraycopy(buff, 0, bu, 0, len); | ||||||
|                     builder.addBinaryBody("file", bu, ContentType.APPLICATION_OCTET_STREAM, |                     builder.addBinaryBody("file", bu, ContentType.APPLICATION_OCTET_STREAM, file.getName()); | ||||||
|                             file.getName()); |  | ||||||
| 
 |  | ||||||
|                 } |                 } | ||||||
|                 HttpEntity multipart = builder.build(); |                 HttpEntity multipart = builder.build(); | ||||||
|                 httpPost.setEntity(multipart); |                 httpPost.setEntity(multipart); | ||||||
| @ -202,10 +190,15 @@ public class HTTPTool { | |||||||
|                 ByteArrayOutputStream bo = new ByteArrayOutputStream(); |                 ByteArrayOutputStream bo = new ByteArrayOutputStream(); | ||||||
|                 response.getEntity().writeTo(bo); |                 response.getEntity().writeTo(bo); | ||||||
|                 System.out.println(TAG + +order + "/" + count + " " + bo); |                 System.out.println(TAG + +order + "/" + count + " " + bo); | ||||||
|                 client.close(); |  | ||||||
|             } |             } | ||||||
|         } catch (IOException | CryptoException e) { |         } catch (IOException | CryptoException e) { | ||||||
|             e.printStackTrace(); |             e.printStackTrace(); | ||||||
|  |         } finally { | ||||||
|  |             try { | ||||||
|  |                 client.close(); | ||||||
|  |             } catch (IOException e) { | ||||||
|  |                 e.printStackTrace(); | ||||||
|  |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -35,7 +35,7 @@ public class HttpUploadTest { | |||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
|         String pubKey = keyPair.getPublicKeyStr(); |         String pubKey = keyPair.getPublicKeyStr(); | ||||||
|         int trunc = 300 * 1024; |         int trunc = 500 * 1024; | ||||||
|         byte[] buff = new byte[trunc]; |         byte[] buff = new byte[trunc]; | ||||||
|         int order = 0; |         int order = 0; | ||||||
|         int count = (int) ((file.length() - 1) / (trunc)) + 1; |         int count = (int) ((file.length() - 1) / (trunc)) + 1; | ||||||
| @ -73,4 +73,9 @@ public class HttpUploadTest { | |||||||
|             e.printStackTrace(); |             e.printStackTrace(); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void deployTest() { | ||||||
|  |         org.bdware.ypkdeploy.HTTPTool.deployWithYpk("/Users/huaqiancai/BDWare/sci-data-trace/backend/deployconf.json", "/Users/huaqiancai/BDWare/sci-data-trace/backend/build/sci-data-trace-2.1.5.ypk"); | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user