1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.plugin.webdav.resources.domain

File DavWikiFile.java

 

Coverage histogram

../../../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

20
38
5
1
141
96
18
0.47
7.6
5
3.6

Classes

Class Line # Actions
DavWikiFile 43 38 0% 18 12
0.809523881%
 

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.plugin.webdav.resources.domain;
21   
22    import java.io.IOException;
23    import java.io.OutputStream;
24   
25    import org.apache.jackrabbit.server.io.IOUtil;
26    import org.apache.jackrabbit.webdav.DavConstants;
27    import org.apache.jackrabbit.webdav.DavException;
28    import org.apache.jackrabbit.webdav.DavResource;
29    import org.apache.jackrabbit.webdav.DavServletResponse;
30    import org.apache.jackrabbit.webdav.io.OutputContext;
31    import org.apache.jackrabbit.webdav.property.DavPropertyName;
32    import org.apache.jackrabbit.webdav.property.DefaultDavProperty;
33   
34    import com.xpn.xwiki.doc.XWikiDocument;
35    import com.xpn.xwiki.plugin.webdav.resources.XWikiDavResource;
36    import com.xpn.xwiki.plugin.webdav.resources.partial.AbstractDavFile;
37   
38    /**
39    * The dav resource used to represent the wiki content of an {@link XWikiDocument}.
40    *
41    * @version $Id: 639863c1e669ba8771bbd81f524d790c425b5b9c $
42    */
 
43    public class DavWikiFile extends AbstractDavFile
44    {
45    /**
46    * Identifier for wiki text file.
47    */
48    public static final String WIKI_TXT = "wiki.txt";
49   
50    /**
51    * Identifier for wiki xml file.
52    */
53    public static final String WIKI_XML = "wiki.xml";
54   
55    /**
56    * The {@link XWikiDocument} of whose content is represented by this resource (file).
57    */
58    private XWikiDocument parentDoc;
59   
 
60  9 toggle @Override
61    public void init(XWikiDavResource parent, String name, String relativePath)
62    throws DavException
63    {
64  9 super.init(parent, name, relativePath);
65  9 if (!(name.equals(WIKI_TXT) || name.equals(WIKI_XML))) {
66  0 throw new DavException(DavServletResponse.SC_INTERNAL_SERVER_ERROR);
67    }
68  9 this.parentDoc = ((DavPage) parent).getDocument();
69  9 String timeStamp = DavConstants.creationDateFormat.format(parentDoc.getCreationDate());
70  9 getProperties().add(new DefaultDavProperty(DavPropertyName.CREATIONDATE, timeStamp));
71  9 timeStamp = DavConstants.modificationDateFormat.format(parentDoc.getContentUpdateDate());
72  9 getProperties().add(new DefaultDavProperty(DavPropertyName.GETLASTMODIFIED, timeStamp));
73  9 getProperties().add(new DefaultDavProperty(DavPropertyName.GETETAG, timeStamp));
74  9 getProperties().add(
75    new DefaultDavProperty(DavPropertyName.GETCONTENTLANGUAGE, parentDoc.getLanguage()));
76  9 String contentType = this.name.equals(WIKI_TXT) ? "text/plain" : "text/xml";
77  9 getProperties().add(new DefaultDavProperty(DavPropertyName.GETCONTENTTYPE, contentType));
78  9 int contentLength =
79  9 this.name.equals(WIKI_TXT) ? parentDoc.getContent().length() : getContext().toXML(
80    parentDoc).length();
81  9 getProperties().add(
82    new DefaultDavProperty(DavPropertyName.GETCONTENTLENGTH, contentLength));
83    }
84   
 
85  19 toggle @Override
86    public boolean exists()
87    {
88  19 return !parentDoc.isNew() && parentResource.getVirtualMembers().contains(this);
89    }
90   
 
91  3 toggle @Override
92    public void spool(OutputContext outputContext) throws IOException
93    {
94    // Protect against direct url referencing.
95  3 if (!getContext().hasAccess("view", parentDoc.getFullName())) {
96  0 throw new IOException("Access rights violation.");
97    }
98  3 outputContext.setContentLanguage(parentDoc.getLanguage());
99  3 int contentLength = 0;
100  3 try {
101  3 contentLength =
102  3 this.name.equals(WIKI_TXT) ? parentDoc.getContent().length() : getContext()
103    .toXML(parentDoc).length();
104    } catch (DavException ex) {
105  0 throw new IOException(ex.getMessage());
106    }
107  3 outputContext.setContentLength(contentLength);
108  3 outputContext.setContentType(this.name.equals(WIKI_TXT) ? "text/plain" : "text/xml");
109  3 outputContext.setETag(DavConstants.modificationDateFormat.format(getModificationTime()));
110  3 outputContext.setModificationTime(getModificationTime());
111  3 if (exists()) {
112  3 OutputStream out = outputContext.getOutputStream();
113  3 if (out != null) {
114  3 try {
115  3 String content =
116  3 this.name.equals(WIKI_TXT) ? parentDoc.getContent() : getContext().toXML(
117    parentDoc);
118  3 out.write(content.getBytes());
119  3 out.flush();
120    } catch (DavException ex) {
121  0 throw new IOException(ex.getMessage());
122    }
123    }
124    }
125    }
126   
 
127  0 toggle @Override
128    public void move(DavResource destination) throws DavException
129    {
130  0 throw new DavException(DavServletResponse.SC_NOT_IMPLEMENTED);
131    }
132   
 
133  6 toggle @Override
134    public long getModificationTime()
135    {
136  6 if (exists()) {
137  6 return parentDoc.getContentUpdateDate().getTime();
138    }
139  0 return IOUtil.UNDEFINED_TIME;
140    }
141    }