1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.vfs.internal.attach

File AttachNode.java

 

Coverage histogram

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

Code metrics

10
32
15
1
188
130
23
0.72
2.13
15
1.53

Classes

Class Line # Actions
AttachNode 57 32 0% 23 25
0.561403556.1%
 

Contributing tests

This file is covered by 1 test. .

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 org.xwiki.vfs.internal.attach;
21   
22    import java.io.IOException;
23    import java.io.InputStream;
24    import java.io.OutputStream;
25    import java.net.URI;
26    import java.util.Set;
27   
28    import javax.annotation.concurrent.Immutable;
29   
30    import com.xpn.xwiki.doc.XWikiAttachment;
31   
32    import net.java.truecommons.cio.Entry;
33    import net.java.truecommons.cio.InputSocket;
34    import net.java.truecommons.cio.IoBufferPool;
35    import net.java.truecommons.cio.IoEntry;
36    import net.java.truecommons.cio.OutputSocket;
37    import net.java.truecommons.shed.BitField;
38    import net.java.truevfs.kernel.spec.FsAbstractNode;
39    import net.java.truevfs.kernel.spec.FsAccessOption;
40    import net.java.truevfs.kernel.spec.FsNodeName;
41    import net.java.truevfs.kernel.spec.FsReadOnlyFileSystemException;
42    import net.java.truevfs.kernel.spec.sl.IoBufferPoolLocator;
43   
44    import static net.java.truecommons.cio.Entry.Access.READ;
45    import static net.java.truecommons.cio.Entry.Access.WRITE;
46    import static net.java.truecommons.cio.Entry.Size.DATA;
47    import static net.java.truecommons.cio.Entry.Type.FILE;
48    import static net.java.truevfs.kernel.spec.FsAccessOptions.NONE;
49   
50    /**
51    * Represents a TrueVFS Node inside an archive located in an attachment in a wiki page.
52    *
53    * @version $Id: 4985f2f72645d8563b0fcf0496e3cedfaecffc3a $
54    * @since 7.4M2
55    */
56    @Immutable
 
57    public class AttachNode extends FsAbstractNode implements IoEntry<AttachNode>
58    {
59    private final URI uri;
60   
61    private final AttachController controller;
62   
63    private final String name;
64   
65    private XWikiModelNode xwikiModelNode;
66   
 
67  4 toggle AttachNode(final AttachController controller, final FsNodeName name)
68    {
69  2 assert null != controller;
70  4 this.controller = controller;
71  4 this.name = name.toString();
72  4 this.uri = controller.resolve(name).getUri();
73  4 this.xwikiModelNode = new XWikiModelNode(controller, name);
74    }
75   
 
76  2 toggle IoBufferPool getPool()
77    {
78  2 return IoBufferPoolLocator.SINGLETON.get();
79    }
80   
 
81  2 toggle protected InputStream newInputStream() throws IOException
82    {
83    // Return the attachment as an input stream
84  2 try {
85  2 return this.xwikiModelNode.getAttachment().getContentInputStream(this.xwikiModelNode.getXWikiContext());
86    } catch (Exception e) {
87  0 throw new IOException(String.format(
88    "Failed to get attachment content for attachment [%s] in URI [%s]", this.name, this.uri), e);
89    }
90    }
91   
 
92  0 toggle protected OutputStream newOutputStream() throws IOException
93    {
94  0 throw new FsReadOnlyFileSystemException(this.controller.getMountPoint());
95    }
96   
 
97  0 toggle @Override
98    public String getName()
99    {
100  0 return name;
101    }
102   
 
103  2 toggle @Override
104    public BitField<Type> getTypes()
105    {
106    // All Attach Driver Nodes are of type File, except if the Node doesn't exist, in which case we should return
107    // NO_TYPES
108  2 return this.xwikiModelNode.hasAttachment() ? FILE_TYPE : NO_TYPES;
109    }
110   
 
111  2 toggle @Override
112    public boolean isType(final Type type)
113    {
114  2 return type == FILE && getTypes().is(FILE);
115    }
116   
 
117  2 toggle @Override
118    public long getSize(final Size type)
119    {
120  2 if (DATA != type) {
121  0 return UNKNOWN;
122    }
123    // Get the size of the attachment in bytes
124  2 try {
125  2 XWikiAttachment attachment = this.xwikiModelNode.getAttachment();
126  2 return attachment.getContentSize(this.xwikiModelNode.getXWikiContext());
127    } catch (Exception e) {
128  0 return UNKNOWN;
129    }
130    }
131   
 
132  2 toggle @Override
133    @SuppressWarnings("deprecation")
134    public long getTime(Access type)
135    {
136  2 if (WRITE != type) {
137  0 return UNKNOWN;
138    }
139    // Get the last modified time for the attachment
140  2 try {
141  2 XWikiAttachment attachment = this.xwikiModelNode.getAttachment();
142  2 return attachment.getDate().getTime();
143    } catch (IOException e) {
144  0 return UNKNOWN;
145    }
146    }
147   
 
148  0 toggle @Override
149    public Boolean isPermitted(final Access type, final Entity entity)
150    {
151  0 if (READ != type) {
152  0 return null;
153    }
154  0 return true;
155    }
156   
 
157  0 toggle @Override
158    public Set<String> getMembers()
159    {
160  0 return null;
161    }
162   
 
163  2 toggle @Override
164    public final InputSocket<AttachNode> input()
165    {
166  2 return input(NONE);
167    }
168   
169    /**
170    * @param options the options for accessing the file system node
171    * @return An input socket for reading this entry
172    */
 
173  4 toggle protected InputSocket<AttachNode> input(BitField<FsAccessOption> options)
174    {
175  4 return new AttachInputSocket(options, this);
176    }
177   
 
178  0 toggle @Override
179    public final OutputSocket<AttachNode> output()
180    {
181  0 return output(NONE, null);
182    }
183   
 
184  0 toggle protected OutputSocket<AttachNode> output(BitField<FsAccessOption> options, Entry template)
185    {
186  0 return new AttachOutputSocket(options, this, template);
187    }
188    }