Search in sources :

Example 6 with Message

use of org.apache.james.mime4j.dom.Message in project sling by apache.

the class MessageStoreImpl method saveAll.

public void saveAll(Iterator<Message> iterator) throws IOException {
    ResourceResolver resolver = null;
    try {
        resolver = getResourceResolver();
        int mcount = 0;
        while (iterator.hasNext()) {
            Message msg = iterator.next();
            save(resolver, msg);
            mcount++;
            if (mcount % 100 == 0) {
                logger.debug(mcount + " messages processed.");
            }
        }
        logger.info(mcount + " messages processed.");
    } catch (LoginException e) {
        throw new RuntimeException("LoginException", e);
    } finally {
        if (resolver != null) {
            resolver.close();
        }
    }
}
Also used : Message(org.apache.james.mime4j.dom.Message) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) LoginException(org.apache.sling.api.resource.LoginException)

Example 7 with Message

use of org.apache.james.mime4j.dom.Message in project sling by apache.

the class MessageStoreImplRepositoryTest method assertSaveMessage.

private void assertSaveMessage(String messageFile) throws MimeException, IOException, FileNotFoundException {
    MessageBuilder builder = new DefaultMessageBuilder();
    Message msg = builder.parseMessage(new FileInputStream(new File(TU.TEST_FOLDER, messageFile)));
    store.save(msg);
    final Resource r = resolver.getResource(getResourcePath(msg, store));
    assertNotNull("Expecting non-null Resource", r);
    final ModifiableValueMap m = r.adaptTo(ModifiableValueMap.class);
    File bodyFile = new File(TU.TEST_FOLDER, specialPathFromFilePath(messageFile, BODY_SUFFIX));
    if (bodyFile.exists()) {
        String expectedBody = readTextFile(bodyFile);
        assertValueMap(m, "Body", expectedBody);
    }
    File headersFile = new File(TU.TEST_FOLDER, specialPathFromFilePath(messageFile, HEADERS_SUFFIX));
    if (headersFile.exists()) {
        MessageStoreImplRepositoryTestUtil.assertHeaders(headersFile, m);
    }
    // test at least something 
    assertTrue(headersFile.exists() || bodyFile.exists());
}
Also used : MessageBuilder(org.apache.james.mime4j.dom.MessageBuilder) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) Message(org.apache.james.mime4j.dom.Message) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) Resource(org.apache.sling.api.resource.Resource) MessageStoreImplRepositoryTestUtil.readTextFile(org.apache.sling.mailarchiveserver.impl.MessageStoreImplRepositoryTestUtil.readTextFile) File(java.io.File) FileInputStream(java.io.FileInputStream) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 8 with Message

use of org.apache.james.mime4j.dom.Message in project sling by apache.

the class StatsTestServlet method doPost.

@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    final RequestParameter param = request.getRequestParameter(IMPORT_FILE_ATTRIB_NAME);
    if (param == null) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Missing required parameter " + IMPORT_FILE_ATTRIB_NAME);
        return;
    }
    InputStream is = null;
    final PrintWriter pw = response.getWriter();
    response.setContentType("text/plain");
    response.setCharacterEncoding("UTF-8");
    try {
        is = param.getInputStream();
        pw.println("Creating stats from supplied mbox file...");
        int counter = 0;
        final Iterator<Message> it = parser.parse(is);
        while (it.hasNext()) {
            final Message m = it.next();
            final String[] to = MailStatsProcessorImpl.toArray(m.getTo());
            final String[] cc = MailStatsProcessorImpl.toArray(m.getCc());
            for (String from : MailStatsProcessorImpl.toArray(m.getFrom())) {
                processor.computeStats(m.getDate(), from.toString(), to, cc);
            }
            counter++;
        }
        pw.println(counter + " messages parsed");
    } finally {
        processor.flush();
        pw.flush();
        if (is != null) {
            is.close();
        }
    }
}
Also used : Message(org.apache.james.mime4j.dom.Message) RequestParameter(org.apache.sling.api.request.RequestParameter) InputStream(java.io.InputStream) PrintWriter(java.io.PrintWriter)

Example 9 with Message

use of org.apache.james.mime4j.dom.Message in project Gargoyle by callakrsos.

the class MimeViewerExam method start.

@Override
public void start(Stage primaryStage) throws Exception {
    WebView center = new WebView();
    WebEngine engine = center.getEngine();
    URL resource = MimeViewerExam.class.getResource("Sample.html");
    // String externalForm = resource.toExternalForm();
    String readToString = FileUtil.readToString(resource.openStream());
    System.out.println("##########################################");
    System.out.println("mime string");
    System.out.println(readToString);
    System.out.println("##########################################");
    final MessageBuilder builder = new DefaultMessageBuilder();
    final Message message = builder.parseMessage(resource.openStream());
    Body body = ((Entity) message).getBody();
    StringBuilder sb = new StringBuilder();
    extracted(sb, body);
    Document parse = Jsoup.parse(sb.toString());
    Elements select = parse.select("img");
    select.forEach(e -> {
        String attr = e.attr("src");
        if (attr.startsWith("cid:")) {
            String cid = attr.replace("cid:", "");
            String string = meta.get(cid);
            e.attr("src", "data:image/jpg;base64,".concat(string));
        }
    });
    System.out.println("##########################################");
    System.out.println("html string");
    System.out.println(sb.toString());
    System.out.println("##########################################");
    System.out.println(parse.toString());
    engine.loadContent(sb.toString(), "text/html");
    primaryStage.setScene(new Scene(new BorderPane(center)));
    primaryStage.show();
}
Also used : Entity(org.apache.james.mime4j.dom.Entity) BorderPane(javafx.scene.layout.BorderPane) Message(org.apache.james.mime4j.dom.Message) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) Scene(javafx.scene.Scene) WebEngine(javafx.scene.web.WebEngine) URL(java.net.URL) MessageBuilder(org.apache.james.mime4j.dom.MessageBuilder) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) WebView(javafx.scene.web.WebView) Body(org.apache.james.mime4j.dom.Body) TextBody(org.apache.james.mime4j.dom.TextBody) BinaryBody(org.apache.james.mime4j.dom.BinaryBody)

Aggregations

Message (org.apache.james.mime4j.dom.Message)9 MessageBuilder (org.apache.james.mime4j.dom.MessageBuilder)5 DefaultMessageBuilder (org.apache.james.mime4j.message.DefaultMessageBuilder)5 Entity (org.apache.james.mime4j.dom.Entity)4 TextBody (org.apache.james.mime4j.dom.TextBody)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)3 MimeException (org.apache.james.mime4j.MimeException)3 BinaryBody (org.apache.james.mime4j.dom.BinaryBody)3 Body (org.apache.james.mime4j.dom.Body)3 MessageStoreImplRepositoryTestUtil.readTextFile (org.apache.sling.mailarchiveserver.impl.MessageStoreImplRepositoryTestUtil.readTextFile)2 Test (org.junit.Test)2 StringBuilder (scala.collection.mutable.StringBuilder)2 Strings (com.google.common.base.Strings)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 CharStreams (com.google.common.io.CharStreams)1 Ints (com.google.common.primitives.Ints)1