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

File DefaultDocumentAccessBridge.java

 

Coverage histogram

../../../../img/srcFileCovDistChart4.png
78% of files have more coverage

Code metrics

86
295
62
1
899
717
123
0.42
4.76
62
1.98

Classes

Class Line # Actions
DefaultDocumentAccessBridge 68 295 0% 123 291
0.3431151234.3%
 

Contributing tests

This file is covered by 181 tests. .

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.doc;
21   
22    import java.io.ByteArrayInputStream;
23    import java.io.InputStream;
24    import java.util.ArrayList;
25    import java.util.Collections;
26    import java.util.List;
27    import java.util.Map;
28   
29    import javax.inject.Inject;
30    import javax.inject.Named;
31    import javax.inject.Provider;
32    import javax.inject.Singleton;
33   
34    import org.apache.commons.lang3.StringUtils;
35    import org.apache.commons.lang3.exception.ExceptionUtils;
36    import org.slf4j.Logger;
37    import org.xwiki.bridge.DocumentAccessBridge;
38    import org.xwiki.bridge.DocumentModelBridge;
39    import org.xwiki.component.annotation.Component;
40    import org.xwiki.model.EntityType;
41    import org.xwiki.model.reference.AttachmentReference;
42    import org.xwiki.model.reference.DocumentReference;
43    import org.xwiki.model.reference.DocumentReferenceResolver;
44    import org.xwiki.model.reference.EntityReference;
45    import org.xwiki.model.reference.EntityReferenceSerializer;
46    import org.xwiki.model.reference.ObjectPropertyReference;
47    import org.xwiki.model.reference.ObjectReference;
48    import org.xwiki.security.authorization.ContextualAuthorizationManager;
49    import org.xwiki.security.authorization.Right;
50   
51    import com.xpn.xwiki.XWikiContext;
52    import com.xpn.xwiki.XWikiException;
53    import com.xpn.xwiki.objects.BaseObject;
54    import com.xpn.xwiki.objects.BaseProperty;
55    import com.xpn.xwiki.objects.classes.PropertyClass;
56    import com.xpn.xwiki.user.api.XWikiRightService;
57   
58    /**
59    * Exposes methods for accessing Document data. This is temporary until we remodel the Model classes and the Document
60    * services. The implementation is inside the old core, and not in a component because it has dependencies on the old
61    * core.
62    *
63    * @version $Id: 3570d78c3d5460c76de9b4390085d7559e7b0d4b $
64    * @since 1.6M1
65    */
66    @Component
67    @Singleton
 
