/** * Add/store new message with given labels and markers * * @param account * @param labels automatically assigned labels * @param markers automatically assigned markers * @param file * @return */ @POST @Produces(MediaType.APPLICATION_JSON) public Response addMessage( @PathParam("user") final String user, @PathParam("domain") final String domain, @QueryParam("label") Set<Integer> labels, @QueryParam("marker") Set<Marker> markers, File file) { Mailbox mailbox = new Mailbox(user, domain); // generate new UUID UUID messageId = new MessageIdBuilder().build(); try { FileInputStream in = new FileInputStream(file); MimeParser parser = new MimeParser(); // parse message parser.parse(in); Message message = parser.getMessage(); message.setSize(file.length()); // update message size in.close(); // add labels to message for (Integer label : labels) { message.addLabel(label); } // add markers to message for (Marker marker : markers) { message.addMarker(marker); } // store message in = new FileInputStream(file); messageDAO.put(mailbox, messageId, message, in); in.close(); } catch (MimeParserException mpe) { logger.error("Unable to parse message: ", mpe); throw new BadRequestException("Parsing error. Invalid MIME message."); } catch (OverQuotaException oqe) { throw new WebApplicationException( Response.status(Status.NOT_ACCEPTABLE) .entity(oqe.getMessage()) .type("text/plain") .build()); } catch (IOException ioe) { logger.error("Unable to read message stream: ", ioe); throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR); } catch (IllegalArgumentException e) { throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR); } catch (Exception e) { // HectorException throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR); } finally { if (file.exists()) { file.delete(); } } // build response URI messageUri = uriInfo.getAbsolutePathBuilder().path(messageId.toString()).build(); String responseJson = new StringBuilder("{\"id\":\"").append(messageId).append("\"}").toString(); return Response.created(messageUri).entity(responseJson).build(); }
@Override public Map<MailAddress, DeliveryReturnCode> deliver(MailEnvelope env, final String deliveryId) throws IOException { StopWatch stopWatch = Activator.getDefault().getStopWatch(); Message message; try { MimeParser parser = new MimeParser(); parser.parse(env.getMessageInputStream()); message = parser.getMessage(); } catch (MimeParserException mpe) { logger.error("DID" + deliveryId + ": unable to parse message: ", mpe); throw new DeliveryException("Unable to parse message: " + mpe.getMessage()); } catch (IOException ioe) { logger.error("DID" + deliveryId + ": unable to read message stream: ", ioe); throw new DeliveryException("Unable to read message stream: " + ioe.getMessage()); } message.setSize((long) env.getSize()); // update message size message.addLabel(ReservedLabels.INBOX.getId()); // default location logEnvelope(env, message, deliveryId); Map<MailAddress, DeliveryReturnCode> replies = new HashMap<MailAddress, DeliveryReturnCode>(); // Deliver to each recipient for (MailAddress recipient : env.getRecipients()) { DeliveryReturnCode reply = DeliveryReturnCode.TEMPORARY_FAILURE; // default LMTP reply DeliveryAction deliveryAction = DeliveryAction.DELIVER; // default delivery action Mailbox mailbox = new Mailbox(recipient.toString()); String logMsg = new StringBuilder(" ") .append(mailbox.getId()) .append(" DID") .append(deliveryId) .toString(); try { switch (deliveryAction) { case DELIVER: try { // generate new UUID UUID messageId = new MessageIdBuilder().build(); // store message messageDAO.put(mailbox, messageId, message, env.getMessageInputStream()); // successfully delivered stopWatch.stop("DELIVERY.success", logMsg); reply = DeliveryReturnCode.OK; } catch (OverQuotaException e) { // account is over quota, reject stopWatch.stop("DELIVERY.reject_overQuota", logMsg + " over quota"); reply = DeliveryReturnCode.OVER_QUOTA; } catch (IOException e) { // delivery error, defer stopWatch.stop("DELIVERY.defer", logMsg); logger.error("DID" + deliveryId + ": delivery error: ", e); reply = DeliveryReturnCode.TEMPORARY_FAILURE; } break; case DISCARD: // Local delivery is disabled. stopWatch.stop("DELIVERY.discard", logMsg); reply = DeliveryReturnCode.OK; break; case DEFER: // Delivery to mailbox skipped. Let MTA retry again later. stopWatch.stop("DELIVERY.defer", logMsg); reply = DeliveryReturnCode.TEMPORARY_FAILURE; break; case REJECT: // Reject delivery. Account or mailbox not found. stopWatch.stop("DELIVERY.reject_nonExistent", logMsg + " unknown mailbox"); reply = DeliveryReturnCode.NO_SUCH_USER; } } catch (Exception e) { stopWatch.stop("DELIVERY.defer_failure", logMsg); reply = DeliveryReturnCode.TEMPORARY_FAILURE; logger.error("DID" + deliveryId + ": delivery failed (defered): ", e); } replies.put(recipient, reply); // set delivery status for invoker } return replies; }