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