Coverage Report - nu.localhost.tapestry.acegi.components.IfLoggedIn
 
Classes in this File Line Coverage Branch Coverage Complexity
IfLoggedIn
0%
0/20
0%
0/5
0
 
 1  
 /*
 2  
  * Copyright 2007 Robin Helgelin
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  0
  */
 16  0
 
 17  
 package nu.localhost.tapestry.acegi.components;
 18  
 
 19  
 import java.security.Principal;
 20  
 
 21  
 import org.apache.tapestry.Block;
 22  
 import org.apache.tapestry.annotations.Parameter;
 23  
 import org.apache.tapestry.ioc.annotations.Inject;
 24  
 import org.apache.tapestry.services.RequestGlobals;
 25  
 
 26  
 /**
 27  
  * Render it's body depending whether the user is logged in or not.
 28  
  * 
 29  
  * @author Robin Helgelin
 30  
  * @author Tapestry Project (doc comments)
 31  
  */
 32  0
 public class IfLoggedIn {
 33  
     /**
 34  0
      * Optional parameter to invert the test. If true, then the body is rendered when the test
 35  0
      * parameter is false (not true).
 36  0
      */
 37  
     @Parameter
 38  
     private boolean negate;
 39  
 
 40  
     /**
 41  
      * An alternate {@link Block} to render if the test parameter is false. The default, null, means
 42  
      * render nothing in that situation.
 43  
      */
 44  0
     @Parameter(name = "else")
 45  0
     private Block elseBlock;
 46  
 
 47  0
     @Inject
 48  
     private RequestGlobals requestGlobals;
 49  
     
 50  
     private boolean test() {
 51  0
         Principal principal = requestGlobals.getHTTPServletRequest().getUserPrincipal();
 52  0
         return principal != null && principal.getName() != "";
 53  
     }
 54  
 
 55  
     /**
 56  0
      * Returns null if the test method returns true, which allows normal rendering (of the body). If
 57  
      * the test parameter is false, returns the else parameter (this may also be null).
 58  
      */
 59  
     Object beginRender() {
 60  0
         if (test() != negate) {
 61  0
             return null;
 62  0
         } else {
 63  0
             return elseBlock;
 64  
         }
 65  
     }
 66  
 
 67  
     /**
 68  
      * If the test method returns true, then the body is rendered, otherwise not. The component does
 69  
      * not have a template or do any other rendering besides its body.
 70  
      */
 71  
     boolean beforeRenderBody() {
 72  0
         return test() != negate;
 73  
     }
 74  
 
 75  
     void setup(String role, boolean negate, Block elseBlock) {
 76  0
         this.negate = negate;
 77  0
         this.elseBlock = elseBlock;
 78  0
     }
 79  
 }