简体   繁体   中英

How to add custom MIME content types to Eclipse RCP application

I have a client/server system that is passing messages using MIME format. I have created some custom mimetypes using the JavaBeans activation framework.

I have created a simple java project with the following:

my-mime
+- src/com/foo/FooContentHandler
+- META-INF/mailcap
+- META-INF/mime.types

mailcap:

application/x-foo; ; x-java-content-handler=com.foo.FooContentHandler

mime.types:

type=application/x-foo desc="foo" exts="foo"

I have demonstrated this working in a standalone testcase. However, when I add it as a plugin the MIME type doesn't get picked up. I presume that this is down to the fact that the Java Activation classes can't see my custom mime type definitions.

How can I add them to my bundle so that they are picked up?

I realize it's been a while since you asked this question, but anyway...

Looks like your RCP application cannot read the file.

First, make sure the mailcap and mime.types resources are owned by the same plug-in that tries to access them.

Secondly, try to construct MimetypesFileTypeMap with explicit mime-type file specification . Start with static file and absolute file path eg /tmp/mime.types and if that works then your problem is resource loading indeed. In that case, get stream from plugin and construct your MimetypesFileTypeMap with that stream.

In my project I had two RCP plugins. One contained Java Mail jar and all its dependencies, including activation.jar . Another contained custom code for composing and sending mails.

Initially, I tried to use MimetypesFileTypeMap in the second plugin and identify MIME type using class method getContentType(String filename) . mime.types file was placed in plugin`s META-INF folder. The problem was almost the same. MIME types identification worked perfectly when RCP app was launched from IDE, but failed when it was launched as export RCP app.

But then I find out that Java Mail tries to identify MIME types but itself while, for example, attaching files to an email. And it uses its dependency activation.jar . After that I needed to solve the problem about how to force activation.jar to find correct mime.types in the scope of the first plugin.

But the only way I found was repackaging activation.jar and adding correct mime.types in jar`s META-INF folder. Unfortunately, this solution is not perfect as it "hacks" third-party library .

From https://docs.oracle.com/cd/E17802_01/j2se/javase/technologies/desktop/javabeans/glasgow/javadocs/javax/activation/MailcapCommandMap.html

Mailcap file search order: The MailcapCommandMap looks in various places in the user's system for mailcap file entries. When requests are made to search for commands in the MailcapCommandMap, it searches mailcap files in the following order:

  1. Programatically added entries to the MailcapCommandMap instance.
  2. The file .mailcap in the user's home directory.
  3. The file /lib/mailcap.
  4. The file or resources named META-INF/mailcap.
  5. The file or resource named META-INF/mailcap.default (usually found > only in the activation.jar file).

From a now-dead link:

JAF uses the context class loader to load classes. If that fails, it uses the class loader that loaded the JAF classes.

When JAF is packaged with the application, the JAF classes are loaded by the same class loader as the other application classes, so even if the context class loader isn't set JAF can find the other application classes.

When JAF is part of the JDK, the JAF classes are loaded by the system class loader. Without the context class loader being set, JAF has no way to find the appropriate class loader to load application classes.

An example of programatically setting the MailcapCommandMap is:

    static { // add handlers for main MIME types
            MailcapCommandMap mcap = new MailcapCommandMap(); 
            mcap.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain"); 
            mcap.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html"); 
            mcap.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
            mcap.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed; x-java-fallback-entry=true");
            mcap.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822"); 
            CommandMap.setDefaultCommandMap(mcap); 
    } 

确保您的项目是一个Plugin Project,并确保build.properties中的mailcap和mime.types已作为导出检查(打开清单并使用Build选项卡)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM