View Javadoc
1   /**
2    * This file Copyright (c) 2015 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.resources.app.availability;
35  
36  import info.magnolia.jcr.util.SessionUtil;
37  import info.magnolia.resourceloader.Resource;
38  import info.magnolia.resourceloader.ResourceOrigin;
39  import info.magnolia.resourceloader.jcr.JcrResourceOrigin;
40  import info.magnolia.resourceloader.layered.LayeredResource;
41  import info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector;
42  
43  import java.util.List;
44  
45  import javax.inject.Inject;
46  import javax.jcr.Node;
47  import javax.jcr.RepositoryException;
48  
49  import org.apache.commons.io.FilenameUtils;
50  import org.slf4j.Logger;
51  import org.slf4j.LoggerFactory;
52  
53  import com.google.common.collect.Lists;
54  import com.google.common.net.MediaType;
55  import com.vaadin.data.Item;
56  import com.vaadin.data.Property;
57  
58  /**
59   * This rule returns {@code true} if the item {@link Resource} does have {@link JcrResourceOrigin#TEXT_PROPERTY},
60   * or if the given resource's {@link MediaType} is different than {@link MediaType#ANY_TEXT_TYPE} or {@link IsNotBinaryFileRule#WHITELISTED_EXTENSIONS}
61   * <p>
62   * This rule returns false if given item has binary sub-node {@link JcrResourceOrigin#BINARY_NODE_NAME}.
63   * <p>
64   * For identifying the mediaType for a given resource, format column {@link info.magnolia.resources.app.workbench.ResourcesContainer#FORMAT_PROPERTY_ID} is used.
65   * @see info.magnolia.resources.app.workbench.ResourcesContainer
66   */
67  public class IsNotBinaryFileRule extends AbstractResourceAvailabilityRule {
68  
69      private static final Logger log = LoggerFactory.getLogger(IsNotBinaryFileRule.class);
70      private static final List WHITELISTED_EXTENSIONS = Lists.newArrayList("bpmn", "css", "dtd", "ftl", "html", "js", "jocl", "json", "lsp", "lst", "ltx", "text", "xml", "xsb", "xsd");
71  
72      private final ContentConnector contentConnector;
73  
74      @Inject
75      public IsNotBinaryFileRule(ResourceOrigin origin, ContentConnector contentConnector) {
76          super(origin);
77          this.contentConnector = contentConnector;
78      }
79  
80      @Override
81      protected boolean isAvailableFor(Resource itemId) {
82          LayeredResource layeredResource = (LayeredResource) itemId;
83          // JCR resources
84          if (layeredResource.getFirst().getOrigin() instanceof JcrResourceOrigin) {
85              String resourcePath = layeredResource.getFirst().getPath();
86              try {
87                  Node node = SessionUtil.getNode(JcrResourceOrigin.RESOURCES_WORKSPACE, resourcePath);
88                  if (node != null) {
89                      if (node.hasNode(JcrResourceOrigin.BINARY_NODE_NAME)) {
90                          return false;
91                      } else if (node.hasProperty(JcrResourceOrigin.TEXT_PROPERTY)) {
92                          return true;
93                      }
94                  }
95              } catch (RepositoryException e) {
96                  log.warn("Error evaluating availability for node [{}]", itemId.getPath(), e.getMessage());
97              }
98              return false;
99          }
100 
101         // Other resources
102         Item item = contentConnector.getItem(itemId.getPath());
103         MediaType type = resolveMediaType(item);
104         return (type != null && type.is(MediaType.ANY_TEXT_TYPE)) || isExtensionWhitelisted(itemId.getName());
105     }
106 
107     private MediaType resolveMediaType(Item item) {
108         Property mimeTypeProperty = item.getItemProperty("format");
109         if (mimeTypeProperty != null && mimeTypeProperty.getValue() != null) {
110             String resourceFormat = mimeTypeProperty.getValue().toString();
111             return MediaType.parse(resourceFormat);
112         }
113         return null;
114     }
115 
116     private boolean isExtensionWhitelisted(String fileName) {
117         String extension = FilenameUtils.getExtension(fileName);
118         return WHITELISTED_EXTENSIONS.contains(extension);
119     }
120 }