1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.dbforms.servlets.base;
25
26 import java.io.IOException;
27 import java.io.PrintWriter;
28 import java.util.Locale;
29
30 import javax.servlet.http.HttpServlet;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33 import javax.servlet.ServletException;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.dbforms.servlets.Controller;
38 import org.dbforms.util.MessageResources;
39 import org.dbforms.util.MultipartRequest;
40 import org.dbforms.util.ParseUtil;
41 import org.dbforms.util.Util;
42
43 /***
44 * This is the abstract base class for generating reports.
45 *
46 * @author Henner Kollmann
47 */
48 public abstract class AbstractServletBase extends HttpServlet {
49 /*** logging category for this class */
50 private static Log logCat = LogFactory.getLog(Controller.class.getName());
51
52 /*** 100KB default upload size */
53 private int maxUploadSize = 102400;
54
55 /***
56 * Basic servlet method, answers requests from the browser.
57 *
58 * @param request
59 * HTTPServletRequest
60 * @param response
61 * HTTPServletResponse
62 *
63 * @throws ServletException
64 * if there is a servlet problem.
65 * @throws IOException
66 * if there is an I/O problem.
67 */
68 public final void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
69 process_internal(request, response);
70 }
71
72 /***
73 * Basic servlet method, answers requests fromt the browser.
74 *
75 * @param request
76 * HTTPServletRequest
77 * @param response
78 * HTTPServletResponse
79 *
80 * @throws ServletException
81 * if there is a servlet problem.
82 * @throws IOException
83 * if there is an I/O problem.
84 */
85 public final void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
86 process_internal(request, response);
87 }
88
89 /***
90 * generates a report from request. Tries to get data from DbForms.
91 *
92 * @param request
93 * HTTPServletRequest
94 * @param response
95 * HTTPServletResponse
96 */
97 private final void process_internal(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
98
99
100
101
102 processLocale(request);
103
104
105
106
107
108
109
110
111 String contentType = request.getContentType();
112
113 if (!Util.isNull(contentType) && contentType.startsWith("multipart")) {
114 try {
115 MultipartRequest multipartRequest = new MultipartRequest(request, maxUploadSize);
116 request.setAttribute("multipartRequest", multipartRequest);
117 } catch (IOException ioe) {
118 logCat.error("::process - check if uploaded file(s) exceeded allowed size", ioe);
119 sendErrorMessage("Check if uploaded file(s) exceeded allowed size.", response);
120 return;
121 }
122 }
123 process(request, response);
124 }
125
126 /***
127 * generates a report from request. Tries to get data from DbForms.
128 *
129 * @param request
130 * HTTPServletRequest
131 * @param response
132 * HTTPServletResponse
133 */
134 abstract protected void process(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException;
135
136
137 /***
138 * Initialize this servlet.
139 *
140 * @exception ServletException
141 * if the initialization fails
142 */
143 public void init() throws ServletException {
144
145
146
147 String maxUploadSizeStr = this.getServletConfig().getInitParameter("maxUploadSize");
148 if (!Util.isNull(maxUploadSizeStr)) {
149 try {
150 this.maxUploadSize = Integer.parseInt(maxUploadSizeStr);
151 } catch (NumberFormatException nfe) {
152 logCat.error("maxUploadSize not a valid number => using default.");
153 }
154 }
155 }
156
157 /***
158 * Set the default locale
159 *
160 * @param request
161 * the request object
162 */
163 private void processLocale(HttpServletRequest request) {
164 String lang = ParseUtil.getParameter(request, "lang");
165 String country = ParseUtil.getParameter(request, "country", "");
166 Locale locale = null;
167
168 if (!Util.isNull(lang)) {
169 locale = new Locale(lang, country);
170 } else if (MessageResources.getLocale(request) == null) {
171 MessageResources.setLocale(request, request.getLocale());
172 }
173
174 if (locale != null) {
175 MessageResources.setLocale(request, locale);
176 }
177 }
178
179 /***
180 * Send error messages to the servlet's output stream
181 *
182 * @param message
183 * the message to display
184 * @param response
185 * the response object
186 */
187 protected void sendErrorMessage(String message, HttpServletResponse response) {
188 try {
189 PrintWriter out = response.getWriter();
190
191 response.setContentType("text/html");
192 out.println("<html><body><h1>ERROR:</h1><p>");
193 out.println(message);
194 out.println("</p></body></html>");
195 } catch (IOException ioe) {
196 logCat.error("::sendErrorMessage - senderror message crashed", ioe);
197 }
198 }
199
200 }