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;
|
|
25 import digilib.io.FileOps;
|
|
26 import digilib.io.ImageFile;
|
|
27 import digilib.io.ImageFileset;
|
1
|
28
|
500
|
29 public class Scaler extends RequestHandler {
|
|
30
|
|
31 /** digilib servlet version (for all components) */
|
|
32 public static final String dlVersion = "1.7.0b";
|
1
|
33
|
500
|
34 /** general error code */
|
|
35 public static final int ERROR_UNKNOWN = 0;
|
|
36
|
|
37 /** error code for authentication error */
|
|
38 public static final int ERROR_AUTH = 1;
|
|
39
|
|
40 /** error code for file operation error */
|
|
41 public static final int ERROR_FILE = 2;
|
|
42
|
|
43 /** error code for image operation error */
|
|
44 public static final int ERROR_IMAGE = 3;
|
|
45
|
|
46 /** DocuDirCache instance */
|
|
47 DocuDirCache dirCache;
|
1
|
48
|
500
|
49 /** authentication error image file */
|
|
50 File denyImgFile;
|
1
|
51
|
500
|
52 /** image error image file */
|
|
53 File errorImgFile;
|
|
54
|
|
55 /** not found error image file */
|
|
56 File notfoundImgFile;
|
1
|
57
|
500
|
58 /** subsampling before scaling */
|
|
59 float minSubsample = 2f;
|
1
|
60
|
500
|
61 /** send files as is? */
|
|
62 boolean sendFileAllowed = true;
|
|
63
|
|
64 /** default scaling quality */
|
|
65 int defaultQuality = 1;
|
1
|
66
|
500
|
67 /** DigilibConfiguration instance */
|
|
68 DigilibConfiguration dlConfig;
|
|
69
|
|
70 /** use authorization database */
|
|
71 boolean useAuthorization = true;
|
|
72
|
|
73 /** AuthOps instance */
|
|
74 AuthOps authOp;
|
|
75
|
|
76 // EXPRIMENTAL
|
|
77 /** try to enlarge cropping area for "oblique" angles */
|
|
78 boolean wholeRotArea = false;
|
1
|
79
|
500
|
80 /**
|
|
81 * Initialisation on first run.
|
|
82 *
|
|
83 * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
|
|
84 */
|
|
85 public void init(ServletConfig config) throws ServletException {
|
|
86 super.init(config);
|
|
87
|
|
88 System.out
|
|
89 .println("***** Digital Image Library Image Scaler Servlet (version "
|
|
90 + dlVersion + ") *****");
|
|
91 // say hello in the log file
|
|
92 logger
|
|
93 .info("***** Digital Image Library Image Scaler Servlet (version "
|
|
94 + dlVersion + ") *****");
|
1
|
95
|
500
|
96 // get our ServletContext
|
|
97 ServletContext context = config.getServletContext();
|
|
98 // see if there is a Configuration instance
|
|
99 dlConfig = (DigilibConfiguration) context
|
|
100 .getAttribute("digilib.servlet.configuration");
|
|
101 if (dlConfig == null) {
|
|
102 // no Configuration
|
|
103 throw new ServletException("No Configuration!");
|
|
104 }
|
|
105 // set our AuthOps
|
|
106 useAuthorization = dlConfig.getAsBoolean("use-authorization");
|
|
107 authOp = (AuthOps) dlConfig.getValue("servlet.auth.op");
|
1
|
108
|
500
|
109 // DocuDirCache instance
|
|
110 dirCache = (DocuDirCache) dlConfig.getValue("servlet.dir.cache");
|
|
111 denyImgFile = ServletOps.getFile((File) dlConfig.getValue("denied-image"), config);
|
|
112 errorImgFile = ServletOps.getFile((File) dlConfig.getValue("error-image"), config);
|
|
113 notfoundImgFile = ServletOps.getFile((File) dlConfig.getValue("notfound-image"), config);
|
|
114 sendFileAllowed = dlConfig.getAsBoolean("sendfile-allowed");
|
|
115 minSubsample = dlConfig.getAsFloat("subsample-minimum");
|
|
116 defaultQuality = dlConfig.getAsInt("default-quality");
|
|
117 }
|
1
|
118
|
500
|
119 @Override
|
|
120 public void processRequest(HttpServletRequest request,
|
|
121 HttpServletResponse response) throws ServletException, ImageOpException {
|
1
|
122
|
500
|
123
|
|
124 if (dlConfig == null) {
|
|
125 throw new ServletException("ERROR: No Configuration!");
|
|
126 }
|
|
127
|
|
128 accountlog.debug("request: " + request.getQueryString());
|
|
129 logger.debug("request: " + request.getQueryString());
|
|
130
|
|
131
|
|
132
|
|
133 // define the job information
|
|
134 ImageJobInformation jobdeclaration = new ImageJobInformation();
|
1
|
135
|
500
|
136 jobdeclaration.setWithRequest(request);
|
|
137 jobdeclaration.setConfig(dlConfig);
|
|
138
|
|
139
|
|
140
|
|
141
|
|
142 // TODO check, if file can be sent without transformations
|
|
143
|
|
144
|
|
145
|
|
146
|
|
147 OutputStream outputstream = null;
|
|
148 try {
|
|
149 outputstream = response.getOutputStream();
|
|
150 } catch (IOException e1) {
|
|
151 // TODO Auto-generated catch block
|
|
152 e1.printStackTrace();
|
|
153 logger.error(e1.getMessage());
|
|
154 }
|
|
155
|
|
156
|
|
157 DigilibWorker job=null;
|
|
158 try {
|
|
159
|
|
160 /* logger.debug(outputstream.toString());
|
|
161 logger.debug(jobdeclaration.get_mimeType().toString());
|
|
162 logger.debug(jobdeclaration.get_scaleQual().toString());
|
|
163 logger.debug(jobdeclaration.getAsFloat("rot"));
|
|
164 logger.debug(jobdeclaration.getAsFloat("cont"));
|
|
165 logger.debug(jobdeclaration.getAsFloat("brgt"));
|
|
166 logger.debug(jobdeclaration.get_paramRGBM().toString());
|
|
167 logger.debug(jobdeclaration.get_paramRGBA().toString());
|
|
168 logger.debug("fileToLoad " + jobdeclaration.get_fileToLoad().toString());
|
|
169 logger.debug("scaleXY " + jobdeclaration.get_scaleXY());
|
|
170 logger.debug("get_outerUserImgArea " + jobdeclaration.get_outerUserImgArea().toString());
|
|
171 logger.debug("get_innerUserImgArea " + jobdeclaration.get_innerUserImgArea().toString());
|
|
172 logger.debug("minSubsample " + minSubsample);
|
|
173 logger.debug("get_wholeRotArea " + jobdeclaration.get_wholeRotArea());
|
|
174 logger.debug("get_forceType "+jobdeclaration.get_forceType()); */
|
|
175
|
|
176 float scaleXY = jobdeclaration.get_scaleXY();
|
|
177
|
|
178 long startTime = System.currentTimeMillis();
|
1
|
179
|
500
|
180
|
|
181 job = new DigilibImageWorker(dlConfig,
|
|
182 outputstream ,
|
|
183 jobdeclaration.get_mimeType(),
|
|
184 jobdeclaration.get_scaleQual(),
|
|
185 //jobdeclaration,
|
|
186 jobdeclaration.getAsFloat("rot"),
|
|
187 jobdeclaration.getAsFloat("cont"),
|
|
188 jobdeclaration.getAsFloat("brgt"),
|
|
189 jobdeclaration.get_paramRGBM(),
|
|
190 jobdeclaration.get_paramRGBA(),
|
|
191 jobdeclaration.get_fileToLoad(),
|
|
192 scaleXY,
|
|
193 jobdeclaration.get_outerUserImgArea(),
|
|
194 jobdeclaration.get_innerUserImgArea(),
|
|
195 minSubsample,
|
|
196 jobdeclaration.get_wholeRotArea(),
|
|
197 jobdeclaration.get_forceType(),
|
|
198 jobdeclaration.get_hmir(),
|
|
199 jobdeclaration.get_vmir());
|
1
|
200
|
500
|
201 job.run();
|
1
|
202
|
500
|
203 if (job.hasError()) {
|
|
204 throw new ImageOpException(job.getError().toString());
|
|
205 }
|
1
|
206
|
500
|
207 try {
|
|
208 outputstream.flush();
|
|
209 logger.debug("Job Processing Time: "+ (System.currentTimeMillis()-startTime) + "ms");
|
|
210 } catch (IOException e) {
|
|
211 // TODO Auto-generated catch block
|
|
212 e.printStackTrace();
|
|
213 logger.error(e.getMessage());
|
|
214 response.sendError(1);
|
|
215 }
|
1
|
216
|
500
|
217
|
|
218 } catch (IOException e) {
|
|
219 // TODO Auto-generated catch block
|
|
220 e.printStackTrace();
|
|
221 logger.error(e.getClass()+": "+ e.getMessage());
|
|
222 //response.sendError(1);
|
|
223 } catch (ImageOpException e) {
|
|
224 // TODO Auto-generated catch block
|
|
225 e.printStackTrace();
|
|
226 logger.error(e.getClass()+": "+ e.getMessage());
|
|
227 //response.sendError(1);
|
|
228 }
|
1
|
229
|
500
|
230
|
|
231
|
1
|
232
|
500
|
233
|
|
234 /*boolean errorMsgHtml = false;
|
|
235
|
|
236 if(jobdeclaration.hasOption("mo","errtxt")){
|
|
237 errorMsgHtml = true;
|
|
238 } else if (jobdeclaration.hasOption("mo","errimg")) {
|
|
239 errorMsgHtml = true;
|
|
240 }
|
|
241
|
|
242
|
|
243
|
|
244 */
|
|
245
|
|
246
|
|
247 }
|
1
|
248
|
500
|
249
|
|
250
|
|
251 /**
|
|
252 * Sends an error to the client as text or image.
|
|
253 *
|
|
254 * @param asHTML
|
|
255 * @param type
|
|
256 * @param msg
|
|
257 * @param response
|
|
258 */
|
|
259 public void digilibError(boolean asHTML, int type, String msg,
|
|
260 HttpServletResponse response) {
|
|
261 try {
|
|
262 File img = null;
|
|
263 if (type == ERROR_AUTH) {
|
|
264 if (msg == null) {
|
|
265 msg = "ERROR: Unauthorized access!";
|
|
266 }
|
|
267 img = denyImgFile;
|
|
268 } else if (type == ERROR_FILE) {
|
|
269 if (msg == null) {
|
|
270 msg = "ERROR: Image file not found!";
|
|
271 }
|
|
272 img = notfoundImgFile;
|
|
273 } else {
|
|
274 if (msg == null) {
|
|
275 msg = "ERROR: Other image error!";
|
|
276 }
|
|
277 img = this.errorImgFile;
|
|
278 }
|
|
279 if (asHTML && (img != null)) {
|
|
280 ServletOps.htmlMessage(msg, response);
|
|
281 } else {
|
|
282 ServletOps.sendFile(img, null, response);
|
|
283 }
|
|
284 } catch (IOException e) {
|
|
285 logger.error("Error sending error!", e);
|
|
286 }
|
1
|
287
|
500
|
288 }
|
1
|
289
|
500
|
290 public static String getVersion(){
|
|
291 return dlVersion;
|
|
292 }
|
|
293
|
1
|
294
|
500
|
295 }
|