Mercurial > hg > digilib-old
annotate servlet/src/digilib/servlet/ServletOps.java @ 95:a82150544f4a
New version 1.8b4.
Uses new Collection classes.
author | robcast |
---|---|
date | Mon, 17 Mar 2003 16:07:05 +0100 |
parents | 3b8797fc3e90 |
children | afe7ff98bb71 |
rev | line source |
---|---|
1 | 1 /* ServletOps -- Servlet utility class |
2 | |
3 Digital Image Library servlet components | |
4 | |
5 Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de) | |
6 | |
7 This program is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
9 Free Software Foundation; either version 2 of the License, or (at your | |
10 option) any later version. | |
11 | |
12 Please read license.txt for the full details. A copy of the GPL | |
13 may be found at http://www.gnu.org/copyleft/lgpl.html | |
14 | |
15 You should have received a copy of the GNU General Public License | |
16 along with this program; if not, write to the Free Software | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 */ | |
20 | |
21 package digilib.servlet; | |
22 | |
23 import javax.servlet.*; | |
24 import javax.servlet.http.*; | |
25 import java.io.*; | |
26 import java.util.*; | |
27 | |
28 import digilib.*; | |
29 import digilib.io.*; | |
30 | |
31 | |
32 public class ServletOps { | |
33 | |
34 private Utils util = null; | |
95 | 35 private HashMap confTable = null; |
1 | 36 |
37 public ServletOps() { | |
38 util = new Utils(); | |
39 } | |
40 | |
41 public ServletOps(Utils u) { | |
42 util = u; | |
43 } | |
44 | |
45 public ServletOps(Utils u, ServletConfig sc) throws ServletException { | |
46 util = u; | |
47 setConfig(sc); | |
48 } | |
49 | |
50 public void setUtils(Utils u) { | |
51 util = u; | |
52 } | |
53 | |
54 /** | |
55 * read parameter list from the XML file in init parameter "config-file" | |
56 */ | |
57 public void setConfig(ServletConfig c) throws ServletException { | |
58 // reset parameter table | |
59 confTable = null; | |
60 if (c == null) { | |
61 return; | |
62 } | |
63 // get config file name | |
64 String fn = c.getInitParameter("config-file"); | |
65 if (fn == null) { | |
66 util.dprintln(4, "setConfig: no param config-file"); | |
67 return; | |
68 } | |
69 File f = new File(fn); | |
70 // setup config file list reader | |
71 XMLListLoader lilo = new XMLListLoader("digilib-config", "parameter", "name", "value"); | |
72 try { | |
73 confTable = lilo.loadURL(f.toURL().toString()); | |
74 } catch (Exception e) { | |
75 util.dprintln(4, "setConfig: unable to read file "+fn); | |
76 throw new ServletException(e); | |
77 } | |
78 } | |
79 | |
80 /** | |
56
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
81 * convert a string with a list of pathnames into an array of strings |
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
82 * using the system's path seperator string |
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
83 */ |
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
84 public String[] getPathArray(String paths) { |
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
85 // split list into directories |
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
86 StringTokenizer dirs = new StringTokenizer(paths, java.io.File.pathSeparator); |
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
87 int n = dirs.countTokens(); |
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
88 if (n < 1) { |
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
89 return null; |
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
90 } |
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
91 // add directories into array |
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
92 String[] pathArray = new String[n]; |
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
93 for (int i = 0; i < n; i++) { |
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
94 pathArray[i] = dirs.nextToken(); |
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
95 } |
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
96 return pathArray; |
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
97 } |
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
98 |
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
99 /** |
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
100 * getPathArray with default fall back |
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
101 */ |
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
102 public String[] tryToGetPathArray(String paths, String[] defaultPath) { |
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
103 String[] pa = getPathArray(paths); |
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
104 return (pa != null) ? pa : defaultPath; |
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
105 } |
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
106 |
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
107 /** |
1 | 108 * print a servlet response and exit |
109 */ | |
110 public static void htmlMessage(String s, HttpServletResponse response) throws IOException { | |
111 response.setContentType("text/html; charset=iso-8859-1"); | |
112 PrintWriter out = response.getWriter(); | |
113 out.println("<html>"); | |
114 out.println("<head><title>Scaler</title></head>"); | |
115 out.println("<body>"); | |
116 out.println("<p>"+s+"</p>"); | |
117 out.println("</body></html>"); | |
118 } | |
119 | |
73 | 120 /** Transfers an image file as-is. |
121 * | |
122 * The local file is copied to the <code>OutputStream</code> of the | |
123 * <code>ServletResponse</code>. The mime-type for the response is detected | |
124 * from the file. | |
125 * | |
126 * @param f Image file to be sent. | |
127 * @param res ServletResponse where the image file will be sent. | |
128 * @throws FileOpException Exception is thrown for a IOException. | |
129 */ | |
130 public void sendFile(File f, ServletResponse response) throws FileOpException { | |
131 util.dprintln(4, "sendFile("+f+")"); | |
132 String mimeType = FileOps.mimeForFile(f); | |
133 if (mimeType == null) { | |
134 util.dprintln(2, "ERROR(sendFile): unknown file Type"); | |
135 throw new FileOpException("Unknown file type."); | |
136 } | |
137 response.setContentType(mimeType); | |
138 // open file | |
139 try { | |
140 FileInputStream inFile = new FileInputStream(f); | |
141 OutputStream outStream = response.getOutputStream(); | |
142 byte dataBuffer[] = new byte[4096]; | |
143 int len; | |
144 while ((len = inFile.read(dataBuffer)) != -1) { | |
145 // copy out file | |
146 outStream.write(dataBuffer, 0, len); | |
147 } | |
148 inFile.close(); | |
149 } catch (IOException e) { | |
150 util.dprintln(2, "ERROR(sendFile): unable to send file"); | |
151 throw new FileOpException("Unable to send file."); | |
152 } | |
153 } | |
154 | |
155 | |
1 | 156 /** |
157 * get a parameter from request and return it if set, otherwise return default | |
158 */ | |
159 public int tryToGetParam(String s, int i, HttpServletRequest r) { | |
160 try { | |
161 i = Integer.parseInt(r.getParameter(s)); | |
162 } catch(Exception e) { | |
163 util.dprintln(4, "trytoGetParam(int) failed on param "+s); | |
164 //e.printStackTrace(); | |
165 } | |
166 return i; | |
167 } | |
168 public float tryToGetParam(String s, float f, HttpServletRequest r) { | |
169 try { | |
170 f = Float.parseFloat(r.getParameter(s)); | |
171 } catch(Exception e) { | |
172 util.dprintln(4, "trytoGetParam(float) failed on param "+s); | |
173 //e.printStackTrace(); | |
174 } | |
175 return f; | |
176 } | |
177 public String tryToGetParam(String s, String x, HttpServletRequest r) { | |
178 if (r.getParameter(s) != null) { | |
179 x = r.getParameter(s); | |
180 } else { | |
181 util.dprintln(4, "trytoGetParam(string) failed on param "+s); | |
182 } | |
183 return x; | |
184 } | |
185 | |
186 | |
187 /** | |
188 * get an init parameter from config and return it if set, otherwise return default | |
189 */ | |
190 public int tryToGetInitParam(String s, int i) { | |
191 //System.out.println("trytogetInitParam("+s+", "+i+")"); | |
192 try { | |
193 //System.out.println("trytogetInitParam: "+(String)confTable.get(s)); | |
194 i = Integer.parseInt((String)confTable.get(s)); | |
195 } catch(Exception e) { | |
196 util.dprintln(4, "trytogetInitParam(int) failed on param "+s); | |
197 //e.printStackTrace(); | |
198 } | |
199 return i; | |
200 } | |
201 public float tryToGetInitParam(String s, float f) { | |
202 try { | |
203 f = Float.parseFloat((String)confTable.get(s)); | |
204 } catch(Exception e) { | |
205 util.dprintln(4, "trytoGetInitParam(float) failed on param "+s); | |
206 //e.printStackTrace(); | |
207 } | |
208 return f; | |
209 } | |
210 public String tryToGetInitParam(String s, String x) { | |
211 if ((confTable != null)&&((String)confTable.get(s) != null)) { | |
212 x = (String)confTable.get(s); | |
213 } else { | |
214 util.dprintln(4, "trytoGetInitParam(string) failed on param "+s); | |
215 } | |
216 return x; | |
217 } | |
218 | |
219 } |