KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > applications > filters > PortalServletRequest


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23 package org.infoglue.deliver.applications.filters;
24
25 import java.security.Principal JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.infoglue.cms.security.InfoGluePrincipal;
40 import org.infoglue.cms.security.InfoGlueRole;
41 import org.infoglue.deliver.portal.PathParser;
42 import org.infoglue.deliver.portal.PortalControlURL;
43
44 public class PortalServletRequest extends HttpServletRequestWrapper JavaDoc
45 {
46     private static final Log log = LogFactory.getLog(PortalServletRequest.class);
47
48     public static final String JavaDoc MULTI_VALUE = PathParser.MULTI_VALUE;
49
50     private Map JavaDoc paramMap;
51     private Principal JavaDoc principal = null;
52     
53     /**
54      * @param req
55      */

56     
57     public PortalServletRequest(HttpServletRequest JavaDoc req)
58     {
59         super(req);
60         paramMap = PathParser.copyParameters(req.getParameterMap());
61         
62         // Extend parameter map with infoglue parameters.
63
// TODO paramMap should be immutable
64
paramMap.putAll(PathParser.parsePathParameters(PortalControlURL.IG, req.getServletPath(), false));
65
66         if (log.isDebugEnabled())
67         {
68             StringBuffer JavaDoc str = new StringBuffer JavaDoc();
69             for (Iterator JavaDoc it = paramMap.keySet().iterator(); it.hasNext();)
70             {
71                 String JavaDoc name = (String JavaDoc) it.next();
72                 str.append(name);
73                 str.append(": ");
74                 str.append(Arrays.asList((String JavaDoc[]) paramMap.get(name)));
75                 if (it.hasNext())
76                     str.append(", ");
77             }
78             log.debug("Available params: " + str);
79         }
80         
81         this.principal = (InfoGluePrincipal)req.getSession().getAttribute("infogluePrincipal");
82         if(req.getUserPrincipal() != null)
83             this.principal = req.getUserPrincipal();
84     }
85     
86
87
88     /* (non-Javadoc)
89      * @see javax.servlet.ServletRequest#getParameterMap()
90      */

91     
92     public Map JavaDoc getParameterMap()
93     {
94         return paramMap;
95     }
96
97     /* (non-Javadoc)
98      * @see javax.servlet.ServletRequest#getParameter(java.lang.String)
99      */

100     
101     public String JavaDoc getParameter(String JavaDoc name)
102     {
103         String JavaDoc[] values = (String JavaDoc[]) paramMap.get(name);
104         if (values != null && values.length > 0)
105         {
106             return values[0];
107         }
108     
109         return null;
110     }
111
112     /* (non-Javadoc)
113      * @see javax.servlet.ServletRequest#getParameterNames()
114      */

115     
116     public Enumeration JavaDoc getParameterNames()
117     {
118         return Collections.enumeration(paramMap.keySet());
119     }
120
121     /* (non-Javadoc)
122      * @see javax.servlet.ServletRequest#getParameterValues(java.lang.String)
123      */

124     
125     public String JavaDoc[] getParameterValues(String JavaDoc name)
126     {
127         return (String JavaDoc[]) paramMap.get(name);
128     }
129     
130     public String JavaDoc getRemoteUser()
131     {
132         return (this.principal == null ? null : this.principal.getName());
133     }
134     
135     public boolean isUserInRole(String JavaDoc role)
136     {
137         boolean isUserInRole = false;
138         
139         if(this.principal != null)
140         {
141             if(this.principal instanceof InfoGluePrincipal)
142             {
143                 List JavaDoc roles = ((InfoGluePrincipal)this.principal).getRoles();
144                 Iterator JavaDoc i = roles.iterator();
145                 
146                 while(i.hasNext())
147                 {
148                     InfoGlueRole currentRole = (InfoGlueRole)i.next();
149                     if(currentRole.getName().equals(role))
150                     {
151                         isUserInRole = true;
152                         break;
153                     }
154                 }
155             }
156             else
157             {
158                 isUserInRole = super.isUserInRole(role);
159             }
160         }
161         
162         return isUserInRole;
163     }
164     
165     public Principal JavaDoc getUserPrincipal()
166     {
167         return this.principal;
168     }
169 }
Popular Tags