1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.web

File PreviewAction.java

 

Coverage histogram

../../../../img/srcFileCovDistChart6.png
69% of files have more coverage

Code metrics

16
29
4
1
120
58
13
0.45
7.25
4
3.25

Classes

Class Line # Actions
PreviewAction 43 29 0% 13 22
0.551020455.1%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.web;
21   
22    import org.apache.commons.lang3.StringUtils;
23   
24    import com.xpn.xwiki.XWikiContext;
25    import com.xpn.xwiki.XWikiException;
26    import com.xpn.xwiki.doc.XWikiDocument;
27   
28    /**
29    * <p>
30    * Action for previewing document changes. It prepares a temporarily changed document which is placed in the context,
31    * without actually saving anything. The response is normally rendered by the {@code preview.vm} template.
32    * </p>
33    * <p>
34    * This action also works like a request dispatcher, an early work-around for the fact that in HTML a form can only have
35    * one destination URL. Thus, the form had to be submitted to one action which would further dispatch the request to
36    * other actions, based on the clicked form button. Since preview is the safest method of the possible form actions, it
37    * was chosen as the dispatcher. Currently this functionality is deprecated and maintained only for backwards
38    * compatibility with older skins, since a cleaner dispatcher was implemented in {@link ActionFilter}.
39    * </p>
40    *
41    * @version $Id: 878c3503149afb89b6de0740065220d27ec46551 $
42    */
 
43    public class PreviewAction extends EditAction
44    {
45    /**
46    * Default constructor.
47    */
 
48  41 toggle public PreviewAction()
49    {
50  41 this.waitForXWikiInitialization = true;
51    }
52   
53    /**
54    * Check if a certain action was selected by the user. This is needed in older skins, which don't make use of the
55    * {@link ActionFilter}'s dispatcher functionality, but rely on detecting the submit button that was clicked.
56    *
57    * @param action the request parameter value that should be tested
58    * @return {@code true} if the value is a non-empty string, {@code false} otherwise
59    */
 
60  9 toggle private boolean isActionSelected(String action)
61    {
62  9 return StringUtils.isNotEmpty(action);
63    }
64   
 
65  3 toggle @Override
66    public boolean action(XWikiContext context) throws XWikiException
67    {
68  3 XWikiRequest request = context.getRequest();
69  3 String formactionsave = request.getParameter("formactionsave");
70  3 String formactioncancel = request.getParameter("formactioncancel");
71  3 String formactionsac = request.getParameter("formactionsac");
72   
73  3 if (isActionSelected(formactionsave)) {
74  0 SaveAction sa = new SaveAction();
75  0 if (sa.action(context)) {
76  0 sa.render(context);
77    }
78  0 return false;
79    }
80   
81  3 if (isActionSelected(formactioncancel)) {
82  0 CancelAction ca = new CancelAction();
83  0 if (ca.action(context)) {
84  0 ca.render(context);
85    }
86  0 return false;
87    }
88   
89  3 if (isActionSelected(formactionsac)) {
90  0 SaveAndContinueAction saca = new SaveAndContinueAction();
91  0 if (saca.action(context)) {
92  0 saca.render(context);
93    }
94  0 return false;
95    }
96  3 return true;
97    }
98   
 
99  3 toggle @Override
100    public String render(XWikiContext context) throws XWikiException
101    {
102  3 XWikiDocument editedDocument = prepareEditedDocument(context);
103   
104    // The current user editing the document should be displayed as author and creator (if the edited document is
105    // new) when the edited document is previewed.
106  3 editedDocument.setAuthorReference(context.getUserReference());
107  3 if (editedDocument.isNew()) {
108  1 editedDocument.setCreatorReference(context.getUserReference());
109    }
110   
111    // Make sure the current user doesn't use the programming rights of the previous content author (by editing a
112    // document saved with programming rights, changing it and then previewing it). Also make sure the code
113    // requiring programming rights is executed in preview mode if the current user has programming rights.
114  3 editedDocument.setContentAuthorReference(context.getUserReference());
115   
116    // Reconfirm edit (captcha) when jcaptcha is not correct.
117  3 Boolean reCheckCaptcha = (Boolean) context.get("recheckcaptcha");
118  3 return reCheckCaptcha != null && reCheckCaptcha ? "captcha" : "preview";
119    }
120    }