View Javadoc

1   /*
2    * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3    * Copyright (C) 2004-2010 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 4785 2009-12-21 20:10:28Z mosipov $
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  	 * 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  			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  	 * 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  		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  			 * 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  			ThreadLocalData.endRequest();
99  		}
100 
101 		out.print(getResponse);
102 		out.flush();
103 		out.close();
104 	}
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 		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 			 * 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 			ThreadLocalData.endRequest();
138 		}
139 
140 		out.print(uploadResponse);
141 		out.flush();
142 		out.close();
143 	}
144 
145 }