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.portlets.security;
18  
19  import java.io.NotSerializableException;
20  import java.security.Principal;
21  import java.util.ArrayList;
22  import java.util.Iterator;
23  
24  import javax.portlet.PortletRequest;
25  import javax.portlet.RenderRequest;
26  import javax.security.auth.Subject;
27  import javax.servlet.http.HttpServletRequest;
28  
29  import org.apache.jetspeed.PortalReservedParameters;
30  import org.apache.jetspeed.request.RequestContext;
31  import org.apache.portals.gems.browser.BrowserPortlet;
32  import org.apache.portals.messaging.PortletMessaging;
33  
34  
35  /***
36   * Abstract Security Browser - factored out common functionality for security browsers 
37   * 
38   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
39   * @version $Id: SecurityUtil.java 348264 2005-11-22 22:06:45Z taylor $
40   */
41  public abstract class SecurityUtil extends BrowserPortlet
42  {    
43          
44      public static Principal getPrincipal(Subject subject, Class classe)
45      {
46          Principal principal = null;
47          Iterator principals = subject.getPrincipals().iterator();
48          while (principals.hasNext())
49          {
50              Principal p = (Principal) principals.next();
51              if (classe.isInstance(p))
52              {
53                  principal = p;
54                  break;
55              }
56          }
57          return principal;
58      }
59  
60      public static boolean isEmpty(String s)
61      {
62          if (s == null) return true;
63          
64          if (s.trim().equals("")) return true;
65          
66          return false;
67      }
68  
69      public static String getAbsoluteUrl(RenderRequest renderRequest, String relativePath)
70      {
71          RequestContext requestContext = (RequestContext) renderRequest.getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE);
72          HttpServletRequest request = requestContext.getRequest();
73          StringBuffer path = new StringBuffer();
74          if ( !requestContext.getPortalURL().isRelativeOnly() )
75          {
76              path.append(request.getScheme()).append("://").append(request.getServerName()).append(":").append(request.getServerPort());
77          }
78          return requestContext.getResponse().encodeURL(path.append(request.getContextPath()).append(request.getServletPath()).append(
79                  relativePath).toString());
80      }
81      
82      public static void publishErrorMessage(PortletRequest request, String message)
83      {
84          try
85          {
86              ArrayList errors = (ArrayList)PortletMessaging.receive(request,SecurityResources.ERROR_MESSAGES);
87              if ( errors == null )
88              {
89                  errors = new ArrayList();
90              }
91              errors.add(message);
92              PortletMessaging.publish(request, SecurityResources.ERROR_MESSAGES, errors);
93          }
94          catch (NotSerializableException e)
95          {
96              // TODO Auto-generated catch block
97              e.printStackTrace();
98          }                
99      }
100     
101     public static void publishErrorMessage(PortletRequest request, String topic, String message)
102     {
103         try
104         {
105             ArrayList errors = (ArrayList)PortletMessaging.receive(request,topic,SecurityResources.ERROR_MESSAGES);
106             if ( errors == null )
107             {
108                 errors = new ArrayList();
109             }
110             errors.add(message);
111             PortletMessaging.publish(request, topic, SecurityResources.ERROR_MESSAGES, errors);
112         }
113         catch (NotSerializableException e)
114         {
115             // TODO Auto-generated catch block
116             e.printStackTrace();
117         }                
118     }
119 }