简体   繁体   English

如何将自定义MIME内容类型添加到Eclipse RCP应用程序

[英]How to add custom MIME content types to Eclipse RCP application

I have a client/server system that is passing messages using MIME format. 我有一个使用MIME格式传递消息的客户端/服务器系统。 I have created some custom mimetypes using the JavaBeans activation framework. 我已经使用JavaBeans激活框架创建了一些自定义的模仿类型。

I have created a simple java project with the following: 我创建了一个简单的java项目,内容如下:

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

mailcap: mailcap:

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

mime.types: 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. 但是,当我将其添加为插件时,MIME类型不会被提取。 I presume that this is down to the fact that the Java Activation classes can't see my custom mime type definitions. 我认为这是因为Java激活类看不到我的自定义mime类型定义。

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. 看起来您的RCP应用程序无法读取该文件。

First, make sure the mailcap and mime.types resources are owned by the same plug-in that tries to access them. 首先,请确保mailcapmime.types资源由尝试访问它们的同一插件拥有。

Secondly, try to construct MimetypesFileTypeMap with explicit mime-type file specification . 其次,尝试使用显式的mime-type文件规范构造MimetypesFileTypeMap Start with static file and absolute file path eg /tmp/mime.types and if that works then your problem is resource loading indeed. 从静态文件和绝对文件路径开始,例如/tmp/mime.types ,如果/tmp/mime.types ,那么您的问题确实是资源加载。 In that case, get stream from plugin and construct your MimetypesFileTypeMap with that stream. 在这种情况下,请从插件获取流并使用该流构造您的MimetypesFileTypeMap

In my project I had two RCP plugins. 在我的项目中,我有两个RCP插件。 One contained Java Mail jar and all its dependencies, including activation.jar . 其中一个包含Java Mail jar及其所有依赖项,包括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) . 最初,我尝试在第二个插件中使用MimetypesFileTypeMap并使用类方法getContentType(String filename)标识MIME类型。 mime.types file was placed in plugin`s META-INF folder. mime.types文件放置在插件的META-INF文件夹中。 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. 从IDE启动RCP应用程序时,MIME类型识别工作正常,但作为导出RCP应用程序启动时,MIME类型识别失败。

But then I find out that Java Mail tries to identify MIME types but itself while, for example, attaching files to an email. 但是后来我发现Java Mail会尝试识别MIME类型,但是会识别自身,例如,将文件附加到电子邮件中。 And it uses its dependency activation.jar . 并且使用其依赖项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. 之后,我需要解决有关如何强制activation.jar在第一个插件范围内找到正确的mime.types的问题。

But the only way I found was repackaging activation.jar and adding correct mime.types in jar`s META-INF folder. 但是我发现的唯一方法是重新包装activation.jar并在jar的META-INF文件夹中添加正确的mime.types 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 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. Mailcap文件搜索顺序:MailcapCommandMap在用户系统中的各个位置查找mailcap文件条目。 When requests are made to search for commands in the MailcapCommandMap, it searches mailcap files in the following order: 当请求在MailcapCommandMap中搜索命令时,它将按以下顺序搜索mailcap文件:

  1. Programatically added entries to the MailcapCommandMap instance. 以编程方式将条目添加到MailcapCommandMap实例。
  2. The file .mailcap in the user's home directory. 用户主目录中的文件.mailcap。
  3. The file /lib/mailcap. 文件/ lib / mailcap。
  4. The file or resources named META-INF/mailcap. 名为META-INF / mailcap的文件或资源。
  5. The file or resource named META-INF/mailcap.default (usually found > only in the activation.jar file). 名为META-INF / mailcap.default的文件或资源(通常仅在activation.jar文件中找到>)。

From a now-dead link: 从现已失效的链接中:

JAF uses the context class loader to load classes. JAF使用上下文类加载器加载类。 If that fails, it uses the class loader that loaded the JAF classes. 如果失败,它将使用加载JAF类的类加载器。

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. 当JAF与应用程序打包在一起时,JAF类由与其他应用程序类相同的类加载器加载,因此即使未设置上下文类加载器,JAF仍可以找到其他应用程序类。

When JAF is part of the JDK, the JAF classes are loaded by the system class loader. 当JAF是JDK的一部分时,JAF类由系统类加载器加载。 Without the context class loader being set, JAF has no way to find the appropriate class loader to load application classes. 没有设置上下文类加载器,JAF无法找到合适的类加载器来加载应用程序类。

An example of programatically setting the MailcapCommandMap is: 以编程方式设置MailcapCommandMap的示例为:

    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选项卡)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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