Mercurial > hg > digilib-old
comparison servlet/src/digilib/servlet/Texter.java @ 161:ace2a4a0ba74
servlet version 1.16a4
- rather experimental
- new Texter servlet for sending text
- reads and caches text files in DocuDirCache
- DocuFile renamed to ImageFile (-Set)
- new TextFile
author | robcast |
---|---|
date | Tue, 16 Sep 2003 18:32:28 +0200 |
parents | |
children | 6002ea661acd |
comparison
equal
deleted
inserted
replaced
160:aecb57fb8f96 | 161:ace2a4a0ba74 |
---|---|
1 /* Texter.java -- Servlet for displaying text | |
2 | |
3 Digital Image Library servlet components | |
4 | |
5 Copyright (C) 2003 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 * Created on 15.09.2003 by casties | |
20 * | |
21 */ | |
22 package digilib.servlet; | |
23 | |
24 import java.io.IOException; | |
25 | |
26 import javax.servlet.ServletConfig; | |
27 import javax.servlet.ServletContext; | |
28 import javax.servlet.ServletException; | |
29 import javax.servlet.http.HttpServlet; | |
30 import javax.servlet.http.HttpServletRequest; | |
31 import javax.servlet.http.HttpServletResponse; | |
32 | |
33 import digilib.Utils; | |
34 import digilib.auth.AuthOps; | |
35 import digilib.io.DocuDirCache; | |
36 import digilib.io.FileOpException; | |
37 import digilib.io.FileOps; | |
38 import digilib.io.TextFile; | |
39 | |
40 /** Servlet for displaying text | |
41 * | |
42 * @author casties | |
43 * | |
44 */ | |
45 public class Texter extends HttpServlet { | |
46 | |
47 /** Servlet version */ | |
48 public static String tlVersion = "0.1a1"; | |
49 /** DigilibConfiguration instance */ | |
50 DigilibConfiguration dlConfig = null; | |
51 /** Utils instance with debuglevel */ | |
52 Utils util; | |
53 /** FileOps instance */ | |
54 FileOps fileOp; | |
55 /** AuthOps instance */ | |
56 AuthOps authOp; | |
57 /** ServletOps instance */ | |
58 ServletOps servletOp; | |
59 /** DocuDirCache instance */ | |
60 DocuDirCache dirCache; | |
61 | |
62 /** use authentication */ | |
63 boolean useAuthentication = false; | |
64 | |
65 /* (non-Javadoc) | |
66 * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig) | |
67 */ | |
68 public void init(ServletConfig config) throws ServletException { | |
69 super.init(config); | |
70 | |
71 System.out.println( | |
72 "***** Digital Image Library Text Servlet (version " | |
73 + tlVersion | |
74 + ") *****"); | |
75 | |
76 // get our ServletContext | |
77 ServletContext context = config.getServletContext(); | |
78 // see if there is a Configuration instance | |
79 dlConfig = | |
80 (DigilibConfiguration) context.getAttribute( | |
81 "digilib.servlet.configuration"); | |
82 if (dlConfig == null) { | |
83 // create new Configuration | |
84 try { | |
85 dlConfig = new DigilibConfiguration(config); | |
86 context.setAttribute("digilib.servlet.configuration", dlConfig); | |
87 } catch (Exception e) { | |
88 throw new ServletException(e); | |
89 } | |
90 } | |
91 // first we need an Utils | |
92 util = dlConfig.getUtil(); | |
93 // set our AuthOps | |
94 useAuthentication = dlConfig.getAsBoolean("use-authorization"); | |
95 authOp = (AuthOps) dlConfig.getValue("servlet.auth.op"); | |
96 // FileOps instance | |
97 fileOp = new FileOps(util); | |
98 // AuthOps instance | |
99 servletOp = new ServletOps(util); | |
100 // DocuDirCache instance | |
101 dirCache = (DocuDirCache) dlConfig.getValue("servlet.dir.cache"); | |
102 } | |
103 | |
104 /* (non-Javadoc) | |
105 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) | |
106 */ | |
107 protected void doGet( | |
108 HttpServletRequest request, | |
109 HttpServletResponse response) | |
110 throws ServletException, IOException { | |
111 // create new request with defaults | |
112 DigilibRequest dlReq = new DigilibRequest(); | |
113 // set with request parameters | |
114 dlReq.setWithRequest(request); | |
115 // add DigilibRequest to ServletRequest | |
116 request.setAttribute("digilib.servlet.request", dlReq); | |
117 // do the processing | |
118 processRequest(request, response); | |
119 } | |
120 | |
121 /* (non-Javadoc) | |
122 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) | |
123 */ | |
124 protected void doPost( | |
125 HttpServletRequest request, | |
126 HttpServletResponse response) | |
127 throws ServletException, IOException { | |
128 // create new request with defaults | |
129 DigilibRequest dlReq = new DigilibRequest(); | |
130 // set with request parameters | |
131 dlReq.setWithRequest(request); | |
132 // add DigilibRequest to ServletRequest | |
133 request.setAttribute("digilib.servlet.request", dlReq); | |
134 // do the processing | |
135 processRequest(request, response); | |
136 } | |
137 | |
138 protected void processRequest( | |
139 HttpServletRequest request, | |
140 HttpServletResponse response) | |
141 throws ServletException, IOException { | |
142 | |
143 /* | |
144 * request parameters | |
145 */ | |
146 | |
147 DigilibRequest dlRequest = | |
148 (DigilibRequest) request.getAttribute("digilib.servlet.request"); | |
149 | |
150 try { | |
151 | |
152 /* | |
153 * find the file to load/send | |
154 */ | |
155 | |
156 // get PathInfo | |
157 String loadPathName = dlRequest.getFilePath(); | |
158 // find the file(set) | |
159 TextFile fileToLoad = | |
160 (TextFile) dirCache.getFile( | |
161 loadPathName, | |
162 dlRequest.getPn(), | |
163 FileOps.CLASS_TEXT); | |
164 if (fileToLoad == null) { | |
165 throw new FileOpException( | |
166 "File " | |
167 + loadPathName | |
168 + "(" | |
169 + dlRequest.getPn() | |
170 + ") not found."); | |
171 } | |
172 | |
173 /* | |
174 * do something with the file | |
175 */ | |
176 | |
177 | |
178 | |
179 /* | |
180 * send the result | |
181 */ | |
182 | |
183 servletOp.sendFile(fileToLoad.getFile(), response); | |
184 | |
185 | |
186 } catch (FileOpException e) { | |
187 util.dprintln(1, "ERROR: File IO Error: " + e); | |
188 try { | |
189 ServletOps.htmlMessage("ERROR: File IO Error: " + e, response); | |
190 } catch (FileOpException ex) { | |
191 } // so we don't get a loop | |
192 } | |
193 | |
194 } | |
195 | |
196 } |