public MimeBodyPart(Body body, String mimeType) throws MessagingException {
   mHeader = new MimeHeader();
   if (mimeType != null) {
     addHeader(MimeHeader.HEADER_CONTENT_TYPE, mimeType);
   }
   MimeMessageHelper.setBody(this, body);
 }
Example #2
0
 private void writeFailureWithExplanation(
     HttpServletResponse response, Exception e, MimeMessage mimeMessage, MdnData mdnData)
     throws IOException {
   try {
     setHeadersForMDN(response, mdnData, mimeMessage);
     response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
     mimeMessage.writeTo(response.getOutputStream());
     log.error("Returned MDN with failure: " + MimeMessageHelper.toString(mimeMessage), e);
     log.error(
         "---------- REQUEST FAILURE INFORMATION ENDS HERE --------------"); // Being helpful to
                                                                             // those who must read
                                                                             // the error logs
   } catch (MessagingException e1) {
     String msg = "Unable to return failure to sender; " + e1.getMessage();
     log.error(msg);
     response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
     response.getWriter().write(msg);
   }
 }
Example #3
0
  /**
   * Receives the POST'ed AS2 message.
   *
   * <p>Important to note that the HTTP headers contains the MIME headers for the payload. Since the
   * the request can only be read once, using getReader()/getInputStream()
   */
  protected void doPost(final HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    /*
    https://www.rfc-editor.org/rfc/rfc2311.txt section 3.4
    There are two formats for signed messages defined for S/MIME:
    application/pkcs7-mime and SignedData, and multipart/signed. In
    general, the multipart/signed form is preferred for sending, and
    receiving agents SHOULD be able to handle both.

       Signing Using application/pkcs7-mime and SignedData

       This signing format uses the application/pkcs7-mime MIME type. The
       steps to create this format are:

         Step 1. The MIME entity is prepared according to section 3.1

         Step 2. The MIME entity and other required data is processed into a
                 PKCS #7 object of type signedData

         Step 3. The PKCS #7 object is inserted into an
                 application/pkcs7-mime MIME entity

       The smime-type parameter for messages using application/pkcs7-mime
       and SignedData is "signed-data". The file extension for this type of
       message is ".p7m".

       Creating a multipart/signed Message

         Step 1. The MIME entity to be signed is prepared according to
                 section 3.1, taking special care for clear-signing.

         Step 2. The MIME entity is presented to PKCS #7 processing in order
                 to obtain an object of type signedData with an empty
                 contentInfo field.

         Step 3. The MIME entity is inserted into the first part of a
                 multipart/signed message with no processing other than that
                 described in section 3.1.

         Step 4. Transfer encoding is applied to the detached signature and
                 it is inserted into a MIME entity of type
                 application/pkcs7-signature

         Step 5. The MIME entity of the application/pkcs7-signature is
                 inserted into the second part of the multipart/signed
                 entity

       The multipart/signed Content type has two required parameters: the
       protocol parameter and the micalg parameter.

       The protocol parameter MUST be "application/pkcs7-signature". Note
       that quotation marks are required around the protocol parameter
       because MIME requires that the "/" character in the parameter value
       MUST be quoted.

    */

    InternetHeaders headers = copyHttpHeadersIntoMap(request);

    // Receives the data, validates the headers, signature etc., invokes the persistence handler
    // and finally returns the MdnData to be sent back to the caller
    try {

      // Performs the actual reception of the message by parsing the HTTP POST request
      MdnData mdnData =
          inboundMessageReceiver.receive(
              headers,
              request.getInputStream(),
              messageRepository,
              rawStatisticsRepository,
              ourAccessPointIdentifier);

      // Creates the S/MIME message to be returned to the sender
      MimeMessage mimeMessage = mdnMimeMessageFactory.createMdn(mdnData, headers);

      // Add MDN headers to http response
      setHeadersForMDN(response, mdnData, mimeMessage);
      response.setStatus(HttpServletResponse.SC_OK);

      // Try to write the MDN mime message to http response
      try {
        mimeMessage.writeTo(response.getOutputStream());
        response.getOutputStream().flush();
        log.debug("Served request, status=OK:\n" + MimeMessageHelper.toString(mimeMessage));
        log.debug("------------- INFO ON PROCESSED REQUEST ENDS HERE -----------");
      } catch (MessagingException e) {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        response.getWriter().write("Severe error during write of MDN " + e.getMessage());
      }

    } catch (ErrorWithMdnException e) {
      // Reception of AS2 message failed, send back a MDN indicating failure (always use HTTP 200
      // for MDN)
      log.warn("AS2 reception error: " + e.getMessage(), e);
      log.warn("Returning negative MDN with explanatory message");
      MdnData mdnData = e.getMdnData();
      MimeMessage mimeMessage = mdnMimeMessageFactory.createMdn(mdnData, headers);
      writeMimeMessageWithNegativeMdn(response, e, mimeMessage, mdnData);
    } catch (Exception e) {
      // Unexpected internal error, cannot proceed, return HTTP 500 and partly MDN to indicating the
      // problem
      log.error("Internal error occured: " + e.getMessage(), e);
      log.error("Attempting to return MDN with explanatory message and HTTP 500 status");
      MdnData mdnData =
          MdnData.Builder.buildProcessingErrorFromHeaders(headers, null, e.getMessage());
      MimeMessage mimeMessage = mdnMimeMessageFactory.createMdn(mdnData, headers);
      writeFailureWithExplanation(response, e, mimeMessage, mdnData);
    }
  }
 MimeBodyPart(MimeHeader header, Body body) throws MessagingException {
   mHeader = header;
   MimeMessageHelper.setBody(this, body);
 }