View Javadoc
1   /**
2    * This file Copyright (c) 2014-2018 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.dam.jcr;
35  
36  import info.magnolia.dam.api.Asset;
37  import info.magnolia.dam.api.metadata.AssetMetadata;
38  import info.magnolia.dam.api.metadata.DublinCore;
39  import info.magnolia.dam.api.metadata.MagnoliaAssetMetadata;
40  import info.magnolia.jcr.util.NodeTypes;
41  import info.magnolia.jcr.util.PropertyUtil;
42  import info.magnolia.objectfactory.Components;
43  
44  import java.io.InputStream;
45  import java.util.Calendar;
46  import java.util.Optional;
47  
48  import javax.jcr.Node;
49  import javax.jcr.Property;
50  
51  import com.machinezoo.noexception.Exceptions;
52  
53  /**
54   * JCR implementation of a {@link Asset} definition.
55   */
56  public class JcrAsset extends AbstractJcrItem implements Asset {
57  
58      public JcrAsset(JcrAssetProvider assetProvider, Node itemNode) {
59          super(assetProvider, itemNode);
60      }
61  
62      @Override
63      public String getLink() {
64          return getAssetProvider().getLink(this);
65      }
66  
67      @Override
68      public boolean isAsset() {
69          return true;
70      }
71  
72      @Override
73      public String getTitle() {
74          return PropertyUtil.getString(getNode(), AssetNodeTypes.Asset.TITLE, "");
75      }
76  
77      @Override
78      public String getSubject() {
79          return getPropertyValue(getNode());
80      }
81  
82      private String getPropertyValue(Node node) {
83          return Exceptions.wrap().get(() -> {
84              if (node.hasProperty(AssetNodeTypes.Asset.SUBJECT)) {
85                  Property property = node.getProperty(AssetNodeTypes.Asset.SUBJECT);
86                  if (property.isMultiple()) {
87                      return property.getValues()[0].getString();
88                  }
89                  return property.getValue().getString();
90              }
91              return "";
92          });
93      }
94  
95      @Override
96      public String getDescription() {
97          return PropertyUtil.getString(getNode(), AssetNodeTypes.Asset.DESCRIPTION, "");
98      }
99  
100     @Override
101     public String getCaption() {
102         return PropertyUtil.getString(getNode(), AssetNodeTypes.Asset.CAPTION, "");
103     }
104 
105     @Override
106     public String getLanguage() {
107         return PropertyUtil.getString(getNode(), AssetNodeTypes.Asset.LANGUAGE, "");
108     }
109 
110     @Override
111     public String getCopyright() {
112         return PropertyUtil.getString(getNode(), AssetNodeTypes.Asset.COPYRIGHT, "");
113     }
114 
115     @Override
116     public String getComment() {
117         return PropertyUtil.getString(getNode(), AssetNodeTypes.Asset.COMMENT, "");
118     }
119 
120     @Override
121     public Calendar getCreated() {
122         return Exceptions.wrap().get(() -> NodeTypes.Created.getCreated(getNode()));
123     }
124 
125     @Override
126     public Calendar getLastModified() {
127         return Exceptions.wrap().get(() -> NodeTypes.LastModified.getLastModified(getNode()));
128     }
129 
130     @Override
131     public <M extends AssetMetadata> M getMetadata(Class<M> metaDataType) {
132         if (!getAssetProvider().supports(metaDataType)) {
133             throw new IllegalArgumentException("Unsupported metaData Type requested " + metaDataType != null ? metaDataType.getName() : "null");
134         }
135         return Components.newInstance(metaDataType, getNode(), getItemKey());
136     }
137 
138     @Override
139     public String getMimeType() {
140         return PropertyUtil.getString(AssetNodeTypes.AssetResource.getResourceNodeFromAsset(getNode()), AssetNodeTypes.AssetResource.MIMETYPE, "");
141     }
142 
143     @Override
144     public long getFileSize() {
145         return PropertyUtil.getLong(AssetNodeTypes.AssetResource.getResourceNodeFromAsset(getNode()), AssetNodeTypes.AssetResource.SIZE, 0l);
146     }
147 
148     @Override
149     public InputStream getContentStream() {
150         return Optional.ofNullable(AssetNodeTypes.AssetResource.getResourceNodeFromAsset(getNode()))
151                 .map(Exceptions.wrap().function(node -> node.getProperty(AssetNodeTypes.AssetResource.DATA).getBinary().getStream()))
152                 .orElse(null);
153     }
154 
155     @Override
156     public String getFileName() {
157         return PropertyUtil.getString(AssetNodeTypes.AssetResource.getResourceNodeFromAsset(getNode()), AssetNodeTypes.AssetResource.FILENAME, "");
158     }
159 
160     /**
161      * JcrAsset specific implementation that let you access a property value linked to an asset node.
162      * This required an open session.
163      *
164      * @return property value Object if the property exist or null otherwise.
165      */
166     public Object getProperty(String propertyName) {
167         return PropertyUtil.getPropertyValueObject(getNode(), propertyName);
168     }
169 
170     @Override
171     public <M extends AssetMetadata> boolean supports(Class<M> metaDataType) {
172         return (MagnoliaAssetMetadata.class.isAssignableFrom(metaDataType) || DublinCore.class.isAssignableFrom(metaDataType));
173     }
174 }