| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package com.xpn.xwiki.web; |
| 21 |
|
|
| 22 |
|
import java.io.IOException; |
| 23 |
|
|
| 24 |
|
import javax.servlet.http.HttpServletResponse; |
| 25 |
|
|
| 26 |
|
import org.apache.commons.lang3.StringUtils; |
| 27 |
|
import org.slf4j.Logger; |
| 28 |
|
import org.slf4j.LoggerFactory; |
| 29 |
|
import org.xwiki.csrf.CSRFToken; |
| 30 |
|
|
| 31 |
|
import com.xpn.xwiki.XWikiContext; |
| 32 |
|
import com.xpn.xwiki.XWikiException; |
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
@version |
| 38 |
|
|
| |
|
| 40.8% |
Uncovered Elements: 74 (125) |
Complexity: 31 |
Complexity Density: 0.39 |
|
| 39 |
|
public class SaveAndContinueAction extends XWikiAction |
| 40 |
|
{ |
| 41 |
|
|
| 42 |
|
|
| 43 |
|
private static final String WRAPPED_ACTION_CONTEXT_KEY = "SaveAndContinueAction.wrappedAction"; |
| 44 |
|
|
| 45 |
|
|
| 46 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(SaveAndContinueAction.class); |
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
@param |
| 52 |
|
@param |
| 53 |
|
@param |
| 54 |
|
|
| |
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 55 |
0 |
private void writeAjaxErrorResponse(int httpStatusCode, String message, XWikiContext context)... |
| 56 |
|
{ |
| 57 |
0 |
try { |
| 58 |
0 |
context.getResponse().setContentType("text/plain"); |
| 59 |
0 |
context.getResponse().setStatus(httpStatusCode); |
| 60 |
0 |
context.getResponse().setCharacterEncoding(context.getWiki().getEncoding()); |
| 61 |
0 |
context.getResponse().getWriter().print(message); |
| 62 |
|
} catch (IOException e) { |
| 63 |
0 |
LOGGER.error("Failed to send error response to AJAX save and continue request.", e); |
| 64 |
|
} |
| 65 |
|
} |
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
@param |
| 72 |
|
@param |
| 73 |
|
@param |
| 74 |
|
@return |
| 75 |
|
@throws |
| 76 |
|
|
| |
|
| 27.6% |
Uncovered Elements: 21 (29) |
Complexity: 7 |
Complexity Density: 0.37 |
|
| 77 |
15 |
private boolean doWrappedAction(boolean isAjaxRequest, String back, XWikiContext context) throws XWikiException... |
| 78 |
|
{ |
| 79 |
|
|
| 80 |
15 |
boolean failure = false; |
| 81 |
|
|
| 82 |
|
|
| 83 |
15 |
if (back != null && back.contains("editor=class")) { |
| 84 |
0 |
PropUpdateAction pua = new PropUpdateAction(); |
| 85 |
|
|
| 86 |
0 |
if (pua.propUpdate(context)) { |
| 87 |
0 |
if (isAjaxRequest) { |
| 88 |
0 |
String errorMessage = localizePlainOrKey((String) context.get("message")); |
| 89 |
0 |
writeAjaxErrorResponse(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, errorMessage, context); |
| 90 |
|
} else { |
| 91 |
0 |
context.put(WRAPPED_ACTION_CONTEXT_KEY, pua); |
| 92 |
|
} |
| 93 |
|
|
| 94 |
0 |
failure = true; |
| 95 |
|
} |
| 96 |
|
} else { |
| 97 |
15 |
SaveAction sa = new SaveAction(); |
| 98 |
15 |
if (sa.save(context)) { |
| 99 |
0 |
if (isAjaxRequest) { |
| 100 |
0 |
String errorMessage = |
| 101 |
|
localizePlainOrKey("core.editors.saveandcontinue.theDocumentWasNotSaved"); |
| 102 |
|
|
| 103 |
|
|
| 104 |
0 |
LOGGER.error("SaveAction.save(context) returned true while using save & continue"); |
| 105 |
0 |
writeAjaxErrorResponse(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, errorMessage, context); |
| 106 |
|
} else { |
| 107 |
0 |
context.put(WRAPPED_ACTION_CONTEXT_KEY, sa); |
| 108 |
|
} |
| 109 |
|
|
| 110 |
0 |
failure = true; |
| 111 |
|
} else { |
| 112 |
|
|
| 113 |
15 |
context.getDoc().getTranslatedDocument(context).setLock(context.getUser(), context); |
| 114 |
|
} |
| 115 |
|
} |
| 116 |
|
|
| 117 |
15 |
return failure; |
| 118 |
|
} |
| 119 |
|
|
| 120 |
|
|
| 121 |
|
@param |
| 122 |
|
@param |
| 123 |
|
@throws |
| 124 |
|
|
| |
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 125 |
0 |
private void handleCSRFValidationFailure(boolean isAjaxRequest, XWikiContext context)... |
| 126 |
|
throws XWikiException |
| 127 |
|
{ |
| 128 |
0 |
final String csrfCheckFailedMessage = localizePlainOrKey("core.editors.saveandcontinue.csrfCheckFailed"); |
| 129 |
0 |
if (isAjaxRequest) { |
| 130 |
0 |
writeAjaxErrorResponse(HttpServletResponse.SC_FORBIDDEN, |
| 131 |
|
csrfCheckFailedMessage, |
| 132 |
|
context); |
| 133 |
|
} else { |
| 134 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_APP, |
| 135 |
|
XWikiException.ERROR_XWIKI_ACCESS_TOKEN_INVALID, |
| 136 |
|
csrfCheckFailedMessage); |
| 137 |
|
} |
| 138 |
|
} |
| 139 |
|
|
| 140 |
|
|
| 141 |
|
@param |
| 142 |
|
@param |
| 143 |
|
@param |
| 144 |
|
@throws |
| 145 |
|
|
| |
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 4 |
Complexity Density: 0.4 |
|
| 146 |
0 |
private void handleException(boolean isAjaxRequest, Exception exception, XWikiContext context)... |
| 147 |
|
throws XWikiException |
| 148 |
|
{ |
| 149 |
0 |
if (isAjaxRequest) { |
| 150 |
0 |
String errorMessage = |
| 151 |
|
localizePlainOrKey("core.editors.saveandcontinue.exceptionWhileSaving", exception.getMessage()); |
| 152 |
|
|
| 153 |
0 |
writeAjaxErrorResponse(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, errorMessage, context); |
| 154 |
|
|
| 155 |
0 |
String logMessage = "Caught exception during save and continue"; |
| 156 |
0 |
if (exception instanceof XWikiException) { |
| 157 |
0 |
LOGGER.info(logMessage, exception); |
| 158 |
|
} else { |
| 159 |
0 |
LOGGER.error(logMessage, exception); |
| 160 |
|
} |
| 161 |
|
} else { |
| 162 |
0 |
if (exception instanceof XWikiException) { |
| 163 |
0 |
throw (XWikiException) exception; |
| 164 |
|
} else { |
| 165 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_UNKNOWN, |
| 166 |
|
"Uncaught exception", exception); |
| 167 |
|
} |
| 168 |
|
} |
| 169 |
|
} |
| 170 |
|
|
| |
|
| 70.8% |
Uncovered Elements: 7 (24) |
Complexity: 6 |
Complexity Density: 0.33 |
|
| 171 |
15 |
@Override... |
| 172 |
|
public boolean action(XWikiContext context) throws XWikiException |
| 173 |
|
{ |
| 174 |
15 |
CSRFToken csrf = Utils.getComponent(CSRFToken.class); |
| 175 |
15 |
String token = context.getRequest().getParameter("form_token"); |
| 176 |
|
|
| 177 |
|
|
| 178 |
|
|
| 179 |
|
|
| 180 |
|
|
| 181 |
|
|
| 182 |
|
|
| 183 |
|
|
| 184 |
15 |
final boolean isAjaxRequest = Utils.isAjaxRequest(context); |
| 185 |
|
|
| 186 |
15 |
if (!csrf.isTokenValid(token)) { |
| 187 |
0 |
handleCSRFValidationFailure(isAjaxRequest, context); |
| 188 |
0 |
return false; |
| 189 |
|
} |
| 190 |
|
|
| 191 |
|
|
| 192 |
15 |
String back = findBackURL(context); |
| 193 |
|
|
| 194 |
15 |
try { |
| 195 |
15 |
if (doWrappedAction(isAjaxRequest, back, context)) { |
| 196 |
0 |
return !isAjaxRequest; |
| 197 |
|
} |
| 198 |
|
} catch (Exception e) { |
| 199 |
0 |
handleException(isAjaxRequest, e, context); |
| 200 |
0 |
return !isAjaxRequest; |
| 201 |
|
} |
| 202 |
|
|
| 203 |
|
|
| 204 |
15 |
if (isAjaxRequest) { |
| 205 |
11 |
context.getResponse().setStatus(HttpServletResponse.SC_NO_CONTENT); |
| 206 |
11 |
return false; |
| 207 |
|
} |
| 208 |
|
|
| 209 |
|
|
| 210 |
4 |
try { |
| 211 |
4 |
context.getResponse().sendRedirect(back); |
| 212 |
|
} catch (IOException ignored) { |
| 213 |
|
|
| 214 |
|
|
| 215 |
|
} |
| 216 |
4 |
return false; |
| 217 |
|
} |
| 218 |
|
|
| |
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 219 |
0 |
@Override... |
| 220 |
|
public String render(XWikiContext context) throws XWikiException |
| 221 |
|
{ |
| 222 |
0 |
XWikiAction wrappedAction = (XWikiAction) context.get(WRAPPED_ACTION_CONTEXT_KEY); |
| 223 |
|
|
| 224 |
0 |
if (wrappedAction != null) { |
| 225 |
0 |
return wrappedAction.render(context); |
| 226 |
|
} |
| 227 |
|
|
| 228 |
0 |
return "exception"; |
| 229 |
|
} |
| 230 |
|
|
| 231 |
|
|
| 232 |
|
|
| 233 |
|
|
| 234 |
|
@param |
| 235 |
|
@return |
| 236 |
|
|
| |
|
| 86.7% |
Uncovered Elements: 2 (15) |
Complexity: 4 |
Complexity Density: 0.44 |
|
| 237 |
15 |
private String findBackURL(XWikiContext context)... |
| 238 |
|
{ |
| 239 |
15 |
XWikiRequest request = context.getRequest(); |
| 240 |
15 |
String back = request.getParameter("xcontinue"); |
| 241 |
15 |
if (StringUtils.isEmpty(back)) { |
| 242 |
1 |
back = request.getParameter("xredirect"); |
| 243 |
|
} |
| 244 |
15 |
if (StringUtils.isEmpty(back)) { |
| 245 |
1 |
back = removeAllParametersFromQueryStringExceptEditor(request.getHeader("Referer")); |
| 246 |
|
} |
| 247 |
15 |
if (StringUtils.isEmpty(back)) { |
| 248 |
0 |
back = context.getDoc().getURL("edit", context); |
| 249 |
|
} |
| 250 |
15 |
return back; |
| 251 |
|
} |
| 252 |
|
|
| 253 |
|
|
| 254 |
|
@param |
| 255 |
|
@return |
| 256 |
|
|
| 257 |
|
|
| |
|
| 60% |
Uncovered Elements: 6 (15) |
Complexity: 4 |
Complexity Density: 0.44 |
|
| 258 |
1 |
private String removeAllParametersFromQueryStringExceptEditor(String url)... |
| 259 |
|
{ |
| 260 |
1 |
if (url == null) { |
| 261 |
0 |
return ""; |
| 262 |
|
} |
| 263 |
|
|
| 264 |
1 |
String[] baseAndQuery = url.split("\\?"); |
| 265 |
|
|
| 266 |
1 |
if (baseAndQuery.length < 2) { |
| 267 |
0 |
return url; |
| 268 |
|
} |
| 269 |
|
|
| 270 |
1 |
String[] queryBeforeAndAfterEditor = baseAndQuery[1].split("editor="); |
| 271 |
|
|
| 272 |
1 |
if (queryBeforeAndAfterEditor.length < 2) { |
| 273 |
1 |
return baseAndQuery[0]; |
| 274 |
|
} |
| 275 |
|
|
| 276 |
0 |
return baseAndQuery[0] + "?editor=" + queryBeforeAndAfterEditor[1].split("&")[0]; |
| 277 |
|
} |
| 278 |
|
} |