Clover icon

Magnolia REST Integration 2.0-rc1

  1. Project Clover database Mon Oct 30 2017 16:27:09 CET
  2. Package info.magnolia.rest.registry

File EndpointDefinitionRegistry.java

 

Coverage histogram

../../../../img/srcFileCovDistChart6.png
41% of files have more coverage

Code metrics

0
23
11
1
170
98
13
0.57
2.09
11
1.18
5.6% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
EndpointDefinitionRegistry 62 23 5.6% 13 16
0.529411852.9%
 

Contributing tests

This file is covered by 5 tests. .

Source view

1    /**
2    * This file Copyright (c) 2013-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.rest.registry;
35   
36    import static info.magnolia.rest.registry.EndpointDefinitionRegistryEventType.*;
37   
38    import info.magnolia.config.registry.AbstractRegistry;
39    import info.magnolia.config.registry.DefinitionMetadata;
40    import info.magnolia.config.registry.DefinitionMetadataBuilder;
41    import info.magnolia.config.registry.DefinitionProvider;
42    import info.magnolia.config.registry.DefinitionType;
43    import info.magnolia.event.EventBus;
44    import info.magnolia.event.SystemEventBus;
45    import info.magnolia.module.ModuleRegistry;
46    import info.magnolia.objectfactory.Components;
47    import info.magnolia.registry.RegistrationException;
48    import info.magnolia.rest.EndpointDefinition;
49   
50    import java.util.Collection;
51    import java.util.List;
52    import java.util.Set;
53   
54    import javax.inject.Inject;
55    import javax.inject.Named;
56    import javax.inject.Singleton;
57   
58    /**
59    * Registry for {@linkplain EndpointDefinition REST endpoint definitions}.
60    */
61    @Singleton
 
62    public class EndpointDefinitionRegistry extends AbstractRegistry<EndpointDefinition> {
63    static final DefinitionType TYPE = new DefinitionType() {
 
64  0 toggle @Override
65    public String name() {
66  0 return "restEndpoint";
67    }
68   
 
69  0 toggle @Override
70    public Class baseClass() {
71  0 return EndpointDefinition.class;
72    }
73    };
74   
75    private EventBus systemEventBus;
76   
 
77  9 toggle @Inject
78    public EndpointDefinitionRegistry(ModuleRegistry moduleRegistry, @Named(SystemEventBus.NAME) EventBus systemEventBus) {
79  9 super(moduleRegistry);
80  9 this.systemEventBus = systemEventBus;
81    }
82   
83    /**
84    * @deprecated since 2.0 use {@link #EndpointDefinitionRegistry(ModuleRegistry, EventBus)} instead.
85    */
 
86  0 toggle @Deprecated
87    public EndpointDefinitionRegistry(@Named(SystemEventBus.NAME) EventBus systemEventBus) {
88  0 this(Components.getComponent(ModuleRegistry.class), systemEventBus);
89    }
90   
 
91  0 toggle @Override
92    public DefinitionType type() {
93  0 return TYPE;
94    }
95   
 
96  0 toggle @Override
97    public DefinitionMetadataBuilder newMetadataBuilder() {
98  0 return DefinitionMetadataBuilder.usingNameAsId();
99    }
100   
 
101  28 toggle @Override
102    public void register(DefinitionProvider<EndpointDefinition> provider) {
103  28 super.register(provider);
104  28 systemEventBus.fireEvent(new EndpointDefinitionRegistryEvent(REGISTERED, provider));
105    }
106   
 
107  3 toggle @Override
108    public Set<DefinitionMetadata> unregisterAndRegister(Collection<DefinitionMetadata> toRemoveIds, Collection<DefinitionProvider<EndpointDefinition>> definitionProviders) {
109  3 Set<DefinitionMetadata> registeredIds = super.unregisterAndRegister(toRemoveIds, definitionProviders);
110   
111  3 toRemoveIds.stream()
112    .filter(metadata -> !registeredIds.contains(metadata))
113    .map(DefinitionMetadata::getReferenceId)
114    .forEach(referenceId -> systemEventBus.fireEvent(new EndpointDefinitionRegistryEvent(UNREGISTERED, referenceId)));
115   
116  3 definitionProviders.stream()
117    .filter(provider -> toRemoveIds.contains(provider.getMetadata()))
118    .filter(provider -> registeredIds.contains(provider.getMetadata()))
119    .forEach(provider -> systemEventBus.fireEvent(new EndpointDefinitionRegistryEvent(REREGISTERED, provider)));
120   
121  3 definitionProviders.stream()
122    .filter(provider -> !toRemoveIds.contains(provider.getMetadata()))
123    .filter(provider -> registeredIds.contains(provider.getMetadata()))
124    .forEach(provider -> systemEventBus.fireEvent(new EndpointDefinitionRegistryEvent(REGISTERED, provider)));
125   
126  3 return registeredIds;
127    }
128   
129    /**
130    * @deprecated since 2.0 use {@link #getProvider(String)} instead.
131    */
 
132  2 toggle @Deprecated
133    public EndpointDefinition getEndpointDefinition(String name) throws RegistrationException {
134  2 final EndpointDefinition endpointDefinition;
135  2 try {
136  2 endpointDefinition = getProvider(name).get();
137    } catch (NoSuchDefinitionException | InvalidDefinitionException e) {
138  1 throw new RegistrationException(e.getMessage(), e);
139    }
140  1 return endpointDefinition;
141    }
142   
143    /**
144    * @deprecated since 2.0 use {@link #unregisterAndRegister(Collection, Collection)} instead.
145    */
 
146  0 toggle @Deprecated
147    public Set<String> unregisterAndRegister(Set<String> registeredNames, List<EndpointDefinitionProvider> providers) {
148  0 return null;
149    }
150   
151    /**
152    * @deprecated since 2.0 use standard registry APIs instead ({@link #getProvider(String)} and {@link DefinitionProvider#isValid()}).
153    */
 
154  0 toggle @Deprecated
155    public boolean isEndpointDefinitionRegistered(String name) {
156  0 try {
157  0 return getProvider(name).isValid();
158    } catch (NoSuchDefinitionException e) {
159  0 return false;
160    }
161    }
162   
163    /**
164    * @deprecated since 2.0 use {@link #getAllDefinitions()} instead.
165    */
 
166    toggle @Deprecated
167    public Collection<EndpointDefinition> getAllEndpointDefinitions() {
168    return getAllDefinitions();
169    }
170    }