View Javadoc
1   /**
2    * This file Copyright (c) 2014-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.dam.core.config;
35  
36  import info.magnolia.dam.api.AssetProvider;
37  import info.magnolia.dam.api.AssetRenderer;
38  import info.magnolia.module.ModuleLifecycle;
39  import info.magnolia.module.ModuleLifecycleContext;
40  import info.magnolia.voting.Voter;
41  
42  import java.util.ArrayList;
43  import java.util.Collections;
44  import java.util.Iterator;
45  import java.util.List;
46  
47  import org.apache.commons.lang3.StringUtils;
48  import org.slf4j.Logger;
49  import org.slf4j.LoggerFactory;
50  
51  /**
52   * Configuration singleton of the dam-core module.
53   * This bean is used to:
54   * - configure the download servlet
55   * - register the asset providers
56   * - register the asset renderers
57   */
58  public class DamCoreConfiguration implements ModuleLifecycle {
59      private static final Logger log = LoggerFactory.getLogger(DamCoreConfiguration.class);
60  
61      private Voter contentDisposition;
62      private boolean enforceDocumentMimeType = false;
63  
64      private String downloadPath = "/dam/";
65      private List<AssetProvider> providers = Collections.emptyList();
66      private List<AssetRenderer> renderers = Collections.emptyList();
67  
68      // TODO does this need to be voters ? Document and fix
69      public void setContentDisposition(Voter contentDisposition) {
70          this.contentDisposition = contentDisposition;
71      }
72  
73      public Voter getContentDisposition() {
74          return contentDisposition;
75      }
76  
77      public boolean isEnforceDocumentMimeType() {
78          return this.enforceDocumentMimeType;
79      }
80  
81      public void setEnforceDocumentMimeType(boolean enforceDocumentMimeType) {
82          this.enforceDocumentMimeType = enforceDocumentMimeType;
83      }
84  
85      public List<AssetProvider> getProviders() {
86          return providers;
87      }
88  
89      public void setProviders(List<AssetProvider> providers) {
90          this.providers = providers;
91      }
92  
93      public List<AssetRenderer> getRenderers() {
94          return renderers;
95      }
96  
97      public void setRenderers(List<AssetRenderer> renderers) {
98          this.renderers = renderers;
99      }
100 
101     public String getDownloadPath() {
102         return downloadPath;
103     }
104 
105     public void setDownloadPath(final String downloadPath) {
106         this.downloadPath = downloadPath;
107     }
108 
109     /**
110      * Validates the configured providers, and ignore those where no identifier is defined, or when two providers attempt to use the same identifier.
111      */
112     @Override
113     public void start(ModuleLifecycleContext moduleLifecycleContext) {
114 
115         List<String> ids = new ArrayList<String>();
116         Iterator<AssetProvider> iterator = providers.iterator();
117         while (iterator.hasNext()) {
118             AssetProvider provider = iterator.next();
119 
120             if (StringUtils.isEmpty(provider.getIdentifier())) {
121                 log.warn("Trying to register an AssetProvider without an identifier: {}", provider);
122                 iterator.remove();
123                 continue;
124             }
125             if (ids.contains(provider.getIdentifier())) {
126                 log.warn("Trying to register an AssetProvider with an existing identifier: {}", provider.getIdentifier());
127                 iterator.remove();
128                 continue;
129             }
130             ids.add(provider.getIdentifier());
131         }
132         // TODO validate renderers too ?
133     }
134 
135     @Override
136     public void stop(ModuleLifecycleContext moduleLifecycleContext) {
137     }
138 
139 }