13 Replies Latest reply on Mar 27, 2007 10:34 PM by alteo

    lost parameter after login/forwarding

    henrik.lindberg

      I have implemented "search" in the general fashion of the "blog" seam example - and it works fine.
      When I added restriction that the user must be logged in to perform the search, the user (if not logged in) is redirected to the login page, and when logged in, the user is forwarded to the search result page.

      All fine so far... but -

      The searchPattern parameter was lost as the user was forwarded.
      Is this a problem with the login/forwarding mechanism, or should I do something different to make this work?

      To trigger the problem (have not tried this, but basically this is the difference in my code compared to the blog example).
      Put a restrict to user having the role 'user' in the pages.xml for /search-xhtml, and put a @Restritct on the factory method in SearchService that produces the searchResults.

        • 1. Re: lost parameter after login/forwarding
          thejavafreak

          What context are you using?

          • 2. Re: lost parameter after login/forwarding
            henrik.lindberg

            on which one of the involved things?

            • 3. Re: lost parameter after login/forwarding
              gavin.king

              You need to use redirect.captureCurrentView, redirect.returnToCapturedView.

              • 4. Re: lost parameter after login/forwarding
                henrik.lindberg

                I have that turned on already - I have this in components.xml (as described in the documentation for 1.2GA).

                (This is copied from my components.xml):

                 <event type="org.jboss.seam.notLoggedIn">
                 <action expression="#{redirect.captureCurrentView}"/>
                 </event>
                 <event type="org.jboss.seam.postAuthenticate">
                 <action expression="#{redirect.returnToCapturedView}"/>
                 </event>
                

                I do get redirected, but the searchPattern is lost in the transition.

                In pages.xml I have:
                 <page view-id="/object-view.xhtml">
                 <param name="oid" value="#{mockLoader.oid}"
                 converterId="javax.faces.Long" />
                 <restrict>#{s:hasRole('user')}</restrict>
                

                (same problem if I take out the ).

                And "mockLoader" looks like this:
                @Stateful
                @Name("mockLoader")
                public class MockLoaderAction implements Serializable, MockLoader
                {
                 private static final long serialVersionUID = 3773875067016207441L;
                
                 @In(required = false) @Out(required = false)
                 private InfoDigest mockInfo;
                
                 @PersistenceContext(type = EXTENDED)
                 private EntityManager em;
                
                 private Long m_oid;
                
                 public Long getOid()
                 {
                 return m_oid;
                 }
                
                 public void setOid(Long oid) throws OidNotFoundException
                 {
                 System.err.print("Reached MockLoader.setOid(oid=" +
                 ((oid == null) ? "null" : oid.toString()));
                
                 m_oid = oid;
                
                 // Load the object if there was a oid
                 if(oid == null)
                 throw new OidNotFoundException(oid);
                 mockInfo = em.find(MockData.class, oid);
                
                 // If not found throw exception
                 if(mockInfo == null)
                 throw new OidNotFoundException(oid);
                 }
                 @Remove @Destroy
                 public void destroy()
                 { }
                }
                

                And Local interface:
                public interface MockLoader
                {
                 public void destroy();
                 public Long getOid();
                 public void setOid(Long oid) throws OidNotFoundException;
                }
                


                • 5. Re: lost parameter after login/forwarding
                  henrik.lindberg

                  above the text "if I take out the..." should have been
                  if I take out the

                  <restrict> . . . </restrict>
                  


                  • 6. Re: lost parameter after login/forwarding
                    gavin.king

                    I don't see where you have defined the page parameter.

                    • 7. Re: lost parameter after login/forwarding
                      gavin.king

                      I see oid, I don't see searchPattern.

                      • 8. Re: lost parameter after login/forwarding
                        henrik.lindberg

                        Duh...

                        @Name("searchService")
                        public class SearchService
                        {
                         @In(create=true)
                         private EntityManager entityManager;
                        
                         private String m_searchPattern;
                        
                         @SuppressWarnings("unchecked")
                         @Factory("searchResults")
                         @Restrict("#{s:hasRole('user')}") // Forces check that user is logged in
                         public List<InfoDigest> getSearchResults()
                         {
                         if(m_searchPattern == null)
                         {
                         return null;
                         }
                         /* TODO replace with proper search */
                         return entityManager.createQuery("select i from MockData i where lower(i.name) like :searchPattern or lower(i.digest) like :searchPattern order by i.name")
                         .setParameter( "searchPattern", getSqlSearchPattern() )
                         .setMaxResults(100)
                         .getResultList();
                         }
                        
                         /**
                         * Turn a typical search patter using * for wildcard, and ? for single character
                         * into the SQL versions (% and _) of the same. Also addes SQL wildcard in the beginning and end
                         */
                         private String getSqlSearchPattern()
                         {
                         return m_searchPattern == null ? "" : '%' + m_searchPattern.toLowerCase().replace('*', '%').replace('?', '_') + '%';
                         }
                        
                         public String getSearchPattern()
                         {
                         return m_searchPattern;
                         }
                        
                         public void setSearchPattern(String searchPattern)
                         {
                         m_searchPattern = searchPattern;
                         }
                        }
                        


                        • 9. Re: lost parameter after login/forwarding
                          gavin.king

                          Yes but you need a line like:

                          <param name="searchPattern" value="#{searchService.searchPattern}"/>


                          • 10. Re: lost parameter after login/forwarding
                            henrik.lindberg

                            Thanks Gavin,
                            Yes, I have that already - from my pages.xml:

                             <!-- Search needs parameter (and restricted viewing) -->
                             <page view-id="/search.xhtml">
                             <param name="searchPattern" value="#{searchService.searchPattern}"/>
                             <restrict>#{s:hasRole('user')}</restrict>
                             </page>
                            


                            • 11. Re: lost parameter after login/forwarding
                              henrik.lindberg

                              Thanks Gavin,
                              Yes, I have that already - from my pages.xml:

                               <!-- Search needs parameter (and restricted viewing) -->
                               <page view-id="/search.xhtml">
                               <param name="searchPattern" value="#{searchService.searchPattern}"/>
                               <restrict>#{s:hasRole('user')}</restrict>
                               </page>
                              


                              • 12. Re: lost parameter after login/forwarding
                                gavin.king

                                Then I have no idea. Put the Seam source in your sourcepath and use your debugger. Debug into Redirect.captureCurrentView().

                                • 13. Re: lost parameter after login/forwarding
                                  alteo

                                  Gavin:
                                  I'm still facing the same problem after adding in the line in the pages.xml file.

                                  <param name="searchPattern" value="#{searchService.searchPattern}"/>


                                  Well, my problem is slightly different from hendrik. I used the seam generator to generate the template for my application and the generator automatically generate the search page. Everything works fine if I don't add in the security feature.

                                  I can't get to display the 2nd page or the next page. It always show the 1st page. Everything works fine if I take out the codes below from the pages.xml. Here's part of the codes in pages.xml
                                  <page view-id="/SuggestionsList.xhtml" login-required="true">
                                   <param name="searchPattern" value="#{searchService.searchPattern}"/>
                                   <restrict>#{s:hasRole('admin')}</restrict>
                                   </page>