[
https://issues.apache.org/jira/browse/CAMEL-11257?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16500324#comment-16500324
]
ASF GitHub Bot commented on CAMEL-11257:
----------------------------------------
oscerd closed pull request #2360: [CAMEL-11257] Fixed content type header
construction for disposition …
URL: https://github.com/apache/camel/pull/2360
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/AS2AsynchronousMDNManager.java
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/AS2AsynchronousMDNManager.java
new file mode 100644
index 00000000000..4318af4d127
--- /dev/null
+++
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/AS2AsynchronousMDNManager.java
@@ -0,0 +1,205 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.as2.api;
+
+import java.io.IOException;
+import java.net.Socket;
+import java.security.PrivateKey;
+import java.security.cert.Certificate;
+
+import
org.apache.camel.component.as2.api.entity.DispositionNotificationMultipartReportEntity;
+import org.apache.camel.component.as2.api.entity.EntityParser;
+import org.apache.camel.component.as2.api.io.AS2BHttpClientConnection;
+import org.apache.camel.component.as2.api.protocol.RequestAsynchronousMDN;
+import org.apache.camel.component.as2.api.util.EntityUtils;
+import org.apache.http.HttpException;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.impl.DefaultBHttpClientConnection;
+import org.apache.http.message.BasicHttpEntityEnclosingRequest;
+import org.apache.http.protocol.HttpCoreContext;
+import org.apache.http.protocol.HttpProcessor;
+import org.apache.http.protocol.HttpProcessorBuilder;
+import org.apache.http.protocol.HttpRequestExecutor;
+import org.apache.http.protocol.RequestConnControl;
+import org.apache.http.protocol.RequestContent;
+import org.apache.http.protocol.RequestDate;
+import org.apache.http.protocol.RequestExpectContinue;
+import org.apache.http.protocol.RequestTargetHost;
+import org.apache.http.protocol.RequestUserAgent;
+
+public class AS2AsynchronousMDNManager {
+
+ //
+ // AS2 HTTP Context Attribute Keys
+ //
+
+ /**
+ * Prefix for all AS2 HTTP Context Attributes used by the AS2 Asynchronous
MDN
+ * Manager.
+ */
+ public static final String CAMEL_AS2_ASYNC_MDN_PREFIX =
"camel-as2.async-mdn.";
+
+ /**
+ * The HTTP Context Attribute containing the HTTP request message
+ * transporting the EDI message
+ */
+ public static final String HTTP_REQUEST = HttpCoreContext.HTTP_REQUEST;
+
+ /**
+ * The HTTP Context Attribute containing the HTTP response message
+ * transporting the EDI message
+ */
+ public static final String HTTP_RESPONSE = HttpCoreContext.HTTP_RESPONSE;
+
+ /**
+ * The HTTP Context Attribute containing the AS2 Connection used to send
+ * request message.
+ */
+ public static final String AS2_CONNECTION = CAMEL_AS2_ASYNC_MDN_PREFIX +
"as2-connection";
+
+ /**
+ * The HTTP Context Attribute indicating the target host MDN sent to.
+ */
+ public static final String TARGET_HOST = CAMEL_AS2_ASYNC_MDN_PREFIX +
"target-host";
+
+ /**
+ * The HTTP Context Attribute indicating the target port MDN sent to.
+ */
+ public static final String TARGET_PORT = CAMEL_AS2_ASYNC_MDN_PREFIX +
"target-port";
+
+ /**
+ * The HTTP Context Attribute containing the subject header sent in MDN.
+ */
+ public static final String SUBJECT = CAMEL_AS2_ASYNC_MDN_PREFIX +
"subject";
+
+ /**
+ * The HTTP Context Attribute containing the internet e-mail address of
this
+ * responding system
+ */
+ public static final String FROM = CAMEL_AS2_ASYNC_MDN_PREFIX + "from";
+
+ /**
+ * The HTTP Context Attribute indicating the AS2 name of MDN recipient.
+ */
+ public static final String AS2_TO = CAMEL_AS2_ASYNC_MDN_PREFIX + "as2-to";
+
+ /**
+ * The HTTP Context Attribute indicating the AS2 name of MDN sender.
+ */
+ public static final String AS2_FROM = CAMEL_AS2_ASYNC_MDN_PREFIX +
"as2-from";
+
+ /**
+ * The HTTP Context Attribute indicating the AS2 name of MDN sender.
+ */
+ public static final String RECIPIENT_ADDRESS = CAMEL_AS2_ASYNC_MDN_PREFIX
+ "recipient-address";
+
+ private HttpProcessor httpProcessor;
+ @SuppressWarnings("unused")
+ private Certificate[] signingCertificateChain;
+ @SuppressWarnings("unused")
+ private PrivateKey signingPrivateKey;
+
+ public AS2AsynchronousMDNManager(String as2Version,
+ String userAgent,
+ String senderFQDN,
+ Certificate[] signingCertificateChain,
+ PrivateKey signingPrivateKey) {
+ this.signingCertificateChain = signingCertificateChain;
+ this.signingPrivateKey = signingPrivateKey;
+
+ // Build Processor
+ httpProcessor = HttpProcessorBuilder.create().add(new
RequestAsynchronousMDN(as2Version, senderFQDN))
+ .add(new RequestTargetHost()).add(new
RequestUserAgent(userAgent)).add(new RequestDate())
+ .add(new RequestContent(true)).add(new
RequestConnControl()).add(new RequestExpectContinue(true))
+ .build();
+ }
+
+ public HttpCoreContext send(DispositionNotificationMultipartReportEntity
mdn,
+ String targetHostName,
+ Integer targetPortNumber,
+ String requestUri,
+ String subject,
+ String from,
+ String as2From,
+ String as2To)
+ throws HttpException {
+ if (targetHostName == null || targetHostName.length() == 0) {
+ targetHostName = "localhost";
+ }
+ if (targetPortNumber == null || targetPortNumber < 0) {
+ targetPortNumber = 80;
+ }
+
+ AS2BHttpClientConnection httpConnection = new
AS2BHttpClientConnection(8 * 1024);
+
+ try {
+ HttpHost targetHost = new HttpHost(targetHostName,
targetPortNumber);
+
+ // Create socket and bind to connection;
+ Socket socket = new Socket(targetHost.getHostName(),
targetHost.getPort());
+ httpConnection.bind(socket);
+
+ // Add Context attributes
+ HttpCoreContext httpContext = HttpCoreContext.create();
+ httpContext.setTargetHost(targetHost);
+ httpContext.setAttribute(AS2AsynchronousMDNManager.SUBJECT,
subject);
+ httpContext.setAttribute(AS2AsynchronousMDNManager.FROM, from);
+ httpContext.setAttribute(AS2AsynchronousMDNManager.AS2_FROM,
as2From);
+ httpContext.setAttribute(AS2AsynchronousMDNManager.AS2_TO, as2To);
+
+ BasicHttpEntityEnclosingRequest request = new
BasicHttpEntityEnclosingRequest("POST", requestUri);
+ request.setHeader(AS2Header.CONTENT_TYPE,
mdn.getMainMessageContentType());
+ httpContext.setAttribute(HttpCoreContext.HTTP_REQUEST, request);
+ mdn.setMainBody(true);
+ EntityUtils.setMessageEntity(request, mdn);
+
+ HttpResponse response;
+ try {
+ httpContext.setAttribute(AS2_CONNECTION, httpConnection);
+ response = send(httpConnection, request, httpContext);
+ EntityParser.parseAS2MessageEntity(response);
+ } catch (IOException e) {
+ throw new HttpException("Failed to send http request message",
e);
+ }
+ httpContext.setAttribute(HTTP_RESPONSE, response);
+
+ return httpContext;
+ } catch (Exception e) {
+ throw new HttpException("failed to send MDN", e);
+ } finally {
+ try {
+ httpConnection.flush();
+ httpConnection.close();
+ } catch (IOException e) {
+ throw new HttpException("Failed to flush and close
connection", e);
+ }
+ }
+ }
+
+ private HttpResponse send(DefaultBHttpClientConnection httpConnection,
HttpRequest request, HttpCoreContext httpContext) throws HttpException,
IOException {
+
+ // Execute Request
+ HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
+ httpexecutor.preProcess(request, httpProcessor, httpContext);
+ HttpResponse response = httpexecutor.execute(request, httpConnection,
httpContext);
+ httpexecutor.postProcess(response, httpProcessor, httpContext);
+
+ return response;
+ }
+}
diff --git
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/AS2ClientManager.java
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/AS2ClientManager.java
index b2daab59113..e0812898c2a 100644
---
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/AS2ClientManager.java
+++
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/AS2ClientManager.java
@@ -46,7 +46,7 @@
//
/**
- * Prefix for all AS2 HTTP Context Attributes used by the Http Client
+ * Prefix for all AS2 HTTP Context Attributes used by the AS2 Client
* Manager.
*/
public static final String CAMEL_AS2_CLIENT_PREFIX = "camel-as2.client.";
diff --git
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/AS2Header.java
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/AS2Header.java
index c6cc323f532..3093e9c5c43 100644
---
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/AS2Header.java
+++
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/AS2Header.java
@@ -103,14 +103,10 @@
/**
* Message Header name for Receipt Address
*/
- public static final String RECEIPT_ADDRESS = "Receipt-Address";
+ public static final String RECIPIENT_ADDRESS = "Recipient-Address";
/**
* Message Header name for Disposition Notification Options
*/
public static final String DISPOSITION_NOTIFICATION_OPTIONS =
"Disposition-Notification-Options";
- /**
- * Message Header name for Disposition Notification Options
- */
- public static final String REPORT_TYPE = "Report-Type";
}
diff --git
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/Util.java
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/Util.java
index e7ea0b7f41f..3d8f4835f7c 100644
---
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/Util.java
+++
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/Util.java
@@ -172,11 +172,15 @@ public static void printMessage(PrintStream out,
HttpMessage message) throws IOE
if (message instanceof HttpEntityEnclosingRequest) {
// Write entity
HttpEntity entity =
((HttpEntityEnclosingRequest)message).getEntity();
- entity.writeTo(out);
+ if (entity != null) {
+ entity.writeTo(out);
+ }
} else if (message instanceof HttpResponse) {
// Write entity
HttpEntity entity = ((HttpResponse)message).getEntity();
- entity.writeTo(out);
+ if (entity != null) {
+ entity.writeTo(out);
+ }
}
}
diff --git
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/DispositionNotificationMultipartReportEntity.java
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/DispositionNotificationMultipartReportEntity.java
index 64708d70989..4706ae61242 100644
---
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/DispositionNotificationMultipartReportEntity.java
+++
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/DispositionNotificationMultipartReportEntity.java
@@ -23,15 +23,21 @@
import org.apache.camel.component.as2.api.AS2MimeType;
import org.apache.camel.component.as2.api.AS2TransferEncoding;
import org.apache.camel.component.as2.api.util.HttpMessageUtils;
-import org.apache.http.Header;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
-import org.apache.http.message.BasicHeader;
+import org.apache.http.entity.ContentType;
import org.apache.http.util.CharArrayBuffer;
public class DispositionNotificationMultipartReportEntity extends
MultipartReportEntity {
+ protected DispositionNotificationMultipartReportEntity(String boundary,
boolean isMainBody) {
+ this.boundary = boundary;
+ this.isMainBody = isMainBody;
+ removeHeaders(AS2Header.CONTENT_TYPE);
+ setContentType(getContentTypeValue(boundary));
+ }
+
public
DispositionNotificationMultipartReportEntity(HttpEntityEnclosingRequest request,
HttpResponse response,
DispositionMode
dispositionMode,
@@ -46,19 +52,17 @@ public
DispositionNotificationMultipartReportEntity(HttpEntityEnclosingRequest r
boolean isMainBody)
throws HttpException {
super(charset, isMainBody, boundary);
- this.contentType = new BasicHeader(AS2Header.CONTENT_TYPE,
AS2MimeType.MULTIPART_REPORT);
- Header reportType = new BasicHeader(AS2Header.REPORT_TYPE,
getReportTypeValue(boundary));
- addHeader(reportType);
+ removeHeaders(AS2Header.CONTENT_TYPE);
+ setContentType(getContentTypeValue(boundary));
- addPart(buildPlainTextReport(request, response, dispositionMode,
dispositionType,
- dispositionModifier, failureFields, errorFields,
warningFields, extensionFields));
+ addPart(buildPlainTextReport(request, response, dispositionMode,
dispositionType, dispositionModifier,
+ failureFields, errorFields, warningFields, extensionFields));
addPart(new AS2MessageDispositionNotificationEntity(request, response,
dispositionMode, dispositionType,
dispositionModifier, failureFields, errorFields,
warningFields, extensionFields, charset, false));
}
- protected DispositionNotificationMultipartReportEntity(String boundary,
boolean isMainBody) {
- this.boundary = boundary;
- this.isMainBody = isMainBody;
+ public String getMainMessageContentType() {
+ return AS2MimeType.MULTIPART_REPORT + ";
report-type=disposition-notification; boundary=\"" + boundary + "\"";
}
protected TextPlainEntity buildPlainTextReport(HttpEntityEnclosingRequest
request,
@@ -69,11 +73,12 @@ protected TextPlainEntity
buildPlainTextReport(HttpEntityEnclosingRequest reques
String[] failureFields,
String[] errorFields,
String[] warningFields,
- Map<String, String>
extensionFields) throws HttpException {
+ Map<String, String>
extensionFields)
+ throws HttpException {
CharArrayBuffer charBuffer = new CharArrayBuffer(10);
- String originalMessageId = HttpMessageUtils.getHeaderValue(request,
AS2Header.MESSAGE_ID);
+ String originalMessageId = HttpMessageUtils.getHeaderValue(request,
AS2Header.MESSAGE_ID);
String sentDate = HttpMessageUtils.getHeaderValue(request,
AS2Header.DATE);
String subject = HttpMessageUtils.getHeaderValue(request,
AS2Header.SUBJECT);
@@ -94,8 +99,10 @@ protected TextPlainEntity
buildPlainTextReport(HttpEntityEnclosingRequest reques
return new TextPlainEntity(charBuffer.toString(), AS2Charset.US_ASCII,
AS2TransferEncoding.SEVENBIT, false);
}
- protected String getReportTypeValue(String boundary) {
- return "disposition-notification; boundary=\"" + boundary + "\"";
+ protected String getContentTypeValue(String boundary) {
+ ContentType contentType =
ContentType.parse(AS2MimeType.MULTIPART_REPORT + ";"
+ + "report-type=disposition-notification; boundary=\"" +
boundary + "\"");
+ return contentType.toString();
}
}
diff --git
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/EntityParser.java
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/EntityParser.java
index c13dead8049..e4e74e0306b 100644
---
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/EntityParser.java
+++
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/EntityParser.java
@@ -39,7 +39,6 @@
import org.apache.http.entity.ContentType;
import org.apache.http.impl.io.AbstractMessageParser;
import org.apache.http.impl.io.HttpTransportMetricsImpl;
-import org.apache.http.impl.io.SessionInputBufferImpl;
import org.apache.http.message.BasicLineParser;
import org.apache.http.message.LineParser;
import org.apache.http.message.ParserCursor;
@@ -297,7 +296,7 @@ public static void
parseMessageDispositionNotificationReportEntity(HttpMessage m
inbuffer.bind(entity.getContent());
// Get Boundary Value
- String boundary =
HttpMessageUtils.getBoundaryParameterValue(message, AS2Header.REPORT_TYPE);
+ String boundary =
HttpMessageUtils.getBoundaryParameterValue(message, AS2Header.CONTENT_TYPE);
if (boundary == null) {
throw new HttpException("Failed to retrive boundary value");
}
@@ -557,8 +556,6 @@ public static DispositionNotificationMultipartReportEntity
parseMultipartReportE
//
// End Disposition Notification Body Part
- ContentType contentType =
ContentType.create(AS2MimeType.MULTIPART_REPORT, charset);
-
dispositionNotificationMultipartReportEntity.setContentType(contentType);
dispositionNotificationMultipartReportEntity.setContentTransferEncoding(contentTransferEncoding);
return dispositionNotificationMultipartReportEntity;
} catch (Exception e) {
@@ -672,7 +669,7 @@ public static MimeEntity
parseEntityBody(AS2SessionInputBuffer inbuffer,
break;
case AS2MimeType.MULTIPART_REPORT:
String multipartReportBoundary =
AS2HeaderUtils.getBoundaryParameterValue(headers,
- AS2Header.REPORT_TYPE);
+ AS2Header.CONTENT_TYPE);
entity = parseMultipartReportEntityBody(inbuffer,
multipartReportBoundary, charset.name(),
contentTransferEncoding);
skipToBoundary(inbuffer, boundary);
diff --git
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/protocol/RequestAsynchronousMDN.java
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/protocol/RequestAsynchronousMDN.java
new file mode 100644
index 00000000000..1b599ca6c0e
--- /dev/null
+++
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/protocol/RequestAsynchronousMDN.java
@@ -0,0 +1,88 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.as2.api.protocol;
+
+import java.io.IOException;
+
+import org.apache.camel.component.as2.api.AS2AsynchronousMDNManager;
+import org.apache.camel.component.as2.api.AS2Constants;
+import org.apache.camel.component.as2.api.AS2Header;
+import org.apache.camel.component.as2.api.InvalidAS2NameException;
+import org.apache.camel.component.as2.api.Util;
+import org.apache.http.HttpException;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpRequestInterceptor;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpCoreContext;
+
+public class RequestAsynchronousMDN implements HttpRequestInterceptor {
+
+ private final String as2Version;
+ private final String senderFQDN;
+
+ public RequestAsynchronousMDN(String as2Version, String senderFQDN) {
+ this.as2Version = as2Version;
+ this.senderFQDN = senderFQDN;
+ }
+
+ @Override
+ public void process(HttpRequest request, HttpContext context) throws
HttpException, IOException {
+
+ HttpCoreContext coreContext = HttpCoreContext.adapt(context);
+
+ /* MIME header */
+ request.addHeader(AS2Header.MIME_VERSION, AS2Constants.MIME_VERSION);
+
+ /* Subject header */
+ String subject =
coreContext.getAttribute(AS2AsynchronousMDNManager.SUBJECT, String.class);
+ request.addHeader(AS2Header.SUBJECT, subject);
+
+ /* From header */
+ String from = coreContext.getAttribute(AS2AsynchronousMDNManager.FROM,
String.class);
+ request.addHeader(AS2Header.FROM, from);
+
+ /* AS2-Version header */
+ request.addHeader(AS2Header.AS2_VERSION, as2Version);
+
+ /* AS2-From header */
+ String as2From =
coreContext.getAttribute(AS2AsynchronousMDNManager.AS2_FROM, String.class);
+ try {
+ Util.validateAS2Name(as2From);
+ } catch (InvalidAS2NameException e) {
+ throw new HttpException("Invalid AS-From name", e);
+ }
+ request.addHeader(AS2Header.AS2_FROM, as2From);
+
+ /* AS2-To header */
+ String as2To =
coreContext.getAttribute(AS2AsynchronousMDNManager.AS2_TO, String.class);
+ try {
+ Util.validateAS2Name(as2To);
+ } catch (InvalidAS2NameException e) {
+ throw new HttpException("Invalid AS-To name", e);
+ }
+ request.addHeader(AS2Header.AS2_TO, as2To);
+
+ /* Message-Id header */
+ // SHOULD be set to aid in message reconciliation
+ request.addHeader(AS2Header.MESSAGE_ID,
Util.createMessageId(senderFQDN));
+
+ /* Recipient-Address header */
+ String recipientAddress =
coreContext.getAttribute(AS2AsynchronousMDNManager.RECIPIENT_ADDRESS,
String.class);
+ request.addHeader(AS2Header.RECIPIENT_ADDRESS, recipientAddress);
+ }
+
+}
diff --git
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/protocol/ResponseMDN.java
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/protocol/ResponseMDN.java
index 581f76c9848..3beec3b76f7 100644
---
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/protocol/ResponseMDN.java
+++
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/protocol/ResponseMDN.java
@@ -23,8 +23,6 @@
import org.apache.camel.component.as2.api.AS2Charset;
import org.apache.camel.component.as2.api.AS2Constants;
import org.apache.camel.component.as2.api.AS2Header;
-import org.apache.camel.component.as2.api.AS2MimeType;
-import org.apache.camel.component.as2.api.AS2ReportType;
import org.apache.camel.component.as2.api.AS2ServerManager;
import org.apache.camel.component.as2.api.AS2SignedDataGenerator;
import org.apache.camel.component.as2.api.AS2TransferEncoding;
@@ -36,11 +34,9 @@
import
org.apache.camel.component.as2.api.entity.DispositionNotificationOptions;
import
org.apache.camel.component.as2.api.entity.DispositionNotificationOptionsParser;
import org.apache.camel.component.as2.api.entity.MultipartSignedEntity;
-import org.apache.camel.component.as2.api.util.AS2HeaderUtils;
import org.apache.camel.component.as2.api.util.EntityUtils;
import org.apache.camel.component.as2.api.util.HttpMessageUtils;
import org.apache.camel.component.as2.api.util.SigningUtils;
-import org.apache.http.Header;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
@@ -70,111 +66,118 @@ public ResponseMDN(String as2Version, String serverFQDN,
Certificate[] signingCe
@Override
public void process(HttpResponse response, HttpContext context) throws
HttpException, IOException {
-
+
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode < 200 || statusCode >= 300) {
- LOG.debug("MDN not added due to response status code: " +
statusCode);
+ // RFC4130 - 7.6 - Status codes in the 200 range SHOULD also be
used when an entity is returned
+ // (a signed receipt in a multipart/signed content type or an
unsigned
+ // receipt in a multipart/report)
+ LOG.debug("MDN not return due to response status code: " +
statusCode);
return;
}
- LOG.debug("Adding MDN to response: " + response);
HttpCoreContext coreContext = HttpCoreContext.adapt(context);
HttpEntityEnclosingRequest request =
coreContext.getAttribute(HttpCoreContext.HTTP_REQUEST,
HttpEntityEnclosingRequest.class);
if (request == null) {
- LOG.debug("MDN not added due to null request");
- return;
+ // Should never happen; but you never know
+ LOG.debug("MDN not returned due to null request");
+ throw new HttpException("request missing from HTTP context");
}
LOG.debug("Processing MDN for request: " + request);
- /* MIME header */
- response.addHeader(AS2Header.MIME_VERSION, AS2Constants.MIME_VERSION);
+ if (HttpMessageUtils.getHeaderValue(request,
AS2Header.DISPOSITION_NOTIFICATION_TO) == null) {
+ // no receipt requested by sender
+ LOG.debug("MDN not returned: no receipt requested");
+ return;
+ }
+
+ // Return a Message Disposition Notification Receipt in response body
+ String boundary = EntityUtils.createBoundaryValue();
+ DispositionNotificationMultipartReportEntity multipartReportEntity =
new DispositionNotificationMultipartReportEntity(
+ request, response,
DispositionMode.AUTOMATIC_ACTION_MDN_SENT_AUTOMATICALLY,
+ AS2DispositionType.PROCESSED, null, null, null, null, null,
AS2Charset.US_ASCII, boundary, true);
- /* AS2-Version header */
- response.addHeader(AS2Header.AS2_VERSION, as2Version);
+ DispositionNotificationOptions dispositionNotificationOptions =
DispositionNotificationOptionsParser
+ .parseDispositionNotificationOptions(
+ HttpMessageUtils.getHeaderValue(request,
AS2Header.DISPOSITION_NOTIFICATION_OPTIONS), null);
- /* Subject header */
- String subjectPrefix =
coreContext.getAttribute(AS2ServerManager.SUBJECT, String.class);
- String subject = HttpMessageUtils.getHeaderValue(request,
AS2Header.SUBJECT);
- if (subjectPrefix != null && subject != null) {
- subject = subjectPrefix + subject;
- } else if (subject != null) {
- subject = "MDN Response To:" + subject;
+ String receiptAddress = HttpMessageUtils.getHeaderValue(request,
AS2Header.RECEIPT_DELIVERY_OPTION);
+ if (receiptAddress != null) {
+ // Asynchronous Delivery
+ // TODO Implement
} else {
- subject = "Your Requested MDN Response";
- }
- response.addHeader(AS2Header.SUBJECT, subject);
-
- /* From header */
- String from = coreContext.getAttribute(AS2ServerManager.FROM,
String.class);
- response.addHeader(AS2Header.FROM, from);
-
- /* AS2-From header */
- String as2From = HttpMessageUtils.getHeaderValue(request,
AS2Header.AS2_TO);
- try {
- Util.validateAS2Name(as2From);
- } catch (InvalidAS2NameException e) {
- throw new HttpException("Invalid AS-From name", e);
- }
- response.addHeader(AS2Header.AS2_FROM, as2From);
-
- /* AS2-To header */
- String as2To = HttpMessageUtils.getHeaderValue(request,
AS2Header.AS2_FROM);
- try {
- Util.validateAS2Name(as2To);
- } catch (InvalidAS2NameException e) {
- throw new HttpException("Invalid AS-To name", e);
- }
- response.addHeader(AS2Header.AS2_TO, as2To);
-
- /* Message-Id header*/
- // SHOULD be set to aid in message reconciliation
- response.addHeader(AS2Header.MESSAGE_ID,
Util.createMessageId(serverFQDN));
-
- if (HttpMessageUtils.getHeaderValue(request,
AS2Header.DISPOSITION_NOTIFICATION_TO) != null) {
- // Return a Message Disposition Notification Receipt in response
body
- String boundary = EntityUtils.createBoundaryValue();
- DispositionNotificationMultipartReportEntity multipartReportEntity
= new DispositionNotificationMultipartReportEntity(
- request, response,
DispositionMode.AUTOMATIC_ACTION_MDN_SENT_AUTOMATICALLY,
- AS2DispositionType.PROCESSED, null, null, null, null,
null, AS2Charset.US_ASCII, boundary, true);
-
- DispositionNotificationOptions dispositionNotificationOptions =
DispositionNotificationOptionsParser
- .parseDispositionNotificationOptions(
- HttpMessageUtils.getHeaderValue(request,
AS2Header.DISPOSITION_NOTIFICATION_OPTIONS), null);
-
- String receiptAddress = HttpMessageUtils.getHeaderValue(request,
AS2Header.RECEIPT_DELIVERY_OPTION);
- if (receiptAddress != null) {
- // Asynchronous Delivery
- // TODO Implement
+ // Synchronous Delivery
+
+ /* MIME header */
+ response.addHeader(AS2Header.MIME_VERSION,
AS2Constants.MIME_VERSION);
+
+ /* AS2-Version header */
+ response.addHeader(AS2Header.AS2_VERSION, as2Version);
+
+ /* Subject header */
+ // RFC4130 - 7.3 - Subject header SHOULD be supplied
+ String subjectPrefix =
coreContext.getAttribute(AS2ServerManager.SUBJECT, String.class);
+ String subject = HttpMessageUtils.getHeaderValue(request,
AS2Header.SUBJECT);
+ if (subjectPrefix != null && subject != null) {
+ subject = subjectPrefix + subject;
+ } else if (subject != null) {
+ subject = "MDN Response To:" + subject;
} else {
- // Synchronous Delivery
+ subject = "Your Requested MDN Response";
+ }
+ response.addHeader(AS2Header.SUBJECT, subject);
+
+ /* From header */
+ String from = coreContext.getAttribute(AS2ServerManager.FROM,
String.class);
+ response.addHeader(AS2Header.FROM, from);
+
+ /* AS2-From header */
+ String as2From = HttpMessageUtils.getHeaderValue(request,
AS2Header.AS2_TO);
+ try {
+ Util.validateAS2Name(as2From);
+ } catch (InvalidAS2NameException e) {
+ throw new HttpException("Invalid AS-From name", e);
+ }
+ response.addHeader(AS2Header.AS2_FROM, as2From);
+
+ /* AS2-To header */
+ String as2To = HttpMessageUtils.getHeaderValue(request,
AS2Header.AS2_FROM);
+ try {
+ Util.validateAS2Name(as2To);
+ } catch (InvalidAS2NameException e) {
+ throw new HttpException("Invalid AS-To name", e);
+ }
+ response.addHeader(AS2Header.AS2_TO, as2To);
- AS2SignedDataGenerator gen = null;
- if (dispositionNotificationOptions.getSignedReceiptProtocol()
!= null && signingCertificateChain != null && signingPrivateKey != null) {
- gen =
SigningUtils.createSigningGenerator(signingCertificateChain, signingPrivateKey);
- }
+ /* Message-Id header*/
+ // RFC4130 - 7.3 - A Message-ID header is added to support
message reconciliation
+ response.addHeader(AS2Header.MESSAGE_ID,
Util.createMessageId(serverFQDN));
- if (gen != null) {
- // Create signed receipt
- try {
- multipartReportEntity.setMainBody(false);
- MultipartSignedEntity multipartSignedEntity = new
MultipartSignedEntity(multipartReportEntity, gen,
- AS2Charset.US_ASCII,
AS2TransferEncoding.BASE64, false, null);
-
response.setHeader(multipartSignedEntity.getContentType());
- EntityUtils.setMessageEntity(response,
multipartSignedEntity);
- } catch (Exception e) {
- LOG.warn("failed to sign receipt");
- }
- } else {
- // Create unsigned receipt
- Header reportTypeHeader =
AS2HeaderUtils.createHeader(AS2Header.REPORT_TYPE, new String[][]
{{AS2ReportType.DISPOSITION_NOTIFICATION}, {BOUNDARY_PARAM_NAME, boundary}});
- response.addHeader(reportTypeHeader);
- response.setHeader(AS2Header.CONTENT_TYPE,
AS2MimeType.MULTIPART_REPORT);
- EntityUtils.setMessageEntity(response,
multipartReportEntity);
- }
+ AS2SignedDataGenerator gen = null;
+ if (dispositionNotificationOptions.getSignedReceiptProtocol() !=
null && signingCertificateChain != null
+ && signingPrivateKey != null) {
+ gen =
SigningUtils.createSigningGenerator(signingCertificateChain, signingPrivateKey);
}
+ if (gen != null) {
+ // Create signed receipt
+ try {
+ multipartReportEntity.setMainBody(false);
+ MultipartSignedEntity multipartSignedEntity = new
MultipartSignedEntity(multipartReportEntity, gen,
+ AS2Charset.US_ASCII, AS2TransferEncoding.BASE64,
false, null);
+ response.setHeader(multipartSignedEntity.getContentType());
+ EntityUtils.setMessageEntity(response,
multipartSignedEntity);
+ } catch (Exception e) {
+ LOG.warn("failed to sign receipt");
+ }
+ } else {
+ // Create unsigned receipt
+ response.setHeader(multipartReportEntity.getContentType());
+ EntityUtils.setMessageEntity(response, multipartReportEntity);
+ }
}
+
LOG.debug(Util.printMessage(response));
}
diff --git
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/util/EntityUtils.java
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/util/EntityUtils.java
index 17b81a915af..926d345d190 100644
---
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/util/EntityUtils.java
+++
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/util/EntityUtils.java
@@ -35,6 +35,7 @@
import org.apache.commons.codec.binary.Base64InputStream;
import org.apache.commons.codec.binary.Base64OutputStream;
import org.apache.commons.codec.net.QuotedPrintableCodec;
+import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpMessage;
@@ -237,6 +238,10 @@ public static void setMessageEntity(HttpMessage message,
HttpEntity entity) {
} else if (message instanceof HttpResponse) {
((HttpResponse) message).setEntity(entity);
}
+ Header contentTypeHeader = entity.getContentType();
+ if (contentTypeHeader != null) {
+ message.setHeader(contentTypeHeader);
+ }
long contentLength = entity.getContentLength();
message.setHeader(AS2Header.CONTENT_LENGTH,
Long.toString(contentLength));
}
diff --git
a/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/AS2MessageTest.java
b/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/AS2MessageTest.java
index e155028eed2..78ec28a0d73 100644
---
a/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/AS2MessageTest.java
+++
b/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/AS2MessageTest.java
@@ -26,13 +26,18 @@
import java.util.ArrayList;
import java.util.List;
+import org.apache.camel.component.as2.api.entity.AS2DispositionType;
import org.apache.camel.component.as2.api.entity.ApplicationEDIEntity;
import org.apache.camel.component.as2.api.entity.ApplicationEDIFACTEntity;
import
org.apache.camel.component.as2.api.entity.ApplicationPkcs7SignatureEntity;
+import org.apache.camel.component.as2.api.entity.DispositionMode;
import
org.apache.camel.component.as2.api.entity.DispositionNotificationMultipartReportEntity;
import org.apache.camel.component.as2.api.entity.MimeEntity;
import org.apache.camel.component.as2.api.entity.MultipartSignedEntity;
+import org.apache.camel.component.as2.api.util.EntityUtils;
+import org.apache.camel.component.as2.api.util.HttpMessageUtils;
import org.apache.http.HttpEntity;
+import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
@@ -41,8 +46,11 @@
import org.apache.http.entity.ContentType;
import org.apache.http.impl.EnglishReasonPhraseCatalog;
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
+import org.apache.http.message.BasicHttpResponse;
+import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpCoreContext;
+import org.apache.http.protocol.HttpDateGenerator;
import org.apache.http.protocol.HttpRequestHandler;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.cms.AttributeTable;
@@ -96,7 +104,6 @@
+ "UNT+23+00000000000117'\n"
+ "UNZ+1+00000000000778'";
- @SuppressWarnings("unused")
private static final Logger LOG =
LoggerFactory.getLogger(AS2MessageTest.class);
private static final String METHOD = "POST";
@@ -113,7 +120,8 @@
private static final String DISPOSITION_NOTIFICATION_TO =
"[email protected]";
private static final String[] SIGNED_RECEIPT_MIC_ALGORITHMS = new String[]
{"sha1", "md5"};
-
+ private static final HttpDateGenerator DATE_GENERATOR = new
HttpDateGenerator();
+
private static AS2ServerConnection testServer;
private AS2SignedDataGenerator gen;
@@ -190,8 +198,9 @@ public void handle(HttpRequest request, HttpResponse
response, HttpContext conte
throws HttpException, IOException {
try {
org.apache.camel.component.as2.api.entity.EntityParser.parseAS2MessageEntity(request);
- context.setAttribute(SUBJECT, SUBJECT);
- context.setAttribute(FROM, AS2_NAME);
+ context.setAttribute(AS2ServerManager.SUBJECT, SUBJECT);
+ context.setAttribute(AS2ServerManager.FROM, AS2_NAME);
+ LOG.debug(Util.printMessage(request));
} catch (Exception e) {
throw new HttpException("Failed to parse AS2 Message
Entity", e);
}
@@ -392,4 +401,31 @@ public void mdnMessageTest() throws Exception {
assertTrue("Signature is invalid", responseSignedEntity.isValid());
}
+ @Test
+ public void synchronousMdnMessageTest() throws Exception {
+
+ AS2AsynchronousMDNManager mdnManager = new
AS2AsynchronousMDNManager(AS2_VERSION, USER_AGENT, CLIENT_FQDN,
+ certList.toArray(new X509Certificate[0]),
signingKP.getPrivate());
+
+ // Create plain edi request message
+ ApplicationEDIEntity ediEntity =
EntityUtils.createEDIEntity(EDI_MESSAGE,
+ ContentType.create(AS2MediaType.APPLICATION_EDIFACT,
AS2Charset.US_ASCII), null, false);
+ HttpEntityEnclosingRequest request = new
BasicHttpEntityEnclosingRequest("POST", REQUEST_URI);
+ HttpMessageUtils.setHeaderValue(request, AS2Header.AS2_TO, AS2_NAME);
+ EntityUtils.setMessageEntity(request, ediEntity);
+
+ HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
200, "OK");
+ String httpdate = DATE_GENERATOR.getCurrentDate();
+ response.setHeader(HTTP.DATE_HEADER, httpdate);
+
+ // Create a receipt for edi message
+ DispositionNotificationMultipartReportEntity mdn = new
DispositionNotificationMultipartReportEntity(request,
+ response,
DispositionMode.AUTOMATIC_ACTION_MDN_SENT_AUTOMATICALLY,
AS2DispositionType.PROCESSED, null,
+ null, null, null, null, null, "boundary", true);
+
+ // Send MDN
+ mdnManager.send(mdn, TARGET_HOST, TARGET_PORT, REQUEST_URI, SUBJECT,
FROM, AS2_NAME, AS2_NAME);
+
+ }
+
}
diff --git
a/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/entity/EntityParserTest.java
b/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/entity/EntityParserTest.java
index eae510d38aa..306d9cdcfc4 100644
---
a/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/entity/EntityParserTest.java
+++
b/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/entity/EntityParserTest.java
@@ -20,7 +20,6 @@
import java.io.InputStream;
import org.apache.camel.component.as2.api.AS2Header;
-import org.apache.camel.component.as2.api.AS2MimeType;
import org.apache.camel.component.as2.api.io.AS2SessionInputBuffer;
import org.apache.camel.component.as2.api.util.EntityUtils;
import org.apache.camel.component.as2.api.util.HttpMessageUtils;
@@ -42,6 +41,9 @@
import static org.junit.Assert.assertTrue;
public class EntityParserTest {
+
+ public static final String REPORT_CONTENT_TYPE_VALUE =
+ "multipart/report; report-type=disposition-notification;
boundary=\"----=_Part_56_1672293592.1028122454656\"";
public static final String REPORT_TYPE_HEADER_VALUE =
"disposition-notification;
boundary=\"----=_Part_56_1672293592.1028122454656\"\r\n";
@@ -160,10 +162,9 @@ public void tearDown() throws Exception {
public void parseMessageDispositionNotificationReportMessageTest() throws
Exception {
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_OK,
EnglishReasonPhraseCatalog.INSTANCE.getReason(HttpStatus.SC_OK, null));
HttpMessageUtils.setHeaderValue(response,
AS2Header.CONTENT_TRANSFER_ENCODING,
DISPOSITION_NOTIFICATION_CONTENT_TRANSFER_ENCODING);
- HttpMessageUtils.setHeaderValue(response, AS2Header.REPORT_TYPE,
REPORT_TYPE_HEADER_VALUE);
BasicHttpEntity entity = new BasicHttpEntity();
- entity.setContentType(AS2MimeType.MULTIPART_REPORT);
+ entity.setContentType(REPORT_CONTENT_TYPE_VALUE);
InputStream is = new
ByteArrayInputStream(DISPOSITION_NOTIFICATION_REPORT_CONTENT.getBytes(DISPOSITION_NOTIFICATION_REPORT_CONTENT_CHARSET_NAME));
entity.setContent(is);
EntityUtils.setMessageEntity(response, entity);
diff --git
a/components/camel-as2/camel-as2-component/src/main/java/org/apache/camel/component/as2/AS2Configuration.java
b/components/camel-as2/camel-as2-component/src/main/java/org/apache/camel/component/as2/AS2Configuration.java
index 6fb4dd32101..93d756ae43e 100644
---
a/components/camel-as2/camel-as2-component/src/main/java/org/apache/camel/component/as2/AS2Configuration.java
+++
b/components/camel-as2/camel-as2-component/src/main/java/org/apache/camel/component/as2/AS2Configuration.java
@@ -220,7 +220,7 @@ public String getServerFqdn() {
*
* <p> Used in message ids sent by endpoint.
*
- * @param clientFqdn - the FQDN of client.
+ * @param clientFqdn - the FQDN of server.
*/
public void setServerFqdn(String serverFqdn) {
if (clientFqdn == null) {
diff --git
a/platforms/spring-boot/components-starter/camel-as2-starter/src/main/java/org/apache/camel/component/as2/springboot/AS2ComponentConfiguration.java
b/platforms/spring-boot/components-starter/camel-as2-starter/src/main/java/org/apache/camel/component/as2/springboot/AS2ComponentConfiguration.java
index 08f0028d0eb..8c464c6397f 100644
---
a/platforms/spring-boot/components-starter/camel-as2-starter/src/main/java/org/apache/camel/component/as2/springboot/AS2ComponentConfiguration.java
+++
b/platforms/spring-boot/components-starter/camel-as2-starter/src/main/java/org/apache/camel/component/as2/springboot/AS2ComponentConfiguration.java
@@ -111,7 +111,7 @@ public void setResolvePropertyPlaceholders(
* Used in message ids sent by endpoint.
*
* @param clientFqdn
- * - the FQDN of client.
+ * - the FQDN of server.
*/
private String serverFqdn;
/**
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> Provide AS2 component to support Business Data Interchange Using HTTP
> ---------------------------------------------------------------------
>
> Key: CAMEL-11257
> URL: https://issues.apache.org/jira/browse/CAMEL-11257
> Project: Camel
> Issue Type: New Feature
> Affects Versions: 2.19.1
> Reporter: William Collins
> Assignee: William Collins
> Priority: Major
> Fix For: 2.22.0
>
>
> AS2 Camel component should provide MIME-Based Secure Peer-to-Peer Business
> Data Interchange Using HTTP as per RFC4120
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)