1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.web

File PreviewAction.java

 

Coverage histogram

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

Code metrics

14
28
4
1
118
57
11
0.39
7
4
2.75

Classes

Class Line # Actions
PreviewAction 43 28 0% 11 21
0.5434782554.3%
 

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: e835b247964bed1d97845c5a729ba788e8f9eb41 $
42    */
 
43    public class PreviewAction extends EditAction
44    {
45    /**
46    * Default constructor.
47    */
 
48  74 toggle public PreviewAction()
49    {
50  74 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  51 toggle private boolean isActionSelected(String action)
61    {
62  51 return StringUtils.isNotEmpty(action);
63    }
64   
 
65  17 toggle @Override
66    public boolean action(XWikiContext context) throws XWikiException
67    {
68  17 XWikiRequest request = context.getRequest();
69  17 String formactionsave = request.getParameter("formactionsave");
70  17 String formactioncancel = request.getParameter("formactioncancel");
71  17 String formactionsac = request.getParameter("formactionsac");
72   
73  17 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  17 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  17 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  17 return true;
97    }
98   
 
99  17 toggle @Override
100    public String render(XWikiContext context) throws XWikiException
101    {
102  17 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  17 editedDocument.setAuthorReference(context.getUserReference());
107  17 if (editedDocument.isNew()) {
108  3 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  17 editedDocument.setContentAuthorReference(context.getUserReference());
115   
116  17 return "preview";
117    }
118    }