290
|
1 /*
|
|
2 * ServletOps -- Servlet utility class
|
|
3 *
|
|
4 * Digital Image Library servlet components
|
|
5 *
|
|
6 * Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de)
|
|
7 *
|
|
8 * This program is free software; you can redistribute it and/or modify it under
|
|
9 * the terms of the GNU General Public License as published by the Free Software
|
|
10 * Foundation; either version 2 of the License, or (at your option) any later
|
|
11 * version.
|
|
12 *
|
|
13 * Please read license.txt for the full details. A copy of the GPL may be found
|
|
14 * at http://www.gnu.org/copyleft/lgpl.html
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License along with
|
|
17 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
|
18 * Place, Suite 330, Boston, MA 02111-1307 USA
|
|
19 *
|
|
20 */
|
1
|
21
|
|
22 package digilib.servlet;
|
|
23
|
290
|
24 import java.io.File;
|
|
25 import java.io.FileInputStream;
|
|
26 import java.io.IOException;
|
|
27 import java.io.OutputStream;
|
|
28 import java.io.PrintWriter;
|
|
29 import java.util.StringTokenizer;
|
1
|
30
|
290
|
31 import javax.servlet.ServletConfig;
|
|
32 import javax.servlet.http.HttpServletResponse;
|
1
|
33
|
290
|
34 import org.apache.log4j.Logger;
|
|
35
|
|
36 import digilib.io.FileOpException;
|
|
37 import digilib.io.FileOps;
|
1
|
38
|
|
39 public class ServletOps {
|
|
40
|
290
|
41 private static Logger logger = Logger.getLogger("servlet.op");
|
1
|
42
|
290
|
43 /**
|
|
44 * convert a string with a list of pathnames into an array of strings using
|
|
45 * the system's path seperator string
|
|
46 */
|
|
47 public static String[] getPathArray(String paths) {
|
|
48 // split list into directories
|
|
49 StringTokenizer dirs = new StringTokenizer(paths,
|
|
50 java.io.File.pathSeparator);
|
|
51 int n = dirs.countTokens();
|
|
52 if (n < 1) {
|
|
53 return null;
|
|
54 }
|
|
55 // add directories into array
|
|
56 String[] pathArray = new String[n];
|
|
57 for (int i = 0; i < n; i++) {
|
|
58 pathArray[i] = dirs.nextToken();
|
|
59 }
|
|
60 return pathArray;
|
|
61 }
|
1
|
62
|
290
|
63 /**
|
|
64 * get a real File for a config File.
|
|
65 *
|
|
66 * If the File is not absolute the path is appended to the WEB-INF directory
|
|
67 * of the web-app.
|
|
68 *
|
|
69 * @param file
|
|
70 * @param sc
|
|
71 * @return
|
|
72 */
|
|
73 public static File getConfigFile(File f, ServletConfig sc) {
|
|
74 // is the filename absolute?
|
|
75 if (!f.isAbsolute()) {
|
|
76 // relative path -> use getRealPath to resolve in WEB-INF
|
|
77 String fn = sc.getServletContext().getRealPath(
|
|
78 "WEB-INF/" + f.getPath());
|
|
79 f = new File(fn);
|
|
80 }
|
|
81 return f;
|
|
82 }
|
1
|
83
|
290
|
84 /**
|
|
85 * get a real file name for a config file pathname.
|
|
86 *
|
|
87 * If filename starts with "/" its treated as absolute else the path is
|
|
88 * appended to the WEB-INF directory of the web-app.
|
|
89 *
|
|
90 * @param filename
|
|
91 * @param sc
|
|
92 * @return
|
|
93 */
|
|
94 public static String getConfigFile(String filename, ServletConfig sc) {
|
|
95 File f = new File(filename);
|
|
96 // is the filename absolute?
|
|
97 if (!f.isAbsolute()) {
|
|
98 // relative path -> use getRealPath to resolve in WEB-INF
|
|
99 filename = sc.getServletContext()
|
|
100 .getRealPath("WEB-INF/" + filename);
|
|
101 }
|
|
102 return filename;
|
|
103 }
|
1
|
104
|
290
|
105 /**
|
|
106 * print a servlet response and exit
|
|
107 */
|
|
108 public static void htmlMessage(String msg, HttpServletResponse response)
|
|
109 throws IOException {
|
|
110 htmlMessage("Scaler", msg, response);
|
|
111 }
|
|
112
|
|
113 /**
|
|
114 * print a servlet response and exit
|
|
115 */
|
|
116 public static void htmlMessage(String title, String msg,
|
|
117 HttpServletResponse response) throws IOException {
|
|
118 response.setContentType("text/html; charset=iso-8859-1");
|
|
119 PrintWriter out = response.getWriter();
|
|
120 out.println("<html>");
|
|
121 out.println("<head><title>" + title + "</title></head>");
|
|
122 out.println("<body>");
|
|
123 out.println("<p>" + msg + "</p>");
|
|
124 out.println("</body></html>");
|
|
125 }
|
1
|
126
|
290
|
127 /**
|
|
128 * Transfers an image file as-is with the mime type mt.
|
|
129 *
|
|
130 * The local file is copied to the <code>OutputStream</code> of the
|
|
131 * <code>ServletResponse</code>. If mt is null then the mime-type is
|
|
132 * auto-detected with mimeForFile.
|
|
133 *
|
|
134 * @param mt
|
|
135 * mime-type of the file.
|
|
136 * @param f
|
|
137 * Image file to be sent.
|
|
138 * @param res
|
|
139 * ServletResponse where the image file will be sent.
|
|
140 * @throws FileOpException
|
|
141 * Exception is thrown for a IOException.
|
|
142 */
|
291
|
143 public static void sendFile(File f, String mt,
|
290
|
144 HttpServletResponse response) throws FileOpException {
|
|
145 logger.debug("sendRawFile(" + mt + ", " + f + ")");
|
|
146 if (mt == null) {
|
|
147 // auto-detect mime-type
|
|
148 mt = FileOps.mimeForFile(f);
|
|
149 if (mt == null) {
|
|
150 throw new FileOpException("Unknown file type.");
|
|
151 }
|
|
152 }
|
|
153 response.setContentType(mt);
|
|
154 // open file
|
|
155 try {
|
|
156 if (mt.equals("application/octet-stream")) {
|
|
157 response.addHeader("Content-Disposition",
|
|
158 "attachment; filename=\"" + f.getName() + "\"");
|
|
159 }
|
|
160 FileInputStream inFile = new FileInputStream(f);
|
|
161 OutputStream outStream = response.getOutputStream();
|
|
162 byte dataBuffer[] = new byte[4096];
|
|
163 int len;
|
|
164 while ((len = inFile.read(dataBuffer)) != -1) {
|
|
165 // copy out file
|
|
166 outStream.write(dataBuffer, 0, len);
|
|
167 }
|
|
168 inFile.close();
|
|
169 response.flushBuffer();
|
|
170 } catch (IOException e) {
|
|
171 throw new FileOpException("Unable to send file.");
|
|
172 }
|
|
173 }
|
1
|
174
|
290
|
175 } |