Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ConnectorServlet |
|
| 3.0;3 |
1 | /* | |
2 | * FCKeditor - The text editor for Internet - http://www.fckeditor.net | |
3 | * Copyright (C) 2004-2009 Frederico Caldeira Knabben | |
4 | * | |
5 | * == BEGIN LICENSE == | |
6 | * | |
7 | * Licensed under the terms of any of the following licenses at your | |
8 | * choice: | |
9 | * | |
10 | * - GNU General Public License Version 2 or later (the "GPL") | |
11 | * http://www.gnu.org/licenses/gpl.html | |
12 | * | |
13 | * - GNU Lesser General Public License Version 2.1 or later (the "LGPL") | |
14 | * http://www.gnu.org/licenses/lgpl.html | |
15 | * | |
16 | * - Mozilla Public License Version 1.1 or later (the "MPL") | |
17 | * http://www.mozilla.org/MPL/MPL-1.1.html | |
18 | * | |
19 | * == END LICENSE == | |
20 | */ | |
21 | package net.fckeditor.connector; | |
22 | ||
23 | import java.io.IOException; | |
24 | import java.io.PrintWriter; | |
25 | ||
26 | import javax.servlet.ServletException; | |
27 | import javax.servlet.http.HttpServlet; | |
28 | import javax.servlet.http.HttpServletRequest; | |
29 | import javax.servlet.http.HttpServletResponse; | |
30 | ||
31 | import net.fckeditor.requestcycle.ThreadLocalData; | |
32 | import net.fckeditor.response.GetResponse; | |
33 | import net.fckeditor.response.UploadResponse; | |
34 | ||
35 | import org.slf4j.Logger; | |
36 | import org.slf4j.LoggerFactory; | |
37 | ||
38 | /** | |
39 | * Connector servlet of the File Browser. It accepts requests begins the request | |
40 | * cycle, forwards requests to the {@link Dispatcher dispatcher} and ends the | |
41 | * request cycle with an appropriate {@link net.fckeditor.response response}. | |
42 | * | |
43 | * @version $Id: ConnectorServlet.java 3729 2009-06-21 18:32:59Z mosipov $ | |
44 | */ | |
45 | 0 | public class ConnectorServlet extends HttpServlet { |
46 | private static final long serialVersionUID = -5742008970929377161L; | |
47 | 0 | private final Logger logger = LoggerFactory.getLogger(ConnectorServlet.class); |
48 | private transient Dispatcher dispatcher; | |
49 | ||
50 | /** | |
51 | * Initializes this servlet. It initializes the dispatcher internally. | |
52 | * | |
53 | * @throws ServletException | |
54 | * if an exception occurs that interrupts the servlet's normal | |
55 | * operation | |
56 | */ | |
57 | @Override | |
58 | public void init() throws ServletException { | |
59 | try { | |
60 | 0 | dispatcher = new Dispatcher(getServletContext()); |
61 | 0 | } catch (Exception e) { |
62 | 0 | logger.error("Dispatcher could not be initialized", e); |
63 | 0 | throw new ServletException(e); |
64 | 0 | } |
65 | 0 | } |
66 | ||
67 | /** | |
68 | * Passes a GET request to the dispatcher. | |
69 | * | |
70 | * @throws IOException | |
71 | * if an input or output error is detected when the servlet | |
72 | * handles the GET request | |
73 | * @throws ServletException | |
74 | * if the request for the GET could not be handled | |
75 | */ | |
76 | @Override | |
77 | protected void doGet(final HttpServletRequest request, | |
78 | final HttpServletResponse response) throws ServletException, | |
79 | IOException { | |
80 | 0 | request.setCharacterEncoding("UTF-8"); |
81 | 0 | response.setCharacterEncoding("UTF-8"); |
82 | 0 | response.setContentType("application/xml"); |
83 | 0 | response.setHeader("Cache-Control", "no-cache"); |
84 | 0 | PrintWriter out = response.getWriter(); |
85 | 0 | GetResponse getResponse = null; |
86 | ||
87 | try { | |
88 | 0 | ThreadLocalData.beginRequest(request); |
89 | 0 | getResponse = dispatcher.doGet(request); |
90 | 0 | } catch (Exception e) { |
91 | 0 | throw new ServletException(e); |
92 | } finally { | |
93 | /* | |
94 | * call this method to prevent detached requests or else the request | |
95 | * will probably never be garbage collected and will fill your | |
96 | * memory | |
97 | */ | |
98 | 0 | ThreadLocalData.endRequest(); |
99 | 0 | } |
100 | ||
101 | 0 | out.print(getResponse); |
102 | 0 | out.flush(); |
103 | 0 | out.close(); |
104 | 0 | } |
105 | ||
106 | /** | |
107 | * Passes a POST request to the dispatcher. | |
108 | * | |
109 | * @throws IOException | |
110 | * if an input or output error is detected when the servlet | |
111 | * handles the request | |
112 | * @throws ServletException | |
113 | * if the request for the POST could not be handled | |
114 | */ | |
115 | @Override | |
116 | protected void doPost(final HttpServletRequest request, | |
117 | final HttpServletResponse response) throws ServletException, | |
118 | IOException { | |
119 | 0 | request.setCharacterEncoding("UTF-8"); |
120 | 0 | response.setCharacterEncoding("UTF-8"); |
121 | 0 | response.setContentType("text/html"); |
122 | 0 | response.setHeader("Cache-Control", "no-cache"); |
123 | 0 | PrintWriter out = response.getWriter(); |
124 | 0 | UploadResponse uploadResponse = null; |
125 | ||
126 | try { | |
127 | 0 | ThreadLocalData.beginRequest(request); |
128 | 0 | uploadResponse = dispatcher.doPost(request); |
129 | 0 | } catch (Exception e) { |
130 | 0 | throw new ServletException(e); |
131 | } finally { | |
132 | /* | |
133 | * call this method to prevent detached requests or else the request | |
134 | * will probably never be garbage collected and will fill your | |
135 | * memory | |
136 | */ | |
137 | 0 | ThreadLocalData.endRequest(); |
138 | 0 | } |
139 | ||
140 | 0 | out.print(uploadResponse); |
141 | 0 | out.flush(); |
142 | 0 | out.close(); |
143 | 0 | } |
144 | ||
145 | } |