This project has retired. For details please refer to its Attic page.
AbstractBaseAttributeRenderer xref
View Javadoc

1   /*
2    * $Id: AbstractBaseAttributeRenderer.java 791161 2009-07-04 18:53:36Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.tiles.renderer.impl;
22  
23  import java.io.IOException;
24  import java.util.Iterator;
25  import java.util.Set;
26  
27  import org.apache.tiles.Attribute;
28  import org.apache.tiles.TilesApplicationContext;
29  import org.apache.tiles.awareness.TilesApplicationContextAware;
30  import org.apache.tiles.awareness.TilesRequestContextFactoryAware;
31  import org.apache.tiles.context.TilesRequestContext;
32  import org.apache.tiles.context.TilesRequestContextFactory;
33  import org.apache.tiles.evaluator.AttributeEvaluator;
34  import org.apache.tiles.evaluator.AttributeEvaluatorFactory;
35  import org.apache.tiles.evaluator.AttributeEvaluatorFactoryAware;
36  import org.apache.tiles.renderer.AttributeRenderer;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  /***
41   * Base abstract class that manages authorization to display the attribute.
42   *
43   * @version $Rev: 791161 $ $Date: 2009-07-04 20:53:36 +0200 (sab, 04 lug 2009) $
44   * @since 2.1.0
45   */
46  public abstract class AbstractBaseAttributeRenderer implements
47          AttributeRenderer, TilesRequestContextFactoryAware,
48          TilesApplicationContextAware, AttributeEvaluatorFactoryAware {
49  
50      /***
51       * The logging object.
52       */
53      private final Logger log = LoggerFactory
54              .getLogger(AbstractBaseAttributeRenderer.class);
55  
56      /***
57       * The Tiles request context factory.
58       *
59       * @since 2.1.1
60       */
61      protected TilesRequestContextFactory contextFactory;
62  
63      /***
64       * The Tiles application context.
65       *
66       * @since 2.1.0
67       */
68      protected TilesApplicationContext applicationContext;
69  
70      /***
71       * The attribute evaluator factory.
72       *
73       * @since 2.2.0
74       */
75      protected AttributeEvaluatorFactory attributeEvaluatorFactory;
76  
77      /*** {@inheritDoc} */
78      public void setRequestContextFactory(TilesRequestContextFactory contextFactory) {
79          this.contextFactory = contextFactory;
80      }
81  
82      /*** {@inheritDoc} */
83      public void setApplicationContext(TilesApplicationContext applicationContext) {
84          this.applicationContext = applicationContext;
85      }
86  
87      /*** {@inheritDoc} */
88      public void setAttributeEvaluatorFactory(AttributeEvaluatorFactory attributeEvaluatorFactory) {
89          this.attributeEvaluatorFactory = attributeEvaluatorFactory;
90      }
91  
92      /*** {@inheritDoc} */
93      public void render(Attribute attribute, TilesRequestContext request) throws IOException {
94          if (!isPermitted(request, attribute.getRoles())) {
95              if (log.isDebugEnabled()) {
96                  log.debug("Access to attribute denied.  User not in role '"
97                          + attribute.getRoles() + "'");
98              }
99              return;
100         }
101 
102         AttributeEvaluator evaluator = attributeEvaluatorFactory
103                 .getAttributeEvaluator(attribute);
104         Object value = evaluator.evaluate(attribute, request);
105 
106         write(value, attribute, request);
107     }
108 
109     /***
110      * Implement this method knowing that the attribute won't be null and it
111      * will be authorized.
112      * @param value The value of the attribute to be rendered.
113      * @param attribute The attribute to render.
114      * @param request The Tiles request object.
115      * @throws IOException If something goes wrong during rendition.
116      * @since 2.1.2
117      */
118     public abstract void write(Object value, Attribute attribute,
119             TilesRequestContext request)
120             throws IOException;
121 
122     /***
123      * Creates a Tiles request context from request items.
124      *
125      * @param requestItems The request items.
126      * @return The created Tiles request context.
127      * @since 2.1.0
128      */
129     protected TilesRequestContext getRequestContext(Object... requestItems) {
130         return contextFactory.createRequestContext(applicationContext,
131                 requestItems);
132     }
133 
134     /***
135      * Checks if the current user is in one of the comma-separated roles
136      * specified in the <code>role</code> parameter.
137      *
138      * @param request The request context.
139      * @param roles The list of roles.
140      * @return <code>true</code> if the current user is in one of those roles.
141      * @since 2.1.0
142      */
143     protected boolean isPermitted(TilesRequestContext request, Set<String> roles) {
144         if (roles == null || roles.isEmpty()) {
145             return true;
146         }
147 
148         boolean retValue = false;
149 
150         for (Iterator<String> roleIt = roles.iterator(); roleIt.hasNext()
151                 && !retValue;) {
152             retValue = request.isUserInRole(roleIt.next());
153         }
154 
155         return retValue;
156     }
157 }