Skip to content

Commit

Permalink
update to Apache HTTP client version 5.1
Browse files Browse the repository at this point in the history
- keeping version 4 for plugins (e.g. IfcOpenShell) and Eclipse aether (see issue #1240)
- update legacy HTTP client lib to 4.5.13
  • Loading branch information
hlg committed May 15, 2022
1 parent e808e03 commit e8266bf
Show file tree
Hide file tree
Showing 14 changed files with 524 additions and 561 deletions.
318 changes: 159 additions & 159 deletions BimServer/src/org/bimserver/webservices/impl/OAuthServiceImpl.java

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions BimServer/src/org/bimserver/webservices/impl/ServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@
import javax.mail.util.ByteArrayDataSource;

import org.apache.commons.collections.comparators.ComparatorChain;
import org.apache.http.Header;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
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.client5.http.classic.methods.HttpPost;
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.bimserver.BimServerImporter;
import org.bimserver.BimserverDatabaseException;
import org.bimserver.client.json.JsonBimServerClientFactory;
Expand Down Expand Up @@ -2624,15 +2625,15 @@ public void triggerRevisionService(Long roid, Long soid) throws ServerException,
}
httpPost.setHeader("Input-Type", newService.getInput());
httpPost.setHeader("Output-Type", newService.getOutput());
httpPost.setEntity(new ByteArrayEntity(baos.toByteArray()));
httpPost.setEntity(new ByteArrayEntity(baos.toByteArray(), ContentType.APPLICATION_OCTET_STREAM));

long start = System.nanoTime();

CloseableHttpResponse response = httpclient.execute(httpPost);

