java/cewolf-1.0/src/main/java/de/laures/cewolf/taglib/tags/ChartImgTag.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 28 Feb 2009 21:31:02 +0100
changeset 1 639991d0808a
permissions -rw-r--r--
Rozbalená knihovna verze 1.0
franta-hg@1
     1
/* ================================================================
franta-hg@1
     2
 * Cewolf : Chart enabling Web Objects Framework
franta-hg@1
     3
 * ================================================================
franta-hg@1
     4
 *
franta-hg@1
     5
 * Project Info:  http://cewolf.sourceforge.net
franta-hg@1
     6
 * Project Lead:  Guido Laures (guido@laures.de);
franta-hg@1
     7
 *
franta-hg@1
     8
 * (C) Copyright 2002, by Guido Laures
franta-hg@1
     9
 *
franta-hg@1
    10
 * This library is free software; you can redistribute it and/or modify it under the terms
franta-hg@1
    11
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
franta-hg@1
    12
 * either version 2.1 of
franta-hg@1
    13
 * the License, or (at your option) any later version.
franta-hg@1
    14
 *
franta-hg@1
    15
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
franta-hg@1
    16
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
franta-hg@1
    17
 * See the GNU Lesser General Public License for more details.
franta-hg@1
    18
 *
franta-hg@1
    19
 * You should have received a copy of the GNU Lesser General Public License along with this
franta-hg@1
    20
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
franta-hg@1
    21
 * Boston, MA 02111-1307, USA.
franta-hg@1
    22
 */
franta-hg@1
    23
franta-hg@1
    24
package de.laures.cewolf.taglib.tags;
franta-hg@1
    25
franta-hg@1
    26
import java.io.IOException;
franta-hg@1
    27
import java.io.Writer;
franta-hg@1
    28
franta-hg@1
    29
import javax.servlet.http.HttpServletRequest;
franta-hg@1
    30
import javax.servlet.http.HttpServletResponse;
franta-hg@1
    31
import javax.servlet.jsp.JspException;
franta-hg@1
    32
import javax.servlet.jsp.JspWriter;
franta-hg@1
    33
import javax.servlet.jsp.PageContext;
franta-hg@1
    34
import javax.servlet.jsp.tagext.BodyContent;
franta-hg@1
    35
franta-hg@1
    36
import de.laures.cewolf.CewolfException;
franta-hg@1
    37
import de.laures.cewolf.ChartHolder;
franta-hg@1
    38
import de.laures.cewolf.ChartImage;
franta-hg@1
    39
import de.laures.cewolf.Configuration;
franta-hg@1
    40
import de.laures.cewolf.Storage;
franta-hg@1
    41
import de.laures.cewolf.WebConstants;
franta-hg@1
    42
import de.laures.cewolf.taglib.ChartImageDefinition;
franta-hg@1
    43
import de.laures.cewolf.taglib.TaglibConstants;
franta-hg@1
    44
import de.laures.cewolf.taglib.html.HTMLImgTag;
franta-hg@1
    45
import de.laures.cewolf.taglib.util.MIMEExtensionHelper;
franta-hg@1
    46
import de.laures.cewolf.taglib.util.PageUtils;
franta-hg@1
    47
franta-hg@1
    48
/**
franta-hg@1
    49
 * This is the tag implementation of the &lt;img&gt; tag. This tag inputs the
franta-hg@1
    50
 * proper &lt;img&gt; tag into the HTML page delivered to the client. It
franta-hg@1
    51
 * therefor determines the chart ID which will be used by the rendering servlet
franta-hg@1
    52
 * to retrieve the chart.
franta-hg@1
    53
 * 
franta-hg@1
    54
 * @author glaures
franta-hg@1
    55
 * @see de.laures.cewolf.ChartImage
franta-hg@1
    56
 */
franta-hg@1
    57
