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

发布于 2024-12-18 22:41:13 字数 544 浏览 0 评论 0原文

我有一个使用 MIME 格式传递消息的客户端/服务器系统。我使用 JavaBeans 激活框架创建了一些自定义 mimetypes。

我创建了一个简单的 java 项目,其中包含以下内容:

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"

我已经在独立的测试用例中演示了这一点。但是,当我将其添加为插件时,MIME 类型不会被选中。我认为这是因为 Java 激活类看不到我的自定义 mime 类型定义。

如何将它们添加到我的捆绑包中以便被拾取?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

临走之时 2024-12-25 22:41:13

确保您的项目是插件项目,并确保 build.properties 将您的 mailcap 和 mime.types 检查为导出(打开清单并使用“构建”选项卡)。

Make sure your project is a Plugin Project, and make sure the build.properties has your mailcap and mime.types checked as exported (open the manifest and use the Build tab).

情话难免假 2024-12-25 22:41:13

我知道你问这个问题已经有一段时间了,但无论如何......

看起来你的 RCP 应用程序无法读取该文件。

首先,确保 mailcapmime.types 资源属于尝试访问它们的同一个插件。

其次,尝试使用 显式 MIME 类型文件规范
从静态文件和绝对文件路径开始,例如 /tmp/mime.types ,如果有效,那么您的问题确实是资源加载。在这种情况下,从插件获取流并且< a href="http://docs.oracle.com/javase/6/docs/api/javax/activation/MimetypesFileTypeMap.html#MimetypesFileTypeMap%28java.io.InputStream" rel="nofollow">使用该流构建您的MimetypesFileTypeMap

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 e.g. /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.

高速公鹿 2024-12-25 22:41:13

在我的项目中,我有两个 RCP 插件。其中一个包含 Java Mail jar 及其所有依赖项,包括 activation.jar。另一个包含用于撰写和发送邮件的自定义代码。

最初,我尝试在第二个插件中使用 MimetypesFileTypeMap 并使用类方法 getContentType(String filename) 识别 MIME 类型。 mime.types 文件被放置在插件的 META-INF 文件夹中。问题几乎是一样的。当从 IDE 启动 RCP 应用程序时,MIME 类型识别工作正常,但当作为导出 RCP 应用程序启动时,MIME 类型识别失败。

但后来我发现 Java Mail 会尝试识别 MIME 类型而不是它本身,例如在将文件附加到电子邮件时。它使用其依赖项 activation.jar。之后,我需要解决如何强制 activation.jar 在第一个插件的范围内找到正确的 mime.types 的问题。

但我发现的唯一方法是重新打包 activation.jar 并在 jar 的 META-INF 文件夹中添加正确的 mime.types 。不幸的是,这个解决方案并不完美,因为它“破解”了第三方库。

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 .

清旖 2024-12-25 22:41:13

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

Mailcap 文件搜索顺序:
MailcapCommandMap 在用户系统的各个位置查找 mailcap 文件条目。当请求在 MailcapCommandMap 中搜索命令时,它会按以下顺序搜索 mailcap 文件:

  1. 以编程方式将条目添加到 MailcapCommandMap 实例。
  2. 用户主目录中的 .mailcap 文件。
  3. 文件 /lib/mailcap。
  4. 名为 META-INF/mailcap 的文件或资源。
  5. 名为 META-INF/mailcap.default 的文件或资源(通常仅在activation.jar 文件中找到)。

来自现已失效的链接:

JAF 使用上下文类加载器来加载类。如果失败了,
它使用加载 JAF 类的类加载器。

当 JAF 与应用程序一起打包时,JAF 类是
由与其他应用程序类相同的类加载器加载,
因此,即使未设置上下文类加载器,JAF 也可以找到
其他应用程序类。

当 JAF 是 JDK 的一部分时,JAF 类由
系统类加载器。如果没有设置上下文类加载器,
JAF没有办法找到合适的类加载器来加载
应用程序类。

以编程方式设置 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); 
    } 

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); 
    } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文