You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-dev@jakarta.apache.org by ab...@apache.org on 2004/04/21 15:41:23 UTC
cvs commit: jakarta-taglibs-sandbox/image/src/org/apache/taglibs/image ImageTag.java
abey 2004/04/21 06:41:23
Modified: image/src/org/apache/taglibs/image ImageTag.java
Log:
Added encoding img src urls - Abey
Revision Changes Path
1.4 +283 -259 jakarta-taglibs-sandbox/image/src/org/apache/taglibs/image/ImageTag.java
Index: ImageTag.java
===================================================================
RCS file: /home/cvs/jakarta-taglibs-sandbox/image/src/org/apache/taglibs/image/ImageTag.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ImageTag.java 9 Oct 2003 15:20:02 -0000 1.3
+++ ImageTag.java 21 Apr 2004 13:41:23 -0000 1.4
@@ -52,7 +52,7 @@
* <http://www.apache.org/>.
*
*/
-
+
package org.apache.taglibs.image;
import java.awt.image.BufferedImage;
@@ -63,6 +63,7 @@
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.Tag;
@@ -72,269 +73,292 @@
import com.mullassery.imaging.util.Util;
/**
- * Images will be processed and saved in a specified directory.
- * The image tag is printed with the src pointing to the newly
- * created image.
- * The attribute to refresh will always generate the image. When it
- * is turned off, the image which was generated earlier is used. This
- * will save a lot of processing and memory usage (especially for usage
- * such as thumbnails etc.).
- * The display option allows one to specify an image to be displayed
- * or just created but not displayed.
+ * Images will be processed and saved in a specified directory. The image tag
+ * is printed with the src pointing to the newly created image. The attribute
+ * to refresh will always generate the image. When it is turned off, the image
+ * which was generated earlier is used. This will save a lot of processing and
+ * memory usage (especially for usage such as thumbnails etc.). The display
+ * option allows one to specify an image to be displayed or just created but
+ * not displayed.
*
- * For truly dynamic images that are made for a particular person
- * only and that is not required after that one use can turn on a
- * "dynamic" option/ attribute that will point it to a servlet
- * that will supply the image and delete it then.
+ * For truly dynamic images that are made for a particular person only and that
+ * is not required after that one use can turn on a "dynamic" option/
+ * attribute that will point it to a servlet that will supply the image and
+ * delete it then.
*
* @author Abey Mullassery
- *
+ *
*/
-public class ImageTag extends BodyTagSupport implements ImageHolder{
+public class ImageTag extends BodyTagSupport implements ImageHolder {
+
+ public static final String GEN_IMAGES_DIR = "gen-images";
+
+ private String src;
- public static final String GEN_IMAGES_DIR = "gen-images";
+ private String attributes;
- private String src;
- private String attributes;
- private String name;
- private boolean deleteOnExit = false;
- private boolean refresh = false;
- private boolean display = true;
- private String dir = GEN_IMAGES_DIR;
-
- private BufferedImage image;
- private File img;
- private Imaging imaging;
- private ServletContext context;
- private HttpServletRequest hr; //should this be ServletRequest?
- private String baseUrl;
- private String imagingType;
- private ImageHolder par;
-
- public int doStartTag() throws JspException {
- context = pageContext.getServletContext();
- hr = (HttpServletRequest) (pageContext.getRequest());
- String reqUrl = hr.getRequestURL().toString();
- baseUrl = reqUrl.substring(0, reqUrl.indexOf(hr.getRequestURI()));
-
- if (refresh && src == null) {
- throw new JspException("Image \"src\" should be specified to refresh forcefully");
- }
- //serving as a nested Tag to the Overlay tag?
- Tag pTag = getParent();
- if (pTag != null && (pTag instanceof ImageHolder)) {
- par = (ImageHolder)getParent();
- if (src == null && name == null) { //taking an existing image
- throw new JspException("Image \"src\" or \"name\" should be specified");
- }
- } else {
- if (src == null) {
- throw new JspException("Image \"src\" should be specified");
- }
- }
-
- //don't throw exceptions for image errors like the <img> tag
- try {
- //if the name is not specified the src will be specified
- if (name == null) {
- URL url = getUrl(src);
- name = url.getFile().substring(url.getFile().lastIndexOf('/') + 1);
- }
- String imgFile = Util.extractFileName(name);
- img = new File(context.getRealPath("/") + dir, imgFile);
-
- createImaging();
- if (refresh || !img.exists()) {
- refresh = true;
- image = imaging.read(getUrl(src));
- } else {
- image = imaging.read(img);
- }
- } catch (Throwable e) {
- refresh = false; //keep quiet; no errors
- }
-
- if (refresh)
- return EVAL_BODY_BUFFERED;
- else
- return SKIP_BODY;
- }
-
- private void createImaging() throws JspException {
- //use init variables to select JAI, Java2D, SWT, etc.
- if (imagingType == null || imagingType.length() == 0) {
- imagingType = (String)pageContext.findAttribute("ImageTag.imagingType");
- }
- if (imagingType != null && imagingType.length() > 0) {
- if (imagingType.equalsIgnoreCase("JAI")) {
- imaging = ImagingFactory.createImagingInstance(ImagingFactory.JAI);
- } else if (imagingType.equalsIgnoreCase("JAVA2D")) {
- imaging = ImagingFactory.createImagingInstance(ImagingFactory.JAVA2D);
- } else {
- throw new IllegalArgumentException("Unknown Imaging type specified!");
- }
- } else {
- imaging = ImagingFactory.createImagingInstance();
- }
- //TODO: use init variables to set delteOnExit and refresh to true or false
- }
-
- public int doAfterBody() throws JspException {
- bodyContent.clearBody();
- return SKIP_BODY;
- }
-
- public int doEndTag() throws JspException {
- if (image == null)
- return EVAL_PAGE;
-
- //serving as a nested Tag to the Overlay tag?
- if (par != null) {
- //just pass the image; no need to write/save
- par.setImage(image);
- return EVAL_PAGE;
- }
-
- if (refresh) {
- File f = img.getParentFile();
- if (!f.exists())
- f.mkdirs();
- if (deleteOnExit) {
- img.deleteOnExit();
- f.deleteOnExit();
- }
- saveImage();
- }
- //if the image need not be displayed
- if (!display) {
- this.image = null;
- return EVAL_PAGE;
- }
-
- StringBuffer html = new StringBuffer("<img ");
- if (attributes != null && attributes.length() != 0) {
- html.append(attributes);
- } else {
- attributes = ""; //a work around for the following conditions.
- }
- //checking if the user has already specified the attributes.
- String al = attributes.toLowerCase();
- if (al.indexOf(" src") == -1) {
- html.append(" src=" + '"' + getUrl('/' + dir + '/' + img.getName()) + '"');
- }
- if (al.indexOf("width") == -1 && al.indexOf("height") == -1) {
- html.append(" width=\"" + image.getWidth() + "\" height=\"" + image.getHeight() + "\"");
- }
- html.append(" />");
- try {
- pageContext.getOut().println(html.toString());
- } catch (IOException e) {
- context.log("Error writing <img> tag-html " + html, e);
- throw new JspException(e);
- }
- this.image = null;
- return EVAL_PAGE;
- }
-
- public void release() {
- this.image = null;
- super.release();
- System.gc();
- }
-
- public void saveImage() {
- String type = Util.getExtentionType(img);
- if (type.equalsIgnoreCase("gif")) { //till GIF is supported
- type = "JPEG";
- String imgName = img.getAbsolutePath();
- int ind_dot = imgName.indexOf('.');
- imgName = imgName.substring(0, ind_dot + 1) + "jpg";
- }
- try {
- com.mullassery.imaging.util.Util.saveImage(imaging, image, type, img);
- } catch (IOException e) {
- new JspException(e);
- }
- }
-
- public URL getUrl(String srcStr) throws JspException {
- if (srcStr.startsWith("./")) //will just act like one without it
- srcStr = srcStr.substring(2);
- String contextUrl = baseUrl + hr.getContextPath();
-
- if (!srcStr.startsWith(hr.getScheme())) {
- if (srcStr.startsWith("/")) {
- if (srcStr.startsWith(hr.getContextPath() + '/'))
- srcStr = baseUrl + srcStr;
- else
- srcStr = contextUrl + srcStr;
- } else {
- String parUrl = hr.getRequestURL().toString(); // can be /xyz?a=b
- // if like "/some/path.html" trim till "/"
- if (parUrl.indexOf(".") != -1) {
- parUrl = parUrl.substring(0, parUrl.lastIndexOf("/"));
- }
-
- // for every "../" trim till last "/" - till context path is reached
- while (srcStr.startsWith("../")) { // require parent directory?
- srcStr = srcStr.substring(3); // remove the "../" anyway
- if (parUrl.indexOf('/', contextUrl.length() - 1) != -1)
- parUrl = parUrl.substring(0, parUrl.lastIndexOf('/'));
- }
- srcStr = parUrl + '/' + srcStr;
- }
- }
- URL url = null;
- try {
- url = new URL(srcStr);
- } catch (MalformedURLException e) {
- context.log("Not a proper URL: " + srcStr, e);
- throw new JspException(e);
- }
- return url;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public void setAttributes(String attributes) {
- this.attributes = attributes;
- }
-
- public void setDir(String dir) {
- this.dir = dir;
- }
-
- public void setSrc(String src) {
- this.src = src;
- }
-
- public Imaging getImaging() {
- return this.imaging;
- }
-
- public BufferedImage getImage() {
- return this.image;
- }
-
- public void setImage(BufferedImage image) {
- this.image = image;
- }
-
- public void setRefresh(boolean refresh) {
- this.refresh = refresh;
- }
-
- public String getDir() {
- return this.dir;
- }
- public void setDisplay(boolean display) {
- this.display = display;
- }
-
- public void setImagingType(String imagingType) {
- this.imagingType = imagingType;
- }
+ private String name;
+
+ private boolean deleteOnExit = false;
+
+ private boolean refresh = false;
+
+ private boolean display = true;
+
+ private String dir = GEN_IMAGES_DIR;
+
+ private BufferedImage image;
+
+ private File img;
+
+ private Imaging imaging;
+
+ private ServletContext context;
+
+ private HttpServletRequest hr; //should this be ServletRequest?
+
+ private String baseUrl;
+
+ private String imagingType;
+
+ private ImageHolder par;
+
+ public int doStartTag() throws JspException {
+ context = pageContext.getServletContext();
+ hr = (HttpServletRequest) (pageContext.getRequest());
+ String reqUrl = hr.getRequestURL().toString();
+ baseUrl = reqUrl.substring(0, reqUrl.indexOf(hr.getRequestURI()));
+
+ if (refresh && src == null) { throw new JspException(
+ "Image \"src\" should be specified to refresh forcefully"); }
+ //serving as a nested Tag to the Overlay tag?
+ Tag pTag = getParent();
+ if (pTag != null && (pTag instanceof ImageHolder)) {
+ par = (ImageHolder) getParent();
+ if (src == null && name == null) { //taking an existing image
+ throw new JspException(
+ "Image \"src\" or \"name\" should be specified"); }
+ } else {
+ if (src == null) { throw new JspException(
+ "Image \"src\" should be specified"); }
+ }
+
+ //don't throw exceptions for image errors like the <img> tag
+ try {
+ //if the name is not specified the src will be specified
+ if (name == null) {
+ URL url = getUrl(src);
+ name = url.getFile().substring(
+ url.getFile().lastIndexOf('/') + 1);
+ }
+ String imgFile = Util.extractFileName(name);
+ img = new File(context.getRealPath("/") + dir, imgFile);
+
+ createImaging();
+ if (refresh || !img.exists()) {
+ refresh = true;
+ image = imaging.read(getUrl(src));
+ } else {
+ image = imaging.read(img);
+ }
+ } catch (Throwable e) {
+ refresh = false; //keep quiet; no errors
+ }
+
+ if (refresh)
+ return EVAL_BODY_BUFFERED;
+ else
+ return SKIP_BODY;
+ }
+
+ private void createImaging() throws JspException {
+ //use init variables to select JAI, Java2D, SWT, etc.
+ if (imagingType == null || imagingType.length() == 0) {
+ imagingType = (String) pageContext
+ .findAttribute("ImageTag.imagingType");
+ }
+ if (imagingType != null && imagingType.length() > 0) {
+ if (imagingType.equalsIgnoreCase("JAI")) {
+ imaging = ImagingFactory
+ .createImagingInstance(ImagingFactory.JAI);
+ } else if (imagingType.equalsIgnoreCase("JAVA2D")) {
+ imaging = ImagingFactory
+ .createImagingInstance(ImagingFactory.JAVA2D);
+ } else {
+ throw new IllegalArgumentException(
+ "Unknown Imaging type specified!");
+ }
+ } else {
+ imaging = ImagingFactory.createImagingInstance();
+ }
+ //TODO: use init variables to set delteOnExit and refresh to true or
+ // false
+ }
+
+ public int doAfterBody() throws JspException {
+ bodyContent.clearBody();
+ return SKIP_BODY;
+ }
+
+ public int doEndTag() throws JspException {
+ if (image == null) return EVAL_PAGE;
+
+ //serving as a nested Tag to the Overlay tag?
+ if (par != null) {
+ //just pass the image; no need to write/save
+ par.setImage(image);
+ return EVAL_PAGE;
+ }
+
+ if (refresh) {
+ File f = img.getParentFile();
+ if (!f.exists()) f.mkdirs();
+ if (deleteOnExit) {
+ img.deleteOnExit();
+ f.deleteOnExit();
+ }
+ saveImage();
+ }
+ //if the image need not be displayed
+ if (!display) {
+ this.image = null;
+ return EVAL_PAGE;
+ }
+
+ StringBuffer html = new StringBuffer("<img ");
+ if (attributes != null && attributes.length() != 0) {
+ html.append(attributes);
+ } else {
+ attributes = ""; //a work around for the following conditions.
+ }
+ //checking if the user has already specified the attributes.
+ String al = attributes.toLowerCase();
+ if (al.indexOf(" src") == -1) {
+ HttpServletResponse response = (HttpServletResponse) pageContext
+ .getResponse();
+ String encodedUrl = response.encodeURL(getUrl(
+ '/' + dir + '/' + img.getName()).toString());
+ html.append(" src=" + '"' + encodedUrl + '"');
+ }
+ if (al.indexOf("width") == -1 && al.indexOf("height") == -1) {
+ html.append(" width=\"" + image.getWidth() + "\" height=\""
+ + image.getHeight() + "\"");
+ }
+ html.append(" />");
+ try {
+ pageContext.getOut().println(html.toString());
+ } catch (IOException e) {
+ context.log("Error writing <img> tag-html " + html, e);
+ throw new JspException(e);
+ }
+ this.image = null;
+ return EVAL_PAGE;
+ }
+
+ public void release() {
+ this.image = null;
+ super.release();
+ System.gc();
+ }
+
+ public void saveImage() {
+ String type = Util.getExtentionType(img);
+ if (type.equalsIgnoreCase("gif")) { //till GIF is supported
+ type = "JPEG";
+ String imgName = img.getAbsolutePath();
+ int ind_dot = imgName.indexOf('.');
+ imgName = imgName.substring(0, ind_dot + 1) + "jpg";
+ }
+ try {
+ com.mullassery.imaging.util.Util.saveImage(imaging, image, type,
+ img);
+ } catch (IOException e) {
+ new JspException(e);
+ }
+ }
+
+ public URL getUrl(String srcStr) throws JspException {
+ if (srcStr.startsWith("./")) //will just act like one without it
+ srcStr = srcStr.substring(2);
+ String contextUrl = baseUrl + hr.getContextPath();
+
+ if (!srcStr.startsWith(hr.getScheme())) {
+ if (srcStr.startsWith("/")) {
+ if (srcStr.startsWith(hr.getContextPath() + '/'))
+ srcStr = baseUrl + srcStr;
+ else
+ srcStr = contextUrl + srcStr;
+ } else {
+ String parUrl = hr.getRequestURL().toString(); // can be
+ // /xyz?a=b
+ // if like "/some/path.html" trim till "/"
+ if (parUrl.indexOf(".") != -1) {
+ parUrl = parUrl.substring(0, parUrl.lastIndexOf("/"));
+ }
+
+ // for every "../" trim till last "/" - till context path is
+ // reached
+ while (srcStr.startsWith("../")) { // require parent directory?
+ srcStr = srcStr.substring(3); // remove the "../" anyway
+ if (parUrl.indexOf('/', contextUrl.length() - 1) != -1)
+ parUrl = parUrl.substring(0, parUrl
+ .lastIndexOf('/'));
+ }
+ srcStr = parUrl + '/' + srcStr;
+ }
+ }
+ URL url = null;
+ try {
+ url = new URL(srcStr);
+ } catch (MalformedURLException e) {
+ context.log("Not a proper URL: " + srcStr, e);
+ throw new JspException(e);
+ }
+ return url;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public void setAttributes(String attributes) {
+ this.attributes = attributes;
+ }
+
+ public void setDir(String dir) {
+ this.dir = dir;
+ }
+
+ public void setSrc(String src) {
+ this.src = src;
+ }
+
+ public Imaging getImaging() {
+ return this.imaging;
+ }
+
+ public BufferedImage getImage() {
+ return this.image;
+ }
+
+ public void setImage(BufferedImage image) {
+ this.image = image;
+ }
+
+ public void setRefresh(boolean refresh) {
+ this.refresh = refresh;
+ }
+
+ public String getDir() {
+ return this.dir;
+ }
+
+ public void setDisplay(boolean display) {
+ this.display = display;
+ }
+
+ public void setImagingType(String imagingType) {
+ this.imagingType = imagingType;
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-dev-help@jakarta.apache.org