View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.profiler.rules.impl;
18  
19  import java.security.Principal;
20  import java.util.Iterator;
21  
22  import javax.security.auth.Subject;
23  
24  import org.apache.jetspeed.profiler.rules.RuleCriterion;
25  import org.apache.jetspeed.profiler.rules.RuleCriterionResolver;
26  import org.apache.jetspeed.request.RequestContext;
27  import org.apache.jetspeed.security.SecurityHelper;
28  
29  /***
30   * Standard Jetspeed-1 style resolver for criterion.
31   * It first looks at the value in the request parameters.
32   * If it is null, it then falls back to the criterion record..
33   * If it is null it gives up and returns null allowing subclasses
34   * to continue processing.
35   *
36   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
37   * @version $Id: StandardResolver.java 516448 2007-03-09 16:25:47Z ate $
38   */
39  public class StandardResolver implements RuleCriterionResolver
40  {
41      public static final String VALUE_DELIMITER = ",";
42      public static final String COMBO_DELIMITER = "-";
43      
44      /* (non-Javadoc)
45       * @see org.apache.jetspeed.profiler.rules.RuleCriterionResolver#resolve(org.apache.jetspeed.request.RequestContext, org.apache.jetspeed.profiler.rules.RuleCriterion)
46       */
47      public String resolve(RequestContext context, RuleCriterion criterion)
48      {
49          String value = context.getRequestParameter(criterion.getName());
50          if (value == null)
51          {
52              value = criterion.getValue();
53          }
54          return value;            
55      }
56  
57      /* (non-Javadoc)
58       * @see org.apache.jetspeed.profiler.rules.RuleCriterionResolver#isControl()
59       */
60      public boolean isControl(RuleCriterion criterion)
61      {
62          if (criterion.getName().equals(RuleCriterionResolver.PATH) ||
63              criterion.getName().equals(RuleCriterionResolver.PAGE))
64          {
65              return false;
66          }
67          return true;
68      }
69  
70      public boolean isNavigation(RuleCriterion criterion)
71      {
72          return false;
73      }
74      
75      protected String resolvePrincipals(RequestContext context, RuleCriterion criterion, Subject subject, Class classe)
76      {
77          StringBuffer result = new StringBuffer();
78          Iterator principals = SecurityHelper.getPrincipals(subject, classe).iterator();
79          int count = 0;
80          while (principals.hasNext())
81          {
82              Principal principal = (Principal)principals.next();
83              if (count > 0)
84              {
85                  result.append(VALUE_DELIMITER);
86              }
87              result.append(principal.getName());
88              count++;
89          }
90          if (count == 0)
91          {
92              return null;
93          }
94          return result.toString();        
95      }
96  
97      protected String combinePrincipals(RequestContext context, RuleCriterion criterion, Subject subject, Class classe)
98      {
99          StringBuffer result = new StringBuffer();
100         Iterator principals = SecurityHelper.getPrincipals(subject, classe).iterator();
101         int count = 0;
102         while (principals.hasNext())
103         {
104             Principal principal = (Principal)principals.next();
105             if (count > 0)
106             {
107                 result.append(COMBO_DELIMITER);
108             }
109             result.append(principal.getName());
110             count++;
111         }
112         if (count == 0)
113         {
114             return null;
115         }
116         return result.toString();        
117     }
118     
119 }