Clover icon

Magnolia REST Content Delivery 2.1

  1. Project Clover database Fri Mar 16 2018 18:21:08 CET
  2. Package info.magnolia.rest.reference.dam

File AssetReferenceResolver.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart0.png
47% of files have more coverage

Code metrics

14
28
2
1
126
72
13
0.46
14
2
6.5

Classes

Class Line # Actions
AssetReferenceResolver 64 28 0% 13 44
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /**
2    * This file Copyright (c) 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.rest.reference.dam;
35   
36    import info.magnolia.context.Context;
37    import info.magnolia.context.MgnlContext;
38    import info.magnolia.context.SystemContext;
39    import info.magnolia.context.WebContext;
40    import info.magnolia.dam.api.Asset;
41    import info.magnolia.dam.api.AssetProvider;
42    import info.magnolia.dam.api.AssetProviderRegistry;
43    import info.magnolia.dam.api.AssetRenderer;
44    import info.magnolia.dam.api.AssetRendition;
45    import info.magnolia.dam.api.ItemKey;
46    import info.magnolia.rest.reference.ReferenceResolver;
47    import info.magnolia.rest.reference.ReferenceResolverDefinition;
48   
49    import java.util.List;
50    import java.util.Optional;
51    import java.util.stream.Collectors;
52   
53    import javax.inject.Inject;
54   
55    import org.apache.commons.collections4.CollectionUtils;
56    import org.slf4j.Logger;
57    import org.slf4j.LoggerFactory;
58   
59    import com.google.common.net.MediaType;
60   
61    /**
62    * The implementation of {@link ReferenceResolver}, expands {@link ItemKey} to Asset.
63    */
 
64    public class AssetReferenceResolver implements ReferenceResolver<String, Asset> {
65   
66    private static final Logger log = LoggerFactory.getLogger(AssetReferenceResolver.class);
67   
68    private final AssetProviderRegistry assetProviderRegistry;
69    private final ReferenceResolverDefinition referenceResolverDefinition;
70   
 
71  0 toggle @Inject
72    public AssetReferenceResolver(AssetProviderRegistry assetProviderRegistry, ReferenceResolverDefinition referenceResolverDefinition) {
73  0 this.assetProviderRegistry = assetProviderRegistry;
74  0 this.referenceResolverDefinition = referenceResolverDefinition;
75    }
76   
 
77  0 toggle @Override
78    public Optional<Asset> resolve(String item) {
79  0 try {
80  0 ItemKey itemKey = ItemKey.from(item);
81  0 AssetProvider provider = assetProviderRegistry.getProviderFor(itemKey);
82  0 Asset asset = provider.getAsset(itemKey);
83  0 if (!(referenceResolverDefinition instanceof AssetReferenceResolverDefinition)) {
84  0 return Optional.of(asset);
85    }
86  0 if (!((AssetReferenceResolverDefinition) referenceResolverDefinition).isExpand() && !((AssetReferenceResolverDefinition) referenceResolverDefinition).isIncludeDownloadLink()) {
87  0 return Optional.empty();
88    }
89   
90  0 List<AssetRendition> renditions = null;
91  0 List<String> renditionNames = ((AssetReferenceResolverDefinition) referenceResolverDefinition).assetRenditions();
92   
93  0 if (CollectionUtils.isNotEmpty(renditionNames)) {
94  0 AssetRenderer assetRenderer = provider.getRendererFor(asset, MediaType.parse(asset.getMimeType()));
95  0 if (assetRenderer != null) {
96    // Wrap fixed set of renditions, for use by the AssetWriter.
97    // Try to use WebContext in order to be able to generate link.
98  0 Context currentContext = MgnlContext.getInstance();
99  0 if (currentContext instanceof SystemContext) {
100  0 Context originalContext = ((SystemContext) currentContext).getOriginalContext();
101  0 if (originalContext instanceof WebContext) {
102  0 MgnlContext.setInstance(originalContext);
103    }
104    }
105   
106  0 renditions = renditionNames.stream()
107    .map(renditionName -> assetRenderer.render(asset, MediaType.parse(asset.getMimeType()), renditionName))
108    .collect(Collectors.toList());
109   
110  0 if (!currentContext.equals(MgnlContext.getInstance())) {
111  0 MgnlContext.setInstance(currentContext);
112    }
113    }
114    }
115  0 return Optional.of(new ConfigurableAssetWrapper(asset, renditions, (AssetReferenceResolverDefinition) referenceResolverDefinition));
116    } catch (IllegalArgumentException e) {
117  0 log.error("Key {} is not valid Asset key", item);
118    } catch (AssetProvider.AssetNotFoundException e) {
119  0 log.error("Asset {} is not found!", item);
120    } catch (AssetProviderRegistry.NoSuchAssetProviderException e) {
121  0 log.error("Cannot find provider for asset {}", item);
122    }
123   
124  0 return Optional.empty();
125    }
126    }