Guest User

Untitled

a guest
Oct 19th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. **String encoding = Base64.getEncoder().encodeToString(("admin:admin").getBytes());
  2. String hawkbitURL = "http://10.x.x.x:8080"; //Replaced with proper IP in my machine
  3. String moduleId=53; //hardcoded for now but I can get this via Java REST API code
  4. String uploadArtifactURL = "/rest/v1/softwaremodules/"+moduleId+"/artifacts";
  5. String boundaryString = "-----------------------------6o2knFse3p53ty9dmcQvWAIx1zInP11uCfbm";
  6. String message = "This is a multipart post";
  7. File artifactFile = new File(artifactPath);
  8.  
  9. // build multipart upload request
  10. HttpEntity data = MultipartEntityBuilder.create()
  11. .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
  12. .addBinaryBody("upfile", artifactFile, ContentType.DEFAULT_BINARY, artifactFile.getName())
  13. .addTextBody("text",message,ContentType.DEFAULT_BINARY)
  14. .setBoundary(boundaryString)
  15. .build();
  16.  
  17. // build http request and assign multipart upload data
  18. HttpUriRequest request = RequestBuilder
  19. .post(hawkbitURL+uploadArtifactURL)
  20. .setHeader("Authorization", "Basic " + encoding)
  21. .setEntity(data)
  22. .build();
  23.  
  24. System.out.println("Executing request " + request.getRequestLine());
  25.  
  26. // Create a custom response handler
  27. ResponseHandler<String> responseHandler = response -> {
  28. System.out.println(response.getStatusLine());
  29. int status = response.getStatusLine().getStatusCode();
  30. if (status >= 200 && status < 300) {
  31. HttpEntity entity = response.getEntity();
  32. return entity != null ? EntityUtils.toString(entity) : null;
  33. } else {
  34. throw new ClientProtocolException("Unexpected response status: " + status);
  35. }
  36. };
  37. String responseBody = httpClient.execute(request, responseHandler);
  38. System.out.println("----------------------------------------");
  39. System.out.println(responseBody);**
Add Comment
Please, Sign In to add comment