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

File DefaultDocumentAccessBridge.java

 

Coverage histogram

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

Code metrics

90
304
73
1
984
781
137
0.45
4.16
73
1.88

Classes

Class Line # Actions
DefaultDocumentAccessBridge 69 304 0% 137 295
0.3683083736.8%
 

Contributing tests

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