1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.rest.registry;
35
36 import static java.util.Collections.emptyList;
37
38 import info.magnolia.config.registry.DefinitionMetadata;
39 import info.magnolia.config.registry.DefinitionMetadataBuilder;
40 import info.magnolia.config.registry.DefinitionProvider;
41 import info.magnolia.config.registry.DefinitionRawView;
42 import info.magnolia.config.registry.Registry;
43 import info.magnolia.config.registry.decoration.DefinitionDecorator;
44 import info.magnolia.event.Event;
45 import info.magnolia.rest.EndpointDefinition;
46
47 import java.util.List;
48
49
50
51
52
53
54
55 public class EndpointDefinitionRegistryEvent implements Event<EndpointDefinitionRegistryEventHandler> {
56
57 private final EndpointDefinitionRegistryEventType type;
58 private final DefinitionProvider<EndpointDefinition> endpointDefinitionProvider;
59
60 public EndpointDefinitionRegistryEvent(EndpointDefinitionRegistryEventType type, DefinitionProvider<EndpointDefinition> endpointDefinitionProvider) {
61 this.type = type;
62 this.endpointDefinitionProvider = endpointDefinitionProvider;
63 }
64
65 public EndpointDefinitionRegistryEvent(EndpointDefinitionRegistryEventType type, String endpointName) {
66 this.type = type;
67 this.endpointDefinitionProvider = new FixedDefinitionProvider(endpointName);
68 }
69
70
71
72
73 @Deprecated
74 public EndpointDefinitionRegistryEvent(EndpointDefinitionRegistryEventType type, EndpointDefinition endpointDefinition) {
75 this.type = type;
76 this.endpointDefinitionProvider = new FixedDefinitionProvider(endpointDefinition);
77 }
78
79 public EndpointDefinitionRegistryEventType getType() {
80 return type;
81 }
82
83 public String getEndpointName() {
84 return endpointDefinitionProvider.getMetadata().getReferenceId();
85 }
86
87 public DefinitionProvider<EndpointDefinition> getEndpointDefinitionProvider() {
88 return endpointDefinitionProvider;
89 }
90
91
92
93
94 @Deprecated
95 public EndpointDefinition getEndpointDefinition() {
96 return endpointDefinitionProvider.get();
97 }
98
99 @Override
100 public void dispatch(EndpointDefinitionRegistryEventHandler handler) {
101 switch(type) {
102 case REGISTERED:
103 handler.onEndpointRegistered(this);
104 break;
105 case REREGISTERED:
106 handler.onEndpointReregistered(this);
107 break;
108 case UNREGISTERED:
109 handler.onEndpointUnregistered(this);
110 break;
111 }
112 }
113
114
115
116
117 private static class FixedDefinitionProvider implements DefinitionProvider<EndpointDefinition> {
118 private final DefinitionMetadata metadata;
119 private final EndpointDefinition endpointDefinition;
120
121 public FixedDefinitionProvider(EndpointDefinition endpointDefinition) {
122 this.endpointDefinition = endpointDefinition;
123 this.metadata = DefinitionMetadataBuilder.usingNameAsId()
124 .name(endpointDefinition.getName())
125 .build();
126 }
127
128 public FixedDefinitionProvider(String endpointName) {
129 this.endpointDefinition = null;
130 this.metadata = DefinitionMetadataBuilder.usingNameAsId()
131 .name(endpointName)
132 .build();
133 }
134
135 @Override
136 public List<DefinitionDecorator<EndpointDefinition>> getDecorators() {
137 return emptyList();
138 }
139
140 @Override
141 public DefinitionMetadata getMetadata() {
142 return metadata;
143 }
144
145 @Override
146 public EndpointDefinition get() throws Registry.InvalidDefinitionException {
147 return endpointDefinition;
148 }
149
150 @Override
151 public DefinitionRawView getRaw() {
152 return null;
153 }
154
155 @Override
156 public boolean isValid() {
157 return endpointDefinition != null;
158 }
159
160 @Override
161 public long getLastModified() {
162 return 0;
163 }
164 }
165 }