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

File DavAttachment.java

 

Coverage histogram

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

Code metrics

18
41
5
1
140
102
15
0.37
8.2
5
3

Classes

Class Line # Actions
DavAttachment 44 41 0% 15 26
0.5937559.4%
 

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.XWikiAttachment;
35    import com.xpn.xwiki.doc.XWikiDocument;
36    import com.xpn.xwiki.plugin.webdav.resources.XWikiDavResource;
37    import com.xpn.xwiki.plugin.webdav.resources.partial.AbstractDavFile;
38   
39    /**
40    * The DAV resource representing an {@link XWikiAttachment}.
41    *
42    * @version $Id: 7e2d5d40179bbe72b2d0e085145d0178b703e801 $
43    */
 
44    public class DavAttachment extends AbstractDavFile
45    {
46    /**
47    * The {@link XWikiAttachment} represented by this resource.
48    */
49    private XWikiAttachment attachment;
50   
 
51  3 toggle @Override
52    public void init(XWikiDavResource parent, String name, String relativePath)
53    throws DavException
54    {
55  3 super.init(parent, name, relativePath);
56  3 if (parent.exists()) {
57  3 this.attachment = ((DavPage) parent).getDocument().getAttachment(this.name);
58    }
59  3 if (exists()) {
60  2 String timeStamp = DavConstants.creationDateFormat.format(attachment.getDate());
61  2 getProperties().add(new DefaultDavProperty(DavPropertyName.CREATIONDATE, timeStamp));
62  2 timeStamp = DavConstants.modificationDateFormat.format(attachment.getDate());
63  2 getProperties().add(
64    new DefaultDavProperty(DavPropertyName.GETLASTMODIFIED, timeStamp));
65  2 getProperties().add(new DefaultDavProperty(DavPropertyName.GETETAG, timeStamp));
66  2 getProperties().add(
67    new DefaultDavProperty(DavPropertyName.GETCONTENTTYPE, getContext().getMimeType(
68    attachment)));
69  2 getProperties().add(
70    new DefaultDavProperty(DavPropertyName.GETCONTENTLANGUAGE, attachment.getDoc()
71    .getLanguage()));
72  2 getProperties()
73    .add(
74    new DefaultDavProperty(DavPropertyName.GETCONTENTLENGTH, attachment
75    .getFilesize()));
76    }
77    }
78   
 
79  11 toggle @Override
80    public boolean exists()
81    {
82  11 return this.attachment != null;
83    }
84   
 
85  1 toggle @Override
86    public void spool(OutputContext outputContext) throws IOException
87    {
88    // Protect against direct url referencing.
89  1 if (!getContext().hasAccess("view", attachment.getDoc().getFullName())) {
90  0 throw new IOException("Access rights violation.");
91    }
92  1 outputContext.setContentLanguage(attachment.getDoc().getLanguage());
93  1 outputContext.setContentLength(attachment.getFilesize());
94  1 outputContext.setContentType(getContext().getMimeType(attachment));
95  1 outputContext.setETag(DavConstants.modificationDateFormat.format(getModificationTime()));
96  1 outputContext.setModificationTime(getModificationTime());
97  1 if (exists()) {
98  1 OutputStream out = outputContext.getOutputStream();
99  1 if (null != out) {
100  1 try {
101  1 out.write(getContext().getContent(attachment));
102  1 out.flush();
103    } catch (DavException ex) {
104  0 throw new IOException(ex.getMessage());
105    }
106    }
107    }
108    }
109   
 
110  0 toggle @Override
111    public void move(DavResource destination) throws DavException
112    {
113  0 getContext().checkAccess("edit", attachment.getDoc().getFullName());
114  0 if (destination instanceof DavAttachment) {
115  0 DavAttachment dAttachment = (DavAttachment) destination;
116    // Check if this is a rename operation.
117  0 if (dAttachment.getCollection().equals(getCollection())) {
118  0 getContext().moveAttachment(attachment, attachment.getDoc(),
119    dAttachment.getDisplayName());
120  0 } else if (dAttachment.getCollection() instanceof DavPage) {
121  0 XWikiDocument dDoc = ((DavPage) dAttachment.getCollection()).getDocument();
122  0 getContext().moveAttachment(attachment, dDoc, dAttachment.getDisplayName());
123    } else {
124  0 throw new DavException(DavServletResponse.SC_BAD_REQUEST);
125    }
126    } else {
127  0 throw new DavException(DavServletResponse.SC_BAD_REQUEST);
128    }
129  0 clearCache();
130    }
131   
 
132  2 toggle @Override
133    public long getModificationTime()
134    {
135  2 if (exists()) {
136  2 return attachment.getDate().getTime();
137    }
138  0 return IOUtil.UNDEFINED_TIME;
139    }
140    }