Mercurial > hg > digilib-old
comparison text/src/main/java/digilib/servlet/Texter.java @ 905:afd82fe0c48a
move Texter servlet to its own maven module
author | robcast |
---|---|
date | Tue, 03 May 2011 12:18:08 +0200 |
parents | servlet2/src/main/java/digilib/servlet/Texter.java@7779b37d1d05 |
children |
comparison
equal
deleted
inserted
replaced
904:4f8f2b9ab539 | 905:afd82fe0c48a |
---|---|
1 /* Texter.java -- Servlet for displaying text | |
2 * Digital Image Library servlet components | |
3 * Copyright (C) 2003 Robert Casties (robcast@mail.berlios.de) | |
4 * | |
5 * This program is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License as published by the Free | |
7 * Software Foundation; either version 2 of the License, or (at your option) | |
8 * any later version. Please read license.txt for the full details. A copy of | |
9 * the GPL may be found at http://www.gnu.org/copyleft/lgpl.html | |
10 * You should have received a copy of the GNU General Public License along with this | |
11 * program; if not, write to the Free Software Foundation, Inc., 59 Temple | |
12 * Place, Suite 330, Boston, MA 02111-1307 USA | |
13 * | |
14 * Created on 15.09.2003 by casties | |
15 */ | |
16 | |
17 package digilib.servlet; | |
18 | |
19 import java.io.IOException; | |
20 | |
21 import javax.servlet.ServletConfig; | |
22 import javax.servlet.ServletContext; | |
23 import javax.servlet.ServletException; | |
24 import javax.servlet.http.HttpServlet; | |
25 import javax.servlet.http.HttpServletRequest; | |
26 import javax.servlet.http.HttpServletResponse; | |
27 | |
28 import org.apache.log4j.Logger; | |
29 | |
30 import digilib.auth.AuthOps; | |
31 import digilib.image.ImageOpException; | |
32 import digilib.io.DocuDirCache; | |
33 import digilib.io.FileOps; | |
34 import digilib.io.FileOps.FileClass; | |
35 import digilib.io.TextFile; | |
36 | |
37 /** | |
38 * Servlet for displaying text | |
39 * | |
40 * | |
41 * @author casties | |
42 * | |
43 */ | |
44 public class Texter extends HttpServlet { | |
45 | |
46 private static final long serialVersionUID = 6678666342141409867L; | |
47 | |
48 /** Servlet version */ | |
49 public static String tlVersion = "0.1b3"; | |
50 | |
51 /** DigilibConfiguration instance */ | |
52 DigilibServletConfiguration dlConfig = null; | |
53 | |
54 /** general logger */ | |
55 Logger logger = Logger.getLogger("digilib.texter"); | |
56 | |
57 /** logger for accounting requests */ | |
58 protected static Logger accountlog = Logger.getLogger("account.texter.request"); | |
59 | |
60 /** FileOps instance */ | |
61 FileOps fileOp; | |
62 | |
63 /** AuthOps instance */ | |
64 AuthOps authOp; | |
65 | |
66 /** ServletOps instance */ | |
67 ServletOps servletOp; | |
68 | |
69 /** DocuDirCache instance */ | |
70 DocuDirCache dirCache; | |
71 | |
72 /** use authentication */ | |
73 boolean useAuthentication = false; | |
74 | |
75 /* | |
76 * (non-Javadoc) | |
77 * | |
78 * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig) | |
79 */ | |
80 public void init(ServletConfig config) throws ServletException { | |
81 super.init(config); | |
82 | |
83 System.out.println("***** Digital Image Library Text Servlet (version " | |
84 + tlVersion + ") *****"); | |
85 | |
86 // get our ServletContext | |
87 ServletContext context = config.getServletContext(); | |
88 // see if there is a Configuration instance | |
89 dlConfig = (DigilibServletConfiguration) context | |
90 .getAttribute("digilib.servlet.configuration"); | |
91 if (dlConfig == null) { | |
92 // no Configuration | |
93 throw new ServletException("No Configuration!"); | |
94 } | |
95 // say hello in the log file | |
96 logger.info("***** Digital Image Library Text Servlet (version " | |
97 + tlVersion + ") *****"); | |
98 | |
99 // set our AuthOps | |
100 useAuthentication = dlConfig.getAsBoolean("use-authorization"); | |
101 authOp = (AuthOps) dlConfig.getValue("servlet.auth.op"); | |
102 // DocuDirCache instance | |
103 dirCache = (DocuDirCache) dlConfig.getValue("servlet.dir.cache"); | |
104 } | |
105 | |
106 /* | |
107 * (non-Javadoc) | |
108 * | |
109 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, | |
110 * javax.servlet.http.HttpServletResponse) | |
111 */ | |
112 protected void doGet(HttpServletRequest request, | |
113 HttpServletResponse response) throws ServletException, IOException { | |
114 accountlog.info("GET from " + request.getRemoteAddr()); | |
115 // do the processing | |
116 processRequest(request, response); | |
117 } | |
118 | |
119 /* | |
120 * (non-Javadoc) | |
121 * | |
122 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, | |
123 * javax.servlet.http.HttpServletResponse) | |
124 */ | |
125 protected void doPost(HttpServletRequest request, | |
126 HttpServletResponse response) throws ServletException, IOException { | |
127 accountlog.info("POST from " + request.getRemoteAddr()); | |
128 // do the processing | |
129 processRequest(request, response); | |
130 } | |
131 | |
132 protected void processRequest(HttpServletRequest request, | |
133 HttpServletResponse response) { | |
134 | |
135 /* | |
136 * request parameters | |
137 */ | |
138 // create new request with defaults | |
139 DigilibServletRequest dlRequest = new DigilibServletRequest(request); | |
140 try { | |
141 | |
142 /* | |
143 * find the file to load/send | |
144 */ | |
145 TextFile f = getTextFile(dlRequest, "/txt"); | |
146 if (f != null) { | |
147 ServletOps.sendFile(f.getFile(), null, null, response, logger); | |
148 } else { | |
149 f = getTextFile(dlRequest, ""); | |
150 if (f != null) { | |
151 ServletOps.sendFile(f.getFile(), null, null, response, logger); | |
152 } else { | |
153 response.sendError(HttpServletResponse.SC_NOT_FOUND, "Text-File not found!"); | |
154 //ServletOps.htmlMessage("No Text-File!", response); | |
155 } | |
156 } | |
157 | |
158 } catch (ImageOpException e) { | |
159 // most likely wrong file format... | |
160 logger.error("ERROR sending text file: ", e); | |
161 try { | |
162 response.sendError(HttpServletResponse.SC_BAD_REQUEST); | |
163 } catch (IOException e1) { | |
164 logger.error("ERROR sending error: ", e1); | |
165 } | |
166 } catch (IOException e) { | |
167 logger.error("ERROR sending text file: ", e); | |
168 } | |
169 } | |
170 | |
171 | |
172 /** | |
173 * Looks for a file in the given subDirectory. | |
174 * | |
175 * @param dlRequest | |
176 * The received request which has the file path. | |
177 * @param subDirectory | |
178 * The subDirectory of the file path where the file should | |
179 * be found. | |
180 * @return The wanted Textfile or null if there wasn't a file. | |
181 */ | |
182 | |
183 private TextFile getTextFile(DigilibServletRequest dlRequest, String subDirectory) { | |
184 String loadPathName = dlRequest.getFilePath() + subDirectory; | |
185 // find the file(set) | |
186 return (TextFile) dirCache.getFile(loadPathName, dlRequest.getAsInt("pn"), | |
187 FileClass.TEXT); | |
188 } | |
189 } |