Ejemplo n.º 1
0
 public void reset() {
   commandMode = true;
   domain = null;
   sender = Tuple.empty();
   recipient = Tuple.empty();
   declaredMailSize = 0;
   realMailSize = 0;
   processingHeaders = true;
   headers.clear();
   if (tempFile != null) {
     tempFile.delete();
     tempFile = null;
   }
 }
Ejemplo n.º 2
0
  @Override
  public FullHttpResponse doGet(
      ChannelHandlerContext ctx, HttpRequest request, Map<String, List<String>> queryParameters)
      throws RestException {

    URI uri = getUri(request);

    Pattern p = Pattern.compile(URL_REGEXP);
    Matcher m = p.matcher(uri.getPath());
    if (!m.matches()) {
      throw new RestException(HttpResponseStatus.BAD_REQUEST);
    }

    Tuple<String, String> loginDomain = Tuple.of(m.group(2), m.group(1));
    MailAccount mailAccount = mailManager.findAccount(loginDomain);
    if (mailAccount == null) {
      throw new RestException(HttpResponseStatus.NOT_FOUND);
    }

    MailMessage mailMessage =
        mailManager.findMessage(mailAccount.getId(), Integer.parseInt(m.group(3)));
    if (mailMessage == null) {
      throw new RestException(HttpResponseStatus.NOT_FOUND);
    }

    InputStream is = MailViewHandler.class.getResourceAsStream("mailview.html");
    String pageContent = IOUtils.streamToString(is);

    File mailFile = null;
    try {
      mailFile = mailFileProvider.getMessageFile(loginDomain, mailMessage.getId());
    } catch (Exception e) {
      throw new RestException(HttpResponseStatus.INTERNAL_SERVER_ERROR, e);
    }

    MailMessageFileReader messageReader = new MailMessageFileReader(mailFile);
    MimeMessageParser parser = messageReader.parseMessage();
    if (parser == null) {
      throw new RestException(HttpResponseStatus.INTERNAL_SERVER_ERROR);
    }

    pageContent = pageContent.replace("%DATE%", DateUtils.iso(mailMessage.getCreated().getTime()));
    try {
      pageContent = pageContent.replace("%SUBJECT%", parser.getSubject());
      pageContent = pageContent.replace("%FROM%", parser.getFrom());

      if (parser.hasHtmlContent()) {
        pageContent = pageContent.replace("%BODY%", parser.getHtmlContent());
      } else {
        pageContent = pageContent.replace("%BODY%", "<pre>" + parser.getPlainContent() + "</pre>");
      }
    } catch (Exception e) {
      throw new RestException(HttpResponseStatus.INTERNAL_SERVER_ERROR, e);
    }

    ByteBuf content = Unpooled.copiedBuffer(pageContent, CharsetUtil.UTF_8);
    HttpResponseProvider httpResponseProvider = new HttpResponseProvider(request);
    return httpResponseProvider.createHttpResponse(HttpResponseStatus.OK, content);
  }
Ejemplo n.º 3
0
public class MailSession {

  public static final int MAX_MESSAGE_SIZE = 10 * 1024 * 1024;

  private boolean commandMode = true;
  private String domain;
  private Tuple<String, String> sender = Tuple.empty();
  private Tuple<String, String> recipient = Tuple.empty();
  private int declaredMailSize = 0;
  private int realMailSize = 0;
  private boolean processingHeaders = true;
  private List<String> headers = new ArrayList<String>();
  private File tempFile = null;

  public boolean processDataLine(String line) throws Exception {
    realMailSize += line.length();
    if (realMailSize > MAX_MESSAGE_SIZE) {
      throw new MailSizeException();
    }

    if (tempFile == null) {
      tempFile = File.createTempFile("mail", ".msg");
      tempFile.deleteOnExit();
    }

    FileWriter fileWriter = new FileWriter(tempFile, true);
    try {
      if (processingHeaders) {
        if (StringUtils.nullOrEmpty(line)) {
          processingHeaders = false;
        } else {
          headers.add(line);
        }
        fileWriter.write(line);
        fileWriter.write(StringUtils.CRLF);
      } else {
        if (isEndOfTransfer(line)) {
          return true;
        }
        fileWriter.write(line);
        fileWriter.write(StringUtils.CRLF);
      }
    } finally {
      IOUtils.closeQuietly(fileWriter);
    }
    return false;
  }

  public boolean isEndOfTransfer(String line) {
    return ".".equals(line);
  }

  public void reset() {
    commandMode = true;
    domain = null;
    sender = Tuple.empty();
    recipient = Tuple.empty();
    declaredMailSize = 0;
    realMailSize = 0;
    processingHeaders = true;
    headers.clear();
    if (tempFile != null) {
      tempFile.delete();
      tempFile = null;
    }
  }

  public boolean isCommandMode() {
    return commandMode;
  }

  public void setCommandMode(boolean commandMode) {
    this.commandMode = commandMode;
  }

  public String getDomain() {
    return domain;
  }

  public void setDomain(String domain) {
    this.domain = domain;
  }

  public Tuple<String, String> getSender() {
    return sender;
  }

  public void setSender(Tuple<String, String> sender) {
    this.sender = sender;
  }

  public Tuple<String, String> getRecipient() {
    return recipient;
  }

  public void setRecipient(Tuple<String, String> recipient) {
    this.recipient = recipient;
  }

  public int getDeclaredMailSize() {
    return declaredMailSize;
  }

  public void setDeclaredMailSize(int declaredMailSize) {
    this.declaredMailSize = declaredMailSize;
  }

  public boolean isProcessingHeaders() {
    return processingHeaders;
  }

  public void setProcessingHeaders(boolean processingHeaders) {
    this.processingHeaders = processingHeaders;
  }

  public int getRealMailSize() {
    return realMailSize;
  }

  public void setRealMailSize(int realMailSize) {
    this.realMailSize = realMailSize;
  }

  public List<String> getHeaders() {
    return headers;
  }

  public void setHeaders(List<String> headers) {
    this.headers = headers;
  }

  public File getTempFile() {
    return tempFile;
  }

  public void setTempFile(File tempFile) {
    this.tempFile = tempFile;
  }
}