Skip to content

Commit

Permalink
Suggested changes (#133)
Browse files Browse the repository at this point in the history
* Suggested changes
  • Loading branch information
vishalhcl-5960 authored Jan 26, 2024
1 parent 75b1e0d commit 8e5cfe3
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 95 deletions.
29 changes: 3 additions & 26 deletions src/main/java/com/hcl/appscan/sdk/http/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,36 +115,13 @@ public HttpResponse post(String url,
*
* @param url The URL string.
* @param headerProperties An optional Map of header properties.
* @param params An optional Map of properties.
* @param parameters An optional Map of properties.
* @return The response as a byte array.
* @throws IOException If an error occurs.
*/
public HttpResponse post(String url, Map<String, String> headerProperties, Map<String, String> params)
throws IOException {
Map<String, Object> objectMap = new HashMap<>();
for (String key : params.keySet()) {
String value = params.get(key);
if (value != null) {
if (value.equalsIgnoreCase("true")) {
objectMap.put(key, true);
} else if (value.equalsIgnoreCase("false")) {
objectMap.put(key, false);
} else {
// If the string is not "true" or "false," keep it as is
objectMap.put(key, value);
}
} else {
// If the value is not a string, keep it as is
objectMap.put(key, value);
}
}
JSONObject json = new JSONObject(objectMap);
String body = json.toString();
return post(url, headerProperties, body);
}

public HttpResponse posts(String url, Map<String, String> headerProperties, JSONObject params)
public HttpResponse post(String url, Map<String, String> headerProperties, Map<String, String> parameters)
throws IOException, JSONException {
JSONObject params = new JSONObject(parameters);
JSONObject objectMap = new JSONObject();
for (Object key : params.keySet()) {
if (params.get(key) != null){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ protected String getReportStatus(String reportId) throws IOException, JSONExcept
}

JSONObject obj = (JSONObject) response.getResponseBodyAsJSON();
JSONArray array = obj.getJSONArray("Items");
JSONArray array = obj.getJSONArray(ITEMS);
JSONObject json= (JSONObject) array.get(0);
return json.getString(STATUS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ public String createAndExecuteScan(String type, Map<String, String> params) {
}
return null;
}

@Override
public String createAndExecuteScanWithJSONParameter(String type, JSONObject params) {
return "";
}

private String createJob(Map<String, String> params) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public CloudScanServiceProvider(IProgress progress, IAuthenticationProvider auth

@Override
public String createAndExecuteScan(String type, Map<String, String> params) {
if(loginExpired() || !verifyApplication(params.get(APP_ID)))
if(loginExpired() || !verifyApplication(params.get(APP_ID).toString()))
return null;

m_progress.setStatus(new Message(Message.INFO, Messages.getMessage(EXECUTING_SCAN)));
Expand Down Expand Up @@ -93,55 +93,6 @@ public String createAndExecuteScan(String type, Map<String, String> params) {
}
return null;
}

@Override
public String createAndExecuteScanWithJSONParameter(String type, JSONObject params) {
try {
if(loginExpired() || !verifyApplication(params.get(APP_ID).toString()))
return null;
} catch (JSONException e) {
throw new RuntimeException(e);
}

m_progress.setStatus(new Message(Message.INFO, Messages.getMessage(EXECUTING_SCAN)));
Map<String, String> request_headers = m_authProvider.getAuthorizationHeader(true);
HttpClient client = new HttpClient(m_authProvider.getProxy(), m_authProvider.getacceptInvalidCerts());

try {
HttpResponse response;
request_headers.put("Content-Type", "application/json");
request_headers.put("accept", "application/json");
String request_url = m_authProvider.getServer() + String.format(API_SCANNER, type);
response = client.posts(request_url, request_headers, params);

int status = response.getResponseCode();

JSONObject json = (JSONObject) response.getResponseBodyAsJSON();

if (status == HttpsURLConnection.HTTP_CREATED || status == HttpsURLConnection.HTTP_OK) {
m_progress.setStatus(new Message(Message.INFO, Messages.getMessage(CREATE_SCAN_SUCCESS)));
return json.getString(ID);
} else if (json != null && json.has(MESSAGE)) {
String errorResponse = json.getString(MESSAGE);
if(json.has(FORMAT_PARAMS) && !json.isNull(FORMAT_PARAMS)) {
JSONArray jsonArray = json.getJSONArray(FORMAT_PARAMS);
if(jsonArray != null){
String[] messageParams = new String[jsonArray.size()];
for (int i = 0; i < jsonArray.size(); i++) {
messageParams[i] = (String)jsonArray.get(i);
}
errorResponse = MessageFormat.format(errorResponse, (Object[]) messageParams);
}
}
m_progress.setStatus(new Message(Message.ERROR, errorResponse));
}
else
m_progress.setStatus(new Message(Message.ERROR, Messages.getMessage(ERROR_SUBMITTING_SCAN, status)));
} catch(IOException | JSONException e) {
m_progress.setStatus(new Message(Message.ERROR, Messages.getMessage(ERROR_SUBMITTING_SCAN, e.getLocalizedMessage())));
}
return null;
}

@Override
public String submitFile(File file) throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/hcl/appscan/sdk/scan/IScan.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface IScan {
* @throws ScannerException if a fatal error occurs in the scan.
* @throws InvalidTargetException if the target is invalid.
*/
public void run() throws ScannerException, InvalidTargetException, JSONException;
public void run() throws ScannerException, InvalidTargetException;

/**
* Gets the id of the scan.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,6 @@ public interface IScanServiceProvider {
*/
public String createAndExecuteScan(String type, Map<String, String> params);

/**
* Creates and executes a scan.
*
* @param type The type of scan to execute. For example DynamicAnalyzer.
* @param params A JSON of scan parameters.
* @return The id of the submitted scan, if successful. Otherwise, null.
*/
public String createAndExecuteScanWithJSONParameter(String type, JSONObject params);

/**
* Submits a file for scanning.
*
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/hcl/appscan/sdk/scanners/dynamic/DASTScan.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.hcl.appscan.sdk.logging.DefaultProgress;
import com.hcl.appscan.sdk.logging.IProgress;
import com.hcl.appscan.sdk.scan.IScanServiceProvider;
import com.hcl.appscan.sdk.scan.CloudScanServiceProvider;
import com.hcl.appscan.sdk.scanners.ASoCScan;
import com.hcl.appscan.sdk.utils.ServiceUtil;
import org.apache.wink.json4j.JSONException;
Expand All @@ -37,7 +38,7 @@ public DASTScan(Map<String, String> properties, IProgress progress, IScanService
}

@Override
public void run() throws ScannerException, InvalidTargetException, JSONException {
public void run() throws ScannerException, InvalidTargetException {
String type = DYNAMIC_ANALYZER;
String target = getTarget();

Expand Down Expand Up @@ -91,8 +92,12 @@ public void run() throws ScannerException, InvalidTargetException, JSONException
}
}

JSONObject propertiesJSON = createJSONForProperties(params);
setScanId(getServiceProvider().createAndExecuteScanWithJSONParameter(type, propertiesJSON));
try {
JSONObject propertiesJSON = createJSONForProperties(params);
setScanId(getServiceProvider().createAndExecuteScan(type, propertiesJSON));
} catch (JSONException e) {
throw new ScannerException(Messages.getMessage(ERROR_RUNNING_SCAN, e.getLocalizedMessage()));
}

if(getScanId() == null)
throw new ScannerException(Messages.getMessage(ERROR_CREATING_SCAN));
Expand Down

0 comments on commit 8e5cfe3

Please sign in to comment.