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

File DavTempFile.java

 

Coverage histogram

../../../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

20
56
14
1
224
145
28
0.5
4
14
2

Classes

Class Line # Actions
DavTempFile 48 56 0% 28 13
0.8555555385.6%
 

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    import java.util.ArrayList;
25    import java.util.Date;
26    import java.util.List;
27   
28    import org.apache.jackrabbit.server.io.IOUtil;
29    import org.apache.jackrabbit.webdav.DavConstants;
30    import org.apache.jackrabbit.webdav.DavException;
31    import org.apache.jackrabbit.webdav.DavResource;
32    import org.apache.jackrabbit.webdav.DavResourceIterator;
33    import org.apache.jackrabbit.webdav.DavResourceIteratorImpl;
34    import org.apache.jackrabbit.webdav.DavServletResponse;
35    import org.apache.jackrabbit.webdav.io.InputContext;
36    import org.apache.jackrabbit.webdav.io.OutputContext;
37    import org.apache.jackrabbit.webdav.property.DavPropertyName;
38    import org.apache.jackrabbit.webdav.property.DefaultDavProperty;
39   
40    import com.xpn.xwiki.plugin.webdav.resources.XWikiDavResource;
41    import com.xpn.xwiki.plugin.webdav.resources.partial.AbstractDavResource;
42   
43    /**
44    * Resource used to represent temporary resources demanded by various dav clients.
45    *
46    * @version $Id: 0b7d4746850c6a6fa0794ca48e1bf874e12d59d0 $
47    */
 