public class ChartImgTag extends HTMLImgTag implements CewolfRootTag, Mapped, TaglibConstants, WebConstants
franta-hg@1
    58
{
franta-hg@1
    59
franta-hg@1
    60
  private static final String  DEFAULT_MIME_TYPE = MIME_PNG;
franta-hg@1
    61
  private static final String  TAG_NAME_SVG      = "EMBED";
franta-hg@1
    62
  private static final int     DEFAULT_TIMEOUT   = 300;
franta-hg@1
    63
franta-hg@1
    64
  private String               chartId           = null;
franta-hg@1
    65
  private String               renderer;
franta-hg@1
    66
  private String               mimeType          = DEFAULT_MIME_TYPE;
franta-hg@1
    67
  private int                  timeout           = DEFAULT_TIMEOUT;
franta-hg@1
    68
  protected String             sessionKey        = null;
franta-hg@1
    69
  
franta-hg@1
    70
  private ChartImageDefinition chartImageDefinition;
franta-hg@1
    71
franta-hg@1
    72
  public int doStartTag() throws JspException
franta-hg@1
    73
  {
franta-hg@1
    74
    final ChartHolder chartHolder = PageUtils.getChartHolder(chartId, pageContext);
franta-hg@1
    75
    this.chartImageDefinition = new ChartImageDefinition(chartHolder, width, height, ChartImage.IMG_TYPE_CHART, mimeType,timeout);
franta-hg@1
    76
    Storage storage = Configuration.getInstance(pageContext.getServletContext()).getStorage();
franta-hg@1
    77
    try
franta-hg@1
    78
    {
franta-hg@1
    79
      this.sessionKey = storage.storeChartImage(chartImageDefinition, pageContext);
franta-hg@1
    80
    }
franta-hg@1
    81
    catch (CewolfException cwex)
franta-hg@1
    82
    {
franta-hg@1
    83
      throw new JspException(cwex.getMessage());
franta-hg@1
    84
    }
franta-hg@1
    85
    return EVAL_PAGE;
franta-hg@1
    86
  }
franta-hg@1
    87
franta-hg@1
    88
  public int doAfterBody() throws JspException
franta-hg@1
    89
  {
franta-hg@1
    90
    try
franta-hg@1
    91
    {
franta-hg@1
    92
      // double checking for null as Resin had problems with that
franta-hg@1
    93
      final BodyContent body = getBodyContent();
franta-hg@1
    94
      if ( body != null )
franta-hg@1
    95
      {
franta-hg@1
    96
        final JspWriter writer = getPreviousOut();
franta-hg@1
    97
        if ( writer != null )
franta-hg@1
    98
        {
franta-hg@1
    99
          body.writeOut(writer);
franta-hg@1
   100
        }
franta-hg@1
   101
      }
franta-hg@1
   102
franta-hg@1
   103
    }
franta-hg@1
   104
    catch (IOException ioex)
franta-hg@1
   105
    {
franta-hg@1
   106
      log.error(ioex);	
franta-hg@1
   107
      throw new JspException(ioex.getMessage());
franta-hg@1
   108
    }
franta-hg@1
   109
    return SKIP_BODY;
franta-hg@1
   110
  }
franta-hg@1
   111
franta-hg@1
   112
  public int doEndTag() throws JspException
franta-hg@1
   113
  {
franta-hg@1
   114
    super.doStartTag();
franta-hg@1
   115
    final StringBuffer buffer = new StringBuffer(" src=\"");
franta-hg@1
   116
    buffer.append(getImgURL());
franta-hg@1
   117
    buffer.append("\"");
franta-hg@1
   118
    try
franta-hg@1
   119
    {
franta-hg@1
   120
      pageContext.getOut().write(buffer.toString());
franta-hg@1
   121
    }
franta-hg@1
   122
    catch (IOException ioex)
franta-hg@1
   123
    {
franta-hg@1
   124
      reset();
franta-hg@1
   125
      log.error(ioex);
franta-hg@1
   126
      throw new JspException(ioex.getMessage());
franta-hg@1
   127
    }
franta-hg@1
   128
    return super.doEndTag();
franta-hg@1
   129
  }
franta-hg@1
   130
franta-hg@1
   131
  /**
franta-hg@1
   132
   * Fix an absolute url given as attribute by adding the full application url path to it.
franta-hg@1
   133
   * It is considered absolute url (not relative) when it starts with "/"
franta-hg@1
   134
   * @param url The url to fix
franta-hg@1
   135
   * @param request The http request
franta-hg@1
   136
   * @return Fixed url contains the full path
franta-hg@1
   137
   */
franta-hg@1
   138
  public static String fixAbsolutURL(String url, HttpServletRequest request) {
franta-hg@1
   139
    if ( url.startsWith("/") )
franta-hg@1
   140
    {
franta-hg@1
   141
      //final HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
franta-hg@1
   142
      final String context = request.getContextPath();
franta-hg@1
   143
      url = context + url;
franta-hg@1
   144
    }
franta-hg@1
   145
    return url;
franta-hg@1
   146
  }
franta-hg@1
   147
  
franta-hg@1
   148
  /**
franta-hg@1
   149
   * Same as the other fixAbsolutURL, convinience only.
franta-hg@1
   150
   * @param url The url to fix
franta-hg@1
   151
   * @param pageContext The page context
franta-hg@1
   152
   * @return Fixed url contains the full path
franta-hg@1
   153
   */
franta-hg@1
   154
  public static String fixAbsolutURL(String url, PageContext pageContext) {
franta-hg@1
   155
  	return fixAbsolutURL(url, (HttpServletRequest) pageContext.getRequest());
franta-hg@1
   156
  }
franta-hg@1
   157
  
franta-hg@1
   158
  /**
franta-hg@1
   159
   * Build the image url
franta-hg@1
   160
   * @param renderer the url of the renderer
franta-hg@1
   161
   * @param pageContext Page context
franta-hg@1
   162
   * @param sessionKey The session key for the image stored.
franta-hg@1
   163
   * @param width The width 
franta-hg@1
   164
   * @param height The height
franta-hg@1
   165
   * @param mimeType the mime-type (for example png) of it
franta-hg@1
   166
   * @return The full url 
franta-hg@1
   167
   */
franta-hg@1
   168
  public static String buildImgURL(
franta-hg@1
   169
		  String renderer, PageContext pageContext, String sessionKey, int width, int height, String mimeType,
franta-hg@1
   170
		  boolean forceSessionId, boolean removeAfterRender) {
franta-hg@1
   171
	renderer = fixAbsolutURL(renderer, pageContext);
franta-hg@1
   172
	final HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
franta-hg@1
   173
	StringBuffer url = new StringBuffer(response.encodeURL(renderer));
franta-hg@1
   174
	if ( url.toString().indexOf(SESSIONID_KEY) == -1 )
franta-hg@1
   175
	{
franta-hg@1
   176
		if (forceSessionId)
franta-hg@1
   177
		{
franta-hg@1
   178
			  final String sessionId = pageContext.getSession().getId();
franta-hg@1
   179
			  url.append(";" + SESSIONID_KEY + "=" + sessionId);			
franta-hg@1
   180
		}
franta-hg@1
   181
	}
franta-hg@1
   182
	url.append("?" + IMG_PARAM + "=" + sessionKey);
franta-hg@1
   183
	url.append(AMPERSAND + WIDTH_PARAM + "=" + width);
franta-hg@1
   184
	url.append(AMPERSAND + HEIGHT_PARAM + "=" + height);
franta-hg@1
   185
	if (removeAfterRender)
franta-hg@1
   186
	{
franta-hg@1
   187
		url.append(AMPERSAND + REMOVE_AFTER_RENDERING + "=true");		
franta-hg@1
   188
	}
franta-hg@1
   189
	url.append(AMPERSAND + "iehack=" + MIMEExtensionHelper.getExtensionForMimeType(mimeType));
franta-hg@1
   190
	return url.toString();  	
franta-hg@1
   191
  }
franta-hg@1
   192
  
franta-hg@1
   193
  /**
franta-hg@1
   194
   * To enable further server side scriptings on JSP output the session ID is
franta-hg@1
   195
   * always encoded into the image URL even if cookies are enabled on the client
franta-hg@1
   196
   * side.
franta-hg@1
   197
   */
franta-hg@1
   198
  protected String getImgURL()
franta-hg@1
   199
  {
franta-hg@1
   200
  	return buildImgURL(renderer, pageContext, sessionKey, width, height, mimeType, forceSessionId, removeAfterRender);
franta-hg@1
   201
  }
franta-hg@1
   202
franta-hg@1
   203
  public Object getRenderingInfo() throws CewolfException
franta-hg@1
   204
  {
franta-hg@1
   205
    return chartImageDefinition.getRenderingInfo();
franta-hg@1
   206
  }
franta-hg@1
   207
franta-hg@1
   208
  protected String getMimeType()
franta-hg@1
   209
  {
franta-hg@1
   210
    return mimeType;
franta-hg@1
   211
  }
franta-hg@1
   212
franta-hg@1
   213
  protected void reset()
franta-hg@1
   214
  {
franta-hg@1
   215
    this.mimeType = DEFAULT_MIME_TYPE;
franta-hg@1
   216
    // as of a weird JSP compiler in resin
franta-hg@1
   217
    // a reused tag's attribute is only set if
franta-hg@1
   218
    // it changes. So width an height may not
franta-hg@1
   219
    // be unset to ensure correct values.
franta-hg@1
   220
    int lHeight = this.height;
franta-hg@1
   221
    int lWidth = this.width;
franta-hg@1
   222
    int lTimeout = this.timeout;
franta-hg@1
   223
    super.reset();
franta-hg@1
   224
    this.height = lHeight;
franta-hg@1
   225
    this.width = lWidth;
franta-hg@1
   226
    this.timeout = lTimeout;
franta-hg@1
   227
  }
franta-hg@1
   228
franta-hg@1
   229
  public void enableMapping()
franta-hg@1
   230
  {
franta-hg@1
   231
    setUsemap("#" + chartId);
franta-hg@1
   232
  }
franta-hg@1
   233
franta-hg@1
   234
  public String getChartId()
franta-hg@1
   235
  {
franta-hg@1
   236
    return getChartid();
franta-hg@1
   237
  }
franta-hg@1
   238
franta-hg@1
   239
  public void setChartid( String id )
franta-hg@1
   240
  {
franta-hg@1
   241
    this.chartId = id;
franta-hg@1
   242
  }
franta-hg@1
   243
franta-hg@1
   244
  public String getChartid()
franta-hg@1
   245
  {
franta-hg@1
   246
    return chartId;
franta-hg@1
   247
  }
franta-hg@1
   248
franta-hg@1
   249
  public void setRenderer( String renderer )
franta-hg@1
   250
  {
franta-hg@1
   251
    this.renderer = renderer;
franta-hg@1
   252
  }
franta-hg@1
   253
franta-hg@1
   254
  protected String getRenderer()
franta-hg@1
   255
  {
franta-hg@1
   256
    return this.renderer;
franta-hg@1
   257
  }
franta-hg@1
   258
franta-hg@1
   259
  public int getWidth()
franta-hg@1
   260
  {
franta-hg@1
   261
    return this.width;
franta-hg@1
   262
  }
franta-hg@1
   263
franta-hg@1
   264
  public int getHeight()
franta-hg@1
   265
  {
franta-hg@1
   266
    return height;
franta-hg@1
   267
  }
franta-hg@1
   268
franta-hg@1
   269
  /**
franta-hg@1
   270
   * Sets the mimeType.
franta-hg@1
   271
   * 
franta-hg@1
   272
   * @param mimeType
franta-hg@1
   273
   *          The mimeType to set
franta-hg@1
   274
   */
franta-hg@1
   275
  public void setMime( String mimeType )
franta-hg@1
   276
  {
franta-hg@1
   277
    this.mimeType = mimeType;
franta-hg@1
   278
  }
franta-hg@1
   279
franta-hg@1
   280
  /**
franta-hg@1
   281
   * @see de.laures.cewolf.taglib.html.AbstractHTMLBaseTag#getTagName()
franta-hg@1
   282
   */
franta-hg@1
   283
  protected String getTagName()
franta-hg@1
   284
  {
franta-hg@1
   285
    if ( MIME_SVG.equals(mimeType) )
franta-hg@1
   286
    {
franta-hg@1
   287
      return TAG_NAME_SVG;
franta-hg@1
   288
    }
franta-hg@1
   289
    return super.getTagName();
franta-hg@1
   290
  }
franta-hg@1
   291
franta-hg@1
   292
  /**
franta-hg@1
   293
   * @see de.laures.cewolf.taglib.html.AbstractHTMLBaseTag#writeAttributes(Writer)
franta-hg@1
   294
   */
franta-hg@1
   295
  public void writeAttributes( Writer wr )
franta-hg@1
   296
  {
franta-hg@1
   297
    super.writeAttributes(wr);
franta-hg@1
   298
    if ( MIME_SVG.equals(mimeType) )
franta-hg@1
   299
    {
franta-hg@1
   300
      try
franta-hg@1
   301
      {
franta-hg@1
   302
        appendAttributeDeclaration(wr, "http://www.adobe.com/svg/viewer/install/", "PLUGINSPAGE");
franta-hg@1
   303
      }
franta-hg@1
   304
      catch (IOException ioex)
franta-hg@1
   305
      {
franta-hg@1
   306
        ioex.printStackTrace();
franta-hg@1
   307
      }
franta-hg@1
   308
    }
franta-hg@1
   309
  }
franta-hg@1
   310
franta-hg@1
   311
  /**
franta-hg@1
   312
   * @return Returns the timeout.
franta-hg@1
   313
   */
franta-hg@1
   314
  public int getTimeout()
franta-hg@1
   315
  {
franta-hg@1
   316
    return timeout;
franta-hg@1
   317
  }
franta-hg@1
   318
  /**
franta-hg@1
   319
   * @param timeout The timeout to set.
franta-hg@1
   320
   */
franta-hg@1
   321
  public void setTimeout( int timeout )
franta-hg@1
   322
  {
franta-hg@1
   323
    this.timeout = timeout;
franta-hg@1
   324
  }
franta-hg@1
   325
}