View Javadoc
1   /**
2    * This file Copyright (c) 2015-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.templating.imaging.support;
35  
36  import info.magnolia.imaging.DefaultImagingSupport;
37  import info.magnolia.imaging.ImagingSupport;
38  import info.magnolia.module.site.Site;
39  import info.magnolia.module.site.SiteManager;
40  import info.magnolia.module.site.SiteModule;
41  import info.magnolia.module.site.theme.Theme;
42  import info.magnolia.module.site.theme.ThemeReference;
43  
44  import javax.inject.Inject;
45  import javax.inject.Provider;
46  import javax.jcr.Node;
47  import javax.jcr.Property;
48  import javax.jcr.RepositoryException;
49  
50  import org.apache.commons.lang3.StringUtils;
51  import org.slf4j.Logger;
52  import org.slf4j.LoggerFactory;
53  
54  /**
55   * {@link info.magnolia.imaging.ImagingSupport} that delegates to the {@link Theme}'s imaging support.
56   *
57   * <p><strong>Do not use this class to configure the {@link ImagingSupport} of a theme! Use
58   * {@link info.magnolia.templating.imaging.VariationAwareImagingSupport} instead.</strong></p>
59   */
60  public class ThemeDelegatingImagingSupport implements ImagingSupport {
61  
62      private static final Logger log = LoggerFactory.getLogger(ThemeDelegatingImagingSupport.class);
63  
64      private final ImagingSupport defaultImagingSupport = new DefaultImagingSupport();
65  
66      private final SiteManager siteManager;
67      private final Provider<SiteModule> siteModuleProvider;
68  
69      @Inject
70      public ThemeDelegatingImagingSupport(SiteManager siteManager, Provider<SiteModule> siteModuleProvider) {
71          this.siteManager = siteManager;
72          this.siteModuleProvider = siteModuleProvider;
73      }
74  
75      @Override
76      public String createLink(Property property) {
77          return getImagingSupport(property).createLink(property);
78      }
79  
80      @Override
81      public String createLink(Property property, String variationName) {
82          return getImagingSupport(property).createLink(property, variationName);
83      }
84  
85      /**
86       * Returns the {@link ImagingSupport} configured on {@link Theme}-level. The theme is linked to a {@link Site} by a
87       * {@link ThemeReference}.
88       */
89      private ImagingSupport getImagingSupport(Property property) {
90          if (property != null) {
91              try {
92                  // We cannot use the method info.magnolia.module.site.functions.SiteFunctions#theme(Site) because it
93                  // would return an new ConfiguredTheme when the theme name is empty (+ passing the injected imaging
94                  // support). Because this ImagingSupport replaces the default component – an instance of this would be
95                  // used, possibly resulting in an endless loop when methods are invoked.
96                  final Node node = property.getParent();
97                  final Site site = siteManager.getAssignedSite(node); // Site should never be null, even when there is none configured
98                  final ThemeReference themeReference = site.getTheme();
99                  if (themeReference != null) {
100                     final String themeName = themeReference.getName();
101                     if (StringUtils.isNotBlank(themeName)) {
102                         final Theme theme = siteModuleProvider.get().getTheme(themeName);
103                         if (theme != null && theme.getImaging() != null) {
104                             return theme.getImaging();
105                         }
106                     }
107                 }
108             } catch (RepositoryException e) {
109                 log.error("Error getting node from property [{}]", property, e);
110             }
111         }
112 
113         return defaultImagingSupport;
114     }
115 
116 }