Skip to content

Commit

Permalink
Merge pull request #80 from IBM/remove_dependency
Browse files Browse the repository at this point in the history
Remove httpmime dependency and upgraded to httpclient5
  • Loading branch information
davenice committed Jun 10, 2024
2 parents ae7eef8 + fa5fb29 commit 32f85b4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 30 deletions.
11 changes: 8 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,14 @@
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.14</version>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.core5</groupId>
<artifactId>httpcore5</artifactId>
<version>5.2.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand Down
61 changes: 35 additions & 26 deletions src/main/java/com/ibm/cics/bundle/deploy/BundleDeployHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,23 @@
import java.util.Map.Entry;
import java.util.stream.Collectors;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.TrustAllStrategy;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.entity.mime.FileBody;
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
import org.apache.hc.client5.http.entity.mime.StringBody;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactoryBuilder;
import org.apache.hc.client5.http.ssl.TrustAllStrategy;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.ssl.SSLContexts;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.Header;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.ssl.SSLContextBuilder;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -92,16 +94,23 @@ public static void deployBundle(URI endpointURL, File bundle, String bunddef, St
HttpEntity httpEntity = mpeb.build();
httpPost.setEntity(httpEntity);

HttpClient httpClient;
CloseableHttpClient httpClient;
if (!allowSelfSignedCertificate) {
httpClient = HttpClientBuilder.create().useSystemProperties().build();
} else {
try {
httpClient = HttpClients.custom()
.useSystemProperties()
.setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, TrustAllStrategy.INSTANCE).build())
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.build();
SSLContextBuilder sslContextBuilder = SSLContexts.custom().loadTrustMaterial(new TrustAllStrategy());
SSLConnectionSocketFactoryBuilder sslSocketFactoryBuilder = SSLConnectionSocketFactoryBuilder.create()
.setSslContext(sslContextBuilder.build())
.setHostnameVerifier(NoopHostnameVerifier.INSTANCE);

PoolingHttpClientConnectionManagerBuilder connectionManagerBuilder = PoolingHttpClientConnectionManagerBuilder.create()
.setSSLSocketFactory(sslSocketFactoryBuilder.build());

httpClient = HttpClients.custom()
.setConnectionManager(connectionManagerBuilder.build())
.useSystemProperties()
.build();
} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
throw new BundleDeployException("Error instantiating secure connection", e);
}
Expand All @@ -118,22 +127,22 @@ public static void deployBundle(URI endpointURL, File bundle, String bunddef, St



HttpResponse response = httpClient.execute(httpPost);
StatusLine responseStatus = response.getStatusLine();
CloseableHttpResponse response = httpClient.execute(httpPost);
int responseStatusCode = response.getCode();
Header[] contentTypeHeaders = response.getHeaders("Content-Type");
String contentType;
if (contentTypeHeaders.length != 1) {
contentType = null;
} else {
contentType = contentTypeHeaders[0].getValue();
}
if (responseStatus.getStatusCode() != 200) {

if (responseStatusCode != 200) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
try {
String responseContent = bufferedReader.lines().collect(Collectors.joining());
if (contentType == null) {
throw new BundleDeployException("Http response: " + responseStatus);
throw new BundleDeployException("Http response: " + responseStatusCode);
} else if (contentType.equals("application/xml")) {
//liberty level error
throw new BundleDeployException(responseContent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void testBundleDeployHelper_unauthenticated401_noContentType() throws Exc
File bundleArchive = new File(bundleFilePath);

expectedException.expect(BundleDeployException.class);
expectedException.expectMessage("Http response: HTTP/1.1 401 Unauthorized");
expectedException.expectMessage("Http response: 401");

BundleDeployHelper.deployBundle(new URI(wireMockRule.baseUrl()), bundleArchive, "bundle", "csdgroup", "cicsplex", "region", "username", "password".toCharArray(), true);
}
Expand Down

0 comments on commit 32f85b4

Please sign in to comment.