Cannot get URL param on facebook redirect

Splash Forums PrettyFaces Users Cannot get URL param on facebook redirect

This topic contains 8 replies, has 3 voices, and was last updated by  Christian Kaltepoth 6 years, 2 months ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #17992

    fabiofrp
    Participant

    Hey guys,

    I’m new to prettyfaces so I don’t know if I’m doing something wrong…

    I have the following URL:

    http://localhost:8080/app/faces/Index.xhtml?shortURLcode=KTAGFaX&code=HQEzI3qAV8MEeTasFkdZy9dhMZHFzO3F9DvKY7IaSDw.eyJpdiI6Ikl6TnFtQ1pCWkVnbDZyalRnNTFQc2cifQ.n23DuzTG4bzoIp-P7fhF2RSFr-7iy9lKV_kJ0C9sRjHCVvCQjH3QxISF22r4cbydV1qhSPWaa_d23gg79q900a2irSamcabq87KakxqiDqihI

    Where the shortURLcode is generated on my application using some code like:

    String callbackURL = “/app/faces/Index.xhtml?shortURLcode=” + URLGenerator.getInstance().getRandomURLKey();

    String redirect_uri = “”;

    redirect_uri = “http://localhost:8080” + FacesContext.getCurrentInstance().getExternalContext().encodeResourceURL(callbackURL);

    return “https://graph.facebook.com/oauth/authorize?client_id=” +

    client_id + “&display=page&redirect_uri=” +

    redirect_uri +”&scope=”+ StringUtil.delimitObjectsToString(“,”, perms);

    and then facebook redirects to the callbackURL, appending the code parameter.

    I’m trying to having something like

    url/shortURLcode

    and I was able to accomplish this using the inbound url rewritting like this:

    <rewrite match=”?code=.+” substitute=”/” redirect=”301″ />

    but then I lose the reference to the code.. which I need to have stored somewhere..

    using url-mappings like the one below don’t seem to work..

    <url-mapping id=”rooms”>

    <pattern value=”/#{ shortURLcode }/#{ /\?code=.+/ code } ” />

    <view-id value=”/app/faces/Index.xhtml” />

    </url-mapping>

    Any suggestions? I’ve been stuck on this for hours :(

    #21152

    Try a custom rewrite Processor: http://ocpsoft.com/docs/prettyfaces/3.3.0/en-US/html_single/#inbound_rewriting.options

    You can create a class to do rewrites in Java, or, if you wait until the morning, I will show you how to use the currently upcoming and unreleased OCPSoft Rewrite library (with the understanding that it is very alpha, but already very stable.) You can try that out if you like. Hang tight.

    http://github.com/ocpsoft/rewrite

    #21153

    fabiofrp
    Participant

    Hi Lincoln, thanks for the fast reply.

    I tried to write my own rewrite processor but I had no success (btw, I think the documentation is a bit outdated on that right? I had to implement two methods, one for inbound rewrites and one for outbound, is this correct?)

    I’d be glad to try the new rewrite library if you show me how.

    Thanks

    #21154

    I don’t know for sure if I fully understand your usecase, but my guess is that you are looking for something like this:

    <rewrite match="/callback/(w+)?code=(w+)" substitute="/faces/Index.xhtml?shortURLcode=$1&code=$2" redirect="301" />

    This way requests like this:

    http://localhost/app/callback/abcdef?code=12345678

    will be redirected to:

    http://localhost/app/faces/Index.xhtml?shortURLcode=abcdef&code=12345678

    This way you won’t have to change your existing code as the incoming request is automatically redirected to the URL with the two query parameters.

    I hope this helps :)

    Christian

    #21155

    Sorry, there were some typos in my example. Just fixed them! :)

    #21156

    fabiofrp
    Participant

    Hi Christian,

    thanks for the post, but what I really want is a friendly url in the form of

    localhost/shortURLcode (e.g.: localhost/KTAGFaX)

    ..while at the same time store the code parameter (=HQEzI3qAV8MEeTasFkdZy9dhMZHFzO3F9DvKY7IaSDw…….) in the HTTP Servlet request parameter map or in a managed bean (and not show it on the url bar), so I can use it in the code for something else..

    Any hints? Thanks

    #21157

    Sorry for my late reply. I was very busy the last days.

    Did you find a solution for your problem?

    If not I suggest you to take a look at the custom rewrite processor to implement something like this. My understanding is that you want to create a shorter version of the ugly long code and use this for the callback that Facebook redirects the users to?

    You could try something like this (untested):

    <rewrite match="/shortcode/(w+)" processor="com.example.myapp.MyCustomProcessor" redirect="chain" />

    And the custom rewrite processor:

    public class MyCustomProcessor implements Processor {

    @Override
    public String processInbound(HttpServletRequest request, HttpServletResponse response, RewriteRule rule, String url) {

    // get the short code by parsing the URL
    String shortCode = getShortCodeFromUrl(url);

    // get the long version of the code
    String code = URLGenerator.getInstance().getCodeFromShortCode(shortCode);

    // forward to the new URL
    return "/faces/Index.xhtml?code="+code;

    }

    @Override
    public String processOutbound(HttpServletRequest request, HttpServletResponse response, RewriteRule rule, String url) {
    // do nothing
    return url;
    }

    }

    #21158

    fabiofrp
    Participant

    Hey Christian, thanks for the follow up on this.

    Actually, my problem was the following:

    I had an address of the following format:

    http://localhost:8080/app/faces/Index.xhtml?shortURLcode=KTAGFaX&code=HQEzI3qAV8MEeTasFkdZy9dhMZHFzO3F9DvKY7IaSDw.eyJpdiI6Ikl6TnFtQ1pCWkVnbDZyalRnNTFQc2cifQ.n23DuzTG4bzoIp-P7fhF2RSFr-7iy9lKV_kJ0C9sRjHCVvCQjH3QxISF22r4cbydV1qhSPWaa_d23gg79q900a2irSamcabq87KakxqiDqihI

    I wanted to get the “code” paramater and store it somewhere else.

    I wanted to get the “shortURLcode” parameter and make a pretty URL with it, of the format:

    localhost:8080/shortURLcode (e.g.: localhost:8080/KTAGFaX).

    I managed to do this using the following approach:

    1. Two rules on the prettyfaces config

    <url-mapping id=”rule1″>

    <pattern value=”/#{ shortURLcode }” />

    <view-id value=”/faces/Index.xhtml” />

    </url-mapping>

    <rewrite match=”code=(.+)” processor=”myapp.util.URLProcessor” redirect=”301″ />

    They have to be in this order, so first we have a “pretty” URL pointing to “/faces/Index.xhtml” and then we remove the “code” parameter of the url using…

    2. a custom processor

    public String processInbound(HttpServletRequest request, HttpServletResponse response, RewriteRule rule, String url) {

    String fbAuthCode = url.substring(url.indexOf(“=”) + 1, url.length());

    //System.out.println(“FB code: ” + fbAuthCode);

    // do whatever we want with fb auth code…

    //remove the parameter part appended by facebook

    String returnUrl = url.substring(0, url.indexOf(“?”));

    //return only the pretty url generated by the first rule.

    return returnUrl;

    }

    Thanks a lot for the help, keep up the good work :)

    #21159

    You’re welcome! Great to hear you got it working!

    Feel free to post again if you have any questions or ideas for improving PrettyFaces! :)

    Christian

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.

Comments are closed.