分享

Separating Tapestry Module

 KyunraWang 2017-06-15

Tapestry is so advance and configurable, even after using it all this time I still found many gems hidden in it.

It started with a problem I found in my production environment. I’m doing some interactive Google Map manipulation with tapestry. Since our Glassfish server sits behind Apache server (using reverse proxy), the link generated by ComponentResources.createEventLink will generate a link that contains the glassfish port (8080), while the browser see the other response is generated from regular http port (80) because of reverse proxying. This would trigger Cross Origin Resource Sharing (CORS) problem. Something that modern web server have to limit a misuse of your URL by another web server. There are few way to fix CORS:

  • You can let Apache send a custom header that allowing everybody to use your resource unrestricted using (which is quite dangerous)
Header set Access-Control-Allow-Origin “*”

To implement the fix wasn’t that hard. All you need to do is go to your AppModule and add this one line :

configuration.add(SymbolConstants.HOSTPORT, "80");

But this wasn’t enough. Since in our development environment port 8080 is the right port. So as a hack we usually comment this config, and only uncomment it when we are deploying to our production environment. Which is prone to mistake and a disaster waiting to happen. Thus come Tapestry Module separation

Separating Tapestry Module

It’s commonly known that you can create multiple module class in tapestry, usually for creating a submodule that later you can extract as a library. But tapestry also provide a way to categorize a module as production, development and QA. This will help when you want to have different configuration for each phases. So what we want is to create a module that will override port 80 to 8080 when we are in our development environment.

As usual, the problem with Tapestry is finding where the documentation is located. After lots of digging, I finally found the documentation in the link bellow, in the setting execution mode.

Basically what you need to do is:

  1. Create a DevelopmentModule.java like this. Notice that we use Configuration.override instead of add for overriding with port 8080
/**
* This module is automatically included as part of the Tapestry IoC Registry if <em>tapestry.execution-mode</em>
* includes <code>development</code>.
*/
public class DevelopmentModule {
public static void contributeApplicationDefaults(
MappedConfiguration<String, Object> configuration) {
// The factory default is true but during the early stages of an application
// overriding to false is a good idea. In addition, this is often overridden
// on the command line as -Dtapestry.production-mode=false
configuration.override(SymbolConstants.PRODUCTION_MODE, false);

// The application version number is incorprated into URLs for some
// assets. Web browsers will cache assets because of the far future expires
// header. If existing assets are changed, the version number should also
// change, to force the browser to download new versions.
// configuration.add(SymbolConstants.APPLICATION_VERSION, "1.0-SNAPSHOT-DEV");


// override back the port to tomcat port in dev mod
configuration.override(SymbolConstants.HOSTPORT, "8080");

/*
* for AMD modules, caching should be disabled by default.
* The default value for the Cache-Control header in this case is:
* "max-age=60,must-revalidate". You can customize that by setting
*
*/

// FIXME must be disabled when deployed
// configuration.add(SymbolConstants.OMIT_EXPIRATION_CACHE_CONTROL_HEADER, "max-age=5,must-revalidate");
}
}

2. Add a new context param in your web.xml

<context-param>
<param-name>tapestry.Development-modules</param-name>
<param-value>net.mreunionlabs.kc.services.DevelopmentModule</param-value>
</context-param>

3. Add tapestry.execution-mode VM args with your Development module name (without the module part) to your jetty runner

And it work like a charm :)

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多