1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
40
41
42
43
44
45 public class ConnectorServlet extends HttpServlet {
46 private static final long serialVersionUID = -5742008970929377161L;
47 private final Logger logger = LoggerFactory.getLogger(ConnectorServlet.class);
48 private transient Dispatcher dispatcher;
49
50
51
52
53
54
55
56
57 @Override
58 public void init() throws ServletException {
59 try {
60 dispatcher = new Dispatcher(getServletContext());
61 } catch (Exception e) {
62 logger.error("Dispatcher could not be initialized", e);
63 throw new ServletException(e);
64 }
65 }
66
67
68
69
70
71
72
73
74
75
76 @Override
77 protected void doGet(final HttpServletRequest request,
78 final HttpServletResponse response) throws ServletException,
79 IOException {
80 request.setCharacterEncoding("UTF-8");
81 response.setCharacterEncoding("UTF-8");
82 response.setContentType("application/xml");
83 response.setHeader("Cache-Control", "no-cache");
84 PrintWriter out = response.getWriter();
85 GetResponse getResponse = null;
86
87 try {
88 ThreadLocalData.beginRequest(request);
89 getResponse = dispatcher.doGet(request);
90 } catch (Exception e) {
91 throw new ServletException(e);
92 } finally {
93
94
95
96
97
98 ThreadLocalData.endRequest();
99 }
100
101 out.print(getResponse);
102 out.flush();
103 out.close();
104 }
105
106
107
108
109
110
111
112
113
114
115 @Override
116 protected void doPost(final HttpServletRequest request,
117 final HttpServletResponse response) throws ServletException,
118 IOException {
119 request.setCharacterEncoding("UTF-8");
120 response.setCharacterEncoding("UTF-8");
121 response.setContentType("text/html");
122 response.setHeader("Cache-Control", "no-cache");
123 PrintWriter out = response.getWriter();
124 UploadResponse uploadResponse = null;
125
126 try {
127 ThreadLocalData.beginRequest(request);
128 uploadResponse = dispatcher.doPost(request);
129 } catch (Exception e) {
130 throw new ServletException(e);
131 } finally {
132
133
134
135
136
137 ThreadLocalData.endRequest();
138 }
139
140 out.print(uploadResponse);
141 out.flush();
142 out.close();
143 }
144
145 }