PageRenderTime 41ms CodeModel.GetById 13ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 1ms

/AuthenticRoast/AuthenticRoast-Impl/src/main/java/name/aikesommer/authenticator/AuthenticationRequestImpl.java

http://authenticroast.googlecode.com/
Java | 544 lines | 354 code | 101 blank | 89 comment | 16 complexity | 5e236966d8648c4ae21445e9fdc0055f MD5 | raw file
  1/**
  2 *    Copyright (C) 2007-2010 Aike J Sommer (http://aikesommer.name/)
  3 *
  4 *    This file is part of AuthenticRoast.
  5 *
  6 *    This library is free software; you can redistribute it and/or
  7 *    modify it under the terms of the GNU Lesser General Public
  8 *    License as published by the Free Software Foundation; either
  9 *    version 3 of the License, or (at your option) any later version.
 10 *
 11 *    This library is distributed in the hope that it will be useful,
 12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14 *    Lesser General Public License for more details.
 15 *
 16 *    You should have received a copy of the GNU Lesser General
 17 *    Public License along with this library; if not, write to the
 18 *    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 19 *    Boston, MA 02110-1301 USA
 20 *
 21 *    You can reach the author and get more information about this
 22 *    project at: http://aikesommer.name/
 23 */
 24package name.aikesommer.authenticator;
 25
 26import java.util.AbstractMap;
 27import java.util.AbstractSet;
 28import java.util.Enumeration;
 29import java.util.Iterator;
 30import java.util.Map;
 31import java.util.Map.Entry;
 32import java.util.Set;
 33import javax.security.auth.Subject;
 34import javax.servlet.ServletContext;
 35import javax.servlet.http.HttpServletRequest;
 36import javax.servlet.http.HttpServletResponse;
 37import org.apache.catalina.connector.Request;
 38import org.apache.catalina.connector.Response;
 39
 40/**
 41 * The class representing the request for authentication to be passed to the
 42 * authenticator-object in aur web-app. This basically encapsulates some 
 43 * of the involved objects for simplicity.
 44 * 
 45 * @author Aike J Sommer
 46 */
 47public abstract class AuthenticationRequestImpl implements ModifiableRequest {
 48
 49    private HttpServletResponse response;
 50    private HttpServletRequest request;
 51    private boolean mandatory;
 52    private boolean forwarded = false;
 53    private boolean crossContext;
 54    private Map<String, Object> authenticationMap;
 55
 56    public AuthenticationRequestImpl(HttpServletRequest request, HttpServletResponse response,
 57            boolean mandatory, boolean crossContext) {
 58        this.request = request;
 59        this.response = response;
 60        this.mandatory = mandatory;
 61        this.crossContext = crossContext;
 62        this.authenticationMap = crossContext ? SuperSession.self(request, response, true).attributes()
 63                : getSessionMap();
 64    }
 65
 66    protected abstract AuthenticationRequest delegate(ServletContext context);
 67
 68    /**
 69     * Get the ServletContext for the current request.
 70     * 
 71     * @return The ServletContext instance for the current request.
 72     */
 73    public ServletContext getServletContext() {
 74        return request.getSession(true).getServletContext();
 75    }
 76
 77    /**
 78     * Get the HttpServletRequest for the current request.
 79     * 
 80     * @return The HttpServletRequest instance for the current request.
 81     */
 82    public HttpServletRequest getHttpServletRequest() {
 83        return request;
 84    }
 85
 86    /**
 87     * Get the HttpServletResponse for the current request.
 88     * 
 89     * @return The HttpServletResponse instance for the current request.
 90     */
 91    public HttpServletResponse getHttpServletResponse() {
 92        return response;
 93    }
 94
 95    /**
 96     * Get the HTTP-Parameter with given name.
 97     * 
 98     * @param name The name of the parameter to return.
 99     * @return The value of the parameter.
100     */
101    public String getParameter(String name) {
102        return request.getParameter(name);
103    }
104
105    /**
106     * Get the request-path.
107     * @return Get the complete path of the current request, starting after
108     *          the context-path and excluding any parameters.
109     */
110    public String getRequestPath() {
111        String requested = request.getRequestURI();
112        if (! requested.startsWith(getContextPath())) {
113            throw new IllegalArgumentException(requested);
114        }
115        return requested.substring(getContextPath().length());
116    }
117
118    /**
119     * Get the context-path.
120     * @return The context-path for the current request.
121     */
122    public String getContextPath() {
123        return request.getContextPath();
124    }
125
126    public boolean isForwarded() {
127        return forwarded;
128    }
129
130    public void setForwarded(boolean forwarded) {
131        this.forwarded = forwarded;
132    }
133
134    public boolean isCrossContext() {
135        return crossContext;
136    }
137
138    public void setCrossContext(boolean b) {
139        crossContext = b;
140    }
141
142    /**
143     * Get a map containing all session attributes.
144     * Changes to this map will be reflected in the session attributes.
145     * 
146     * @return A map representing all session attributes.
147     */
148    public Map<String, Object> getSessionMap() {
149        return new AttributeMap() {
150
151            @Override
152            public Enumeration<String> getAttributeNames() {
153                return request.getSession(true).getAttributeNames();
154            }
155
156            @Override
157            public Object getAttribute(String s) {
158                return request.getSession(true).getAttribute(s);
159            }
160
161            @Override
162            public void setAttribute(String s, Object o) {
163                request.getSession(true).setAttribute(s, o);
164            }
165
166            @Override
167            public void removeAttribute(String s) {
168                request.getSession(true).removeAttribute(s);
169            }
170        };
171    }
172
173    /**
174     * Get a map containing all request attributes.
175     * Changes to this map will be reflected in the request attributes.
176     * 
177     * @return A map representing all request attributes.
178     */
179    public Map<String, Object> getRequestMap() {
180        return new AttributeMap() {
181
182            @Override
183            public Enumeration<String> getAttributeNames() {
184                return request.getAttributeNames();
185            }
186
187            @Override
188            public Object getAttribute(String s) {
189                return request.getAttribute(s);
190            }
191
192            @Override
193            public void setAttribute(String s, Object o) {
194                request.setAttribute(s, o);
195            }
196
197            @Override
198            public void removeAttribute(String s) {
199                request.removeAttribute(s);
200            }
201        };
202    }
203
204    public Map<String, Object> getApplicationMap() {
205        final ServletContext context = getServletContext();
206        return new AttributeMap() {
207
208            @Override
209            public Enumeration<String> getAttributeNames() {
210                return context.getAttributeNames();
211            }
212
213            @Override
214            public Object getAttribute(String s) {
215                return context.getAttribute(s);
216            }
217
218            @Override
219            public void setAttribute(String s, Object o) {
220                context.setAttribute(s, o);
221            }
222
223            @Override
224            public void removeAttribute(String s) {
225                context.removeAttribute(s);
226            }
227        };
228    }
229
230    public Map<String, Object> getAuthenticationMap() {
231        return authenticationMap;
232    }
233
234    /**
235     * Get whether authentication is mandatory for the current request.
236     * If authentication is not mandatory the authenticator can still return
237     * Success to signal that the resource should be served anyways.
238     * 
239     * @return If authentication is mandatory for the requested resource.
240     */
241    public boolean isMandatory() {
242        return mandatory;
243    }
244
245    /**
246     * You dont need to call this and it should be hidden in an impl-class.
247     * 
248     * @param mandatory
249     */
250    public void setMandatory(boolean mandatory) {
251        this.mandatory = mandatory;
252    }
253
254    public ServletContext getOriginalContext() {
255        return getServletContext();
256    }
257
258    public static class JSR196 extends AuthenticationRequestImpl implements JSR196Request {
259
260        private Subject clientSubject;
261
262        public JSR196(HttpServletRequest request, HttpServletResponse response,
263                Subject clientSubject, boolean mandatory, boolean crossContext) {
264            super(request, response, mandatory, crossContext);
265            this.clientSubject = clientSubject;
266        }
267
268        /**
269         * Get the ClientSubject for the current request.
270         * 
271         * @return The ClientSubject instance for the current request.
272         */
273        public Subject getClientSubject() {
274            return clientSubject;
275        }
276
277        @Override
278        protected JSR196Request delegate(ServletContext context) {
279            return new WrappedRequest.JSR196(context, this);
280        }
281
282    }
283
284    public static class Tomcat6 extends AuthenticationRequestImpl implements Tomcat6Request {
285
286        private Request catalinaRequest;
287        private Response catalinaResponse;
288
289        public Tomcat6(Request request, Response response, boolean mandatory, boolean crossContext) {
290            super(request.getRequest(), response.getResponse(), mandatory, crossContext);
291            this.catalinaRequest = request;
292            this.catalinaResponse = response;
293        }
294
295        public Request getCatalinaRequest() {
296            return catalinaRequest;
297        }
298
299        public Response getCatalinaResponse() {
300            return catalinaResponse;
301        }
302
303        @Override
304        protected Tomcat6Request delegate(ServletContext context) {
305            return new WrappedRequest.Tomcat6(context, this);
306        }
307
308    }
309
310    public abstract static class AttributeMap extends NoteMap {
311
312        @Override
313        public Iterator<String> getNoteNames() {
314            return new Iterator<String>() {
315
316                Enumeration<String> pos = getAttributeNames();
317                String current = null;
318                String last = null;
319
320                public boolean hasNext() {
321                    return pos.hasMoreElements();
322                }
323
324                public String next() {
325                    last = current;
326                    current = pos.nextElement();
327                    return current;
328                }
329
330                public void remove() {
331                    if (current == null) {
332                        return;
333                    }
334                    String before = last;
335                    removeAttribute(current);
336                    if (before != null) {
337                        while (hasNext() && !before.equals(next())) {
338                        }
339                    }
340                }
341            };
342        }
343
344        @Override
345        public Object getNote(String s) {
346            return getAttribute(s);
347        }
348
349        @Override
350        public void setNote(String s, Object o) {
351            setAttribute(s, o);
352        }
353
354        @Override
355        public void removeNote(String s) {
356            removeAttribute(s);
357        }
358
359        public abstract Enumeration<String> getAttributeNames();
360
361        public abstract Object getAttribute(String s);
362
363        public abstract void setAttribute(String s, Object o);
364
365        public abstract void removeAttribute(String s);
366    }
367
368    public abstract static class NoteMap extends AbstractMap<String, Object> {
369
370        public abstract Iterator<String> getNoteNames();
371
372        public abstract Object getNote(String s);
373
374        public abstract void setNote(String s, Object o);
375
376        public abstract void removeNote(String s);
377
378        @Override
379        public Set<Entry<String, Object>> entrySet() {
380            return new NoteEntrySet(this);
381        }
382
383        @Override
384        public int size() {
385            int count = 0;
386            for (Iterator<String> it = getNoteNames(); it.hasNext();) {
387                it.next();
388                count++;
389            }
390
391            return count;
392        }
393
394        @Override
395        public Object get(Object key) {
396            if (!(key instanceof String)) {
397                return null;
398            }
399            return getNote((String) key);
400        }
401
402        @Override
403        public boolean containsKey(Object key) {
404            if (!(key instanceof String)) {
405                return false;
406            }
407
408            String keyStr = (String) key;
409            if (get(keyStr) != null) {
410                return true;
411            }
412
413            for (Iterator<String> it = getNoteNames(); it.hasNext();) {
414                String name = it.next();
415                if (name.equals(keyStr)) {
416                    return true;
417                }
418            }
419
420            return false;
421        }
422
423        @Override
424        public boolean isEmpty() {
425            return !getNoteNames().hasNext();
426        }
427
428        @Override
429        public Set<String> keySet() {
430            return new NoteKeySet(this);
431        }
432
433        @Override
434        public void clear() {
435            for (Iterator<String> it = keySet().iterator(); it.hasNext();) {
436                it.remove();
437            }
438        }
439
440        @Override
441        public Object put(String key, Object value) {
442            Object old = get(key);
443            setNote(key, value);
444            return old;
445        }
446
447        @Override
448        public Object remove(Object key) {
449            if (!(key instanceof String)) {
450                return null;
451            }
452
453            String keyStr = (String) key;
454            Object old = get(keyStr);
455            removeNote(keyStr);
456            return old;
457        }
458    }
459
460    public static class NoteKeySet extends AbstractSet<String> {
461
462        private NoteMap map;
463
464        public NoteKeySet(NoteMap map) {
465            this.map = map;
466        }
467
468        @Override
469        public Iterator<String> iterator() {
470            return new Iterator<String>() {
471
472                private String current = null;
473                private Iterator<String> pos = map.getNoteNames();
474
475                public boolean hasNext() {
476                    return pos.hasNext();
477                }
478
479                public String next() {
480                    current = pos.next();
481                    return current;
482                }
483
484                public void remove() {
485                    pos.remove();
486                }
487            };
488        }
489
490        @Override
491        public int size() {
492            return map.size();
493        }
494    }
495
496    public static class NoteEntrySet extends AbstractSet<Map.Entry<String, Object>> {
497
498        private NoteMap map;
499
500        public NoteEntrySet(NoteMap map) {
501            this.map = map;
502        }
503
504        @Override
505        public Iterator<Map.Entry<String, Object>> iterator() {
506            return new Iterator<Map.Entry<String, Object>>() {
507
508                private Iterator<String> it = map.keySet().iterator();
509
510                public boolean hasNext() {
511                    return it.hasNext();
512                }
513
514                public Map.Entry<String, Object> next() {
515                    return new Map.Entry() {
516
517                        private String key = it.next();
518
519                        public Object getKey() {
520                            return key;
521                        }
522
523                        public Object getValue() {
524                            return map.get(key);
525                        }
526
527                        public Object setValue(Object value) {
528                            return map.put(key, value);
529                        }
530                    };
531                }
532
533                public void remove() {
534                    it.remove();
535                }
536            };
537        }
538
539        @Override
540        public int size() {
541            return map.size();
542        }
543    }
544}