View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/servlets/base/AbstractServletBase.java,v 1.2 2006/03/18 19:14:48 hkollmann Exp $
3    * $Revision: 1.2 $
4    * $Date: 2006/03/18 19:14:48 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
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        // Verify if Locale have been setted in session with "LOCALE_KEY"
99        // if not, take the request.getLocale() as default and put it in session
100       // Verify if Locale have been setted in session with "LOCALE_KEY"
101       // if not, take the request.getLocale() as default and put it in session
102       processLocale(request);
103 
104       // create RFC-1867 data wrapper for the case the form was sent in
105       // multipart mode
106       // this is needed because common HttpServletRequestImpl - classes do not
107       // support it
108       // the whole application has to access Request via the "ParseUtil" class
109       // this is also true for jsp files written by the user
110       // #fixme taglib needed for convenient access to ParseUtil wrapper methods
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       // if existing and valid, override default maxUploadSize, which determinates
145       // how
146       // big the http/multipart uploads may get
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 }