10 Replies Latest reply on Mar 12, 2008 10:13 PM by pete007

    Is there an equivalent to mod_rewrite in seam

      Hello,


      some of my forms depend on the location of the client.


      When I call the application the first time, I can give the location in as a parameter.


      But if I store it in the session, the session can expire and the parameter is lost. So I thought about using an URL prefix for my application.


      Instead of calling:


      http://myhost/gdc/form.seam



      I want to use:


      http://myhost/gdc/PARAMETER/form.seam



      and have the value of PARAMETER accessed in my application.


      Is there a way to do this in seam?


      ..


      Otherwise I thought about saving the IP addresses in the DB and selecting the value dependent on the IP.


      How can I read the clients IP address from within a form?


      ..


      Are there even better solutions for my task?


      Thanks in advance, Peter

        • 1. Re: Is there an equivalent to mod_rewrite in seam
          damianharvey.damianharvey.gmail.com

          Have a look at UrlRewrite which is bundled with Seam. You should be able to change your URL from http://myhost/gdc/form.seam?parameter=PARAMETER into what you require (or something very close to it).


          Cheers,


          Damian.

          • 2. Re: Is there an equivalent to mod_rewrite in seam

            This sounds wonderful, thanks a lot! :D


            I only have some trouble to get it to work.
            I have added this blocks to the beginning of web.xml:


                <!-- URL Rewrite -->
            
                <filter>
                  <filter-name>UrlRewriteFilter</filter-name>
                  <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
                </filter>
                <filter-mapping>
                  <filter-name>UrlRewriteFilter</filter-name>
                  <url-pattern>/*</url-pattern>
                </filter-mapping>



            Then I created my urlrewrite.xml:



            <?xml version="1.0" encoding="utf-8"?>
            <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN" "http://tuckey.org/res/dtds/urlrewrite3.0.dtd">
            <urlrewrite>
              <rule>
                <from>^/gdc/([A-Za-z0-9]+)/([*]+)$</from>
                <to>/gdc/$2?start=$1</to>
              </rule>
            </urlrewrite>



            So far, so good, but on deploy I get the following Exception:



            11:36:56,437 INFO  [EARDeployer] Init J2EE application: file:/E:/qi/installs/jboss/server/default/deploy/gdc.ear
            11:36:58,640 WARN  [SessionFactoryObjectFactory] InitialContext did not implement EventContext
            11:37:00,125 INFO  [ServletContextListener] Welcome to Seam 2.0.0.GA
            11:37:02,515 WARN  [Initialization] Did not install PojoCache due to NoClassDefFoundError: org/jgroups/MembershipListener
            11:37:03,406 ERROR [[/gdc]] Exception starting filter UrlRewriteFilter
            java.lang.ClassNotFoundException: org.tuckey.web.filters.urlrewrite.UrlRewriteFilter
                    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1358)
                    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1204)
                    ...



            Then after some thinking I found the urlrewritefilter.jar in my project/lib-directory and copied it to the jboss/server/default/deploy/lib-directory, and it deployed without error.


            Now I only need the right syntax to get it to work.


            I tried to enter the following URL in the new format:


            http://localhost:8080/gdc/roomsbyname.seam?start=Terminal1



            http://localhost:8080/gdc/Terminal1/roomsbyname.seam



            But I only get an 404 with the message:


            The requested resource (/gdc/Terminal1/roomsbyname.seam) is not available.



            What have I forgotten? On the homepage, something is written that I have to put the Filter before my ServletMappings?!? Do I have any? I put the Filter as the first element.


            So, how can I debug, if the filter does anything at all?

            • 3. Re: Is there an equivalent to mod_rewrite in seam

              UPDATE: I changed the rule to


              <rule>
                <from>^/([^/]*)/(.*)$</from>
                <to type="redirect">/gis47user/$2?start=$1</to>
              </rule>



              Now this page


              http://localhost:8080/gis47user/Terminal1/roomsbyname.seam



              gets redirected to


              http://localhost:8080/gis47user/roomsbyname.seam?start=Terminal1



              but I need the URL to stay as it is.


              I removed type="redirect" to use the forward-type, but this does not work.


              Has anybody an idea??


              Can I define the parameter start to be used an passed along in all my forms with some rule in my pages.xml??

              • 4. Re: Is there an equivalent to mod_rewrite in seam
                norman

                Several of the Seam examples (wiki, dvdstore, seambay) use URLRewrite.  I'd suggest starting there to get an idea how to write the rules.


                As a side note, Seam 2.1 will have a much simpler way to specify rewrite rules that work both for incoming and outgoing rewrites.

                • 5. Re: Is there an equivalent to mod_rewrite in seam

                  Ah, looking forward to Seam 2.1, simpler is always good.


                  Okay, I started there, got the same idea as before, but this time I realised, what went wrong, thanks for the hint!


                  The real problem is, hopefully:


                  How do I reference an image placed in the root deployer?

                  Previously, I used in my forms images like:


                  ../images/mainmenu_rooms.png



                  Now I need something like:


                  ../images/mainmenu_rooms.png for the normal stuff
                  ../../images/mainmenu_rooms.png for the rewrite stuff



                  So, the little html programmer inside me wanted to use:


                  /images/mainmenu_rooms.png



                  But this gets converted to:


                  /gdc/images/mainmenu_rooms.png



                  because it is starting with a slash.


                  btw, wouldnt it be good to have a parameter for using the img-filename as htmlAltValue, at least in the debug mode?


                  And how do I highlight text inside of code areas?

                  • 6. Re: Is there an equivalent to mod_rewrite in seam
                    norman

                    I don't have any experience with trying to use urlrewrite with URLs outside of the context-root of the web application. It's definitely not a path I'd want to go down.

                    • 7. Re: Is there an equivalent to mod_rewrite in seam

                      Me neither, but I have to generate new images, so I cannot put them in the ear file or anywhere else, right?


                      Sorry, maybe I formulated it a little bit misunderstandable. The urlrewrite works. This problem is new and completely without urlrewrite.


                      I have for example a simple image:


                      http://localhost:8080/images/simple.png



                      and I can hardcode it into my application with:


                      <h:graphicImage value="http://localhost:8080/images/simple.png"/>



                      This works great on my development machine, but when I deploy it to a production machine, it won't work.


                      But when I try to use it without the hostpart, something is prepending the project name:


                      <h:graphicImage value="/images/simple.png"/>



                      so the link in the html is like:


                      http://localhost:8080/gdc/images/simple.png



                      Can I turn off this behaviour by a page filter or something?

                      • 8. Re: Is there an equivalent to mod_rewrite in seam

                        Now I found a way to refer to files in the images folder of my server.


                        HACK: he-he


                        <h:graphicImage value="/../images/simple.png"/>



                        gets parsed as:


                        <img src="/gis47user/../images/simple.png" alt="" />



                        tested with ie and ff


                        good? bad?


                        What would be the correct way? Is there any?


                        Greetings, Pete

                        • 9. Re: Is there an equivalent to mod_rewrite in seam
                          davidfed

                          You could add this to your components.xml:



                             <factory name="rootPath" value="#{facesContext.externalContext.request.scheme}://#{facesContext.externalContext.request.serverName}:#{facesContext.externalContext.request.serverPort}"/>
                          



                          and then use it like this:


                           <h:graphicImage value="#{rootPath}/images/simple.png"/>
                          


                          • 10. Re: Is there an equivalent to mod_rewrite in seam

                            Hello, this works well, and I can use it even from forms in subdirectories. Thank you!