use of com.helger.mail.datasource.ByteArrayDataSource in project as2-lib by phax.
the class AS2MDNReceiverHandler method handleIncomingMessage.
public void handleIncomingMessage(@Nonnull @Nonempty final String sClientInfo, @Nonnull final DataSource aMsgData, @Nonnull final AS2Message aMsg, @Nonnull final IAS2HttpResponseHandler aResponseHandler) {
// Read in the message request, headers, and data
try (final AS2ResourceHelper aResHelper = new AS2ResourceHelper()) {
final byte[] aData = StreamHelper.getAllBytes(aMsgData.getInputStream());
// Asynch MDN 2007-03-12
// check if the requested URL is defined in attribute "as2_receipt_option"
// in one of partnerships, if yes, then process incoming AsyncMDN
final String sReceivedContentType = AS2HttpHelper.getCleanContentType(aMsg.getHeader(CHttpHeader.CONTENT_TYPE));
final MimeBodyPart aReceivedPart = new MimeBodyPart(AS2HttpHelper.getAsInternetHeaders(aMsg.headers()), aData);
aMsg.setData(aReceivedPart);
// MimeBodyPart receivedPart = new MimeBodyPart();
aReceivedPart.setDataHandler(new ByteArrayDataSource(aData, sReceivedContentType, null).getAsDataHandler());
// Must be set AFTER the DataHandler!
aReceivedPart.setHeader(CHttpHeader.CONTENT_TYPE, sReceivedContentType);
aMsg.setData(aReceivedPart);
receiveMDN(aMsg, aData, aResponseHandler, aResHelper);
if (LOGGER.isInfoEnabled())
LOGGER.info("Received async MDN " + sClientInfo + aMsg.getLoggingText());
} catch (final Exception ex) {
m_aReceiverModule.handleError(aMsg, WrappedAS2Exception.wrap(ex));
}
}
use of com.helger.mail.datasource.ByteArrayDataSource in project as2-lib by phax.
the class AS2MDNReceiverHandler method handle.
public void handle(@Nonnull final AbstractActiveNetModule aOwner, @Nonnull final Socket aSocket) {
final String sClientInfo = getClientInfo(aSocket);
if (LOGGER.isInfoEnabled())
LOGGER.info("incoming connection for receiving AsyncMDN [" + sClientInfo + "]");
final AS2Message aMsg = new AS2Message();
final boolean bQuoteHeaderValues = m_aReceiverModule.isQuoteHeaderValues();
final IAS2HttpResponseHandler aResponseHandler = new AS2HttpResponseHandlerSocket(aSocket, bQuoteHeaderValues);
// Time the transmission
final StopWatch aSW = StopWatch.createdStarted();
DataSource aMdnDataSource = null;
try {
// Read in the message request, headers, and data
final IHTTPIncomingDumper aIncomingDumper = getEffectiveHttpIncomingDumper();
aMdnDataSource = HTTPHelper.readAndDecodeHttpRequest(new AS2HttpRequestDataProviderInputStream(aSocket.getInputStream()), aResponseHandler, aMsg, aIncomingDumper);
} catch (final Exception ex) {
new AS2NetException(aSocket.getInetAddress(), aSocket.getPort(), ex).terminate();
}
aSW.stop();
if (aMdnDataSource == null) {
LOGGER.error("Not having a data source to operate on");
} else {
if (aMdnDataSource instanceof ByteArrayDataSource) {
if (LOGGER.isInfoEnabled())
LOGGER.info("received " + AS2IOHelper.getTransferRate(((ByteArrayDataSource) aMdnDataSource).directGetBytes().length, aSW) + " from " + sClientInfo + aMsg.getLoggingText());
} else {
LOGGER.info("received message from " + sClientInfo + aMsg.getLoggingText() + " in " + aSW.getMillis() + " ms");
}
handleIncomingMessage(sClientInfo, aMdnDataSource, aMsg, aResponseHandler);
}
}
use of com.helger.mail.datasource.ByteArrayDataSource in project as2-lib by phax.
the class AbstractAS2ReceiveBaseXServletHandler method onRequest.
public final void onRequest(@Nonnull final HttpServletRequest aHttpRequest, @Nonnull final HttpServletResponse aHttpResponse, @Nonnull final EHttpVersion eHttpVersion, @Nonnull final EHttpMethod eHttpMethod, @Nonnull final IRequestWebScope aRequestScope) throws ServletException, IOException {
// Handle the incoming message, and return the MDN if necessary
final String sClientInfo = aHttpRequest.getRemoteAddr() + ":" + aHttpRequest.getRemotePort();
LOGGER.info("Starting to handle incoming AS2 request - " + sClientInfo);
// Create empty message
final AS2Message aMsg = new AS2Message();
aMsg.attrs().putIn(CNetAttribute.MA_SOURCE_IP, aHttpRequest.getRemoteAddr());
aMsg.attrs().putIn(CNetAttribute.MA_SOURCE_PORT, aHttpRequest.getRemotePort());
aMsg.attrs().putIn(CNetAttribute.MA_DESTINATION_IP, aHttpRequest.getLocalAddr());
aMsg.attrs().putIn(CNetAttribute.MA_DESTINATION_PORT, aHttpRequest.getLocalPort());
// Request type (e.g. "POST")
aMsg.attrs().putIn(HTTPHelper.MA_HTTP_REQ_TYPE, aHttpRequest.getMethod());
// Request URL (e.g. "/as2")
aMsg.attrs().putIn(HTTPHelper.MA_HTTP_REQ_URL, ServletHelper.getRequestRequestURI(aHttpRequest));
// Add all request headers to the AS2 message
aMsg.headers().setAllHeaders(aRequestScope.headers());
// Build the handler that performs the response handling
final boolean bQuoteHeaderValues = isQuoteHeaderValues();
final AS2OutputStreamCreatorHttpServletResponse aResponseHandler = new AS2OutputStreamCreatorHttpServletResponse(aHttpResponse, bQuoteHeaderValues);
// Read the S/MIME content in a byte array - memory!
// Chunked encoding was already handled, so read "as-is"
final long nContentLength = aHttpRequest.getContentLengthLong();
if (nContentLength > Integer.MAX_VALUE)
throw new IllegalStateException("Currently only payload with up to 2GB can be handled! This request has " + nContentLength + " bytes.");
// Open it once, and close it at the end
try (final ServletInputStream aRequestIS = aHttpRequest.getInputStream()) {
// Time the transmission
final StopWatch aSW = StopWatch.createdStarted();
DataSource aMsgDataSource = null;
try {
// Read in the message request, headers, and data
final IHTTPIncomingDumper aIncomingDumper = getEffectiveHttpIncomingDumper();
aMsgDataSource = HTTPHelper.readAndDecodeHttpRequest(new AS2HttpRequestDataProviderServletRequest(aRequestScope, aRequestIS), aResponseHandler, aMsg, aIncomingDumper);
} catch (final Exception ex) {
AS2Exception.log(ex.getClass(), true, "Failed to read Servlet Request: " + ex.getMessage(), null, null, ex.getCause());
}
aSW.stop();
if (aMsgDataSource == null) {
LOGGER.error("Not having a data source to operate on");
} else {
if (aMsgDataSource instanceof ByteArrayDataSource) {
if (LOGGER.isInfoEnabled())
LOGGER.info("received " + AS2IOHelper.getTransferRate(((ByteArrayDataSource) aMsgDataSource).directGetBytes().length, aSW) + " from " + sClientInfo + aMsg.getLoggingText());
} else {
LOGGER.info("received message from " + sClientInfo + aMsg.getLoggingText() + " in " + aSW.getMillis() + " ms");
}
handleIncomingMessage(sClientInfo, aMsgDataSource, aMsg, aResponseHandler);
}
}
}
use of com.helger.mail.datasource.ByteArrayDataSource in project as2-lib by phax.
the class AS2ReceiverHandler method handle.
public void handle(@Nonnull final AbstractActiveNetModule aOwner, @Nonnull final Socket aSocket) {
final String sClientInfo = getClientInfo(aSocket);
if (LOGGER.isInfoEnabled())
LOGGER.info("Incoming connection " + sClientInfo);
final AS2Message aMsg = createMessage(aSocket);
final boolean bQuoteHeaderValues = m_aReceiverModule.isQuoteHeaderValues();
final IAS2HttpResponseHandler aResponseHandler = new AS2HttpResponseHandlerSocket(aSocket, bQuoteHeaderValues);
// Time the transmission
final StopWatch aSW = StopWatch.createdStarted();
DataSource aMsgDataSource = null;
try {
// Read in the message request, headers, and data
final IHTTPIncomingDumper aIncomingDumper = getEffectiveHttpIncomingDumper();
aMsgDataSource = HTTPHelper.readAndDecodeHttpRequest(new AS2HttpRequestDataProviderInputStream(aSocket.getInputStream()), aResponseHandler, aMsg, aIncomingDumper);
} catch (final Exception ex) {
new AS2NetException(aSocket.getInetAddress(), aSocket.getPort(), ex).terminate();
}
aSW.stop();
if (aMsgDataSource == null) {
LOGGER.error("Not having a data source to operate on");
} else {
if (aMsgDataSource instanceof ByteArrayDataSource) {
if (LOGGER.isInfoEnabled())
LOGGER.info("received " + AS2IOHelper.getTransferRate(((ByteArrayDataSource) aMsgDataSource).directGetBytes().length, aSW) + " from " + sClientInfo + aMsg.getLoggingText());
} else {
LOGGER.info("received message from " + sClientInfo + aMsg.getLoggingText() + " in " + aSW.getMillis() + " ms");
}
handleIncomingMessage(sClientInfo, aMsgDataSource, aMsg, aResponseHandler);
}
}
use of com.helger.mail.datasource.ByteArrayDataSource in project as2-lib by phax.
the class AbstractDirectoryPollingModule method updateMessage.
public void updateMessage(@Nonnull final IMessage aMsg, @Nonnull final File aFile) throws AS2Exception {
final MessageParameters aParams = new MessageParameters(aMsg);
final String sDefaults = attrs().getAsString(ATTR_DEFAULTS);
if (sDefaults != null)
aParams.setParameters(sDefaults);
final String sFilename = aFile.getName();
final String sFormat = attrs().getAsString(ATTR_FORMAT);
if (sFormat != null) {
final String sDelimiters = attrs().getAsString(ATTR_DELIMITERS, ".-");
aParams.setParameters(sFormat, sDelimiters, sFilename);
}
try {
final byte[] aData = SimpleFileIO.getAllFileBytes(aFile);
String sContentType = attrs().getAsString(ATTR_MIMETYPE);
if (sContentType == null) {
// Default to application/octet-stream
sContentType = CMimeType.APPLICATION_OCTET_STREAM.getAsString();
} else {
try {
sContentType = aParams.format(sContentType);
} catch (final AS2InvalidParameterException ex) {
LOGGER.error("Bad content-type '" + sContentType + "'" + aMsg.getLoggingText());
// Default to application/octet-stream
sContentType = CMimeType.APPLICATION_OCTET_STREAM.getAsString();
}
}
final ByteArrayDataSource aByteSource = new ByteArrayDataSource(aData, sContentType, null);
final MimeBodyPart aBody = new MimeBodyPart();
aBody.setDataHandler(aByteSource.getAsDataHandler());
// Headers must be set AFTER the DataHandler
final String sCTE = aMsg.partnership().getContentTransferEncodingSend(EContentTransferEncoding.AS2_DEFAULT.getID());
aBody.setHeader(CHttpHeader.CONTENT_TRANSFER_ENCODING, sCTE);
// below statement is not filename related, just want to make it
// consist with the parameter "mimetype="application/EDI-X12""
// defined in config.xml 2007-06-01
aBody.setHeader(CHttpHeader.CONTENT_TYPE, sContentType);
// add below statement will tell the receiver to save the filename
// as the one sent by sender. 2007-06-01
final String sSendFilename = attrs().getAsString(ATTR_SENDFILENAME);
if ("true".equals(sSendFilename)) {
final String sMAFilename = aMsg.attrs().getAsString(CFileAttribute.MA_FILENAME);
final String sContentDisposition = "Attachment; filename=\"" + sMAFilename + "\"";
aBody.setHeader(CHttpHeader.CONTENT_DISPOSITION, sContentDisposition);
aMsg.setContentDisposition(sContentDisposition);
}
aMsg.setData(aBody);
} catch (final MessagingException ex) {
throw WrappedAS2Exception.wrap(ex);
}
if (LOGGER.isDebugEnabled())
LOGGER.debug("Updating partnership for AS2 message" + aMsg.getLoggingText());
// update the message's partnership with any stored information
getSession().getPartnershipFactory().updatePartnership(aMsg, true);
if (LOGGER.isDebugEnabled())
LOGGER.debug("Finished updating partnership for AS2 message");
aMsg.updateMessageID();
if (LOGGER.isDebugEnabled())
LOGGER.debug("Updated message ID to " + aMsg.getMessageID());
}
Aggregations