class MultiPayloadMessageMimeEncoder extends MimeEncoderImpl<OutputStream, InputStream> {

  @Override
  public void writeMessage(AdaptrisMessage msg, OutputStream target) throws CoreException {
    try {
      MultiPartOutput output = new MultiPartOutput(msg.getUniqueId());
      if (msg instanceof MultiPayloadAdaptrisMessage) {
        MultiPayloadAdaptrisMessage message = (MultiPayloadAdaptrisMessage)msg;
        for (String id : message.getPayloadIDs()) {
          message.switchPayload(id);
          output.addPart(payloadAsMimePart(message), PAYLOAD_CONTENT_ID + "/" + id);
        }
      } else {
        output.addPart(payloadAsMimePart(msg), PAYLOAD_CONTENT_ID);
      }
      output.addPart(getMetadata(msg), getMetadataEncoding(), METADATA_CONTENT_ID);
      if (msg.getObjectHeaders().containsKey(CoreConstants.OBJ_METADATA_EXCEPTION)) {
        output.addPart(asMimePart((Exception) msg.getObjectHeaders().get(CoreConstants.OBJ_METADATA_EXCEPTION)), EXCEPTION_CONTENT_ID);
      }
      output.writeTo(target);
    } catch (Exception e) {
      throw ExceptionHelper.wrapCoreException(e);
    }
  }

  public  MultiPayloadMessageMimeEncoder();

}