1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 }