1
|
1 package digilib.servlet;
|
|
2
|
500
|
3 import java.awt.Image;
|
|
4 import java.awt.geom.Rectangle2D;
|
|
5 import java.io.BufferedOutputStream;
|
|
6 import java.io.File;
|
|
7 import java.io.IOException;
|
|
8 import java.io.InputStream;
|
|
9 import java.io.OutputStream;
|
|
10 import java.util.List;
|
|
11
|
|
12 import javax.servlet.ServletConfig;
|
|
13 import javax.servlet.ServletContext;
|
|
14 import javax.servlet.ServletException;
|
|
15 import javax.servlet.http.HttpServletRequest;
|
|
16 import javax.servlet.http.HttpServletResponse;
|
1
|
17
|
500
|
18 import org.apache.log4j.Logger;
|
1
|
19
|
500
|
20 import digilib.auth.AuthOpException;
|
|
21 import digilib.auth.AuthOps;
|
|
22 import digilib.image.ImageOpException;
|
|
23 import digilib.io.DocuDirCache;
|
|
24 import digilib.io.DocuDirent;
|
502
|
25 import digilib.io.FileOpException;
|
500
|
26 import digilib.io.FileOps;
|
|
27 import digilib.io.ImageFile;
|
|
28 import digilib.io.ImageFileset;
|
1
|
29
|
500
|
30 public class Scaler extends RequestHandler {
|
|
31
|
|
32 /** digilib servlet version (for all components) */
|
|
33 public static final String dlVersion = "1.7.0b";
|
1
|
34
|
500
|
35 /** general error code */
|
|
36 public static final int ERROR_UNKNOWN = 0;
|
|
37
|
|
38 /** error code for authentication error */
|
|
39 public static final int ERROR_AUTH = 1;
|
|
40
|
|
41 /** error code for file operation error */
|
|
42 public static final int ERROR_FILE = 2;
|
|
43
|
|
44 /** error code for image operation error */
|
|
45 public static final int ERROR_IMAGE = 3;
|
|
46
|
|
47 /** DocuDirCache instance */
|
|
48 DocuDirCache dirCache;
|
1
|
49
|
500
|
50 /** authentication error image file */
|
|
51 File denyImgFile;
|
1
|
52
|
500
|
53 /** image error image file */
|
|
54 File errorImgFile;
|
|
55
|
|
56 /** not found error image file */
|
|
57 File notfoundImgFile;
|
1
|
58
|
500
|
59 /** subsampling before scaling */
|
|
60 float minSubsample = 2f;
|
1
|
61
|
500
|
62 /** send files as is? */
|
|
63 boolean sendFileAllowed = true;
|
|
64
|
|
65 /** default scaling quality */
|
|
66 int defaultQuality = 1;
|
1
|
67
|
500
|
68 /** DigilibConfiguration instance */
|
|
69 DigilibConfiguration dlConfig;
|
|
70
|
|
71 /** use authorization database */
|
|
72 boolean useAuthorization = true;
|
|
73
|
|
74 /** AuthOps instance */
|
|
75 AuthOps authOp;
|
|
76
|
|
77 // EXPRIMENTAL
|
|
78 /** try to enlarge cropping area for "oblique" angles */
|
|
79 boolean wholeRotArea = false;
|
1
|
80
|
500
|
81 /**
|
|
82 * Initialisation on first run.
|
|
83 *
|
|
84 * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
|
|
85 */
|
|
86 public void init(ServletConfig config) throws ServletException {
|
|
87 super.init(config);
|
|
88
|
|
89 System.out
|
|
90 .println("***** Digital Image Library Image Scaler Servlet (version "
|
|
91 + dlVersion + ") *****");
|
|
92 // say hello in the log file
|
|
93 logger
|
|
94 .info("***** Digital Image Library Image Scaler Servlet (version "
|
|
95 + dlVersion + ") *****");
|
1
|
96
|
500
|
97 // get our ServletContext
|
|
98 ServletContext context = config.getServletContext();
|
|
99 // see if there is a Configuration instance
|
|
100 dlConfig = (DigilibConfiguration) context
|
|
101 .getAttribute("digilib.servlet.configuration");
|
|
102 if (dlConfig == null) {
|
|
103 // no Configuration
|
|
104 throw new ServletException("No Configuration!");
|
|
105 }
|
|
106 // set our AuthOps
|
|
107 useAuthorization = dlConfig.getAsBoolean("use-authorization");
|
|
108 authOp = (AuthOps) dlConfig.getValue("servlet.auth.op");
|
1
|
109
|
500
|
110 // DocuDirCache instance
|
|
111 dirCache = (DocuDirCache) dlConfig.getValue("servlet.dir.cache");
|
|
112 denyImgFile = ServletOps.getFile((File) dlConfig.getValue("denied-image"), config);
|
|
113 errorImgFile = ServletOps.getFile((File) dlConfig.getValue("error-image"), config);
|
|
114 notfoundImgFile = ServletOps.getFile((File) dlConfig.getValue("notfound-image"), config);
|
|
115 sendFileAllowed = dlConfig.getAsBoolean("sendfile-allowed");
|
|
116 minSubsample = dlConfig.getAsFloat("subsample-minimum");
|
|
117 defaultQuality = dlConfig.getAsInt("default-quality");
|
|
118 }
|
1
|
119
|
500
|
120 @Override
|
|
121 public void processRequest(HttpServletRequest request,
|
|
122 HttpServletResponse response) throws ServletException, ImageOpException {
|
1
|
123
|
500
|
124
|
|
125 if (dlConfig == null) {
|
|
126 throw new ServletException("ERROR: No Configuration!");
|
|
127 }
|
|
128
|
502
|
129
|
|
130
|
500
|
131 accountlog.debug("request: " + request.getQueryString());
|
|
132 logger.debug("request: " + request.getQueryString());
|
|
133
|
|
134
|
|
135
|
|
136 // define the job information
|
|
137 ImageJobInformation jobdeclaration = new ImageJobInformation();
|
|
138 jobdeclaration.setWithRequest(request);
|
|
139 jobdeclaration.setConfig(dlConfig);
|
|
140
|
502
|
141 ImageFile fileToLoad = null;
|
|
142 try {
|
|
143 fileToLoad = jobdeclaration.get_fileToLoad();
|
|
144 } catch (IOException e2) {
|
|
145 // TODO Auto-generated catch block
|
|
146 e2.printStackTrace();
|
|
147 return;
|
|
148 }
|
500
|
149
|
|
150
|
502
|
151 // if requested, send image as a file
|
|
152 if(sendFileAllowed && jobdeclaration.checkSendAsFile()){
|
|
153 String mt = null;
|
|
154 if (jobdeclaration.hasOption("mo", "rawfile")) {
|
|
155 mt = "application/octet-stream";
|
|
156 }
|
|
157 logger.debug("Sending RAW File as is.");
|
|
158 try {
|
|
159 ServletOps.sendFile(fileToLoad.getFile(), mt, response);
|
|
160 } catch (FileOpException e) {
|
|
161 e.printStackTrace();
|
|
162 }
|
|
163
|
|
164 return;
|
|
165 }
|
|
166
|
500
|
167
|
|
168
|
502
|
169 // if possible, send the image without actually having to transform it
|
|
170 if(jobdeclaration.noTransformRequired()){
|
|
171 logger.debug("Sending File as is.");
|
|
172
|
|
173 try {
|
|
174 ServletOps.sendFile(fileToLoad.getFile(), null, response);
|
|
175 } catch (FileOpException e) {
|
|
176 e.printStackTrace();
|
|
177 }
|
|
178
|
|
179 //logger.info("Done in "
|
|
180 // + (System.currentTimeMillis() - startTime) + "ms");
|
|
181 return;
|
|
182 }
|
|
183
|
|
184
|
|
185
|
500
|
186
|
|
187 OutputStream outputstream = null;
|
|
188 try {
|
|
189 outputstream = response.getOutputStream();
|
|
190 } catch (IOException e1) {
|
|
191 // TODO Auto-generated catch block
|
|
192 e1.printStackTrace();
|
|
193 logger.error(e1.getMessage());
|
|
194 }
|
502
|
195
|
|
196
|
|
197
|
|
198
|
|
199 if (! DigilibWorker.canRun()) {
|
|
200 logger.error("Servlet overloaded!");
|
|
201 try {
|
|
202 response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
|
|
203 } catch (IOException e) {
|
|
204 e.printStackTrace();
|
|
205 }
|
|
206 return;
|
|
207 }
|
|
208
|
|
209
|
|
210
|
500
|
211
|
|
212
|
|
213 DigilibWorker job=null;
|
|
214 try {
|
|
215
|
|
216 long startTime = System.currentTimeMillis();
|
1
|
217
|
501
|
218 /* check permissions */
|
|
219 if (useAuthorization) {
|
|
220 // get a list of required roles (empty if no restrictions)
|
|
221 List rolesRequired;
|
|
222 try {
|
|
223 rolesRequired = authOp.rolesForPath(jobdeclaration.getFilePath(), request);
|
|
224 if (rolesRequired != null) {
|
|
225 authlog.debug("Role required: " + rolesRequired);
|
|
226 authlog.debug("User: " + request.getRemoteUser());
|
|
227 // is the current request/user authorized?
|
|
228 if (!authOp.isRoleAuthorized(rolesRequired, request)) {
|
|
229 // send deny answer and abort
|
|
230 throw new AuthOpException();
|
|
231 }
|
|
232 }
|
|
233
|
|
234 } catch (AuthOpException e) {
|
|
235 // TODO Auto-generated catch block
|
|
236 e.printStackTrace();
|
|
237 }
|
|
238 }
|
|
239
|
500
|
240
|
|
241 job = new DigilibImageWorker(dlConfig,
|
|
242 outputstream ,
|
|
243 jobdeclaration.get_mimeType(),
|
|
244 jobdeclaration.get_scaleQual(),
|
|
245 jobdeclaration.getAsFloat("rot"),
|
|
246 jobdeclaration.getAsFloat("cont"),
|
|
247 jobdeclaration.getAsFloat("brgt"),
|
|
248 jobdeclaration.get_paramRGBM(),
|
|
249 jobdeclaration.get_paramRGBA(),
|
|
250 jobdeclaration.get_fileToLoad(),
|
501
|
251 jobdeclaration.get_scaleXY(),
|
500
|
252 jobdeclaration.get_outerUserImgArea(),
|
|
253 jobdeclaration.get_innerUserImgArea(),
|
|
254 minSubsample,
|
|
255 jobdeclaration.get_wholeRotArea(),
|
|
256 jobdeclaration.get_forceType(),
|
|
257 jobdeclaration.get_hmir(),
|
|
258 jobdeclaration.get_vmir());
|
1
|
259
|
500
|
260 job.run();
|
1
|
261
|
500
|
262 if (job.hasError()) {
|
|
263 throw new ImageOpException(job.getError().toString());
|
|
264 }
|
1
|
265
|
500
|
266 try {
|
|
267 outputstream.flush();
|
|
268 logger.debug("Job Processing Time: "+ (System.currentTimeMillis()-startTime) + "ms");
|
|
269 } catch (IOException e) {
|
|
270 e.printStackTrace();
|
|
271 logger.error(e.getMessage());
|
|
272 response.sendError(1);
|
|
273 }
|
1
|
274
|
500
|
275
|
|
276 } catch (IOException e) {
|
|
277 // TODO Auto-generated catch block
|
|
278 e.printStackTrace();
|
|
279 logger.error(e.getClass()+": "+ e.getMessage());
|
|
280 //response.sendError(1);
|
|
281 } catch (ImageOpException e) {
|
|
282 // TODO Auto-generated catch block
|
|
283 e.printStackTrace();
|
|
284 logger.error(e.getClass()+": "+ e.getMessage());
|
|
285 //response.sendError(1);
|
|
286 }
|
1
|
287
|
500
|
288
|
|
289
|
1
|
290
|
500
|
291
|
|
292 /*boolean errorMsgHtml = false;
|
|
293
|
|
294 if(jobdeclaration.hasOption("mo","errtxt")){
|
|
295 errorMsgHtml = true;
|
|
296 } else if (jobdeclaration.hasOption("mo","errimg")) {
|
|
297 errorMsgHtml = true;
|
|
298 }
|
|
299
|
|
300
|
|
301
|
|
302 */
|
|
303
|
|
304
|
|
305 }
|
1
|
306
|
500
|
307
|
|
308
|
|
309 /**
|
|
310 * Sends an error to the client as text or image.
|
|
311 *
|
|
312 * @param asHTML
|
|
313 * @param type
|
|
314 * @param msg
|
|
315 * @param response
|
|
316 */
|
|
317 public void digilibError(boolean asHTML, int type, String msg,
|
|
318 HttpServletResponse response) {
|
|
319 try {
|
|
320 File img = null;
|
|
321 if (type == ERROR_AUTH) {
|
|
322 if (msg == null) {
|
|
323 msg = "ERROR: Unauthorized access!";
|
|
324 }
|
|
325 img = denyImgFile;
|
|
326 } else if (type == ERROR_FILE) {
|
|
327 if (msg == null) {
|
|
328 msg = "ERROR: Image file not found!";
|
|
329 }
|
|
330 img = notfoundImgFile;
|
|
331 } else {
|
|
332 if (msg == null) {
|
|
333 msg = "ERROR: Other image error!";
|
|
334 }
|
|
335 img = this.errorImgFile;
|
|
336 }
|
|
337 if (asHTML && (img != null)) {
|
|
338 ServletOps.htmlMessage(msg, response);
|
|
339 } else {
|
|
340 ServletOps.sendFile(img, null, response);
|
|
341 }
|
|
342 } catch (IOException e) {
|
|
343 logger.error("Error sending error!", e);
|
|
344 }
|
1
|
345
|
500
|
346 }
|
1
|
347
|
500
|
348 public static String getVersion(){
|
|
349 return dlVersion;
|
|
350 }
|
|
351
|
1
|
352
|
500
|
353 }
|