KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > servlet > ImageServlet


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.servlet;
11
12 import javax.servlet.http.HttpServletResponse JavaDoc;
13 import javax.servlet.ServletException JavaDoc;
14
15 import java.util.Map JavaDoc;
16
17 import org.mmbase.bridge.*;
18 import org.mmbase.storage.search.*;
19 import org.mmbase.security.Rank;
20
21 import org.mmbase.module.builders.Images;
22 import org.mmbase.util.logging.*;
23 import org.mmbase.util.functions.*;
24
25 /**
26  * ImageServlet handles nodes as images. If you want to convert an image (resize it, turn it, change
27  * its colors etc) then you want to serve an 'icaches' node ('icaches' are cached conversions of
28  * images), which you have to create yourself before calling this servlet. The cache() function of
29  * Images can be used for this. An URL can be gotten with cachepath().
30  *
31  * @version $Id: ImageServlet.java,v 1.28 2006/01/27 20:20:43 michiel Exp $
32  * @author Michiel Meeuwissen
33  * @since MMBase-1.6
34  * @see org.mmbase.module.builders.AbstractImages
35  * @see org.mmbase.module.builders.Images#executeFunction
36  * @see AttachmentServlet
37  */

38 public class ImageServlet extends HandleServlet {
39     private static Logger log;
40
41     /**
42      * Wheter this servlet is capable of doing transformations by itself.
43      * @since MMBase-1.7.4
44      */

45     private boolean convert = false;
46
47     public void init() throws ServletException JavaDoc {
48         super.init();
49         String JavaDoc convertParameter = getInitParameter("convert");
50         convert = "true".equals(convertParameter);
51         log = Logging.getLoggerInstance(ImageServlet.class);
52         if (convert) {
53             log.service("Image servlet will accept image conversion templates");
54         }
55     }
56
57     // just to get ImageServlet in the stacktrace.
58
protected final Cloud getClassCloud() {
59         return super.getClassCloud();
60     }
61
62     public String JavaDoc getServletInfo() {
63         return "Serves (cached) MMBase images";
64     }
65
66     protected Map JavaDoc getAssociations() {
67         Map JavaDoc a = super.getAssociations();
68         a.put("images", new Integer JavaDoc(50)); // Is good in images (knows icaches)
69
a.put("attachments", new Integer JavaDoc(5)); // Can do attachments a little
70
a.put("downloads", new Integer JavaDoc(-10)); // Can do downloads even worse.
71
return a;
72     }
73
74     protected String JavaDoc getMimeType(Node node) {
75         return node.getFunctionValue("mimetype", null).toString();
76     }
77     
78
79     /**
80      * Content-Disposition header
81      * {@inheritDoc}
82      */

83
84     protected boolean setContent(QueryParts query, Node node, String JavaDoc mimeType) throws java.io.IOException JavaDoc {
85         Node originalNode;
86         if (node.getNodeManager().getName().equals("icaches")) {
87             Cloud c = node.getCloud();
88             int originalNodeNumber = node.getIntValue("id");
89
90             if (! c.mayRead(originalNodeNumber) && c.getUser().getRank().equals(Rank.ANONYMOUS)) {
91                 // try (again?) cloud from session
92
c = getCloud(query);
93             }
94
95             if (c == null || ! c.mayRead(originalNodeNumber)) {
96                 query.getResponse().sendError(HttpServletResponse.SC_FORBIDDEN, "Permission denied on original image node '" + originalNodeNumber + "'");
97                 return false;
98             }
99             originalNode = c.getNode(originalNodeNumber);
100         } else { // 'images', but as you see this is not explicit, so you can also name your image builder otherwise.
101
originalNode = node;
102         }
103
104         query.getResponse().setHeader("Content-Disposition", "inline; filename=\"" + getFileName(node, originalNode, "mmbase-image")+ "\"");
105         return true;
106     }
107
108     /**
109      * ImageServlet can serve a icache node in stead (using the 'extra parameters)'
110      *
111      * @since MMBase-1.7.4
112      */

113     protected Node getServedNode(QueryParts query, Node node) throws java.io.IOException JavaDoc {
114         if (node == null) {
115             return null;
116         }
117         Node n = query.getServedNode();
118         if (n != null) {
119             return n;
120         }
121         String JavaDoc nodeNumber = query.getNodeNumber();
122         String JavaDoc nodeIdentifier = query.getNodeIdentifier();
123         if (node.getNodeManager().getName().equals("icaches")) {
124             if (! nodeNumber.equals(nodeIdentifier)) {
125                 query.getResponse().sendError(HttpServletResponse.SC_FORBIDDEN, "Cannot convert icache node");
126                 return null;
127             } else {
128                 n = getNode(query);
129                 n.getFunctionValue("wait", null);
130             }
131         } else {
132             // This _is_ an original node.
133
if (! nodeNumber.equals(nodeIdentifier)) {
134                 if (convert) {
135                     Parameters args = new Parameters(Images.CACHE_PARAMETERS);
136                     String JavaDoc template = nodeIdentifier.substring(nodeNumber.length() + 1);
137                     try {
138                         template = java.net.URLDecoder.decode(template, "UTF-8");
139                     } catch (IllegalArgumentException JavaDoc iae) {
140                         // never mind
141
}
142                     args.set("template", template);
143                     int icacheNodeNumber = node.getFunctionValue("cache", args).toInt();
144                     Cloud cloud = node.getCloud();
145                     cloud = findCloud(cloud, "" + icacheNodeNumber, query);
146                     if (cloud == null) {
147                         return null;
148                     }
149                     Node icache = cloud.getNode(icacheNodeNumber);
150                     icache.getFunctionValue("wait", null);
151                     n = icache;
152                 } else {
153                     query.getResponse().sendError(HttpServletResponse.SC_FORBIDDEN, "This server does not allow you to convert an image in this way");
154                     return null;
155                 }
156             } else {
157                 n = getNode(query);
158             }
159         }
160         query.setServedNode(n);
161         return n;
162     }
163
164     /**
165      * Overridden to support 'title aliases'.
166      * @since MMBase-1.7.5
167      */

168     protected Node desperatelyGetNode(Cloud cloud, String JavaDoc nodeIdentifier) {
169         log.debug("Desperately searching node '" + nodeIdentifier + "'");
170         NodeManager nm = cloud.getNodeManager("images");
171         NodeQuery nq = nm.createQuery();
172         Constraint c = nq.createConstraint(nq.createStepField("title"), nodeIdentifier);
173         nq.setConstraint(c);
174         nq.addSortOrder(nq.createStepField("number"), SortOrder.ORDER_DESCENDING);
175         NodeList result = nm.getList(nq);
176         if (result.size() == 0) return null;
177         return result.getNode(0);
178     }
179
180
181 }
182
Popular Tags