简体   繁体   English

如何防止Java Web Start应用程序缓存其他下载资源

[英]How to prevent Java Web Start application caching additional download resources

My JWS application uses a library (also my code) that under the hood retrieves various XML documents from server: 我的JWS应用程序使用一个库(也是我的代码),该库在后台从服务器检索各种XML文档:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder(); 
Document doc = db.parse(xmlFileUrl);

The contents at the URLs change from time to time, and application needs to re-fetch the content at start up and later while running. URL上的内容会不时更改,并且应用程序需要在启动时以及运行时稍后重新获取内容。

The problem is that JWS caches the content and returns cached content for subsequent requests. 问题在于JWS缓存了内容,并为随后的请求返回了缓存的内容。

Telling JWS to remove cached content for the URL does work: 告诉JWS删除URL的缓存内容确实有效:

DownloadService ds = (DownloadService)ServiceManager.lookup("javax.jnlp.DownloadService");
ds.removeResource(xmlFileUrl, null);

But the library is used by other front ends, and I want to avoid making the library dependent on javaws.jar. 但是该库被其他前端使用,并且我想避免使该库依赖于javaws.jar。 My current solution defines an interface in the library that allows library to request cache clearing. 我当前的解决方案在库中定义了一个接口,该接口允许库请求清除缓存。 JWS launcher passes an implementation of the interface to the library. JWS启动器将接口的实现传递给该库。 There are multiple components that fetch various resources and while I can make it work, the whole thing is clumsy. 有多个组件可以获取各种资源,尽管我可以使其工作,但整个过程都很笨拙。

Another workaround is to append a unique query for every request, eg 另一个解决方法是为每个请求附加唯一查询,例如

url += "?ignored="+System.currentTimeMillis()+Math.random();

but that will pollute JWS cache and I see it as source of confusion, wtf and bloat. 但这会污染JWS缓存,我将其视为混乱,wtf和膨胀的原因。

The XML documents are generated on the server. XML文档是在服务器上生成的。 Setting header: 设置标题:

Cache-Control: no-cache'

does not help. 没有帮助。

I am interested if there is a cleaner solution to this problem. 我很感兴趣,如果有一个更清洁的解决了这个问题。 If I could set some HTTP headers on server that would be ideal. 如果可以在服务器上设置一些HTTP标头,那将是理想的选择。 Listing resources not to be cached in .jnlp would be acceptable but not ideal as URLs are constructed in the library and I'd have to significantly change the init code. 列出不缓存在.jnlp中的资源将是可以接受的,但并不理想,因为URL是在库中构建的,因此我必须对初始代码进行重大更改。 Other methods and ideas are welcome. 欢迎其他方法和想法。

One thing that seems to work is to change request method to POST: 似乎有效的一件事是将请求方法更改为POST:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder(); 

// open connection manually to set request method to POST
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");

Document doc = db.parse(connection.getInputStream());

This solves it for my current tasks. 这可以解决我当前的任务。 I am not sure this will work for re-fetching eg images. 我不确定这是否可以重新获取图像。

Also building XML Document directly from InputStream is probably missing out on the opportunity to read HTTP response headers and therefore may have trouble with encoding. 直接从InputStream构建XML Document可能会失去读取HTTP响应标头的机会,因此可能在编码方面遇到麻烦。

I'm surprised I did not see void java.net.URLConnection.setUseCaches(boolean usecaches) method before. 我很惊讶我之前没有看到void java.net.URLConnection.setUseCaches(boolean usecaches)方法。

This solution works including in JWS. 该解决方案适用于JWS。 It does not make dependency on javaws.jar which is clean and simple. 它不依赖于干净简单的javaws.jar。

URL url = new URL(xmlFileLocation);
URLConnection connection = url.openConnection();
// Prevent JavaWebStart from returning cached copy.
connection.setUseCaches(false);

// Now fetch the content, e.g.
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder(); 
Document doc = db.parse(connection.getInputStream());

This is still backward in that the application that consumes the server response decides to not use the cache, rather than the framework(JWS) honoring server side cache control HTTP headers. 这仍然是落后的,因为消耗服务器响应的应用程序决定不使用缓存,而不是使用框架(JWS)遵循服务器端缓存控制HTTP标头。

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

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