1 /**
2 * This file Copyright (c) 2017 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.virtualuri.compatibility;
35
36 import static java.util.stream.Collectors.toList;
37
38 import info.magnolia.cms.beans.config.VirtualURIManager;
39 import info.magnolia.virtualuri.VirtualUriMapping;
40 import info.magnolia.virtualuri.VirtualUriRegistry;
41
42 import java.net.URI;
43 import java.net.URISyntaxException;
44 import java.util.Collection;
45 import java.util.List;
46 import java.util.Optional;
47
48 import javax.inject.Inject;
49 import javax.inject.Singleton;
50 import javax.jcr.Node;
51 import javax.jcr.RepositoryException;
52
53 import org.apache.commons.lang3.StringUtils;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 /**
58 * Compatibility adapter for {@link VirtualURIManager#getInstance()} and other locations where the manager is injected.
59 *
60 * @deprecated since 5.5.6, VirtualURIManager has been replaced by {@link VirtualUriRegistry} for configuration (see MAGNOLIA-3349),
61 * and by {@link info.magnolia.virtualuri.VirtualUriFilter VirtualUriFilter} for evaluation.
62 */
63 @Singleton
64 @Deprecated
65 public class VirtualUriManagerAdapter extends VirtualURIManager {
66 private static final Logger log = LoggerFactory.getLogger(VirtualUriManagerAdapter.class);
67 private final VirtualUriRegistry registry;
68
69 @Inject
70 public VirtualUriManagerAdapter(VirtualUriRegistry registry) {
71 super(null, null, null);
72 this.registry = registry;
73 }
74
75 /**
76 * @deprecated only for backward compatibility w/ VirtualURIManager; evaluation of mappings is now performed by {@link info.magnolia.virtualuri.VirtualUriFilter} instead.
77 */
78 @Override
79 @Deprecated
80 public String getURIMapping(String uri, String queryString) {
81
82 String targetUri = StringUtils.EMPTY;
83 final URI currentUri;
84
85 try {
86 currentUri = new URI(null, null, uri, queryString, null);
87 } catch (URISyntaxException e) {
88 log.warn("Path cannot be parsed. {0}, {1}", uri, e.getMessage());
89 return targetUri;
90 }
91
92 Optional<VirtualUriMapping.Result> mappingBestMatch = registry.getAllDefinitions().stream()
93 .map(uriMapping -> uriMapping.mapUri(currentUri))
94 .filter(Optional::isPresent)
95 .map(Optional::get)
96 .max(VirtualUriMapping.Result::compareTo);
97
98 if (mappingBestMatch.isPresent()) {
99 VirtualUriMapping.Result result = mappingBestMatch.get();
100 targetUri = result.getToUri();
101 }
102
103 return targetUri;
104 }
105
106 @Override
107 protected void reload(List<Node> nodes) throws RepositoryException {
108 // Don't need to do anything in here.
109 }
110
111 /**
112 * @deprecated only for backward compatibility w/ VirtualURIManager; only returns mappings of the old deprecated type (as per signature).
113 */
114 @Deprecated
115 public Collection<info.magnolia.cms.beans.config.VirtualURIMapping> getURIMappings() {
116 Collection<VirtualUriMapping> uriMappings = registry.getAllDefinitions();
117 return uriMappings.stream()
118 .filter(mapping -> mapping instanceof VirtualUriMappingAdapter)
119 .map(mapping -> (VirtualUriMappingAdapter) mapping)
120 .map(VirtualUriMappingAdapter::asOldMapping)
121 .collect(toList());
122 }
123 }