68    public class DefaultDocumentAccessBridge implements DocumentAccessBridge
69    {
70    /** Needed for accessing the XWikiContext. */
71    @Inject
72    private Provider<XWikiContext> contextProvider;
73   
74    /**
75    * Used to resolve a string into a proper Document Reference using the current document's reference to fill the
76    * blanks, except for the page name for which the default page name is used instead.
77    */
78    @Inject
79    @Named("currentmixed")
80    private DocumentReferenceResolver<String> currentMixedDocumentReferenceResolver;
81   
82    /**
83    * Used to serialize full reference of current user.
84    */
85    @Inject
86    private EntityReferenceSerializer<String> defaultEntityReferenceSerializer;
87   
88    /**
89    * Used to convert a Document Reference to string (compact form without the wiki part if it matches the current
90    * wiki).
91    */
92    @Inject
93    @Named("compactwiki")
94    private EntityReferenceSerializer<String> compactWikiEntityReferenceSerializer;
95   
96    @Inject
97    private Provider<ContextualAuthorizationManager> authorizationProvider;
98   
99    @Inject
100    private Logger logger;
101   
 
102  3166520 toggle private XWikiContext getContext()
103    {
104  3166509 return this.contextProvider.get();
105    }
106   
 
107  0 toggle @Override
108    @Deprecated
109    public DocumentModelBridge getDocument(String documentReference) throws Exception
110    {
111  0 XWikiContext xcontext = getContext();
112  0 return xcontext.getWiki().getDocument(documentReference, xcontext).getTranslatedDocument(xcontext);
113    }
114   
 
115  13499 toggle @Override
116    public DocumentModelBridge getDocument(DocumentReference documentReference) throws Exception
117    {
118  13499 XWikiContext xcontext = getContext();
119  13499 return xcontext.getWiki().getDocument(documentReference, xcontext).getTranslatedDocument(xcontext);
120    }
121   
 
122  225866 toggle @Override
123    public DocumentReference getCurrentDocumentReference()
124    {
125  225859 XWikiDocument currentDocument = null;
126  225836 XWikiContext context = getContext();
127  225873 if (context != null) {
128  225857 currentDocument = context.getDoc();
129    }
130   
131  225855 return currentDocument == null ? null : currentDocument.getDocumentReference();
132    }
133   
 
134  0 toggle @Override
135    @Deprecated
136    public String getDocumentContent(String documentReference) throws Exception
137    {
138  0 XWikiContext xcontext = getContext();
139  0 return getDocumentContent(documentReference, xcontext.getLanguage());
140    }
141   
 
142  0 toggle @Override
143    public String getDocumentContentForDefaultLanguage(DocumentReference documentReference) throws Exception
144    {
145  0 XWikiContext xcontext = getContext();
146  0 return xcontext.getWiki().getDocument(documentReference, xcontext).getContent();
147    }
148   
 
149  0 toggle @Override
150    @Deprecated
151    public String getDocumentContentForDefaultLanguage(String documentReference) throws Exception
152    {
153  0 XWikiContext xcontext = getContext();
154  0 return xcontext.getWiki().getDocument(documentReference, xcontext).getContent();
155    }
156   
 
157  0 toggle @Override
158    public String getDocumentContent(DocumentReference documentReference, String language) throws Exception
159    {
160  0 XWikiContext xcontext = getContext();
161  0 String originalRev = (String) xcontext.get("rev");
162  0 try {
163  0 xcontext.remove("rev");
164  0 return xcontext.getWiki().getDocument(documentReference, xcontext).getTranslatedContent(language, xcontext);
165    } finally {
166  0 if (originalRev != null) {
167  0 xcontext.put("rev", originalRev);
168    }
169    }
170    }
171   
 
172  0 toggle @Override
173    @Deprecated
174    public String getDocumentContent(String documentReference, String language) throws Exception
175    {
176  0 XWikiContext xcontext = getContext();
177  0 String originalRev = (String) xcontext.get("rev");
178  0 try {
179  0 xcontext.remove("rev");
180  0 return xcontext.getWiki().getDocument(documentReference, xcontext).getTranslatedContent(language, xcontext);
181    } finally {
182  0 if (originalRev != null) {
183  0 xcontext.put("rev", originalRev);
184    }
185    }
186    }
187   
 
188  3666 toggle @Override
189    public boolean exists(DocumentReference documentReference)
190    {
191  3666 XWikiContext context = getContext();
192  3668 if (context != null) {
193  3668 return context.getWiki().exists(documentReference, context);
194    } else {
195  0 return false;
196    }
197    }
198   
 
199  0 toggle @Override
200    @Deprecated
201    public boolean exists(String documentReference)
202    {
203  0 XWikiContext context = getContext();
204  0 if (context != null) {
205  0 return context.getWiki().exists(documentReference, context);
206    } else {
207  0 return false;
208    }
209    }
210   
 
211  0 toggle @Override
212    public void setDocumentContent(DocumentReference documentReference, String content, String editComment,
213    boolean isMinorEdit) throws Exception
214    {
215  0 XWikiContext xcontext = getContext();
216  0 XWikiDocument doc = xcontext.getWiki().getDocument(documentReference, xcontext);
217  0 doc.setContent(content);
218  0 saveDocument(doc, editComment, isMinorEdit);
219    }
220   
 
221  0 toggle @Override
222    @Deprecated
223    public void setDocumentContent(String documentReference, String content, String editComment, boolean isMinorEdit)
224    throws Exception
225    {
226  0 XWikiContext xcontext = getContext();
227  0 XWikiDocument doc = xcontext.getWiki().getDocument(documentReference, xcontext);
228  0 doc.setContent(content);
229  0 saveDocument(doc, editComment, isMinorEdit);
230    }
231   
 
232  0 toggle @Override
233    @Deprecated
234    public String getDocumentSyntaxId(String documentReference) throws Exception
235    {
236  0 XWikiContext xcontext = getContext();
237  0 XWikiDocument doc = xcontext.getWiki().getDocument(documentReference, xcontext);
238   
239  0 return doc.getSyntaxId();
240    }
241   
 
242  0 toggle @Override
243    public void setDocumentSyntaxId(DocumentReference documentReference, String syntaxId) throws Exception
244    {
245  0 XWikiContext xcontext = getContext();
246  0 XWikiDocument doc = xcontext.getWiki().getDocument(documentReference, xcontext);
247  0 doc.setSyntaxId(syntaxId);
248  0 saveDocument(doc, String.format("Changed document syntax from [%s] to [%s].", doc.getSyntax(), syntaxId), true);
249    }
250   
 
251  0 toggle @Override
252    @Deprecated
253    public void setDocumentSyntaxId(String documentReference, String syntaxId) throws Exception
254    {
255  0 XWikiContext xcontext = getContext();
256  0 XWikiDocument doc = xcontext.getWiki().getDocument(documentReference, xcontext);
257  0 String oldSyntaxId = doc.getSyntaxId();
258  0 doc.setSyntaxId(syntaxId);
259  0 saveDocument(doc, String.format("Changed document syntax from [%s] to [%s].", oldSyntaxId, syntaxId), true);
260    }
261   
 
262  0 toggle @Override
263    public void setDocumentParentReference(DocumentReference documentReference, DocumentReference parentReference)
264    throws Exception
265    {
266  0 XWikiContext xcontext = getContext();
267  0 XWikiDocument doc = xcontext.getWiki().getDocument(documentReference, xcontext);
268  0 doc.setParent(this.compactWikiEntityReferenceSerializer.serialize(parentReference, doc.getDocumentReference()));
269  0 saveDocument(
270    doc,
271    String.format("Changed document parent to [%s].",
272    this.defaultEntityReferenceSerializer.serialize(parentReference)), true);
273    }
274   
 
275  0 toggle @Override
276    public void setDocumentTitle(DocumentReference documentReference, String title) throws Exception
277    {
278  0 XWikiContext xcontext = getContext();
279  0 XWikiDocument doc = xcontext.getWiki().getDocument(documentReference, xcontext);
280  0 doc.setTitle(title);
281  0 saveDocument(doc, String.format("Changed document title to [%s].", title), true);
282    }
283   
 
284  18 toggle @Override
285    public int getObjectNumber(DocumentReference documentReference, DocumentReference classReference,
286    String propertyName, String valueToMatch)
287    {
288  18 try {
289  18 XWikiContext xcontext = getContext();
290  18 XWikiDocument doc = xcontext.getWiki().getDocument(documentReference, xcontext);
291  18 BaseObject object = doc.getXObject(classReference, propertyName, valueToMatch, false);
292  18 return object != null ? object.getNumber() : -1;
293    } catch (XWikiException e) {
294  0 return -1;
295    }
296    }
297   
 
298  1 toggle @Override
299    public Object getProperty(ObjectPropertyReference objectPropertyReference)
300    {
301  1 ObjectReference objectReference = (ObjectReference) objectPropertyReference.extractReference(EntityType.OBJECT);
302   
303  1 return getProperty(objectReference, objectPropertyReference.getName());
304    }
305   
 
306  1 toggle @Override
307    public Object getProperty(ObjectReference objectReference, String propertyName)
308    {
309  1 Object value = null;
310   
311  1 try {
312  1 XWikiContext xcontext = getContext();
313   
314  1 if (xcontext != null && xcontext.getWiki() != null) {
315  1 DocumentReference documentReference =
316    (DocumentReference) objectReference.extractReference(EntityType.DOCUMENT);
317  1 XWikiDocument doc = xcontext.getWiki().getDocument(documentReference, xcontext);
318  1 BaseObject object = doc.getXObject(objectReference);
319  1 if (object != null) {
320  1 BaseProperty property = (BaseProperty) object.get(propertyName);
321  1 if (property != null) {
322  1 value = property.getValue();
323    }
324    }
325    }
326    } catch (Exception e) {
327  0 this.logger.error("Failed to get property", e);
328    }
329   
330  1 return value;
331    }
332   
 
333  0 toggle @Override
334    public Object getProperty(String documentReference, String className, int objectNumber, String propertyName)
335    {
336  0 Object value = null;
337   
338  0 try {
339  0 XWikiContext xcontext = getContext();
340   
341  0 if (xcontext != null && xcontext.getWiki() != null) {
342  0 XWikiDocument doc = xcontext.getWiki().getDocument(documentReference, xcontext);
343  0 BaseObject object = doc.getObject(className, objectNumber);
344  0 if (object != null) {
345  0 BaseProperty property = (BaseProperty) object.get(propertyName);
346  0 if (property != null) {
347  0 value = property.getValue();
348    }
349    }
350    }
351    } catch (Exception e) {
352  0 this.logger.error("Failed to get property", e);
353    }
354   
355  0 return value;
356    }
357   
 
358  0 toggle @Override
359    @Deprecated
360    public Object getProperty(String documentReference, String className, String propertyName)
361    {
362  0 Object value = null;
363   
364  0 try {
365  0 XWikiContext xcontext = getContext();
366   
367  0 if (xcontext != null && xcontext.getWiki() != null) {
368  0 XWikiDocument doc = xcontext.getWiki().getDocument(documentReference, xcontext);
369  0 BaseObject object = doc.getObject(className);
370  0 if (object != null) {
371  0 BaseProperty property = (BaseProperty) object.get(propertyName);
372  0 if (property != null) {
373  0 value = property.getValue();
374    }
375    }
376    }
377    } catch (Exception e) {
378  0 this.logger.error("Failed to get property", e);
379    }
380   
381  0 return value;
382    }
383   
 
384  2651 toggle @Override
385    public Object getProperty(DocumentReference documentReference, DocumentReference classReference, String propertyName)
386    {
387  2651 Object value = null;
388   
389  2651 try {
390  2651 XWikiContext xcontext = getContext();
391   
392  2651 if (xcontext != null && xcontext.getWiki() != null) {
393  2651 XWikiDocument doc = xcontext.getWiki().getDocument(documentReference, xcontext);
394  2651 BaseObject object = doc.getXObject(classReference);
395  2651 if (object != null) {
396  612 BaseProperty property = (BaseProperty) object.get(propertyName);
397  612 if (property != null) {
398  586 value = property.getValue();
399    }
400    }
401    }
402    } catch (Exception e) {
403  0 this.logger.error("Failed to get property", e);
404    }
405   
406  2651 return value;
407    }
408   
 
409  18 toggle @Override
410    public Object getProperty(DocumentReference documentReference, DocumentReference classReference, int objectNumber,
411    String propertyName)
412    {
413  18 Object value = null;
414   
415  18 try {
416  18 XWikiContext xcontext = getContext();
417   
418  18 if (xcontext != null && xcontext.getWiki() != null) {
419  18 XWikiDocument doc = xcontext.getWiki().getDocument(documentReference, xcontext);
420  18 BaseObject object = doc.getXObject(classReference, objectNumber);
421  18 if (object != null) {
422  18 BaseProperty property = (BaseProperty) object.get(propertyName);
423  18 if (property != null) {
424  18 value = property.getValue();
425    }
426    }
427    }
428    } catch (Exception e) {
429  0 this.logger.error("Failed to get property", e);
430    }
431   
432  18 return value;
433    }
434   
 
435  0 toggle @Override
436    public Object getProperty(String documentReference, String propertyName)
437    {
438  0 Object value = null;
439   
440  0 try {
441  0 XWikiContext xcontext = getContext();
442   
443  0 if (xcontext != null && xcontext.getWiki() != null) {
444  0 XWikiDocument doc = xcontext.getWiki().getDocument(documentReference, xcontext);
445  0 BaseObject object = doc.getFirstObject(propertyName, xcontext);
446  0 if (object != null) {
447  0 BaseProperty property = (BaseProperty) object.get(propertyName);
448  0 if (property != null) {
449  0 value = property.getValue();
450    }
451    }
452    }
453    } catch (Exception e) {
454  0 this.logger.error("Failed to get property", e);
455    }
456   
457  0 return value;
458    }
459   
 
460  0 toggle @Override
461    public List<Object> getProperties(String documentReference, String className)
462    {
463  0 List<Object> result;
464  0 try {
465  0 XWikiContext xcontext = getContext();
466  0 result =
467    new ArrayList<Object>(xcontext.getWiki().getDocument(documentReference, xcontext).getObject(className)
468    .getFieldList());
469    } catch (Exception ex) {
470  0 result = Collections.emptyList();
471    }
472  0 return result;
473    }
474   
 
475  228 toggle @Override
476    public String getPropertyType(String className, String propertyName) throws Exception
477    {
478  228 XWikiContext xcontext = getContext();
479  228 PropertyClass pc = null;
480  228 try {
481  228 pc = (PropertyClass) xcontext.getWiki().getDocument(className, xcontext).getXClass().get(propertyName);
482    } catch (XWikiException e) {
483  0 this.logger.warn("Failed to get document [{}]. Root cause: [{}]", className,
484    ExceptionUtils.getRootCauseMessage(e));
485    }
486   
487  228 return pc == null ? null : pc.newProperty().getClass().getName();
488    }
489   
 
490  252 toggle @Override
491    public boolean isPropertyCustomMapped(String className, String property) throws Exception
492    {
493  252 XWikiContext xcontext = getContext();
494  252 if (!xcontext.getWiki().hasCustomMappings()) {
495  0 return false;
496    }
497  252 List<String> lst = xcontext.getWiki().getClass(className, xcontext).getCustomMappingPropertyList(xcontext);
498  252 return lst != null && lst.contains(property);
499    }
500   
 
501  0 toggle @Override
502    @Deprecated
503    public void setProperty(String documentReference, String className, String propertyName, Object propertyValue)
504    throws Exception
505    {
506  0 XWikiContext xcontext = getContext();
507  0 XWikiDocument doc = xcontext.getWiki().getDocument(documentReference, xcontext);
508  0 BaseObject obj = doc.getObject(className, true, xcontext);
509  0 if (obj != null) {
510  0 obj.set(propertyName, propertyValue, xcontext);
511  0 saveDocument(doc, String.format("Property [%s] set.", propertyName), true);
512    }
513    }
514   
 
515  0 toggle @Override
516    public void setProperty(DocumentReference documentReference, DocumentReference classReference, String propertyName,
517    Object propertyValue) throws Exception
518    {
519  0 XWikiContext xcontext = getContext();
520  0 XWikiDocument doc = xcontext.getWiki().getDocument(documentReference, xcontext);
521  0 BaseObject obj = doc.getXObject(classReference, true, xcontext);
522  0 if (obj != null) {
523  0 obj.set(propertyName, propertyValue, xcontext);
524  0 saveDocument(doc, String.format("Property [%s] set.", propertyName), true);
525    }
526    }
527   
 
528  0 toggle @Override
529    @Deprecated
530    public byte[] getAttachmentContent(String documentReference, String attachmentFilename) throws Exception
531    {
532  0 XWikiContext xcontext = getContext();
533  0 return xcontext.getWiki().getDocument(documentReference, xcontext).getAttachment(attachmentFilename)
534    .getContent(xcontext);
535    }
536   
 
537  0 toggle @Override
538    public InputStream getAttachmentContent(AttachmentReference attachmentReference) throws Exception
539    {
540  0 XWikiContext xcontext = getContext();
541  0 XWikiDocument attachmentDocument =
542    xcontext.getWiki().getDocument(attachmentReference.getDocumentReference(), xcontext);
543  0 return new ByteArrayInputStream(attachmentDocument.getAttachment(attachmentReference.getName()).getContent(
544    xcontext));
545    }
546   
 
547  0 toggle @Override
548    public void setAttachmentContent(AttachmentReference attachmentReference, byte[] attachmentData) throws Exception
549    {
550  0 XWikiContext xcontext = getContext();
551  0 XWikiDocument doc = xcontext.getWiki().getDocument(attachmentReference.getDocumentReference(), xcontext);
552  0 XWikiAttachment attachment = doc.getAttachment(attachmentReference.getName());
553  0 if (attachment == null) {
554  0 attachment = new XWikiAttachment();
555  0 doc.getAttachmentList().add(attachment);
556  0 doc.setComment("Add new attachment " + attachmentReference.getName());
557    } else {
558  0 doc.setComment("Update attachment " + attachmentReference.getName());
559    }
560  0 attachment.setContent(attachmentData);
561  0 attachment.setFilename(attachmentReference.getName());
562  0 attachment.setAuthor(getCurrentUser());
563  0 attachment.setDoc(doc);
564  0 doc.setAuthorReference(getContext().getUserReference());
565  0 if (doc.isNew()) {
566  0 doc.setCreatorReference(getContext().getUserReference());
567    }
568  0 doc.saveAttachmentContent(attachment, xcontext);
569    }
570   
 
571  0 toggle @Override
572    @Deprecated
573    public void setAttachmentContent(String documentReference, String attachmentFilename, byte[] attachmentData)
574    throws Exception
575    {
576  0 XWikiContext xcontext = getContext();
577  0 XWikiDocument doc = xcontext.getWiki().getDocument(documentReference, xcontext);
578  0 XWikiAttachment attachment = doc.getAttachment(attachmentFilename);
579  0 if (attachment == null) {
580  0 attachment = new XWikiAttachment();
581  0 doc.getAttachmentList().add(attachment);
582  0 doc.setComment("Add new attachment " + attachmentFilename);
583    } else {
584  0 doc.setComment("Update attachment " + attachmentFilename);
585    }
586  0 attachment.setContent(attachmentData);
587  0 attachment.setFilename(attachmentFilename);
588  0 attachment.setAuthor(getCurrentUser());
589  0 attachment.setDoc(doc);
590  0 doc.setAuthor(getCurrentUser());
591  0 if (doc.isNew()) {
592  0 doc.setCreator(getCurrentUser());
593    }
594  0 doc.saveAttachmentContent(attachment, xcontext);
595    }
596   
 
597  0 toggle @Override
598    public List<AttachmentReference> getAttachmentReferences(DocumentReference documentReference) throws Exception
599    {
600  0 XWikiContext xcontext = getContext();
601  0 List<XWikiAttachment> attachments =
602    xcontext.getWiki().getDocument(documentReference, xcontext).getAttachmentList();
603   
604  0 List<AttachmentReference> attachmentReferences = new ArrayList<AttachmentReference>(attachments.size());
605  0 for (XWikiAttachment attachment : attachments) {
606  0 attachmentReferences.add(attachment.getReference());
607    }
608   
609  0 return attachmentReferences;
610    }
611   
 
612  0 toggle @Override
613    public String getAttachmentVersion(AttachmentReference attachmentReference) throws Exception
614    {
615  0 XWikiContext xcontext = getContext();
616  0 XWikiDocument doc = xcontext.getWiki().getDocument(attachmentReference.getDocumentReference(), xcontext);
617  0 XWikiAttachment attachment = doc.getAttachment(attachmentReference.getName());
618  0 return attachment == null ? null : attachment.getVersion();
619    }
620   
 
621  266 toggle @Override
622    public String getDocumentURL(DocumentReference documentReference, String action, String queryString, String anchor)
623    {
624  266 return getDocumentURL(documentReference, action, queryString, anchor, false);
625    }
626   
 
627  266 toggle @Override
628    public String getDocumentURL(final DocumentReference documentReference, final String action,
629    final String queryString, final String anchor, final boolean isFullURL)
630    {
631  266 if (documentReference == null) {
632  0 return this.getDocumentURL(this.getContext().getDoc().getDocumentReference(), action, queryString, anchor,
633    isFullURL);
634    }
635  266 if (isFullURL) {
636  0 return this.getContext().getURLFactory().createExternalURL(extractSpacesFromDocumentReference(
637    documentReference), documentReference.getName(), action, queryString, anchor,
638    documentReference.getWikiReference().getName(), this.getContext()).toString();
639    } else {
640  266 return this.getContext().getWiki()
641    .getURL(documentReference, action, queryString, anchor, this.getContext());
642    }
643    }
644   
 
645  2 toggle @Override
646    @Deprecated
647    public String getURL(String documentReference, String action, String queryString, String anchor)
648    {
649  2 XWikiContext xcontext = getContext();
650   
651    // If the document name is empty then use the current document
652  2 String computedDocumentName = documentReference;
653  2 if (StringUtils.isEmpty(documentReference)) {
654  2 computedDocumentName = xcontext.getDoc().getFullName();
655    }
656   
657  2 return xcontext.getWiki().getURL(computedDocumentName, action, queryString, anchor, xcontext);
658    }
659   
 
660  0 toggle @Override
661    @Deprecated
662    public String getAttachmentURL(String documentReference, String attachmentName)
663    {
664  0 XWikiContext xcontext = getContext();
665  0 String attachmentURL;
666  0 try {
667  0 attachmentURL =
668    xcontext.getWiki().getAttachmentURL(
669  0 documentReference == null ? xcontext.getDoc().getFullName() : documentReference, attachmentName,
670    xcontext);
671    } catch (XWikiException e) {
672    // This cannot happen. There's a bug in the definition of XWiki.getAttachmentURL: it says it can generate
673    // an exception but in fact no exception is raised in the current implementation.
674  0 throw new RuntimeException("Failed to get attachment URL", e);
675    }
676  0 return attachmentURL;
677    }
678   
 
679  0 toggle @Override
680    public String getAttachmentURL(AttachmentReference attachmentReference, boolean isFullURL)
681    {
682  0 return getAttachmentURL(attachmentReference, null, isFullURL);
683    }
684   
 
685  276 toggle @Override
686    public String getAttachmentURL(AttachmentReference attachmentReference, String queryString, boolean isFullURL)
687    {
688  276 String url;
689  276 if (isFullURL) {
690  0 XWikiContext xcontext = getContext();
691  0 url = xcontext.getURLFactory().createAttachmentURL(attachmentReference.getName(),
692    extractSpacesFromDocumentReference(attachmentReference.getDocumentReference()),
693    attachmentReference.getDocumentReference().getName(), "download", queryString,
694    attachmentReference.getDocumentReference().getWikiReference().getName(), xcontext).toString();
695    } else {
696  276 XWikiContext xcontext = getContext();
697  276 String documentReference =
698    this.defaultEntityReferenceSerializer.serialize(attachmentReference.getDocumentReference());
699  276 if (documentReference == null) {
700  0 documentReference = xcontext.getDoc().getFullName();
701    }
702  276 String fileName = attachmentReference.getName();
703  276 try {
704  276 url = xcontext.getWiki().getAttachmentURL(documentReference, fileName, queryString, xcontext);
705    } catch (XWikiException e) {
706    // This cannot happen. There's a bug in the definition of XWiki.getAttachmentURL: it says it can
707    // generate an exception but in fact no exception is raised in the current implementation.
708  0 throw new RuntimeException("Failed to get attachment URL", e);
709    }
710    }
711  276 return url;
712    }
713   
 
714  0 toggle @Override
715    @Deprecated
716    public List<String> getAttachmentURLs(DocumentReference documentReference, boolean isFullURL) throws Exception
717    {
718  0 List<String> urls = new ArrayList<String>();
719  0 for (AttachmentReference attachmentReference : getAttachmentReferences(documentReference)) {
720  0 urls.add(getAttachmentURL(attachmentReference, isFullURL));
721    }
722  0 return urls;
723    }
724   
 
725  2992 toggle @Override
726    public boolean isDocumentViewable(DocumentReference documentReference)
727    {
728  2992 return hasRight(documentReference, "view");
729    }
730   
 
731  0 toggle @Override
732    @Deprecated
733    public boolean isDocumentViewable(String documentReference)
734    {
735  0 return hasRight(documentReference, "view");
736    }
737   
 
738  0 toggle @Override
739    @Deprecated
740    public boolean isDocumentEditable(String documentReference)
741    {
742  0 return hasRight(documentReference, "edit");
743    }
744   
 
745  0 toggle @Override
746    public boolean isDocumentEditable(DocumentReference documentReference)
747    {
748  0 return hasRight(documentReference, "edit");
749    }
750   
 
751  90 toggle @Override
752    public boolean hasProgrammingRights()
753    {
754  90 return this.authorizationProvider.get().hasAccess(Right.PROGRAM);
755    }
756   
 
757  247 toggle @Override
758    @Deprecated
759    public String getCurrentUser()
760    {
761  247 DocumentReference userReference = getContext().getUserReference();
762   
763    // Make sure to always return the full reference of the user
764  247 if (userReference != null) {
765  54 return this.defaultEntityReferenceSerializer.serialize(userReference);
766    } else {
767  193 return XWikiRightService.GUEST_USER_FULLNAME;
768    }
769    }
770   
 
771  2891787 toggle @Override
772    public DocumentReference getCurrentUserReference()
773    {
774  2891782 XWikiContext xcontext = getContext();
775  2891893 return xcontext != null ? xcontext.getUserReference() : null;
776    }
777   
 
778  1126 toggle @Override
779    public void setCurrentUser(String userName)
780    {
781  1126 getContext().setUser(userName);
782    }
783   
 
784  0 toggle @Override
785    public String getDefaultEncoding()
786    {
787  0 return getContext().getWiki().getEncoding();
788    }
789   
 
790  15043 toggle @Override
791    public void popDocumentFromContext(Map<String, Object> backupObjects)
792    {
793  15041 XWikiDocument.restoreContext(backupObjects, getContext());
794    }
795   
 
796  0 toggle @Override
797    @Deprecated
798    public void pushDocumentInContext(Map<String, Object> backupObjects, String documentReference) throws Exception
799    {
800  0 XWikiContext xcontext = getContext();
801   
802    // Backup current context state
803  0 XWikiDocument.backupContext(backupObjects, xcontext);
804   
805    // Make sure to get the current XWikiContext after ExcutionContext clone
806  0 xcontext = getContext();
807   
808    // Change context document
809  0 xcontext.getWiki().getDocument(documentReference, xcontext).setAsContextDoc(xcontext);
810    }
811   
 
812  4160 toggle @Override
813    public void pushDocumentInContext(Map<String, Object> backupObjects, DocumentReference documentReference)
814    throws Exception
815    {
816  4160 pushDocumentInContext(backupObjects, getDocument(documentReference));
817    }
818   
 
819  4170 toggle @Override
820    public void pushDocumentInContext(Map<String, Object> backupObjects, DocumentModelBridge document) throws Exception
821    {
822  4170 XWikiContext xcontext = getContext();
823   
824    // Backup current context state
825  4170 XWikiDocument.backupContext(backupObjects, xcontext);
826   
827    // Make sure to get the current XWikiContext after ExcutionContext clone
828  4170 xcontext = getContext();
829   
830    // Change context document
831  4170 ((XWikiDocument) document).setAsContextDoc(xcontext);
832    }
833   
 
834  0 toggle @Override
835    public String getCurrentWiki()
836    {
837  0 XWikiContext xcontext = getContext();
838  0 return xcontext.getWikiId();
839    }
840   
841    /**
842    * Utility method for checking access rights of the current user on a target document.
843    *
844    * @param documentReference the reference of the document
845    * @param right Access right requested.
846    * @return True if the current user has the given access right, false otherwise.
847    */
 
848  2992 toggle private boolean hasRight(DocumentReference documentReference, String right)
849    {
850  2992 return hasRight(this.defaultEntityReferenceSerializer.serialize(documentReference), right);
851    }
852   
853    /**
854    * Utility method for checking access rights of the current user on a target document.
855    *
856    * @param documentReference the reference of the document
857    * @param right Access right requested.
858    * @return True if the current user has the given access right, false otherwise.
859    */
 
860  2992 toggle private boolean hasRight(String documentReference, String right)
861    {
862  2992 boolean hasRight = false;
863  2991 XWikiContext xcontext = getContext();
864  2992 try {
865  2992 hasRight =
866    xcontext.getWiki().getRightService()
867    .hasAccessLevel(right, xcontext.getUser(), documentReference, xcontext);
868    } catch (XWikiException e) {
869    // Do nothing
870    }
871  2992 return hasRight;
872    }
873   
874    /**
875    * Utility method for saving an {@link XWikiDocument}. This method takes care of setting authors and creators
876    * appropriately.
877    *
878    * @param doc the {@link XWikiDocument} to be saved.
879    * @param comment the edit comment.
880    * @param isMinorEdit if the change in document is minor.
881    * @throws Exception if an error occurs while saving the document.
882    */
 
883  0 toggle private void saveDocument(XWikiDocument doc, String comment, boolean isMinorEdit) throws Exception
884    {
885  0 doc.setAuthorReference(getContext().getUserReference());
886  0 if (doc.isNew()) {
887  0 doc.setCreatorReference(getContext().getUserReference());
888    }
889  0 getContext().getWiki().saveDocument(doc, comment, isMinorEdit, getContext());
890    }
891   
 
892  0 toggle private String extractSpacesFromDocumentReference(DocumentReference reference)
893    {
894    // Extract and escape the spaces portion of the passed reference to pass to the old createURL() API which
895    // unfortunately doesn't accept a DocumentReference...
896  0 EntityReference spaceReference = reference.getLastSpaceReference().removeParent(reference.getWikiReference());
897  0 return this.defaultEntityReferenceSerializer.serialize(spaceReference);
898    }
899    }