Skip to content

Commit

Permalink
Updated to Apache Http Client v5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Aug 18, 2022
1 parent 50980d5 commit 03e2e65
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 54 deletions.
4 changes: 2 additions & 2 deletions dcng-api/src/main/java/com/helger/dcng/api/DcngConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ public static boolean isTLSTrustAll ()
return getConfig ().getAsBoolean ("http.tls.trustall", false);
}

public static int getConnectionTimeoutMS ()
public static int getConnectTimeoutMS ()
{
// -1 = system default
return getConfig ().getAsInt ("http.connection-timeout", -1);
}

public static int getReadTimeoutMS ()
public static int getResponseTimeoutMS ()
{
// -1 = system default
return getConfig ().getAsInt ("http.read-timeout", -1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

import java.security.GeneralSecurityException;

import org.apache.http.HttpHost;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.util.Timeout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -64,20 +65,20 @@ public DcngHttpClientSettings ()
}

// Set timeouts
final int nConnectionTimeoutMS = DcngConfig.HTTP.getConnectionTimeoutMS ();
if (nConnectionTimeoutMS >= 0)
final int nConnectTimeoutMS = DcngConfig.HTTP.getConnectTimeoutMS ();
if (nConnectTimeoutMS >= 0)
{
setConnectionTimeoutMS (nConnectionTimeoutMS);
setConnectTimeout (Timeout.ofMilliseconds (nConnectTimeoutMS));
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("Using HTTP connection timeout from configuration for request");
LOGGER.debug ("Using HTTP connection timeout from configuration");
}

final int nReadTimeoutMS = DcngConfig.HTTP.getReadTimeoutMS ();
if (nReadTimeoutMS >= 0)
final int nResponseTimeoutMS = DcngConfig.HTTP.getResponseTimeoutMS ();
if (nResponseTimeoutMS >= 0)
{
setSocketTimeoutMS (nReadTimeoutMS);
setResponseTimeout (Timeout.ofMilliseconds (nResponseTimeoutMS));
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("Using HTTP read timeout from configuration for request");
LOGGER.debug ("Using HTTP response timeout from configuration");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import org.apache.http.client.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;

import com.helger.commons.ValueEnforcer;
import com.helger.commons.annotation.Nonempty;
Expand All @@ -42,6 +43,7 @@
import com.helger.dcng.core.http.DcngHttpClientSettings;
import com.helger.httpclient.HttpClientManager;
import com.helger.httpclient.response.ResponseHandlerByteArray;
import com.helger.xml.serialize.write.XMLWriterSettings;

import eu.de4a.kafkaclient.DE4AKafkaClient;

Expand All @@ -57,7 +59,8 @@ private DcngDPTriggerViaHttp ()
{}

@Nonnull
private static ESuccess _forwardMessage (@Nonnull final DCNGIncomingMessage aMsg, @Nonnull @Nonempty final String sDestURL)
private static ESuccess _forwardMessage (@Nonnull final DCNGIncomingMessage aMsg,
@Nonnull @Nonempty final String sDestURL)
{
ValueEnforcer.notNull (aMsg, "Msg");
ValueEnforcer.notEmpty (sDestURL, "Destination URL");
Expand All @@ -74,17 +77,21 @@ private static ESuccess _forwardMessage (@Nonnull final DCNGIncomingMessage aMsg
if (aPayload == null)
throw new IllegalStateException ();

DE4AKafkaClient.send (EErrorLevel.INFO, () -> "Sending inbound message to '" + sDestURL + "' with " + aPayload.length + " bytes");
DE4AKafkaClient.send (EErrorLevel.INFO,
() -> "Sending inbound message to '" + sDestURL + "' with " + aPayload.length + " bytes");

// Main sending, using DCNG http settings
try (final HttpClientManager aHCM = HttpClientManager.create (new DcngHttpClientSettings ()))
{
final HttpPost aPost = new HttpPost (sDestURL);
aPost.setEntity (new ByteArrayEntity (aPayload));
aPost.setEntity (new ByteArrayEntity (aPayload,
ContentType.APPLICATION_XML.withCharset (XMLWriterSettings.DEFAULT_XML_CHARSET_OBJ)));
final byte [] aResult = aHCM.execute (aPost, new ResponseHandlerByteArray ());

DE4AKafkaClient.send (EErrorLevel.INFO,
() -> "Sending inbound message was successful. Got " + ArrayHelper.getSize (aResult) + " bytes back");
() -> "Sending inbound message was successful. Got " +
ArrayHelper.getSize (aResult) +
" bytes back");
return ESuccess.SUCCESS;
}
catch (final Exception ex)
Expand Down Expand Up @@ -142,7 +149,9 @@ public static ESuccess forwardMessage (@Nonnull final MEMessage aRequest, @Nonnu
final DCNGIncomingMessage aMsg = new DCNGIncomingMessage ();
aMsg.setMetadata (_createMetadata (aRequest));
for (final MEPayload aPayload : aRequest.payloads ())
aMsg.addPayload (_createPayload (aPayload.getData ().bytes (), aPayload.getContentID (), aPayload.getMimeType ()));
aMsg.addPayload (_createPayload (aPayload.getData ().bytes (),
aPayload.getContentID (),
aPayload.getMimeType ()));
return _forwardMessage (aMsg, sDestURL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.client.HttpResponseException;
import org.apache.hc.client5.http.HttpResponseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -101,7 +101,8 @@ public EHandled applyExceptionOnResponse (@Nonnull final InvokableAPIDescriptor
}

if (LOGGER.isDebugEnabled ())
LOGGER.debug ("Received unhandled Exception of type " + (aThrowable != null ? aThrowable.getClass ().getName () : "null"));
LOGGER.debug ("Received unhandled Exception of type " +
(aThrowable != null ? aThrowable.getClass ().getName () : "null"));

// We don't know that exception
return EHandled.UNHANDLED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

import java.io.IOException;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
import org.apache.hc.core5.util.Timeout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.helger.commons.CGlobal;
import com.helger.commons.io.resource.ClassPathResource;
import com.helger.commons.io.stream.StreamHelper;
import com.helger.commons.mime.CMimeType;
Expand All @@ -50,8 +51,10 @@ public static void main (final String [] args) throws IOException
{
final DCNGOutgoingMetadata aMetadata = new DCNGOutgoingMetadata ();
aMetadata.setSenderID (DcngRestJAXB.createDCNGID (DcngIdentifierFactory.PARTICIPANT_SCHEME, "9915:de4atest"));
aMetadata.setReceiverID (DcngRestJAXB.createDCNGID (DcngIdentifierFactory.PARTICIPANT_SCHEME, "9991:nl990000106"));
aMetadata.setDocTypeID (DcngRestJAXB.createDCNGID (DcngIdentifierFactory.DOCTYPE_SCHEME_CANONICAL_EVIDENCE, "CompanyRegistration"));
aMetadata.setReceiverID (DcngRestJAXB.createDCNGID (DcngIdentifierFactory.PARTICIPANT_SCHEME,
"9991:nl990000106"));
aMetadata.setDocTypeID (DcngRestJAXB.createDCNGID (DcngIdentifierFactory.DOCTYPE_SCHEME_CANONICAL_EVIDENCE,
"CompanyRegistration"));
aMetadata.setProcessID (DcngRestJAXB.createDCNGID (DcngIdentifierFactory.PROCESS_SCHEME, "request"));
// aMetadata.setPayloadType (DCNGPayloadType.REQUEST);
aMetadata.setTransportProtocol (EMEProtocol.AS4.getTransportProfileID ());
Expand All @@ -67,11 +70,12 @@ public static void main (final String [] args) throws IOException

LOGGER.info (DcngRestJAXB.outgoingMessage ().getAsString (aOM));

try (final HttpClientManager aHCM = HttpClientManager.create (new HttpClientSettings ().setSocketTimeoutMS ((int) (30 *
CGlobal.MILLISECONDS_PER_SECOND))))
final HttpClientSettings aHCS = new HttpClientSettings ().setResponseTimeout (Timeout.ofSeconds (30));
try (final HttpClientManager aHCM = HttpClientManager.create (aHCS))
{
final HttpPost aPost = new HttpPost ("http://localhost:9092/api/lookup/send");
aPost.setEntity (new ByteArrayEntity (DcngRestJAXB.outgoingMessage ().getAsBytes (aOM)));
aPost.setEntity (new ByteArrayEntity (DcngRestJAXB.outgoingMessage ().getAsBytes (aOM),
ContentType.APPLICATION_XML));
final IJson aJson = aHCM.execute (aPost, new ResponseHandlerJson ());
LOGGER.info (new JsonWriter (new JsonWriterSettings ().setIndentEnabled (true)).writeAsString (aJson));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

import java.io.IOException;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
import org.apache.hc.core5.util.Timeout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.helger.commons.CGlobal;
import com.helger.commons.io.resource.ClassPathResource;
import com.helger.commons.io.stream.StreamHelper;
import com.helger.commons.mime.CMimeType;
Expand All @@ -50,8 +51,10 @@ public static void main (final String [] args) throws IOException
{
final DCNGOutgoingMetadata aMetadata = new DCNGOutgoingMetadata ();
aMetadata.setSenderID (DcngRestJAXB.createDCNGID (DcngIdentifierFactory.PARTICIPANT_SCHEME, "9915:de4atest"));
aMetadata.setReceiverID (DcngRestJAXB.createDCNGID (DcngIdentifierFactory.PARTICIPANT_SCHEME, "9991:ro000000006"));
aMetadata.setDocTypeID (DcngRestJAXB.createDCNGID (DcngIdentifierFactory.DOCTYPE_SCHEME_CANONICAL_EVIDENCE, "CompanyRegistration"));
aMetadata.setReceiverID (DcngRestJAXB.createDCNGID (DcngIdentifierFactory.PARTICIPANT_SCHEME,
"9991:ro000000006"));
aMetadata.setDocTypeID (DcngRestJAXB.createDCNGID (DcngIdentifierFactory.DOCTYPE_SCHEME_CANONICAL_EVIDENCE,
"CompanyRegistration"));
aMetadata.setProcessID (DcngRestJAXB.createDCNGID (DcngIdentifierFactory.PROCESS_SCHEME, "request"));
// aMetadata.setPayloadType (DCNGPayloadType.REQUEST);
aMetadata.setTransportProtocol (EMEProtocol.AS4.getTransportProfileID ());
Expand All @@ -67,11 +70,12 @@ public static void main (final String [] args) throws IOException

LOGGER.info (DcngRestJAXB.outgoingMessage ().getAsString (aOM));

try (final HttpClientManager aHCM = HttpClientManager.create (new HttpClientSettings ().setSocketTimeoutMS ((int) (30 *
CGlobal.MILLISECONDS_PER_SECOND))))
final HttpClientSettings aHCS = new HttpClientSettings ().setResponseTimeout (Timeout.ofSeconds (30));
try (final HttpClientManager aHCM = HttpClientManager.create (aHCS))
{
final HttpPost aPost = new HttpPost ("http://localhost:9092/api/lookup/send");
aPost.setEntity (new ByteArrayEntity (DcngRestJAXB.outgoingMessage ().getAsBytes (aOM)));
aPost.setEntity (new ByteArrayEntity (DcngRestJAXB.outgoingMessage ().getAsBytes (aOM),
ContentType.APPLICATION_XML));
final IJson aJson = aHCM.execute (aPost, new ResponseHandlerJson ());
LOGGER.info (new JsonWriter (new JsonWriterSettings ().setIndentEnabled (true)).writeAsString (aJson));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

import java.io.IOException;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
import org.apache.hc.core5.util.Timeout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.helger.commons.CGlobal;
import com.helger.commons.io.resource.ClassPathResource;
import com.helger.commons.io.stream.StreamHelper;
import com.helger.commons.mime.CMimeType;
Expand All @@ -50,8 +51,10 @@ public static void main (final String [] args) throws IOException
{
final DCNGOutgoingMetadata aMetadata = new DCNGOutgoingMetadata ();
aMetadata.setSenderID (DcngRestJAXB.createDCNGID (DcngIdentifierFactory.PARTICIPANT_SCHEME, "9915:de4atest"));
aMetadata.setReceiverID (DcngRestJAXB.createDCNGID (DcngIdentifierFactory.PARTICIPANT_SCHEME, "9991:se000000013"));
aMetadata.setDocTypeID (DcngRestJAXB.createDCNGID (DcngIdentifierFactory.DOCTYPE_SCHEME_CANONICAL_EVIDENCE, "CompanyRegistration"));
aMetadata.setReceiverID (DcngRestJAXB.createDCNGID (DcngIdentifierFactory.PARTICIPANT_SCHEME,
"9991:se000000013"));
aMetadata.setDocTypeID (DcngRestJAXB.createDCNGID (DcngIdentifierFactory.DOCTYPE_SCHEME_CANONICAL_EVIDENCE,
"CompanyRegistration"));
aMetadata.setProcessID (DcngRestJAXB.createDCNGID (DcngIdentifierFactory.PROCESS_SCHEME, "request"));
// aMetadata.setPayloadType (DCNGPayloadType.REQUEST);
aMetadata.setTransportProtocol (EMEProtocol.AS4.getTransportProfileID ());
Expand All @@ -69,11 +72,12 @@ public static void main (final String [] args) throws IOException

LOGGER.info (DcngRestJAXB.outgoingMessage ().getAsString (aOM));

try (final HttpClientManager aHCM = HttpClientManager.create (new HttpClientSettings ().setSocketTimeoutMS ((int) (30 *
CGlobal.MILLISECONDS_PER_SECOND))))
final HttpClientSettings aHCS = new HttpClientSettings ().setResponseTimeout (Timeout.ofSeconds (30));
try (final HttpClientManager aHCM = HttpClientManager.create (aHCS))
{
final HttpPost aPost = new HttpPost ("http://localhost:9092/api/lookup/send");
aPost.setEntity (new ByteArrayEntity (DcngRestJAXB.outgoingMessage ().getAsBytes (aOM)));
aPost.setEntity (new ByteArrayEntity (DcngRestJAXB.outgoingMessage ().getAsBytes (aOM),
ContentType.APPLICATION_XML));
final IJson aJson = aHCM.execute (aPost, new ResponseHandlerJson ());
LOGGER.info (new JsonWriter (new JsonWriterSettings ().setIndentEnabled (true)).writeAsString (aJson));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import java.io.IOException;

import org.apache.http.client.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import java.io.IOException;

import org.apache.http.client.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
14 changes: 7 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
<properties>
<jetty.version>9.4.48.v20220622</jetty.version>
<log4j.version>2.18.0</log4j.version>
<phase4.version>1.3.9</phase4.version>
<phase4.version>1.4.0</phase4.version>
</properties>

<dependencyManagement>
Expand All @@ -87,7 +87,7 @@
<dependency>
<groupId>com.helger.web</groupId>
<artifactId>ph-web-parent-pom</artifactId>
<version>9.6.4</version>
<version>9.7.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand All @@ -101,28 +101,28 @@
<dependency>
<groupId>com.helger.schematron</groupId>
<artifactId>ph-schematron-parent-pom</artifactId>
<version>6.3.2</version>
<version>6.3.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.helger.peppol</groupId>
<artifactId>peppol-commons-parent-pom</artifactId>
<version>8.7.6</version>
<version>8.8.0</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.helger.photon</groupId>
<artifactId>ph-oton-parent-pom</artifactId>
<version>8.4.0</version>
<version>8.4.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.helger.phive</groupId>
<artifactId>phive-parent-pom</artifactId>
<version>7.2.3</version>
<version>7.2.4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down Expand Up @@ -152,7 +152,7 @@
<dependency>
<groupId>eu.de4a</groupId>
<artifactId>de4a-kafka-client</artifactId>
<version>0.2.12</version>
<version>0.2.13</version>
</dependency>
<dependency>
<groupId>com.helger</groupId>
Expand Down

0 comments on commit 03e2e65

Please sign in to comment.