1 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 ; 19 import javax.servlet.jsp.JspException ; 20 import javax.servlet.jsp.PageContext ; 21 import javax.servlet.http.Cookie ; 22 import javax.servlet.http.HttpServletResponse ; 23 import javax.servlet.http.HttpServletRequest ; 24 25 import org.mmbase.util.logging.Logger; 26 import org.mmbase.util.logging.Logging; 27 28 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; 43 public static final String MAX_COOKIE_AGE_KEY = "org.mmbase.taglib.max_cookie_age"; 44 45 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 54 private Attribute value = Attribute.NULL; 55 private Attribute container = Attribute.NULL; 56 57 public void setSession(String s) throws JspTagException { 58 sessionVar = getAttribute(s); 59 } 60 61 public void setCookie(String s) throws JspTagException { 62 cookie = getAttribute(s); 63 } 64 65 68 public void setApplication(String s) throws JspTagException { 69 applicationVar = getAttribute(s); 70 } 71 74 public void setRequest(String s) throws JspTagException { 75 requestVar = getAttribute(s); 76 } 77 78 79 80 86 public void setValue(String v) throws JspTagException { 87 value = getAttribute(v); 88 } 89 public void setContainer(String c) throws JspTagException { 90 container = getAttribute(c); } 92 93 94 protected Object getObject() throws JspTagException { 95 if (log.isDebugEnabled()) { 96 log.debug("getting object " + getReferid()); 97 } 98 if (getReferid() == null && value == Attribute.NULL) { return findWriter().getWriterValue(); 100 } 101 102 if (value != Attribute.NULL) { 103 if (getReferid() != null) { 104 throw new JspTagException ("Cannot specify the 'value' atribute and the 'referid' attribute at the same time"); 105 } 106 return value.getString(this); } 108 109 if (helper.getVartype() == WriterHelper.TYPE_BYTES) { 110 return getContextTag().getBytes(getReferid()); } 112 Object 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 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 { 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 ("Cannot write to session if session is disabled"); 140 } 141 pageContext.getSession().setAttribute(sessionVar.getString(this), getEscapedValue(helper.getValue())); 142 helper.overrideWrite(false); } 144 if (requestVar != Attribute.NULL) { 145 pageContext.setAttribute(requestVar.getString(this), getEscapedValue(helper.getValue()), PageContext.REQUEST_SCOPE); 146 helper.overrideWrite(false); } 148 if (applicationVar != Attribute.NULL) { 149 pageContext.setAttribute(applicationVar.getString(this), getEscapedValue(helper.getValue()), PageContext.APPLICATION_SCOPE); 150 helper.overrideWrite(false); } 152 153 if (cookie != Attribute.NULL) { 154 Object v = getEscapedValue(helper.getValue()); 155 String cookievalue; 156 if (v instanceof Node) { 157 cookievalue = "" + ((Node) v).getNumber(); 158 } else if (v instanceof String || v instanceof Number ) { 159 cookievalue = "" + v; 160 } else { 161 throw new JspTagException (v.toString() + " is not of the right type to write to cookie. It is a (" + v.getClass().getName() + ")"); 162 } 163 164 HttpServletRequest request = ((HttpServletRequest ) pageContext.getRequest()); 166 HttpServletResponse response = ((HttpServletResponse ) pageContext.getResponse()); 167 168 if (log.isDebugEnabled()) { 169 log.debug("Writing cookie " + cookie + " / " + v); 170 } 171 int cookiecount = 0; 173 Cookie [] cookies = request.getCookies(); 174 if (cookies != null) { 175 for (int i = 0; i< cookies.length; i++) { 176 Cookie c = cookies[i]; 177 if (c.getName().equals(cookie.toString())) { 178 cookiecount++; 179 } 180 } 181 } 182 183 int maxCookieAge = getMaxCookieAge(); 184 { Cookie c = new Cookie (cookie.getString(this), cookievalue); 186 String 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) { Cookie c = new Cookie (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 { 204 return helper.doAfterBody(); 205 } 206 207 208 public int doEndTag() throws JspTagException { 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 |