1 /**
2 * Copyright 2005-2006 the original author or authors.
3 *
4 * Licensed under the Gnu General Pubic License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/gpl-license.php
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the Gnu General Public License for more details.
14 */
15 package org.figure8.join.control.taglib;
16
17 import org.figure8.join.control.JoinAction;
18
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.jsp.JspWriter;
21 import javax.servlet.jsp.tagext.BodyContent;
22 import javax.servlet.jsp.tagext.BodyTagSupport;
23 /**
24 * Body tag designed to protect view resources (ie. JSP fragment) depending on
25 * the login status of the current user. If you want that body content of
26 * LoggedInTag to be reserved to logged in user : set <b>flag</b> attribute to true.
27 * On the other hand, if you want that body content to be reserved to logged off
28 * user : set it to false.
29 *
30 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
31 * @version $Revision: 1.1 $
32 */
33 public class LoggedInTag extends BodyTagSupport{
34
35
36
37 /** Must the user be logged in to access resources ? */
38 private boolean flag;
39 /** Logged In flag. Set by doStartTag(). */
40 private boolean logged = false;
41
42
43
44
45 /** @return The value of flag attribute */
46 public boolean getFlag(){
47 return flag;
48 }
49 /** @param flag The value of flag attribute */
50 public void setFlag(boolean flag){
51 this.flag = flag;
52 }
53
54
55
56
57 /**
58 * Retrieve the current request and check if user is logged in using
59 * JoinAction. Update the <b>logged</b> inner field and evaluate body content.
60 */
61 public int doStartTag(){
62
63 HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
64 logged = JoinAction.isLoggedIn(request);
65 return (EVAL_BODY_BUFFERED);
66 }
67
68 /**
69 * Get the body content. If this content is not null and <b>flag</b> ==
70 * <b>logged</b>, write the body content to the JspWriter.
71 */
72 public int doEndTag(){
73 try{
74
75 BodyContent content = getBodyContent();
76 if (content != null && (flag == logged)){
77 JspWriter out = content.getEnclosingWriter();
78 out.print(content.getString());
79 }
80 }
81 catch (Exception e){
82 System.err.println("Exception in doEndTag() handling LoggedInTag: " + e);
83 }
84 return (EVAL_PAGE);
85 }
86
87 /** Release all allocated resources. */
88 public void release(){
89 super.release();
90 logged = false;
91 }
92 }