if (response.getStatusLine().getStatusCode() == 401) {
if (response.getCode() == 401) {
throw new UserException("Remote service responded with a 401 Unauthorized");
} else if (response.getStatusLine().getStatusCode() == 200) {
} else if (response.getCode() == 200) {
Header[] headers = response.getHeaders("Content-Disposition");
String filename = "unknown";
if (headers.length > 0) {
Expand Down Expand Up @@ -2683,7 +2684,7 @@ public void triggerRevisionService(Long roid, Long soid) throws ServerException,
newService.getOid());
}
} else {
throw new UserException("Remote service responded with a " + response.getStatusLine());
throw new UserException("Remote service responded with a " + response.getCode() + " (" + response.getReasonPhrase() + ")");
}
} catch (Exception e) {
handleException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;

import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.apache.hc.core5.http.config.Registry;
import org.apache.hc.core5.http.config.RegistryBuilder;
import org.apache.hc.client5.http.socket.ConnectionSocketFactory;
import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
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.io.PoolingHttpClientConnectionManager;
import org.apache.hc.core5.ssl.SSLContextBuilder;
import org.apache.hc.core5.ssl.SSLContexts;
import org.apache.hc.client5.http.ssl.HttpsSupport;
import org.bimserver.emf.MetaDataManager;
import org.bimserver.interfaces.SServiceInterfaceService;
import org.bimserver.plugins.services.BimServerClientInterface;
Expand Down Expand Up @@ -122,7 +123,7 @@ private void initHttpClient(SSLContext sslContext) {
sslContext == null ? SSLContexts.createSystemDefault() : sslContext,
new String[] { "TLSv1.2" },
null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier()
HttpsSupport.getDefaultHostnameVerifier()
);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
Expand Down
161 changes: 63 additions & 98 deletions BimServerClientLib/src/org/bimserver/client/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
import java.util.Set;
import java.util.zip.DeflaterInputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.hc.client5.http.ClientProtocolException;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.client5.http.entity.mime.HttpMultipartMode;
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
import org.apache.hc.client5.http.entity.mime.InputStreamBody;
import org.apache.hc.client5.http.entity.mime.StringBody;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.bimserver.interfaces.objects.SLongCheckinActionState;
import org.bimserver.shared.ChannelConnectionException;
import org.bimserver.shared.ConnectDisconnectListener;
Expand Down Expand Up @@ -116,33 +116,32 @@ protected void finish(Reflector reflector, ReflectorFactory reflectorFactory) {
public SLongCheckinActionState checkinSync(String baseAddress, String token, long poid, String comment, long deserializerOid, boolean merge, long fileSize, String filename, InputStream inputStream, Long topicId) throws ServerException, UserException {
String address = baseAddress + "/upload";
HttpPost httppost = new HttpPost(address);
try {
if (topicId == null) {
topicId = getServiceInterface().initiateCheckin(poid, deserializerOid);
}
// TODO find some GzipInputStream variant that _compresses_ instead
// of _decompresses_ using deflate for now
InputStreamBody data = new InputStreamBody(new DeflaterInputStream(inputStream), filename);
if (topicId == null) {
topicId = getServiceInterface().initiateCheckin(poid, deserializerOid);
}
// TODO find some GzipInputStream variant that _compresses_ instead
// of _decompresses_ using deflate for now
InputStreamBody data = new InputStreamBody(new DeflaterInputStream(inputStream), filename);

MultipartEntityBuilder multipartEntityBuilder = createMultiPart();
MultipartEntityBuilder multipartEntityBuilder = createMultiPart();

multipartEntityBuilder.addPart("topicId", new StringBody("" + topicId, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("token", new StringBody(token, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("deserializerOid", new StringBody("" + deserializerOid, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("merge", new StringBody("" + merge, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("poid", new StringBody("" + poid, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("comment", new StringBody("" + comment, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("sync", new StringBody("" + true, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("compression", new StringBody("deflate", ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("data", data);
multipartEntityBuilder.addPart("topicId", new StringBody("" + topicId, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("token", new StringBody(token, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("deserializerOid", new StringBody("" + deserializerOid, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("merge", new StringBody("" + merge, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("poid", new StringBody("" + poid, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("comment", new StringBody("" + comment, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("sync", new StringBody("" + true, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("compression", new StringBody("deflate", ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("data", data);

httppost.setEntity(multipartEntityBuilder.build());
httppost.setEntity(multipartEntityBuilder.build());

HttpResponse httpResponse = closeableHttpClient.execute(httppost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
try {
CloseableHttpResponse httpResponse = closeableHttpClient.execute(httppost);
if (httpResponse.getCode() == 200) {
ObjectMapper objectMapper = new ObjectMapper();
InputStreamReader in = new InputStreamReader(httpResponse.getEntity().getContent());
try {
try (InputStreamReader in = new InputStreamReader(httpResponse.getEntity().getContent())) {
ObjectNode result = objectMapper.readValue(in, ObjectNode.class);
if (result.has("exception")) {
ObjectNode exceptionJson = (ObjectNode) result.get("exception");
Expand All @@ -161,30 +160,20 @@ public SLongCheckinActionState checkinSync(String baseAddress, String token, lon
throw new ServerException(e);
}
}
} finally {
in.close();
}
} else {
throw new ServerException("HTTP Status Code " + httpResponse.getStatusLine().getStatusCode() + " " + httpResponse.getStatusLine().getReasonPhrase());
throw new ServerException("HTTP Status Code " + httpResponse.getCode() + " " + httpResponse.getReasonPhrase());
}
} catch (ClientProtocolException e) {
throw new ServerException(e);
} catch (IOException e) {
throw new ServerException(e);
} catch (PublicInterfaceNotFoundException e) {
throw new ServerException(e);
} catch (Exception e) {
throw new ServerException(e);
} finally {
httppost.releaseConnection();
}
throw new ServerException("Null result");
}

private MultipartEntityBuilder createMultiPart() {
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.setCharset(Charsets.UTF_8);
multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntityBuilder.setMode(HttpMultipartMode.LEGACY);
return multipartEntityBuilder;
}

Expand All @@ -199,30 +188,29 @@ protected SServicesMap getSServicesMap() {
public long checkinAsync(String baseAddress, String token, long poid, String comment, long deserializerOid, boolean merge, long fileSize, String filename, InputStream inputStream, long topicId) throws ServerException, UserException {
String address = baseAddress + "/upload";
HttpPost httppost = new HttpPost(address);
try {
// TODO find some GzipInputStream variant that _compresses_ instead
// of _decompresses_ using deflate for now
InputStreamBody data = new InputStreamBody(new DeflaterInputStream(inputStream), filename);

MultipartEntityBuilder multipartEntityBuilder = createMultiPart();
// TODO find some GzipInputStream variant that _compresses_ instead
// of _decompresses_ using deflate for now
InputStreamBody data = new InputStreamBody(new DeflaterInputStream(inputStream), filename);

MultipartEntityBuilder multipartEntityBuilder = createMultiPart();

multipartEntityBuilder.addPart("topicId", new StringBody("" + topicId, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("token", new StringBody(token, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("deserializerOid", new StringBody("" + deserializerOid, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("merge", new StringBody("" + merge, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("poid", new StringBody("" + poid, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("comment", new StringBody("" + comment, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("sync", new StringBody("" + false, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("compression", new StringBody("deflate", ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("data", data);

multipartEntityBuilder.addPart("topicId", new StringBody("" + topicId, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("token", new StringBody(token, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("deserializerOid", new StringBody("" + deserializerOid, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("merge", new StringBody("" + merge, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("poid", new StringBody("" + poid, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("comment", new StringBody("" + comment, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("sync", new StringBody("" + false, ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("compression", new StringBody("deflate", ContentType.DEFAULT_TEXT));
multipartEntityBuilder.addPart("data", data);

httppost.setEntity(multipartEntityBuilder.build());

HttpResponse httpResponse = closeableHttpClient.execute(httppost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
httppost.setEntity(multipartEntityBuilder.build());

try {
CloseableHttpResponse httpResponse = closeableHttpClient.execute(httppost);
if (httpResponse.getCode() == 200) {
ObjectMapper objectMapper = new ObjectMapper();
InputStreamReader in = new InputStreamReader(httpResponse.getEntity().getContent());
try {
try (InputStreamReader in = new InputStreamReader(httpResponse.getEntity().getContent())) {
ObjectNode result = objectMapper.readValue(in, ObjectNode.class);
if (result.has("exception")) {
ObjectNode exceptionJson = (ObjectNode) result.get("exception");
Expand All @@ -240,18 +228,10 @@ public long checkinAsync(String baseAddress, String token, long poid, String com
throw new ServerException("No topicId found in response: " + result.toString());
}
}
} finally {
in.close();
}
}
} catch (ClientProtocolException e) {
throw new ServerException(e);
} catch (IOException e) {
throw new ServerException(e);
} catch (PublicInterfaceNotFoundException e) {
throw new ServerException(e);
} finally {
httppost.releaseConnection();
}
return -1;
}
Expand All @@ -260,15 +240,12 @@ public InputStream getDownloadData(String baseAddress, String token, long topicI
String address = baseAddress + "/download?token=" + token + "&topicId=" + topicId;
HttpPost httppost = new HttpPost(address);
try {
HttpResponse httpResponse = closeableHttpClient.execute(httppost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
CloseableHttpResponse httpResponse = closeableHttpClient.execute(httppost);
if (httpResponse.getCode() == 200) {
return httpResponse.getEntity().getContent();
} else {
LOGGER.error(httpResponse.getStatusLine().getStatusCode() + " - " + httpResponse.getStatusLine().getReasonPhrase());
httppost.releaseConnection();
LOGGER.error(httpResponse.getCode() + " - " + httpResponse.getReasonPhrase());
}
} catch (ClientProtocolException e) {
LOGGER.error("", e);
} catch (IOException e) {
LOGGER.error("", e);
}
Expand All @@ -278,16 +255,13 @@ public InputStream getDownloadData(String baseAddress, String token, long topicI
public InputStream getDownloadExtendedData(String baseAddress, String token, long edid) throws IOException {
String address = baseAddress + "/download?token=" + token + "&action=extendeddata&edid=" + edid;
HttpPost httppost = new HttpPost(address);
try {
HttpResponse httpResponse = closeableHttpClient.execute(httppost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
try {
CloseableHttpResponse httpResponse = closeableHttpClient.execute(httppost);
if (httpResponse.getCode() == 200) {
return httpResponse.getEntity().getContent();
} else {
LOGGER.error(httpResponse.getStatusLine().getStatusCode() + " - " + httpResponse.getStatusLine().getReasonPhrase());
httppost.releaseConnection();
LOGGER.error(httpResponse.getCode() + " - " + httpResponse.getReasonPhrase());
}
} catch (ClientProtocolException e) {
LOGGER.error("", e);
} catch (IOException e) {
LOGGER.error("", e);
}
Expand Down Expand Up @@ -370,11 +344,10 @@ public void bulkCheckin(String baseAddress, String token, long poid, String comm

httppost.setEntity(multipartEntityBuilder.build());

HttpResponse httpResponse = closeableHttpClient.execute(httppost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
CloseableHttpResponse httpResponse = closeableHttpClient.execute(httppost);
if (httpResponse.getCode() == 200) {
ObjectMapper objectMapper = new ObjectMapper();
InputStreamReader in = new InputStreamReader(httpResponse.getEntity().getContent());
try {
try (InputStreamReader in = new InputStreamReader(httpResponse.getEntity().getContent())) {
ObjectNode result = objectMapper.readValue(in, ObjectNode.class);
if (result.has("exception")) {
ObjectNode exceptionJson = (ObjectNode) result.get("exception");
Expand All @@ -386,18 +359,10 @@ public void bulkCheckin(String baseAddress, String token, long poid, String comm
throw new ServerException(message);
}
}
} finally {
in.close();
}
}
} catch (ClientProtocolException e) {
throw new ServerException(e);
} catch (IOException e) {
throw new ServerException(e);
} catch (PublicInterfaceNotFoundException e) {
throw new ServerException(e);
} finally {
httppost.releaseConnection();
}
}
}
20 changes: 10 additions & 10 deletions BimServerClientLib/src/org/bimserver/client/DirectChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import javax.activation.DataHandler;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.bimserver.interfaces.objects.SDownloadResult;
import org.bimserver.interfaces.objects.SLongCheckinActionState;
import org.bimserver.models.log.AccessMethod;
Expand All @@ -39,9 +39,9 @@
import org.bimserver.utils.InputStreamDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class DirectChannel extends Channel implements TokenChangeListener {
private static final Logger LOGGER = LoggerFactory.getLogger(DirectChannel.class);
private static final Logger LOGGER = LoggerFactory.getLogger(DirectChannel.class);
private ServiceFactory serviceFactory;
private SServicesMap sServicesMap;

Expand All @@ -61,12 +61,12 @@ public void connect() throws UserException {
}
}
}
notifyOfConnect();
}

@Override
public void disconnect() {
notifyOfDisconnect();
notifyOfConnect();
}

@Override
public void disconnect() {
notifyOfDisconnect();
}

@Override
Expand Down Expand Up @@ -108,5 +108,5 @@ public void newToken(String token) {
LOGGER.error("", e);
}
}
}
}
}
Loading

0 comments on commit e8266bf

Please sign in to comment.