|
34 | 34 | import org.apache.commons.logging.Log; |
35 | 35 | import org.apache.commons.logging.LogFactory; |
36 | 36 | import org.apache.http.HttpResponse; |
| 37 | +import org.apache.http.client.HttpClient; |
37 | 38 | import org.apache.http.client.methods.CloseableHttpResponse; |
38 | 39 | import org.apache.http.client.methods.HttpGet; |
39 | 40 | import org.apache.http.client.methods.HttpHead; |
| 41 | +import org.apache.http.client.methods.HttpPost; |
40 | 42 | import org.apache.http.client.methods.HttpPut; |
| 43 | +import org.apache.http.entity.ContentType; |
41 | 44 | import org.apache.http.entity.StringEntity; |
| 45 | +import org.apache.http.entity.mime.HttpMultipartMode; |
| 46 | +import org.apache.http.entity.mime.MultipartEntityBuilder; |
42 | 47 | import org.apache.http.impl.client.CloseableHttpClient; |
| 48 | +import org.apache.http.util.EntityUtils; |
43 | 49 | import org.json.JSONArray; |
44 | 50 | import org.json.JSONTokener; |
45 | 51 | import org.json.simple.JSONObject; |
|
97 | 103 | import java.io.IOException; |
98 | 104 | import java.io.InputStream; |
99 | 105 | import java.io.InputStreamReader; |
100 | | -import java.io.OutputStream; |
101 | | -import java.io.OutputStreamWriter; |
102 | | -import java.io.PrintWriter; |
103 | 106 | import java.net.HttpURLConnection; |
104 | 107 | import java.net.MalformedURLException; |
105 | 108 | import java.net.URI; |
@@ -499,93 +502,53 @@ private static void updateAuditApi(String apiDefinition, String apiToken, String |
499 | 502 | * @throws ParseException In the event of any parse errors from the response |
500 | 503 | */ |
501 | 504 | private static String createAuditApi(String collectionId, String apiToken, APIIdentifier apiIdentifier, |
502 | | - String apiDefinition, String baseUrl, boolean isDebugEnabled, String organization) |
| 505 | + String apiDefinition, String baseUrl, boolean isDebugEnabled, String organization) |
503 | 506 | throws IOException, APIManagementException, ParseException { |
504 | | - HttpURLConnection httpConn; |
505 | | - OutputStream outputStream; |
506 | | - PrintWriter writer; |
507 | 507 | String auditUuid = null; |
508 | | - URL url = new URL(baseUrl); |
509 | | - httpConn = (HttpURLConnection) url.openConnection(); |
510 | | - httpConn.setUseCaches(false); |
511 | | - httpConn.setDoOutput(true); // indicates POST method |
512 | | - httpConn.setDoInput(true); |
513 | | - httpConn.setRequestProperty(APIConstants.HEADER_CONTENT_TYPE, |
514 | | - APIConstants.MULTIPART_CONTENT_TYPE + APIConstants.MULTIPART_FORM_BOUNDARY); |
515 | | - httpConn.setRequestProperty(APIConstants.HEADER_ACCEPT, APIConstants.APPLICATION_JSON_MEDIA_TYPE); |
516 | | - httpConn.setRequestProperty(APIConstants.HEADER_API_TOKEN, apiToken); |
517 | | - httpConn.setRequestProperty(APIConstants.HEADER_USER_AGENT, APIConstants.USER_AGENT_APIM); |
518 | | - outputStream = httpConn.getOutputStream(); |
519 | | - writer = new PrintWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8), true); |
520 | | - // Name property |
521 | | - writer.append("--" + APIConstants.MULTIPART_FORM_BOUNDARY).append(APIConstants.MULTIPART_LINE_FEED) |
522 | | - .append("Content-Disposition: form-data; name=\"name\"") |
523 | | - .append(APIConstants.MULTIPART_LINE_FEED).append(APIConstants.MULTIPART_LINE_FEED) |
524 | | - .append(apiIdentifier.getApiName()).append(APIConstants.MULTIPART_LINE_FEED); |
525 | | - writer.flush(); |
526 | | - // Specfile property |
527 | | - writer.append("--" + APIConstants.MULTIPART_FORM_BOUNDARY).append(APIConstants.MULTIPART_LINE_FEED) |
528 | | - .append("Content-Disposition: form-data; name=\"specfile\"; filename=\"swagger.json\"") |
529 | | - .append(APIConstants.MULTIPART_LINE_FEED) |
530 | | - .append(APIConstants.HEADER_CONTENT_TYPE + ": " + APIConstants.APPLICATION_JSON_MEDIA_TYPE) |
531 | | - .append(APIConstants.MULTIPART_LINE_FEED).append(APIConstants.MULTIPART_LINE_FEED) |
532 | | - .append(apiDefinition).append(APIConstants.MULTIPART_LINE_FEED); |
533 | | - writer.flush(); |
534 | | - // CollectionID property |
535 | | - writer.append("--" + APIConstants.MULTIPART_FORM_BOUNDARY).append(APIConstants.MULTIPART_LINE_FEED) |
536 | | - .append("Content-Disposition: form-data; name=\"cid\"").append(APIConstants.MULTIPART_LINE_FEED) |
537 | | - .append(APIConstants.MULTIPART_LINE_FEED).append(collectionId) |
538 | | - .append(APIConstants.MULTIPART_LINE_FEED); |
539 | | - writer.flush(); |
540 | | - writer.append("--" + APIConstants.MULTIPART_FORM_BOUNDARY + "--") |
541 | | - .append(APIConstants.MULTIPART_LINE_FEED); |
542 | | - writer.close(); |
543 | | - // Checks server's status code first |
544 | | - int status = httpConn.getResponseCode(); |
545 | | - if (status == HttpURLConnection.HTTP_OK) { |
546 | | - if (isDebugEnabled) { |
547 | | - log.debug(HTTP_STATUS_LOG + status); |
548 | | - } |
549 | | - BufferedReader reader = new BufferedReader( |
550 | | - new InputStreamReader(httpConn.getInputStream(), StandardCharsets.UTF_8)); |
551 | | - String inputLine; |
552 | | - StringBuilder responseString = new StringBuilder(); |
553 | | - |
554 | | - while ((inputLine = reader.readLine()) != null) { |
555 | | - responseString.append(inputLine); |
556 | | - } |
557 | | - reader.close(); |
558 | | - httpConn.disconnect(); |
559 | | - JSONObject responseJson = (JSONObject) new JSONParser().parse(responseString.toString()); |
560 | | - auditUuid = (String) ((JSONObject) responseJson.get(APIConstants.DESC)).get(APIConstants.ID); |
561 | | - ApiMgtDAO.getInstance().addAuditApiMapping(apiIdentifier, auditUuid, organization); |
562 | | - } else { |
563 | | - if (httpConn.getErrorStream() != null) { |
564 | | - BufferedReader reader = |
565 | | - new BufferedReader(new InputStreamReader(httpConn.getErrorStream(), StandardCharsets.UTF_8)); |
566 | | - String inputLine; |
567 | | - StringBuilder responseString = new StringBuilder(); |
568 | | - |
569 | | - while ((inputLine = reader.readLine()) != null) { |
570 | | - responseString.append(inputLine); |
571 | | - } |
572 | | - reader.close(); |
573 | | - httpConn.disconnect(); |
574 | | - JSONObject responseJson = (JSONObject) new JSONParser().parse(responseString.toString()); |
575 | | - String errorMessage = httpConn.getResponseMessage(); |
576 | | - if (responseJson.containsKey("message")) { |
577 | | - errorMessage = (String) responseJson.get("message"); |
| 508 | + HttpClient httpClient = APIUtil.getHttpClient(baseUrl); |
| 509 | + HttpPost httpPost = new HttpPost(baseUrl); |
| 510 | + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); |
| 511 | + builder.setBoundary(APIConstants.MULTIPART_FORM_BOUNDARY); |
| 512 | + builder.setCharset(StandardCharsets.UTF_8); |
| 513 | + builder.setMode(HttpMultipartMode.STRICT); |
| 514 | + // "name" field (text) |
| 515 | + builder.addTextBody("name", apiIdentifier.getApiName(), |
| 516 | + ContentType.TEXT_PLAIN.withCharset(StandardCharsets.UTF_8)); |
| 517 | + // "specfile" field (file part with filename and content type) |
| 518 | + builder.addBinaryBody("specfile", apiDefinition.getBytes(StandardCharsets.UTF_8), |
| 519 | + ContentType.create(APIConstants.APPLICATION_JSON_MEDIA_TYPE, StandardCharsets.UTF_8), "swagger.json"); |
| 520 | + // "cid" field (text) |
| 521 | + builder.addTextBody("cid", collectionId, ContentType.TEXT_PLAIN.withCharset(StandardCharsets.UTF_8)); |
| 522 | + httpPost.setEntity(builder.build()); |
| 523 | + httpPost.setHeader(APIConstants.HEADER_API_TOKEN, apiToken); |
| 524 | + httpPost.setHeader(APIConstants.HEADER_USER_AGENT, APIConstants.USER_AGENT_APIM); |
| 525 | + httpPost.setHeader(APIConstants.HEADER_ACCEPT, APIConstants.APPLICATION_JSON_MEDIA_TYPE); |
| 526 | + try (CloseableHttpResponse response = (CloseableHttpResponse) httpClient.execute(httpPost)) { |
| 527 | + int statusCode = response.getStatusLine().getStatusCode(); |
| 528 | + String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); |
| 529 | + if (statusCode == HttpURLConnection.HTTP_OK) { |
| 530 | + if (isDebugEnabled) { |
| 531 | + log.debug(HTTP_STATUS_LOG + statusCode); |
578 | 532 | } |
579 | | - throw new APIManagementException( |
580 | | - "Error while retrieving data for the API Security Audit Report. Found http status: " + |
581 | | - httpConn.getResponseCode() + " - " + errorMessage); |
| 533 | + JSONObject responseJson = (JSONObject) new JSONParser().parse(responseString); |
| 534 | + auditUuid = (String) ((JSONObject) responseJson.get(APIConstants.DESC)).get(APIConstants.ID); |
| 535 | + ApiMgtDAO.getInstance().addAuditApiMapping(apiIdentifier, auditUuid, organization); |
582 | 536 | } else { |
| 537 | + String errorMessage = response.getStatusLine().getReasonPhrase(); |
| 538 | + try { |
| 539 | + JSONObject errorJson = (JSONObject) new JSONParser().parse(responseString); |
| 540 | + if (errorJson.containsKey("message")) { |
| 541 | + errorMessage = (String) errorJson.get("message"); |
| 542 | + } |
| 543 | + } catch (ParseException ignored) { |
| 544 | + // response body is not valid JSON, keep default error message |
| 545 | + } |
583 | 546 | throw new APIManagementException( |
584 | | - "Error while retrieving data for the API Security Audit Report. Found http status: " + |
585 | | - httpConn.getResponseCode() + " - " + httpConn.getResponseMessage()); |
| 547 | + "Error while retrieving data for the API Security Audit Report. Found http status: " |
| 548 | + + statusCode + " - " + errorMessage); |
586 | 549 | } |
| 550 | + return auditUuid; |
587 | 551 | } |
588 | | - return auditUuid; |
589 | 552 | } |
590 | 553 |
|
591 | 554 | /** |
|
0 commit comments