Skip to content

Commit 78324a8

Browse files
authored
Introduce push notifications / webhooks
1 parent 73dd77f commit 78324a8

24 files changed

+721
-18
lines changed

NetLicensingClient-demo/src/main/java/com/labs64/netlicensing/examples/CallEveryAPIMethod.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public class CallEveryAPIMethod implements NetLicensingExample {
7070
private static final String randomLicenseeSecret = UUID.randomUUID().toString();
7171

7272
@Override
73+
@SuppressWarnings("deprecation")
7374
public void execute() {
7475

7576
final Context context = new Context();

NetLicensingClient/src/main/java/com/labs64/netlicensing/domain/Constants.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ public static final class LicenseTemplate {
101101
public static final String AUTOMATIC = "automatic";
102102
public static final String HIDDEN = "hidden";
103103
public static final String HIDE_LICENSES = "hideLicenses";
104+
/**
105+
* @deprecated Use the NodeLocked licensing model instead.
106+
*/
107+
@Deprecated
104108
public static final String PROP_LICENSEE_SECRET = "licenseeSecret";
105109
}
106110

@@ -123,6 +127,10 @@ public static final class License {
123127
public static final String ENDPOINT_PATH = "license";
124128
public static final String HIDDEN = "hidden";
125129
public static final String LICENSE_NUMBER = "licenseNumber";
130+
/**
131+
* @deprecated Use the NodeLocked licensing model instead.
132+
*/
133+
@Deprecated
126134
public static final String PROP_LICENSEE_SECRET = "licenseeSecret";
127135
}
128136

@@ -211,5 +219,13 @@ public static final class ValidationResult {
211219
public static final String WARNING_LEVEL = "warningLevel";
212220
}
213221

222+
public static final class Notification {
223+
public static final String ENDPOINT_PATH = "notification";
224+
public static final String EVENTS = "events";
225+
public static final String PROTOCOL = "protocol";
226+
public static final String ENDPOINT = "endpoint";
227+
public static final String PAYLOAD = "payload";
228+
}
229+
214230
// CHECKSTYLE:ON
215231
}

NetLicensingClient/src/main/java/com/labs64/netlicensing/domain/EntityFactory.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.labs64.netlicensing.domain.entity.License;
2323
import com.labs64.netlicensing.domain.entity.LicenseTemplate;
2424
import com.labs64.netlicensing.domain.entity.Licensee;
25+
import com.labs64.netlicensing.domain.entity.Notification;
2526
import com.labs64.netlicensing.domain.entity.PaymentMethod;
2627
import com.labs64.netlicensing.domain.entity.Product;
2728
import com.labs64.netlicensing.domain.entity.ProductModule;
@@ -44,6 +45,7 @@
4445
import com.labs64.netlicensing.schema.converter.ItemToLicenseTypePropertiesConverter;
4546
import com.labs64.netlicensing.schema.converter.ItemToLicenseeConverter;
4647
import com.labs64.netlicensing.schema.converter.ItemToLicensingModelPropertiesConverter;
48+
import com.labs64.netlicensing.schema.converter.ItemToNotificationConverter;
4749
import com.labs64.netlicensing.schema.converter.ItemToPaymentMethodConverter;
4850
import com.labs64.netlicensing.schema.converter.ItemToProductConverter;
4951
import com.labs64.netlicensing.schema.converter.ItemToProductModuleConverter;
@@ -72,6 +74,7 @@ public class EntityFactory {
7274
entityToConverterMap.put(Country.class, ItemToCountryConverter.class);
7375
entityToConverterMap.put(LicensingModelProperties.class, ItemToLicensingModelPropertiesConverter.class);
7476
entityToConverterMap.put(LicenseTypeProperties.class, ItemToLicenseTypePropertiesConverter.class);
77+
entityToConverterMap.put(Notification.class, ItemToNotificationConverter.class);
7578
}
7679

7780
private Map<Class<?>, Converter<Item, ?>> convertersCache;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.labs64.netlicensing.domain.entity;
2+
3+
import java.util.Set;
4+
5+
import com.labs64.netlicensing.domain.vo.Event;
6+
import com.labs64.netlicensing.domain.vo.NotificationProtocol;
7+
8+
public interface Notification extends BaseEntity {
9+
String getName();
10+
11+
void setName(String name);
12+
13+
Set<Event> getEvents();
14+
15+
void setEvents(Set<Event> events);
16+
17+
void addEvent(final Event event);
18+
19+
NotificationProtocol getProtocol();
20+
21+
void setProtocol(NotificationProtocol notificationProtocol);
22+
23+
String getPayload();
24+
25+
void setPayload(String payload);
26+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/* Licensed under the Apache License, Version 2.0 (the "License");
2+
* you may not use this file except in compliance with the License.
3+
* You may obtain a copy of the License at
4+
*
5+
* https://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
package com.labs64.netlicensing.domain.entity.impl;
14+
15+
import java.util.HashSet;
16+
import java.util.List;
17+
import java.util.Set;
18+
import java.util.stream.Collectors;
19+
20+
import javax.ws.rs.core.MultivaluedMap;
21+
22+
import com.labs64.netlicensing.domain.Constants;
23+
import com.labs64.netlicensing.domain.entity.Notification;
24+
import com.labs64.netlicensing.domain.entity.Product;
25+
import com.labs64.netlicensing.domain.vo.Event;
26+
import com.labs64.netlicensing.domain.vo.NotificationProtocol;
27+
28+
/**
29+
* Default implementation of {@link Product}.
30+
*/
31+
public class NotificationImpl extends BaseEntityImpl implements Notification {
32+
33+
private static final long serialVersionUID = -4059438768485180571L;
34+
35+
private String name;
36+
37+
private Set<Event> events = new HashSet<>();
38+
39+
private NotificationProtocol protocol;
40+
41+
private String payload;
42+
43+
44+
@Override
45+
public String getName() {
46+
return name;
47+
}
48+
49+
@Override
50+
public void setName(final String name) {
51+
this.name = name;
52+
}
53+
54+
@Override
55+
public Set<Event> getEvents() {
56+
return events;
57+
}
58+
59+
@Override
60+
public void setEvents(final Set<Event> events) {
61+
this.events = events;
62+
}
63+
64+
@Override
65+
public void addEvent(final Event event) {
66+
getEvents().add(event);
67+
}
68+
69+
@Override
70+
public NotificationProtocol getProtocol() {
71+
return protocol;
72+
}
73+
74+
@Override
75+
public void setProtocol(final NotificationProtocol protocol) {
76+
this.protocol = protocol;
77+
}
78+
79+
@Override
80+
public String getPayload() {
81+
return payload;
82+
}
83+
84+
@Override
85+
public void setPayload(final String payload) {
86+
this.payload = payload;
87+
}
88+
89+
public static List<String> getReservedProps() {
90+
final List<String> reserved = BaseEntityImpl.getReservedProps();
91+
reserved.add(Constants.Notification.EVENTS);
92+
reserved.add(Constants.Notification.PROTOCOL);
93+
reserved.add(Constants.Notification.ENDPOINT);
94+
reserved.add(Constants.Notification.PAYLOAD);
95+
return reserved;
96+
}
97+
98+
@Override
99+
protected MultivaluedMap<String, Object> asPropertiesMap() {
100+
final Set<Event> events = getEvents();
101+
final NotificationProtocol protocol = getProtocol();
102+
103+
final MultivaluedMap<String, Object> map = super.asPropertiesMap();
104+
map.add(Constants.NAME, getName());
105+
map.add(Constants.Notification.EVENTS, events.stream().map(Enum::name).collect(Collectors.joining(",")));
106+
107+
if (protocol != null) {
108+
map.add(Constants.Notification.PROTOCOL, protocol.name());
109+
}
110+
111+
if (NotificationProtocol.WEBHOOK.equals(protocol)) {
112+
map.add(Constants.Notification.ENDPOINT, getProperties().get(Constants.Notification.ENDPOINT));
113+
}
114+
115+
map.add(Constants.Notification.PAYLOAD, getPayload());
116+
117+
return map;
118+
}
119+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.labs64.netlicensing.domain.vo;
2+
3+
import java.util.Arrays;
4+
5+
public enum Event {
6+
CREATE_LICENSEE, CREATE_LICENSE;
7+
8+
public static Event parseString(final String value) {
9+
return Arrays.stream(Event.values()).filter((e) -> e.name().equalsIgnoreCase(value)).findFirst().orElse(null);
10+
}
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.labs64.netlicensing.domain.vo;
2+
3+
import java.util.Arrays;
4+
5+
public enum NotificationProtocol {
6+
WEBHOOK;
7+
8+
public static NotificationProtocol parseString(final String value) {
9+
return Arrays.stream(NotificationProtocol.values()).filter((t) -> t.name().equalsIgnoreCase(value))
10+
.findFirst().orElse(null);
11+
}
12+
}

NetLicensingClient/src/main/java/com/labs64/netlicensing/domain/vo/ValidationParameters.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,23 @@ public String getLicenseeName() {
5757

5858
/**
5959
* Sets the licensee secret
60+
*
61+
* @deprecated Use the NodeLocked licensing model instead.
6062
*
6163
* @param licenseeSecret
6264
* licensee secret stored on the client side. Refer to Licensee Secret documentation for details.
6365
*/
66+
@Deprecated
6467
public void setLicenseeSecret(final String licenseeSecret) {
6568
setLicenseeProperty(Constants.Licensee.PROP_LICENSEE_SECRET, licenseeSecret);
6669
}
6770

71+
/**
72+
* Gets the licensee secret
73+
*
74+
* @deprecated Use the NodeLocked licensing model instead.
75+
*/
76+
@Deprecated
6877
public String getLicenseeSecret() {
6978
return getLicenseeProperties().get(Constants.Licensee.PROP_LICENSEE_SECRET);
7079
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* Licensed under the Apache License, Version 2.0 (the "License");
2+
* you may not use this file except in compliance with the License.
3+
* You may obtain a copy of the License at
4+
*
5+
* https://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
package com.labs64.netlicensing.schema.converter;
14+
15+
import java.util.Arrays;
16+
import java.util.Set;
17+
import java.util.stream.Collectors;
18+
19+
import com.labs64.netlicensing.domain.Constants;
20+
import com.labs64.netlicensing.domain.entity.Notification;
21+
import com.labs64.netlicensing.domain.entity.Product;
22+
import com.labs64.netlicensing.domain.entity.impl.NotificationImpl;
23+
import com.labs64.netlicensing.domain.vo.Event;
24+
import com.labs64.netlicensing.domain.vo.NotificationProtocol;
25+
import com.labs64.netlicensing.exception.ConversionException;
26+
import com.labs64.netlicensing.schema.SchemaFunction;
27+
import com.labs64.netlicensing.schema.context.Item;
28+
import com.labs64.netlicensing.schema.context.Property;
29+
30+
/**
31+
* Convert {@link Item} entity into {@link Product} object.
32+
*/
33+
public class ItemToNotificationConverter extends ItemToEntityBaseConverter<Notification> {
34+
35+
@Override
36+
public Notification convert(final Item source) throws ConversionException {
37+
final Notification target = super.convert(source);
38+
39+
target.setName(SchemaFunction.propertyByName(source.getProperty(), Constants.NAME).getValue());
40+
41+
final Set<Event> events = Arrays
42+
.stream(SchemaFunction.propertyByName(source.getProperty(), Constants.Notification.EVENTS).getValue().split(","))
43+
.map(Event::parseString)
44+
.collect(Collectors.toSet());
45+
46+
target.setEvents(events);
47+
target.setProtocol(NotificationProtocol.parseString(SchemaFunction.propertyByName(source.getProperty(), Constants.Notification.PROTOCOL).getValue()));
48+
49+
final String endpoint = SchemaFunction.propertyByName(source.getProperty(), Constants.Notification.ENDPOINT).getValue();
50+
51+
if (endpoint != null) {
52+
target.addProperty(Constants.Notification.ENDPOINT, endpoint);
53+
}
54+
55+
target.setPayload(SchemaFunction.propertyByName(source.getProperty(), Constants.Notification.PAYLOAD).getValue());
56+
57+
// Custom properties
58+
for (final Property property : source.getProperty()) {
59+
if (!NotificationImpl.getReservedProps().contains(property.getName())) {
60+
target.addProperty(property.getName(), property.getValue());
61+
}
62+
}
63+
64+
return target;
65+
}
66+
67+
@Override
68+
public Notification newTarget() {
69+
return new NotificationImpl();
70+
}
71+
72+
}

0 commit comments

Comments
 (0)