KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > WriteTag


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.bridge.jsp.taglib;
11 import org.mmbase.bridge.Node;
12
13 import org.mmbase.bridge.jsp.taglib.containers.FunctionContainerReferrer;
14 import org.mmbase.bridge.jsp.taglib.util.Attribute;
15
16 import org.mmbase.util.Casting;
17
18 import javax.servlet.jsp.JspTagException JavaDoc;
19 import javax.servlet.jsp.JspException JavaDoc;
20 import javax.servlet.jsp.PageContext JavaDoc;
21 import javax.servlet.http.Cookie JavaDoc;
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24
25 import org.mmbase.util.logging.Logger;
26 import org.mmbase.util.logging.Logging;
27
28 /**
29  * The writetag can take a variable from the context and put it in a
30  * jsp variable, or write it to the page.
31  *
32  * This is also more or less the simplest possible implemententation
33  * of a 'Writer' tag.
34  *
35  * @author Michiel Meeuwissen
36  * @version $Id: WriteTag.java,v 1.49 2005/05/13 09:47:12 michiel Exp $
37  */

38
39 public class WriteTag extends ContextReferrerTag implements Writer, FunctionContainerReferrer {
40
41     public static final int DEFAULT_MAX_COOKIE_AGE = 60 * 60 * 24 * 30 * 6; // half year
42

43     public static final String JavaDoc MAX_COOKIE_AGE_KEY = "org.mmbase.taglib.max_cookie_age";
44
45     //public static final String COOKIE_PATH = "/";
46
private static final Logger log = Logging.getLoggerInstance(WriteTag.class);
47
48     private Attribute sessionVar = Attribute.NULL;
49     private Attribute cookie = Attribute.NULL;
50     private Attribute applicationVar = Attribute.NULL;
51     private Attribute requestVar = Attribute.NULL;
52     // private Attribute page = Attribute.NULL;
53

54     private Attribute value = Attribute.NULL;
55     private Attribute container = Attribute.NULL;
56
57     public void setSession(String JavaDoc s) throws JspTagException JavaDoc {
58         sessionVar = getAttribute(s);
59     }
60
61     public void setCookie(String JavaDoc s) throws JspTagException JavaDoc {
62         cookie = getAttribute(s);
63     }
64
65     /**
66      * @since MMBase-1.7.4
67      */

68     public void setApplication(String JavaDoc s) throws JspTagException JavaDoc {
69         applicationVar = getAttribute(s);
70     }
71     /**
72      * @since MMBase-1.7.4
73      */

74     public void setRequest(String JavaDoc s) throws JspTagException JavaDoc {
75         requestVar = getAttribute(s);
76     }
77
78
79
80     /*
81       // A page attribute is not needed, because we have already taglib vars, which take the same function (and are actually stored here)
82       public void setPage(String s) throws JspTagException {
83       page = getAttribute(s);
84     }
85     */

86     public void setValue(String JavaDoc v) throws JspTagException JavaDoc {
87         value = getAttribute(v);
88     }
89     public void setContainer(String JavaDoc c) throws JspTagException JavaDoc {
90         container = getAttribute(c); // not yet implemented
91
}
92
93
94     protected Object JavaDoc getObject() throws JspTagException JavaDoc {
95         if (log.isDebugEnabled()) {
96             log.debug("getting object " + getReferid());
97         }
98         if (getReferid() == null && value == Attribute.NULL) { // get from parent Writer.
99
return findWriter().getWriterValue();
100         }
101
102         if (value != Attribute.NULL) {
103             if (getReferid() != null) {
104                  throw new JspTagException JavaDoc("Cannot specify the 'value' atribute and the 'referid' attribute at the same time");
105             }
106             return value.getString(this); // with value attribute only strings, of course.
107
}
108
109         if (helper.getVartype() == WriterHelper.TYPE_BYTES) {
110             return getContextTag().getBytes(getReferid()); // a hack..
111
}
112         Object JavaDoc res = getObject(getReferid());
113         if (log.isDebugEnabled()) {
114             log.debug("found " + res + " " + (res == null ? "" : res.getClass().getName()) + " with " + getReferid());
115         }
116         return res;
117     }
118
119
120     protected int getMaxCookieAge() {
121         Object JavaDoc o = pageContext.getAttribute(MAX_COOKIE_AGE_KEY);
122         if (o == null) return DEFAULT_MAX_COOKIE_AGE;
123         return Integer.parseInt("" + o);
124     }
125
126
127     public int doStartTag() throws JspTagException JavaDoc {
128         if (log.isDebugEnabled()) {
129             log.debug("start writetag id: '" +getId() + "' referid: '" + getReferid() + "' value '" + value + "'");
130         }
131         
132         helper.setValue(getObject());
133
134         if (getId() != null) {
135             getContextProvider().getContextContainer().register(getId(), helper.getValue());
136         }
137         if (sessionVar != Attribute.NULL) {
138             if (pageContext.getSession() == null) {
139                 throw new JspTagException JavaDoc("Cannot write to session if session is disabled");
140             }
141             pageContext.getSession().setAttribute(sessionVar.getString(this), getEscapedValue(helper.getValue()));
142             helper.overrideWrite(false); // default behavior is not to write to page if wrote to session.
143
}
144         if (requestVar != Attribute.NULL) {
145             pageContext.setAttribute(requestVar.getString(this), getEscapedValue(helper.getValue()), PageContext.REQUEST_SCOPE);
146             helper.overrideWrite(false); // default behavior is not to write to page if wrote to request.
147
}
148         if (applicationVar != Attribute.NULL) {
149             pageContext.setAttribute(applicationVar.getString(this), getEscapedValue(helper.getValue()), PageContext.APPLICATION_SCOPE);
150             helper.overrideWrite(false); // default behavior is not to write to page if wrote to application.
151
}
152
153         if (cookie != Attribute.NULL) {
154             Object JavaDoc v = getEscapedValue(helper.getValue());
155             String JavaDoc cookievalue;
156             if (v instanceof Node) {
157                 cookievalue = "" + ((Node) v).getNumber();
158             } else if (v instanceof String JavaDoc || v instanceof Number JavaDoc) {
159                 cookievalue = "" + v;
160             } else {
161                 throw new JspTagException JavaDoc(v.toString() + " is not of the right type to write to cookie. It is a (" + v.getClass().getName() + ")");
162             }
163
164             // remove all cookies with given name
165
HttpServletRequest JavaDoc request = ((HttpServletRequest JavaDoc) pageContext.getRequest());
166             HttpServletResponse JavaDoc response = ((HttpServletResponse JavaDoc) pageContext.getResponse());
167             
168             if (log.isDebugEnabled()) {
169                 log.debug("Writing cookie " + cookie + " / " + v);
170             }
171             // count present cookies of this name
172
int cookiecount = 0;
173             Cookie JavaDoc[] cookies = request.getCookies();
174             if (cookies != null) {
175                 for (int i = 0; i< cookies.length; i++) {
176                     Cookie JavaDoc c = cookies[i];
177                     if (c.getName().equals(cookie.toString())) {
178                         cookiecount++;
179                     }
180                 }
181             }
182
183             int maxCookieAge = getMaxCookieAge();
184             { // on root (keep things simple)
185
Cookie JavaDoc c = new Cookie JavaDoc(cookie.getString(this), cookievalue);
186                 String JavaDoc path = request.getContextPath();
187                 if (path.length() == 0) path = "/";
188                 c.setPath(path);
189                 c.setMaxAge(maxCookieAge);
190                 response.addCookie(c);
191
192             }
193             if (cookiecount > 1) { //also in current dir (in case it was there already)
194
Cookie JavaDoc c = new Cookie JavaDoc(cookie.getString(this), cookievalue);
195                 c.setMaxAge(maxCookieAge);
196                 response.addCookie(c);
197             }
198             helper.overrideWrite(false);
199         }
200         return EVAL_BODY_BUFFERED;
201     }
202
203     public int doAfterBody() throws JspException JavaDoc {
204         return helper.doAfterBody();
205     }
206
207
208     public int doEndTag() throws JspTagException JavaDoc {
209         if (log.isDebugEnabled()) {
210             log.debug("End writetag id: '" +getId() + "' referid: '" + getReferid() + "' value '" + value + "'");
211         }
212         helper.doEndTag();
213         return super.doEndTag();
214     }
215 }
216
Popular Tags