Source code: com/xpn/xwiki/web/ViewEditAction.java
1 /**
2 * ===================================================================
3 *
4 * Copyright (c) 2003 Ludovic Dubost, All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program 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
14 * GNU General Public License for more details, published at
15 * http://www.gnu.org/copyleft/gpl.html or in gpl.txt in the
16 * root folder of this distribution.
17 *
18 * Created by
19 * User: Ludovic Dubost
20 * Date: 25 nov. 2003
21 * Time: 21:20:04
22 */
23
24
25 package com.xpn.xwiki.web;
26
27 import com.xpn.xwiki.XWiki;
28 import com.xpn.xwiki.XWikiContext;
29 import com.xpn.xwiki.XWikiException;
30 import com.xpn.xwiki.XWikiService;
31 import com.xpn.xwiki.monitor.api.MonitorPlugin;
32 import com.xpn.xwiki.render.XWikiVelocityRenderer;
33 import com.xpn.xwiki.xmlrpc.*;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.apache.log4j.MDC;
37 import org.apache.struts.action.ActionForm;
38 import org.apache.struts.action.ActionForward;
39 import org.apache.struts.action.ActionMapping;
40 import org.apache.velocity.VelocityContext;
41 import org.apache.xmlrpc.XmlRpcServer;
42
43 import javax.servlet.ServletException;
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpServletResponse;
46 import java.io.IOException;
47 import java.io.OutputStream;
48
49 /**
50 * <p>A simple action that handles the display and editing of an
51 * wiki page.. </p>
52 *
53 * <p>The action support an <i>action</i> URL. The action in the URL
54 * controls what this action class does. The following values are supported:</p>
55 * <ul>
56 * <li>view - view the Wiki Document
57 * <li>edit - edit the Wiki Document
58 * <li>preview - preview the Wiki Document
59 * <li>save - save the Wiki Document
60 * </ul>
61 *
62 */
63 public class ViewEditAction extends XWikiAction
64 {
65 private static final Log log = LogFactory.getLog(ViewEditAction.class);
66 private static final long UPLOAD_DEFAULT_MAXSIZE = 10000000L;
67 private static final long UPLOAD_DEFAULT_SIZETHRESHOLD = 100000L;
68
69 public ViewEditAction() throws Exception {
70 super();
71 }
72
73 // --------------------------------------------------------- Public Methods
74 /**
75 * Handle server requests.
76 *
77 * @param mapping The ActionMapping used to select this instance
78 * @param form The optional ActionForm bean for this request (if any)
79 * @param req The HTTP request we are processing
80 * @param resp The HTTP response we are creating
81 *
82 * @exception IOException if an input/output error occurs
83 * @exception ServletException if a servlet exception occurs
84 */
85 public ActionForward execute(ActionMapping mapping,
86 ActionForm form,
87 HttpServletRequest req,
88 HttpServletResponse resp)
89 throws Exception, ServletException
90 {
91 String action = mapping.getName();
92
93 if (action.equals("xmlrpc")) {
94 renderXMLRPC(req, resp);
95 return null;
96 }
97
98 MonitorPlugin monitor = null;
99 try {
100 XWikiRequest request = new XWikiServletRequest(req);
101 XWikiResponse response = new XWikiServletResponse(resp);
102 XWikiContext context = Utils.prepareContext(action, request, response, new XWikiServletContext(servlet.getServletContext()));
103 XWikiService xwikiservice = new XWikiService();
104
105 // Add the form to the context
106 context.setForm((XWikiForm) form);
107
108 // We should not go further for the Database Status
109 // To make sure we don't have more database connections
110 if (action.equals("status")) {
111 String renderResult = xwikiservice.renderStatus(context);
112 String page = Utils.getPage(request, renderResult);
113 Utils.parseTemplate(page, !page.equals("direct"), context);
114 return null;
115 }
116
117 XWiki xwiki = XWiki.getXWiki(context);
118 // Any error before this will be treated using a redirection to an error page
119
120 // Start monitoring timer
121 monitor = (MonitorPlugin) xwiki.getPlugin("monitor", context);
122 if (monitor!=null)
123 monitor.startRequest("", action, context.getURL());
124 if (monitor!=null)
125 monitor.startTimer("request");
126
127 VelocityContext vcontext = null;
128 // Prepare velocity context
129 vcontext = XWikiVelocityRenderer.prepareContext(context);
130
131 try {
132 // Prepare documents and put them in the context
133 if (xwiki.prepareDocuments(request, context, vcontext)==false)
134 return null;
135
136 if (monitor!=null)
137 monitor.setWikiPage(context.getDoc().getFullName());
138
139 String renderResult = null;
140
141 // Determine what to do
142 if (action.equals("view")) {
143 renderResult = xwikiservice.renderView(context);
144 }
145 else if ( action.equals("inline")) {
146 renderResult = xwikiservice.renderInline(context);
147 }
148 else if ( action.equals("edit") ) {
149 renderResult = xwikiservice.renderEdit(context);
150 }
151 else if ( action.equals("preview")) {
152 renderResult = xwikiservice.renderPreview(context);
153 }
154 else if (action.equals("save")) {
155 xwikiservice.actionSave(context);
156 }
157 else if (action.equals("rollback")) {
158 xwikiservice.actionRollback(context);
159 }
160 else if (action.equals("cancel")) {
161 xwikiservice.actionCancel(context);
162 }
163 else if (action.equals("delete")) {
164 if (xwikiservice.actionDelete(context))
165 renderResult = xwikiservice.renderDelete(context);
166 }
167 else if (action.equals("pdf")) {
168 renderResult = xwikiservice.renderPDF(context);
169 }
170 else if (action.equals("propupdate")) {
171 xwikiservice.actionPropupdate(context);
172 }
173 else if (action.equals("propadd")) {
174 xwikiservice.actionPropadd(context);
175 }
176 else if (action.equals("objectadd")) {
177 xwikiservice.actionObjectadd(context);
178 }
179 else if (action.equals("commentadd")) {
180 xwikiservice.actionCommentadd(context);
181 }
182 else if (action.equals("objectremove")) {
183 xwikiservice.actionObjectremove(context);
184 }
185 else if (action.equals("download")) {
186 renderResult = xwikiservice.renderDownload(context);
187 }
188 else if (action.equals("dot")) {
189 renderResult = xwikiservice.renderDot(context);
190 }
191 else if (action.equals("attach")) {
192 renderResult = xwikiservice.renderAttach(context);
193 }
194 else if (action.equals("upload")) {
195 xwikiservice.actionUpload(context);
196 }
197 else if (action.equals("delattachment")) {
198 xwikiservice.actionDelattachment(context);
199 }
200 else if (action.equals("skin")) {
201 if (xwikiservice.actionSkin(context))
202 renderResult = xwikiservice.renderSkin(context);
203 }
204 else if (action.equals("login")) {
205 renderResult = xwikiservice.renderLogin(context);
206 }
207 else if (action.equals("loginerror")) {
208 renderResult = xwikiservice.renderLoginerror(context);
209 }
210 else if (action.equals("logout")) {
211 xwikiservice.actionLogout(context);
212 }
213 else if (action.equals("lifeblog")) {
214 renderResult = xwikiservice.renderLifeblog(context);
215 }
216
217 if (renderResult!=null) {
218 String page = Utils.getPage(request, renderResult);
219 Utils.parseTemplate(page, !page.equals("direct"), context);
220 }
221 return null;
222 } catch (Throwable e) {
223 if (!(e instanceof XWikiException)) {
224 e = new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_UNKNOWN,
225 "Uncaught exception", e);
226 }
227
228 vcontext.put("exp", e);
229 try {
230 XWikiException xex = (XWikiException) e;
231 if (xex.getCode()==XWikiException.ERROR_XWIKI_ACCESS_DENIED) {
232 String page = Utils.getPage(request, "accessdenied");
233 Utils.parseTemplate(page, context);
234 return null;
235 } else if (xex.getCode()==XWikiException.ERROR_XWIKI_USER_INACTIVE) {
236 String page = Utils.getPage(request, "userinactive");
237 Utils.parseTemplate(page, context);
238 return null;
239 }
240
241 if (log.isWarnEnabled()) {
242 log.warn("Uncaught exception: " + e.getMessage(), e);
243 }
244 Utils.parseTemplate(Utils.getPage(request, "exception"), context);
245 return null;
246 } catch (Exception e2) {
247 // I hope this never happens
248 e.printStackTrace();
249 e2.printStackTrace();
250 return null;
251 }
252 } finally {
253
254 // Let's make sure we have flushed content and closed
255 try {
256 response.getWriter().flush();
257 } catch (Throwable e) {
258 }
259
260 if (monitor!=null)
261 monitor.endTimer("request");
262
263 if (monitor!=null)
264 monitor.startTimer("notify");
265
266 // Let's handle the notification and make sure it never fails
267 try {
268 xwiki.getNotificationManager().verify(context.getDoc(), action, context);
269 } catch (Throwable e) {
270 e.printStackTrace();
271 }
272
273 if (monitor!=null)
274 monitor.endTimer("notify");
275
276 // Make sure we cleanup database connections
277 // There could be cases where we have some
278 if ((context!=null)&&(xwiki!=null)) {
279 xwiki.getStore().cleanUp(context);
280 }
281 }
282 } finally {
283 // End request
284 if (monitor!=null)
285 monitor.endRequest();
286
287 MDC.remove("url");
288 }
289 }
290
291 public void renderXMLRPC(HttpServletRequest request, HttpServletResponse response) {
292 XmlRpcServer xmlrpcserver = new XmlRpcServer();
293 xmlrpcserver.addHandler("wiki", new XWikiRpcHandler(
294 new XWikiXMLRPCRequest(request),
295 new XWikiXMLRPCResponse(response),
296 new XWikiXMLRPCContext(servlet.getServletContext())));
297 xmlrpcserver.addHandler("confluence1", new ConfluenceRpcHandler(
298 new XWikiXMLRPCRequest(request),
299 new XWikiXMLRPCResponse(response),
300 new XWikiXMLRPCContext(servlet.getServletContext())));
301 try {
302 byte[] result = xmlrpcserver.execute( request.getInputStream() );
303 response.setContentType( "text/xml; charset=utf-8" );
304 response.setContentLength( result.length );
305 OutputStream out = response.getOutputStream();
306 out.write( result );
307 out.flush();
308 } catch (Exception e) {
309 e.printStackTrace();
310 }
311 }
312
313 }
314