View Javadoc

1   /*
2    * SymmetricDS is an open source database synchronization solution.
3    *   
4    * Copyright (C) Keith Naas <knaas@users.sourceforge.net>
5    * 
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 3 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, see
18   * <http://www.gnu.org/licenses/>.
19   */
20  package org.jumpmind.symmetric.web;
21  
22  import java.io.IOException;
23  
24  import javax.servlet.ServletResponse;
25  import javax.servlet.http.HttpServletResponse;
26  
27  class ServletUtils {
28      /***
29       * Because you can't send an error when the response is already committed,
30       * this helps to avoid unnecessary errors in the logs.
31       * 
32       * @param resp
33       * @param statusCode
34       * @return true if the error could be sent to the response
35       * @throws IOException
36       */
37      public static boolean sendError(final HttpServletResponse resp, final int statusCode) throws IOException {
38          return sendError(resp, statusCode, null);
39      }
40  
41      /***
42       * Because you can't send an error when the response is already committed,
43       * this helps to avoid unnecessary errors in the logs.
44       * 
45       * @param resp
46       * @param statusCode
47       * @param message
48       *                a message to put in the body of the response
49       * @return true if the error could be sent to the response
50       * @throws IOException
51       */
52      public static boolean sendError(final HttpServletResponse resp, final int statusCode, final String message)
53              throws IOException {
54          boolean retVal = false;
55          if (!resp.isCommitted()) {
56              resp.sendError(statusCode, message);
57              retVal = true;
58          }
59          return retVal;
60      }
61  
62      /***
63       * Because you can't send an error when the response is already committed,
64       * this helps to avoid unnecessary errors in the logs.
65       * 
66       * @param resp
67       * @param statusCode
68       * @return true if the error could be sent to the response
69       * @throws IOException
70       */
71      public static boolean sendError(final ServletResponse resp, final int statusCode) throws IOException {
72          return sendError(resp, statusCode, null);
73      }
74  
75      /***
76       * Because you can't send an error when the response is already committed,
77       * this helps to avoid unnecessary errors in the logs.
78       * 
79       * @param resp
80       * @param statusCode
81       * @param message
82       *                a message to put in the body of the response
83       * @return true if the error could be sent to the response
84       * @throws IOException
85       */
86      public static boolean sendError(final ServletResponse resp, final int statusCode, final String message)
87              throws IOException {
88          boolean retVal = false;
89          if (resp instanceof HttpServletResponse) {
90              retVal = sendError((HttpServletResponse) resp, statusCode, message);
91          }
92          return retVal;
93      }
94  }