48    public class DavTempFile extends AbstractDavResource
49    {
50    /**
51    * Flag indicating whether this resource is a collection.
52    */
53    private boolean isCollection;
54   
55    /**
56    * Content of this resource (file).
57    */
58    private byte[] data;
59   
60    /**
61    * Indicates if this resource has been created or not. Here creation means if the resource has been actually PUT /
62    * MKCOL by the client as opposed to being initialized. This flag will be set to true once setModified() has been
63    * invoked for the first time.
64    */
65    private boolean created;
66   
67    /**
68    * Created on.
69    */
70    private Date timeOfCreation;
71   
72    /**
73    * Last modified.
74    */
75    private Date timeOfLastModification;
76   
77    /**
78    * Default constructor.
79    */
 
80  36 toggle public DavTempFile()
81    {
82  36 timeOfCreation = new Date(IOUtil.UNDEFINED_TIME);
83  36 timeOfLastModification = (Date) timeOfCreation.clone();
84    }
85   
 
86  62 toggle @Override
87    public void init(XWikiDavResource parent, String name, String relativePath) throws DavException
88    {
89  62 super.init(parent, name, relativePath);
90  62 String strTimeOfCreation = DavConstants.creationDateFormat.format(timeOfCreation);
91  62 getProperties().add(new DefaultDavProperty(DavPropertyName.CREATIONDATE, strTimeOfCreation));
92  62 String strTimeOfModification = DavConstants.modificationDateFormat.format(timeOfLastModification);
93  62 getProperties().add(new DefaultDavProperty(DavPropertyName.GETLASTMODIFIED, strTimeOfModification));
94  62 getProperties().add(new DefaultDavProperty(DavPropertyName.GETETAG, strTimeOfModification));
95  62 getProperties().add(new DefaultDavProperty(DavPropertyName.GETCONTENTLANGUAGE, "en"));
96  62 String contentType = isCollection() ? "text/directory" : "application/octet-stream";
97  62 getProperties().add(new DefaultDavProperty(DavPropertyName.GETCONTENTTYPE, contentType));
98  62 int contentLength = (data != null) ? data.length : 0;
99  62 getProperties().add(new DefaultDavProperty(DavPropertyName.GETCONTENTLENGTH, contentLength));
100    }
101   
 
102  10 toggle @Override
103    public XWikiDavResource decode(String[] tokens, int next) throws DavException
104    {
105  10 return super.decode(tokens, next);
106    }
107   
 
108  42 toggle @Override
109    public boolean exists()
110    {
111  42 return parentResource.getVirtualMembers().contains(this);
112    }
113   
 
114  4 toggle @Override
115    public void spool(OutputContext outputContext) throws IOException
116    {
117  4 outputContext.setContentLanguage("en");
118  4 outputContext.setContentLength(data != null ? data.length : 0);
119  4 outputContext.setContentType(isCollection() ? "text/directory" : "application/octet-stream");
120  4 outputContext.setETag(DavConstants.modificationDateFormat.format(getModificationTime()));
121  4 outputContext.setModificationTime(getModificationTime());
122  4 if (exists() && !isCollection()) {
123  4 OutputStream out = outputContext.getOutputStream();
124  4 if (out != null) {
125  4 out.write(this.data);
126  4 out.flush();
127    }
128    }
129    }
130   
 
131  0 toggle @Override
132    public DavResourceIterator getMembers()
133    {
134  0 List<DavResource> children = new ArrayList<DavResource>();
135  0 children.addAll(getVirtualMembers());
136  0 return new DavResourceIteratorImpl(children);
137    }
138   
 
139  2 toggle @Override
140    public void addMember(DavResource resource, InputContext inputContext) throws DavException
141    {
142  2 if (resource instanceof DavTempFile) {
143  2 addVirtualMember(resource, inputContext);
144    } else {
145  0 throw new DavException(DavServletResponse.SC_INTERNAL_SERVER_ERROR);
146    }
147    }
148   
 
149  2 toggle @Override
150    public void removeMember(DavResource resource) throws DavException
151    {
152  2 if (resource instanceof DavTempFile) {
153  2 removeVirtualMember(resource);
154    } else {
155  0 throw new DavException(DavServletResponse.SC_INTERNAL_SERVER_ERROR);
156    }
157    }
158   
 
159  4 toggle @Override
160    public void move(DavResource destination) throws DavException
161    {
162  4 if (destination instanceof DavTempFile && destination.getCollection().equals(getCollection())
163    && !destination.isCollection() && !isCollection()) {
164    // A file rename operation
165  2 DavTempFile destTempFile = (DavTempFile) destination;
166  2 parentResource.getVirtualMembers().remove(this);
167  2 parentResource.getVirtualMembers().add(destTempFile);
168  2 destTempFile.update(this.data.clone(), this.timeOfLastModification);
169    } else {
170  2 throw new DavException(DavServletResponse.SC_FORBIDDEN);
171    }
172  2 clearCache();
173    }
174   
175    /**
176    * @param data Data to be set as the content of this temporary file.
177    * @param modificationTime Time of modification.
178    */
 
179  4 toggle public void update(byte[] data, Date modificationTime)
180    {
181  4 this.data = data.clone();
182  4 setModified(modificationTime);
183  4 getProperties().add(new DefaultDavProperty(DavPropertyName.GETCONTENTLENGTH, this.data.length));
184    }
185   
186    /**
187    * Changes the time of modification of this resource.
188    *
189    * @param modificationTime Time of modification.
190    */
 
191  8 toggle public void setModified(Date modificationTime)
192    {
193  8 if (!created) {
194  8 timeOfCreation = (Date) modificationTime.clone();
195  8 String strTimeOfCreation = DavConstants.creationDateFormat.format(timeOfCreation);
196  8 getProperties().add(new DefaultDavProperty(DavPropertyName.CREATIONDATE, strTimeOfCreation));
197  8 created = true;
198    }
199  8 timeOfLastModification = (Date) modificationTime.clone();
200  8 String strTimeOfModification = DavConstants.modificationDateFormat.format(timeOfLastModification);
201  8 getProperties().add(new DefaultDavProperty(DavPropertyName.GETLASTMODIFIED, strTimeOfModification));
202  8 getProperties().add(new DefaultDavProperty(DavPropertyName.GETETAG, String.valueOf(timeOfLastModification)));
203    }
204   
205    /**
206    * Sets the isCollection flag to true.
207    */
 
208  5 toggle public void setCollection()
209    {
210  5 this.isCollection = true;
211    }
212   
 
213  151 toggle @Override
214    public boolean isCollection()
215    {
216  151 return isCollection;
217    }
218   
 
219  8 toggle @Override
220    public long getModificationTime()
221    {
222  8 return timeOfLastModification.getTime();
223    }
224    }