1 23 24 package de.laures.cewolf.taglib.tags; 25 26 import java.io.IOException ; 27 import java.io.Writer ; 28 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 import javax.servlet.jsp.JspException ; 32 import javax.servlet.jsp.JspWriter ; 33 import javax.servlet.jsp.PageContext ; 34 import javax.servlet.jsp.tagext.BodyContent ; 35 36 import de.laures.cewolf.CewolfException; 37 import de.laures.cewolf.ChartHolder; 38 import de.laures.cewolf.ChartImage; 39 import de.laures.cewolf.Configuration; 40 import de.laures.cewolf.Storage; 41 import de.laures.cewolf.WebConstants; 42 import de.laures.cewolf.taglib.ChartImageDefinition; 43 import de.laures.cewolf.taglib.TaglibConstants; 44 import de.laures.cewolf.taglib.html.HTMLImgTag; 45 import de.laures.cewolf.taglib.util.MIMEExtensionHelper; 46 import de.laures.cewolf.taglib.util.PageUtils; 47 48 57 public class ChartImgTag extends HTMLImgTag implements CewolfRootTag, Mapped, TaglibConstants, WebConstants 58 { 59 60 private static final String DEFAULT_MIME_TYPE = MIME_PNG; 61 private static final String TAG_NAME_SVG = "EMBED"; 62 private static final int DEFAULT_TIMEOUT = 300; 63 64 private String chartId = null; 65 private String renderer; 66 private String mimeType = DEFAULT_MIME_TYPE; 67 private int timeout = DEFAULT_TIMEOUT; 68 protected String sessionKey = null; 69 70 private ChartImageDefinition chartImageDefinition; 71 72 public int doStartTag() throws JspException 73 { 74 final ChartHolder chartHolder = PageUtils.getChartHolder(chartId, pageContext); 75 this.chartImageDefinition = new ChartImageDefinition(chartHolder, width, height, ChartImage.IMG_TYPE_CHART, mimeType,timeout); 76 Storage storage = Configuration.getInstance(pageContext.getServletContext()).getStorage(); 77 try 78 { 79 this.sessionKey = storage.storeChartImage(chartImageDefinition, pageContext); 80 } 81 catch (CewolfException cwex) 82 { 83 throw new JspException (cwex.getMessage()); 84 } 85 return EVAL_PAGE; 86 } 87 88 public int doAfterBody() throws JspException 89 { 90 try 91 { 92 final BodyContent body = getBodyContent(); 94 if ( body != null ) 95 { 96 final JspWriter writer = getPreviousOut(); 97 if ( writer != null ) 98 { 99 body.writeOut(writer); 100 } 101 } 102 103 } 104 catch (IOException ioex) 105 { 106 log.error(ioex); 107 throw new JspException (ioex.getMessage()); 108 } 109 return SKIP_BODY; 110 } 111 112 public int doEndTag() throws JspException 113 { 114 super.doStartTag(); 115 final StringBuffer buffer = new StringBuffer (" SRC=\""); 116 buffer.append(getImgURL()); 117 buffer.append("\""); 118 try 119 { 120 pageContext.getOut().write(buffer.toString()); 121 } 122 catch (IOException ioex) 123 { 124 reset(); 125 log.error(ioex); 126 throw new JspException (ioex.getMessage()); 127 } 128 return super.doEndTag(); 129 } 130 131 138 public static String fixAbsolutURL(String url, HttpServletRequest request) { 139 if ( url.startsWith("/") ) 140 { 141 final String context = request.getContextPath(); 143 url = context + url; 144 } 145 return url; 146 } 147 148 154 public static String fixAbsolutURL(String url, PageContext pageContext) { 155 return fixAbsolutURL(url, (HttpServletRequest ) pageContext.getRequest()); 156 } 157 158 168 public static String buildImgURL( 169 String renderer, PageContext pageContext, String sessionKey, int width, int height, String mimeType, 170 boolean forceSessionId, boolean removeAfterRender) { 171 renderer = fixAbsolutURL(renderer, pageContext); 172 final HttpServletResponse response = (HttpServletResponse ) pageContext.getResponse(); 173 StringBuffer url = new StringBuffer (response.encodeURL(renderer)); 174 if ( url.toString().indexOf(SESSIONID_KEY) == -1 ) 175 { 176 if (forceSessionId) 177 { 178 final String sessionId = pageContext.getSession().getId(); 179 url.append(";" + SESSIONID_KEY + "=" + sessionId); 180 } 181 } 182 url.append("?" + IMG_PARAM + "=" + sessionKey); 183 url.append(AMPERSAND + WIDTH_PARAM + "=" + width); 184 url.append(AMPERSAND + HEIGHT_PARAM + "=" + height); 185 if (removeAfterRender) 186 { 187 url.append(AMPERSAND + REMOVE_AFTER_RENDERING + "=true"); 188 } 189 url.append(AMPERSAND + "iehack=" + MIMEExtensionHelper.getExtensionForMimeType(mimeType)); 190 return url.toString(); 191 } 192 193 198 protected String getImgURL() 199 { 200 return buildImgURL(renderer, pageContext, sessionKey, width, height, mimeType, forceSessionId, removeAfterRender); 201 } 202 203 public Object getRenderingInfo() throws CewolfException 204 { 205 return chartImageDefinition.getRenderingInfo(); 206 } 207 208 protected String getMimeType() 209 { 210 return mimeType; 211 } 212 213 protected void reset() 214 { 215 this.mimeType = DEFAULT_MIME_TYPE; 216 int lHeight = this.height; 221 int lWidth = this.width; 222 int lTimeout = this.timeout; 223 super.reset(); 224 this.height = lHeight; 225 this.width = lWidth; 226 this.timeout = lTimeout; 227 } 228 229 public void enableMapping() 230 { 231 setUsemap("#" + chartId); 232 } 233 234 public String getChartId() 235 { 236 return getChartid(); 237 } 238 239 public void setChartid( String id ) 240 { 241 this.chartId = id; 242 } 243 244 public String getChartid() 245 { 246 return chartId; 247 } 248 249 public void setRenderer( String renderer ) 250 { 251 this.renderer = renderer; 252 } 253 254 protected String getRenderer() 255 { 256 return this.renderer; 257 } 258 259 public int getWidth() 260 { 261 return this.width; 262 } 263 264 public int getHeight() 265 { 266 return height; 267 } 268 269 275 public void setMime( String mimeType ) 276 { 277 this.mimeType = mimeType; 278 } 279 280 283 protected String getTagName() 284 { 285 if ( MIME_SVG.equals(mimeType) ) 286 { 287 return TAG_NAME_SVG; 288 } 289 return super.getTagName(); 290 } 291 292 295 public void writeAttributes( Writer wr ) 296 { 297 super.writeAttributes(wr); 298 if ( MIME_SVG.equals(mimeType) ) 299 { 300 try 301 { 302 appendAttributeDeclaration(wr, "http://www.adobe.com/svg/viewer/install/", "PLUGINSPAGE"); 303 } 304 catch (IOException ioex) 305 { 306 ioex.printStackTrace(); 307 } 308 } 309 } 310 311 314 public int getTimeout() 315 { 316 return timeout; 317 } 318 321 public void setTimeout( int timeout ) 322 { 323 this.timeout = timeout; 324 } 325 } | Popular Tags |