Clover icon

Magnolia REST Client Module 1.0.1

  1. Project Clover database Fri Dec 5 2014 14:52:06 EST
  2. Package info.magnolia.rest.client.registry

File ConfiguredRestClientManager.java

 

Coverage histogram

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

Code metrics

4
19
5
1
121
64
8
0.42
3.8
5
1.6

Classes

Class Line # Actions
ConfiguredRestClientManager 62 19 0% 8 28
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /**
2    * This file Copyright (c) 2014 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.client.registry;
35   
36    import info.magnolia.cms.util.ModuleConfigurationObservingManager;
37    import info.magnolia.jcr.util.NodeTypes;
38    import info.magnolia.jcr.util.NodeUtil;
39    import info.magnolia.jcr.util.NodeVisitor;
40    import info.magnolia.module.ModuleRegistry;
41    import info.magnolia.objectfactory.ComponentProvider;
42    import info.magnolia.rest.client.RestClientDefinition;
43   
44    import java.util.ArrayList;
45    import java.util.HashSet;
46    import java.util.List;
47    import java.util.Set;
48   
49    import javax.inject.Inject;
50    import javax.inject.Singleton;
51    import javax.jcr.Node;
52    import javax.jcr.RepositoryException;
53   
54    import org.apache.jackrabbit.commons.predicate.NodeTypePredicate;
55    import org.slf4j.Logger;
56    import org.slf4j.LoggerFactory;
57   
58    /**
59    * ObservedManager for {@link info.magnolia.rest.client.RestClientDefinition}(s) configured in repository.
60    */
61    @Singleton
 
62    public class ConfiguredRestClientManager extends ModuleConfigurationObservingManager {
63   
64    private static final Logger log = LoggerFactory.getLogger(ConfiguredRestClientManager.class);
65   
66    private Set<String> registeredIds = new HashSet<String>();
67    private RestClientRegistry restClientDefinitionRegistry;
68    private final ComponentProvider componentProvider;
69   
 
70  0 toggle @Inject
71    public ConfiguredRestClientManager(ModuleRegistry moduleRegistry, RestClientRegistry restClientDefinitionRegistry, ComponentProvider componentProvider) {
72  0 super("rest-client", moduleRegistry);
73  0 this.restClientDefinitionRegistry = restClientDefinitionRegistry;
74  0 this.componentProvider = componentProvider;
75    }
76   
 
77  0 toggle @Override
78    protected void reload(List<Node> nodes) throws RepositoryException {
79   
80  0 final List<RestClientProvider> providers = new ArrayList<RestClientProvider>();
81   
82  0 for (Node node : nodes) {
83   
84  0 NodeUtil.visit(node, new NodeVisitor() {
85   
 
86  0 toggle @Override
87    public void visit(Node nodeToVisit) throws RepositoryException {
88  0 for (Node configNode : NodeUtil.getNodes(nodeToVisit, NodeTypes.ContentNode.NAME)) {
89  0 if (isNotConfigured(configNode)) {
90  0 continue;
91    }
92  0 RestClientProvider provider = createProvider(configNode);
93  0 if (provider != null) {
94  0 providers.add(provider);
95    }
96    }
97    }
98    }, new NodeTypePredicate(NodeTypes.Content.NAME, false));
99    }
100  0 this.registeredIds = restClientDefinitionRegistry.unregisterAndRegister(registeredIds, providers);
101    }
102   
 
103  0 toggle protected RestClientProvider createProvider(Node restClientDefinitionNode) throws RepositoryException {
104  0 final String name = restClientDefinitionNode.getName();
105   
106  0 try {
107  0 return new ConfiguredRestClientProvider(name, restClientDefinitionNode, componentProvider);
108    } catch (Exception e) {
109  0 log.error("Unable to create provider for rest client definition [" + restClientDefinitionNode.getPath() + "]", e);
110    }
111  0 return null;
112    }
113   
114    /**
115    * Checks if the restClient is completely configured.
116    */
 
117  0 toggle private boolean isNotConfigured(Node node) throws RepositoryException {
118  0 return !node.hasProperty(RestClientDefinition.PROPERTY_REST_CLIENT_DEFINITION_CLASS);
119    }
120   
121    }