Skip to content

Commit e505e97

Browse files
committed
Add custom SSL Context
1 parent 96b9814 commit e505e97

File tree

1 file changed

+46
-83
lines changed

1 file changed

+46
-83
lines changed

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/restapi/publisher/ApisApiServiceImplUtils.java

Lines changed: 46 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,18 @@
3434
import org.apache.commons.logging.Log;
3535
import org.apache.commons.logging.LogFactory;
3636
import org.apache.http.HttpResponse;
37+
import org.apache.http.client.HttpClient;
3738
import org.apache.http.client.methods.CloseableHttpResponse;
3839
import org.apache.http.client.methods.HttpGet;
3940
import org.apache.http.client.methods.HttpHead;
41+
import org.apache.http.client.methods.HttpPost;
4042
import org.apache.http.client.methods.HttpPut;
43+
import org.apache.http.entity.ContentType;
4144
import org.apache.http.entity.StringEntity;
45+
import org.apache.http.entity.mime.HttpMultipartMode;
46+
import org.apache.http.entity.mime.MultipartEntityBuilder;
4247
import org.apache.http.impl.client.CloseableHttpClient;
48+
import org.apache.http.util.EntityUtils;
4349
import org.json.JSONArray;
4450
import org.json.JSONTokener;
4551
import org.json.simple.JSONObject;
@@ -97,9 +103,6 @@
97103
import java.io.IOException;
98104
import java.io.InputStream;
99105
import java.io.InputStreamReader;
100-
import java.io.OutputStream;
101-
import java.io.OutputStreamWriter;
102-
import java.io.PrintWriter;
103106
import java.net.HttpURLConnection;
104107
import java.net.MalformedURLException;
105108
import java.net.URI;
@@ -499,93 +502,53 @@ private static void updateAuditApi(String apiDefinition, String apiToken, String
499502
* @throws ParseException In the event of any parse errors from the response
500503
*/
501504
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)
503506
throws IOException, APIManagementException, ParseException {
504-
HttpURLConnection httpConn;
505-
OutputStream outputStream;
506-
PrintWriter writer;
507507
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);
578532
}
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);
582536
} 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+
}
583546
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);
586549
}
550+
return auditUuid;
587551
}
588-
return auditUuid;
589552
}
590553

591554
/**

0 commit comments

Comments
 (0)