Ejemplo n.º 1
0
 @Override
 public Object nullSafeGet(
     ResultSet resultSet, String[] names, SessionImplementor sessionImplementor, Object o)
     throws HibernateException, SQLException {
   Attachment attachment = null;
   String contentType = resultSet.getString(names[0]);
   String name = resultSet.getString(names[1]);
   byte[] content = resultSet.getBytes(names[2]);
   if (contentType != null && name != null && content != null) {
     attachment = new Attachment();
     attachment.setContentType(contentType);
     attachment.setName(name);
     attachment.setContent(content);
   }
   return attachment;
 }
  private void extractPart(final Part part) throws MessagingException, IOException {
    if (part.getContent() instanceof Multipart) {
      Multipart mp = (Multipart) part.getContent();
      for (int i = 0; i < mp.getCount(); i++) {
        extractPart(mp.getBodyPart(i));
      }
      return;
    }

    if (part.getContentType().startsWith("text/html")) {
      if (bodytext == null) {
        bodytext = (String) part.getContent();
      } else {
        bodytext = bodytext + "<HR/>" + (String) part.getContent();
      }
    } else if (!part.getContentType().startsWith("text/plain")) {
      Attachment attachment = new Attachment();
      attachment.setContenttype(part.getContentType());
      attachment.setFilename(part.getFileName());

      InputStream in = part.getInputStream();
      ByteArrayOutputStream bos = new ByteArrayOutputStream();

      byte[] buffer = new byte[8192];
      int count = 0;
      while ((count = in.read(buffer)) >= 0) bos.write(buffer, 0, count);
      in.close();
      attachment.setContent(bos.toByteArray());
      attachments.add(attachment);
    }

    if (!StringUtils.isNull(bodytext) && bodytext.indexOf("Original Message") > -1) {
      subBody = bodytext.substring(0, bodytext.indexOf("Original Message"));
    } else if (!StringUtils.isNull(bodytext)
        && bodytext.indexOf(", \"[email protected]\" <*****@*****.**> wrote:") > -1) {
      String tempstr =
          bodytext.substring(
              0, bodytext.indexOf(", \"[email protected]\" <*****@*****.**> wrote:"));
      subBody = tempstr.substring(0, tempstr.lastIndexOf("On"));
    }
  }