View Javadoc

1   /**
2    * This file Copyright (c) 2010-2014 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.core;
35  
36  import info.magnolia.cms.security.AccessDeniedException;
37  import info.magnolia.jcr.util.NodeUtil;
38  import info.magnolia.jcr.wrapper.I18nNodeWrapper;
39  
40  import java.io.InputStream;
41  import java.util.ArrayList;
42  import java.util.Calendar;
43  import java.util.Collection;
44  
45  import javax.jcr.Node;
46  import javax.jcr.Property;
47  import javax.jcr.PropertyIterator;
48  import javax.jcr.PropertyType;
49  import javax.jcr.RepositoryException;
50  import javax.jcr.Value;
51  
52  /**
53   * A node data hiding the fact that node datas of type BINARY are stored as nodes of type {@link ItemType#NT_RESOURCE}.
54   * @author pbaerfuss
55   * @version $Id$
56   */
57  public class BinaryNodeData extends AbstractNodeData {
58  
59      /**
60       * The node containing the binary and attributes.
61       */
62      private Node binaryNode;
63  
64      /**
65       * The property containing the binary.
66       * @see ItemType.JCR_DATA
67       */
68      private Property binaryProperty;
69  
70      protected BinaryNodeData(Content parent, String name) {
71          super(parent, name);
72      }
73  
74      @Override
75      public Property getJCRProperty() {
76          if(binaryProperty == null){
77              if(isExist()){
78                  try {
79                      binaryProperty = getBinaryNode(false).getProperty(ItemType.JCR_DATA);
80                  }
81                  catch (RepositoryException e) {
82                      throw new IllegalStateException(e);
83                  }
84              }
85          }
86          return binaryProperty;
87      }
88  
89      protected Node getBinaryNode(boolean createIfNotExisting) {
90          if(binaryNode == null){
91              Node parentJCRNode = getParent().getJCRNode();
92  
93              try {
94                  if(parentJCRNode.hasNode(name)){
95                      binaryNode = parentJCRNode.getNode(name);
96                  }
97                  else if(createIfNotExisting){
98                      binaryNode = parentJCRNode.addNode(name, ItemType.NT_RESOURCE);
99                  }
100 
101                 binaryNode = NodeUtil.deepUnwrapAll(binaryNode, I18nNodeWrapper.class);
102 
103             }
104             catch (RepositoryException e) {
105                 throw new RuntimeException(e);
106             }
107         }
108         return binaryNode;
109     }
110 
111     @Override
112     public boolean isExist() {
113         return getBinaryNode(false) != null;
114     }
115 
116     @Override
117     public InputStream getStream() {
118         if (isExist()) {
119             try {
120                 return getJCRProperty().getStream();
121             }
122             catch (RepositoryException e) {
123                 throw new RuntimeException("Can't read value of node data " + toString(), e);
124             }
125         }
126         return null;
127     }
128 
129     @Override
130     public void setValue(InputStream value) throws RepositoryException, AccessDeniedException {
131         getBinaryNode(true).setProperty(ItemType.JCR_DATA, value);
132     }
133 
134     @Override
135     public void delete() throws RepositoryException {
136         if(isExist()){
137             getBinaryNode(false).remove();
138         }
139     }
140 
141     @Override
142     public void setAttribute(String name, String value) throws RepositoryException, AccessDeniedException, UnsupportedOperationException {
143         getBinaryNode(true).setProperty(name, value);
144     }
145 
146     @Override
147     public void setAttribute(String name, Calendar value) throws RepositoryException, AccessDeniedException, UnsupportedOperationException {
148         getBinaryNode(true).setProperty(name, value);
149     }
150 
151     @Override
152     public String getAttribute(String name) {
153         if(isExist()){
154             Node binaryNode = getBinaryNode(false);
155             try {
156                 if(binaryNode.hasProperty(name)){
157                     return binaryNode.getProperty(name).getString();
158                 }
159             }
160             catch (RepositoryException e) {
161                 throw new IllegalStateException("Can't read attribute", e);
162             }
163         }
164         return "";
165     }
166 
167     @Override
168     public Collection<String> getAttributeNames() throws RepositoryException {
169         Collection<String> names = new ArrayList<String>();
170         if(isExist()){
171             PropertyIterator properties = getBinaryNode(false).getProperties();
172             while (properties.hasNext()) {
173                 String name = properties.nextProperty().getName();
174                 if (!name.equalsIgnoreCase(ItemType.JCR_DATA)) {
175                     names.add(name);
176                 }
177             }
178         }
179         return names;
180     }
181 
182     @Override
183     public int getType() {
184         return PropertyType.BINARY;
185     }
186 
187     @Override
188     public Value getValue() {
189         if(isExist()){
190             try {
191                 return getJCRProperty().getValue();
192             }
193             catch (RepositoryException e) {
194                 throw new IllegalStateException(e);
195             }
196         }
197         return null;
198     }
199 
200     @Override
201     public long getContentLength() {
202         if(!isExist()){
203             return 0;
204         }
205 
206         try {
207             return getJCRProperty().getLength();
208         }
209         catch (RepositoryException re) {
210             throw new RuntimeException(re);
211         }
212     }
213 
214     @Override
215     public int isMultiValue() {
216         return MULTIVALUE_FALSE;
217     }
218 
219     @Override
220     public void refresh(boolean keepChanges) throws RepositoryException {
221         if(isExist()){
222             getBinaryNode(false).refresh(keepChanges);
223         }
224     }
225 
226     @Override
227     public void save() throws RepositoryException {
228         if(isExist()){
229             getBinaryNode(false).save();
230         }
231     }
232 
233     @Override
234     public Content getReferencedContent() throws RepositoryException {
235         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
236     }
237 
238     @Override
239     public Content getReferencedContent(String repositoryId) throws RepositoryException {
240         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
241     }
242 
243     @Override
244     protected Content getContentFromJCRReference() throws RepositoryException {
245         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
246     }
247 
248     @Override
249     public String getString() {
250         if(isExist()){
251             try {
252                 return getJCRProperty().getString();
253             }
254             catch (RepositoryException e) {
255                 throw new RuntimeException("Can't read value of node data" + toString());
256             }
257         }
258         return "";
259     }
260 
261     @Override
262     public Calendar getDate() {
263         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
264     }
265 
266     @Override
267     public boolean getBoolean() {
268         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
269     }
270 
271     @Override
272     public double getDouble() {
273         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
274     }
275 
276     @Override
277     public long getLong() {
278         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
279     }
280 
281     @Override
282     public Value[] getValues() {
283         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
284     }
285 
286     @Override
287     public void setValue(String value) throws RepositoryException {
288         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
289     }
290 
291     @Override
292     public void setValue(int value) throws RepositoryException {
293         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
294     }
295 
296     @Override
297     public void setValue(long value) throws RepositoryException {
298         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
299     }
300 
301     @Override
302     public void setValue(double value) throws RepositoryException {
303         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
304     }
305 
306     @Override
307     public void setValue(boolean value) throws RepositoryException {
308         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
309     }
310 
311     @Override
312     public void setValue(Calendar value) throws RepositoryException {
313         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
314     }
315 
316     @Override
317     public void setValue(Content value) throws RepositoryException {
318         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
319     }
320 
321     @Override
322     public void setValue(Value value) throws RepositoryException {
323         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
324     }
325 
326     @Override
327     public void setValue(Value[] value) throws RepositoryException {
328         throw new UnsupportedOperationException("This operation is not supported on node datas of type BINARY");
329     }
330 }