Skip to content

Commit

Permalink
Adding apache Multipart request builder
Browse files Browse the repository at this point in the history
  • Loading branch information
kasundharmadasa committed Oct 5, 2018
1 parent 940f168 commit aa9ae37
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 215 deletions.
5 changes: 2 additions & 3 deletions internal/sgs-webapp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.1</version>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -83,7 +83,7 @@
</dependency>
</dependencies>
<build>
<finalName>automation-manager-webapp</finalName>
<finalName>scan-manager-webapp</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
Expand All @@ -92,7 +92,6 @@
<fork>true</fork>
<mainClass>org.wso2.security.tools.sgs.webapp.Application</mainClass>
<jvmArguments>-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005</jvmArguments>

</configuration>
<executions>
<execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class HttpRequestHandler {
* @return
* @throws RestClientException
*/
public static ResponseEntity<String> sendPOST(String url, MultiValueMap<String, String> requestHeaders, Map<String, Object>
public ResponseEntity<String> sendPOST(String url, MultiValueMap<String, String> requestHeaders, Map<String, Object>
requestParams) throws RestClientException {

HttpEntity<?> request = new HttpEntity<>(requestParams, requestHeaders);
Expand All @@ -56,7 +56,7 @@ public static ResponseEntity<String> sendPOST(String url, MultiValueMap<String,
* @return
* @throws RestClientException
*/
public static ResponseEntity<String> sendGET(String url, MultiValueMap<String, String> requestHeaders, Map<String, Object>
public ResponseEntity<String> sendGET(String url, MultiValueMap<String, String> requestHeaders, Map<String, Object>
requestParams) throws RestClientException {

HttpEntity<?> request = new HttpEntity<>(requestParams, requestHeaders);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@

package org.wso2.security.tools.sgs.webapp.service;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -34,7 +41,6 @@
import org.wso2.security.tools.sgs.webapp.entity.Scan;
import org.wso2.security.tools.sgs.webapp.entity.Scanner;
import org.wso2.security.tools.sgs.webapp.handlers.HttpRequestHandler;
import org.wso2.security.tools.sgs.webapp.handlers.MultipartRequestHandler;

import java.io.IOException;
import java.net.URI;
Expand All @@ -58,7 +64,6 @@ public class ScanService {
private static final String OWNER = "owner";
private static final String STATUS = "status";
private static final String SCHEME = "http";
private static final String CHARSET_UTF8 = "UTF-8";

/**
* Get Scanner details for a given scanner ID
Expand Down Expand Up @@ -99,6 +104,7 @@ public int startScan(MultiValueMap<String, MultipartFile> multipartFileMultiValu
Map<String, String[]> parameterMap) {

int responseStatus = -1;
CloseableHttpClient client = null;

try {
URI uri = (new URIBuilder())
Expand All @@ -107,33 +113,54 @@ public int startScan(MultiValueMap<String, MultipartFile> multipartFileMultiValu
.setScheme(SCHEME).setPath(scanManagerConfiguration.getStartScanURI())
.build();

MultipartRequestHandler multipartRequest = new MultipartRequestHandler(uri.toString(),
CHARSET_UTF8);
HttpPost httppost = new HttpPost(uri);
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();

for (Map.Entry<String, MultipartFile> entry : multipartFileMultiValueMap
.toSingleValueMap().entrySet()) {
multipartRequest.addFilePart(entry.getKey(), entry.getValue()
.getInputStream(), entry.getValue().getName());
multipartEntityBuilder.addBinaryBody(entry.getKey(), entry.getValue()
.getInputStream(), ContentType.MULTIPART_FORM_DATA, entry.getValue().getOriginalFilename());
}

for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
multipartRequest.addFormField(entry.getKey(), String.valueOf(entry.getValue()));
multipartEntityBuilder.addTextBody(entry.getKey(), convertStringArrayToString(entry.getValue(), ","));
}

responseStatus = multipartRequest.finish();
HttpEntity httpEntity = multipartEntityBuilder.build();
httppost.setEntity(httpEntity);
client = HttpClients.createDefault();
HttpResponse httpResponse = client.execute(httppost);
responseStatus = httpResponse.getStatusLine().getStatusCode();

if (responseStatus != HttpStatus.SC_OK) {
logger.error("Error occurred while initiating the scan. " + responseStatus + " "
+ multipartRequest.getResponseMessage());
+ httpResponse.getStatusLine().getReasonPhrase());
}
} catch (NumberFormatException e) {
logger.error("Unable to retrieve the scan manager port", e);
} catch (URISyntaxException e) {
logger.error("Unable to build the start scan URI", e);
} catch (IOException e) {
logger.error("Unable to start the scan", e);
} finally {
try {
if (client != null) {
client.close();
}
} catch (IOException e) {
logger.error("Unable to close the http connection with scan manager", e);
}
}
return responseStatus;
}

private static String convertStringArrayToString(String[] strArr, String delimiter) {
StringBuilder sb = new StringBuilder();
for (String str : strArr)
sb.append(str).append(delimiter);
return sb.substring(0, sb.length() - 1);
}

/**
* Get the list of scans in the pool
*
Expand All @@ -144,6 +171,7 @@ public ArrayList<Scan> getScans() {
MultiValueMap headerMap = new LinkedMultiValueMap();
Map<String, Object> paramMap = new HashMap<>();
ArrayList<Scan> scanList = new ArrayList<>();
HttpRequestHandler httpRequestHandler = new HttpRequestHandler();

try {
URI uri = (new URIBuilder())
Expand All @@ -152,7 +180,7 @@ public ArrayList<Scan> getScans() {
.setScheme(SCHEME).setPath(scanManagerConfiguration.getScansURI())
.build();

ResponseEntity<String> responseEntity = HttpRequestHandler.sendGET(uri.toString(), headerMap, paramMap);
ResponseEntity<String> responseEntity = httpRequestHandler.sendGET(uri.toString(), headerMap, paramMap);
JSONArray responseArray = new JSONArray(responseEntity.getBody());

for (int arrayIndex = 0; arrayIndex < responseArray.length(); arrayIndex++) {
Expand Down Expand Up @@ -183,6 +211,8 @@ public ResponseEntity stopScan(String id) {
MultiValueMap headerMap = new LinkedMultiValueMap();
headerMap.add("Content-Type", "application/json");
Map<String, Object> paramMap = new HashMap<>();
HttpRequestHandler httpRequestHandler = new HttpRequestHandler();

paramMap.put(ID, id);

try {
Expand All @@ -192,7 +222,7 @@ public ResponseEntity stopScan(String id) {
.setScheme(SCHEME).setPath(scanManagerConfiguration.getStopScanURI())
.build();

responseEntity = HttpRequestHandler.sendPOST(uri.toString(), headerMap, paramMap);
responseEntity = httpRequestHandler.sendPOST(uri.toString(), headerMap, paramMap);

} catch (RestClientException | JSONException | URISyntaxException e) {
logger.error("Unable to stop the scan " + id, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

import java.util.ArrayList;

/**
* Wrapper class for returning the scan list.
*/
public class ScanListWrapper {
private ArrayList<Scan> scans;

Expand Down
Loading

0 comments on commit aa9ae37

Please sign in to comment.