Hi, Richard!

When passing information from JavaScript to a page or component class, I avoid the the encoding problem by using query parameters (which are decoded normally instead of using the Tapestry decoder). With the new @RequestParameter and @ActivationRequestParameter annotations this is even easier.

On Thu, 06 Jan 2011 09:37:35 -0200, Richard Hill <r...@su3analytics.com> wrote:


Hi Taha,

Thanks for the response. The latter suggestion doesn't work for me,
these urls are external urls input by the user. I tried the first
suggestion but it does not work. Not sure exactly what all the issues
are, but I note that what encodeURIComponent encodes and what Tapestry
encodes are not consistent (e.g tapestry doesn't encode a ":" whereas
encodeURIComponent does). Also Tapestry converts a "$" to "$$", and
Tapestry's hex is lower-case, whereas encodeURIComponent is upper-cased.
There may be other differences too.

So I have instead ported the tapestry service method
URLEncoderImpl.encode(String input) to javascript. I haven't tested on a
huge number of urls, but it seems to work. I have though tested the hex
encoding against the main pertinent characters (!, &, ?, %,
~, ; /, ....) and they seem to all tally. For posterity this is the
javascript:


_SU3.tapestryEncodeUri = function(uri) {
        
        var ENCODED_NULL = "$N";
        var ENCODED_BLANK = "$B";
        
        if (uri == null) return ENCODED_NULL;
        if (uri == "") return ENCODED_BLANK;
        
        var dirty = false;
        
        var encodedUri = "";
        
        var safe = new RegExp(/[a-zA-Z0-9\-_\.:]/);
        
        for (var i = 0; i < uri.length; i++) {
                
                var ch = uri.charAt(i);
                
                if (ch == '$') {
                        encodedUri = encodedUri + "$$";
                        dirty = true;
                        continue;
                }
                
                if (ch.match(safe)) {
                        encodedUri = encodedUri + ch;
                        continue;
                }
                
                var encodedCh = _SU3.tapestryEncodeChar(ch);
                
                encodedUri = encodedUri + encodedCh;
                        
                dirty = true;
        }
        
        return dirty ? encodedUri : uri;
        
};


_SU3.tapestryEncodeChar = function(char) {
        
        var chAsInt = char.charCodeAt(0);
        
        var hex = chAsInt.toString(16);
        
        var encodedCh;
        
        if (hex.length == 2) {
                encodedCh = "$00" + hex;
        } else if (hex.length == 3) {
                encodedCh = "$0" + hex;
        } else if (hex.length == 1) {
                encodedCh = "$000" + hex;
        } else {
                encodedCh = "$" + hex;
        }
        
        return encodedCh;
};



Cheers, Richard.



On Wed, 2011-01-05 at 11:31 +0530, Taha Hafeez wrote:
Hi Richard

There are two ways of doing it...

1. after you have applied encodeURIComponent() replace '%' by '$00' (ensure
that the converted number is 4 digit)
2. Use ?name=value instead of '/' and use request.getParameter() or
@RequestParameter

regards
Taha


On Tue, Jan 4, 2011 at 11:26 PM, Richard Hill <r...@su3analytics.com> wrote:

>
> Ah, apologies - I thought the threading was based on the subject line.
>
> R.
>
>
>
> On Tue, 2011-01-04 at 15:52 -0200, Thiago H. de Paula Figueiredo wrote:
> > Hi!
> >
> > Please post new discussions as new messages instead of replying to
> > existing ones. Every threaded visualization of the mailing list will show
> > your message in the wrong place. ;)
> >
> > On Tue, 04 Jan 2011 15:24:28 -0200, Richard Hill <r...@su3analytics.com>
> > wrote:
> >
> >
> > > Happy New Year All,
> > >
> > > I'm attempting to pass an url in an ajax request to an onEvent handler.
> > > So I'm making a request like:
> > >
> > > GET /MyPage.MyComponent:event/http://blah.com/....
> > >
> > > MyComponent.java:
> > >
> > > public StreamResponse onEvent(String url) {
> > >     ....
> > > }
> > >
> > >
> > > The problem is that what's being passed to onEvent() is just "http:"
> > >
> > > Now I realise that "/" is the context delimiter. However I've tried
> > > encoding the url with escape(url), encodeURI(url), and
> > > encodeURIComponent(url) (the latter 2 encode the "/") but to no avail -
> > > same issue.
> > >
> > > I also realise that Tapestry employs it's own encoding scheme - do I
> > > have to encode in this way? If so, does anyone have some code to
> > > implement Tapestry url encoding in javascript?
> > >
> > > Many thanks,
> > >
> > > Richard.
> > >
> > >
> > >
> > >
> > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
> > > For additional commands, e-mail: users-h...@tapestry.apache.org
> > >
> >
> >
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
> For additional commands, e-mail: users-h...@tapestry.apache.org
>
>



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



--
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer, and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
Consultor, desenvolvedor e instrutor em Java, Tapestry e Hibernate
Coordenador e professor da Especialização em Engenharia de Software com Ênfase em Java da Faculdade Pitágoras
http://www.arsmachina.com.br

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

Reply via email to