Detecting GWT permutation on server based on user agent

六眼飞鱼酱① 提交于 2019-12-08 03:36:20

问题


I'm currently working on adding HTML5 offline support to my web application, and am mostly following the same approach of mgwt:

  1. Generate manifest files per permutation at compulation time
  2. Using a servlet to serve the manifest file, based on the user-agent of the browser.

My question involves step 2: In my servlet, I want to detect which permutation to serve for a certain request. The way I do this now is:

  • Get the user agent string from the request
  • Map this (using simple string operations (e.g. userAgent.contains("safari")) to the 'agent id', which I can map using a mapping file to the permutation strong name. In other words, map Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1) to ie6, which I can map (using the generated mapping file, see example below) to 15B454D690F2CCAD57F1DD809429BF42.

    <permutation name="15B454D690F2CCAD57F1DD809429BF42">
      <user.agent>ie6</user.agent>
    </permutation>
    

The problem I'm facing: I want to use the same method of linking a user agent string to a permutation as GWT uses (i.e. map Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1) to ie6). This way I don't have to fix my code whenever my GWT gets updated with other permutations/browser versions. In other words, I don't like my current solution of naively matching the complete user agent string in the servlet with the user agent 'id' (in my example 'ie6') in my permutation mapping. The solutions I thought about were:

  • after page load, detect the loaded permutation, and pass that as argument to the servlet. Then after fetching the manifest for that permutation, insert that as an attribute of the html tag using javascript. However, inserting this dynamically does not seem to work properly. (See Dynamically Trigger HTML5 Cache Manifest file? ) (the same post explains a workaround of doing this dynamically using an iframe, but I prefer a cleaner solution)
  • somehow use the client-side to map the complete user agent string to a permutation, in my servlet. I'd prefer this solution, but havent found a way to do achieve this... GWT uses javascript to achieve this (see UserAgentPropertyGenerator). I could execute this in my servlet, but this javascript method not only uses the user agent, but uses the dom document as well... Are there other solutions I am overlooking? Surely others must have had this issue as well when creating HTML5 manifest files...

回答1:


You can take advantage of the HTML5Manifest solution provided with mgwt. They have a linker which produces a file which can be read by the servlet they provide, and return the appropriate list of files to cache by the browser based on the user-agent header.

If you want to do it by yourself, you can figure out the most suitable permutation per browser, based on the http user-agent header, and on the compilation-mappings.txt file which is generated by the gwt compiler if you are using xsiframe linker.



来源:https://stackoverflow.com/questions/18696716/detecting-gwt-permutation-on-server-based-on-user-agent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!