You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by "Robert J. Walker" <rw...@mshare.net> on 2006/12/19 19:23:15 UTC

External page URL change in Tapestry 4

I'm upgrading our existing applications from Tapestry 3.1 to Tapestry 4.1. One problem we have is that we send emails to our users with links to an IExternalPage. The URL format for this has changed in Tapestry 4, so when we release, all the links in emails sent before the release will be broken.

I imagine that it must be possible to set up an URL encoder to translate the old URLs to the new ones. I also imagine that someone else out there must have already encountered this and written one, but I've been unable to find any such thing. I'd rather not re-invent the wheel if the solution already exists.

Robert J. Walker


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


RE: External page URL change in Tapestry 4

Posted by "Robert J. Walker" <rw...@mshare.net>.
Well, in the meantime I read up more on ServiceEncoders and came to the conclusion that it shouldn't be too tough, so I went ahead and wrote one that seems to work fine. It was almost done by the time I got your post. The thing I like about this solution is that it doesn't require another library. :) I include the code below for anybody who's interested.

Caveats: 1) It assumes /app instead of whatever your application might be using, 2) it's designed to work only for one page per instance, rather than all external pages in your app, and 3) it requires Java 1.5 (because of the for loop construct). All of these things could be changed easily enough. Obviously, I hope it's useful, but no warranty, yada yada yada.

Robert

=====

package mshare.web.tapestry.page;

import org.apache.tapestry.engine.ServiceEncoder;
import org.apache.tapestry.engine.ServiceEncoding;
import org.apache.tapestry.services.ServiceConstants;

/**
 * Decodes old Tapestry 3 external page URLs and converts them to Tapestry 4 URLs. It does not
 * re-encode them back to Tapestry 3 URLs, since this is only here to provide backward support for
 * the Tapestry 3 URLs.
 * @author Robert J. Walker
 */
public class Tap3ExternalPageEncoder implements ServiceEncoder {
	private static final String TAP_3_PARAM_NAME = "service";
	private static final String EXTERNAL_SERVICE = "external";
	private static final String SERVICE_PARAMETER = "sp";

	private String pageName;

	public void decode(ServiceEncoding encoding) {
		String tap3Param = encoding.getParameterValue(TAP_3_PARAM_NAME);

		// No service parameter?
		if(tap3Param == null) {
			return;
		}

		String[] arr = tap3Param.split("/");

		// Serivce name has a slash?
		if(arr.length != 2) {
			return;
		}

		// It's the external service?
		if(!EXTERNAL_SERVICE.equals(arr[0])) {
			return;
		}

		// Right page?
		if(!arr[1].equals(pageName)) {
			return;
		}

		// Update servlet path
		StringBuilder path = new StringBuilder("/app?");
		path.append(ServiceConstants.SERVICE)
			.append('=')
			.append(EXTERNAL_SERVICE)
			.append('&')
			.append(ServiceConstants.PAGE)
			.append('=')
			.append(pageName);

		for(String param : encoding.getParameterValues(SERVICE_PARAMETER)) {
			path.append("&sp=").append(param);
		}

		encoding.setServletPath(path.toString());

		// Update parameter values
		encoding.setParameterValue(ServiceConstants.SERVICE, EXTERNAL_SERVICE);
		encoding.setParameterValue(ServiceConstants.PAGE, pageName);
	}

	public void encode(ServiceEncoding encoding) {
		// Do nothing here; we don't want to encode the URLs back to Tapestry 3 style
	}

	public void setPageName(String pageName) {
		this.pageName = pageName;
	}
}


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: External page URL change in Tapestry 4

Posted by Martin Strand <do...@gmail.com>.
Hi Robert.
I solved this by using the UrlRewriteFilter:
http://tuckey.org/urlrewrite/

Since you don't need to generate the old urls anymore, a custom service  
encoder isn't really necessary and the UrlRewriteFilter will probably be  
sufficient.




You can even unit test the rewrite rules. :)
Something like this:

	private static final String CONF_FILE =  
"src/main/webapp/WEB-INF/urlrewrite.xml";
	private MockResponse response;
	private UrlRewriter urlRewriter;

	public UrlRewriteTest()
		throws FileNotFoundException
	{
		response = new MockResponse();
		Conf conf = new Conf(new FileInputStream(CONF_FILE), null);
		conf.initialise();
		urlRewriter = new UrlRewriter(conf);
	}

	@Test(dataProvider = "rewrite")
	public void rewrite(String in, String expected)
	{
		MockRequest request = new MockRequest(in);
		RewrittenUrl rewrittenRequest = urlRewriter.processRequest(request,  
response);

		assert (rewrittenRequest != null);
		assert (rewrittenRequest.isForward());
		assert (rewrittenRequest.getTarget().equals(expected));
	}

	@DataProvider(name = "rewrite")
	public Object[][] getRewriteData()
		throws IOException
	{
[loads parameters from *.properties file]
	}



Martin

On Tue, 19 Dec 2006 19:23:15 +0100, Robert J. Walker <rw...@mshare.net>  
wrote:

> I'm upgrading our existing applications from Tapestry 3.1 to Tapestry  
> 4.1. One problem we have is that we send emails to our users with links  
> to an IExternalPage. The URL format for this has changed in Tapestry 4,  
> so when we release, all the links in emails sent before the release will  
> be broken.
>
> I imagine that it must be possible to set up an URL encoder to translate  
> the old URLs to the new ones. I also imagine that someone else out there  
> must have already encountered this and written one, but I've been unable  
> to find any such thing. I'd rather not re-invent the wheel if the  
> solution already exists.
>
> Robert J. Walker

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org