Skip to content

Commit d0b32c1

Browse files
committed
Fixed issue with empty subscription parameters
1 parent 8cd0760 commit d0b32c1

File tree

9 files changed

+62
-41
lines changed

9 files changed

+62
-41
lines changed

devicehive-auth/src/main/resources/messages.properties

+1
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,5 @@ PLUGIN_NOT_FOUND=Plugin is not found
137137
PLUGIN_NOT_ACTIVE=Plugin is not active
138138
PLUGIN_ALREADY_EXISTS=Plugin with name %s already exists
139139
ACTIVE_PLUGIN_UPDATED=Plugin's subscription filter can't be updated if plugin is ACTIVE
140+
PLUGIN_SUBSCRIPTION_NOT_VALID=Requested subscription is not valid. Please, set at least one 'return*' parameter to true.
140141
NO_ACCESS_TO_PLUGIN=No access to plugin

devicehive-backend/src/main/resources/messages.properties

+1
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,5 @@ PLUGIN_NOT_FOUND=Plugin is not found
131131
PLUGIN_NOT_ACTIVE=Plugin is not active
132132
PLUGIN_ALREADY_EXISTS=Plugin with name %s already exists
133133
ACTIVE_PLUGIN_UPDATED=Plugin's subscription filter can't be updated if plugin is ACTIVE
134+
PLUGIN_SUBSCRIPTION_NOT_VALID=Requested subscription is not valid. Please, set at least one 'return*' parameter to true.
134135
NO_ACCESS_TO_PLUGIN=No access to plugin

devicehive-common/src/main/java/com/devicehive/configuration/Messages.java

+1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public class Messages {
139139
public static final String PLUGIN_NOT_ACTIVE = BidBundle.get("PLUGIN_NOT_ACTIVE");
140140
public static final String PLUGIN_ALREADY_EXISTS = BidBundle.get("PLUGIN_ALREADY_EXISTS");
141141
public static final String ACTIVE_PLUGIN_UPDATED = BidBundle.get("ACTIVE_PLUGIN_UPDATED");
142+
public static final String PLUGIN_SUBSCRIPTION_NOT_VALID = BidBundle.get("PLUGIN_SUBSCRIPTION_NOT_VALID");
142143
public static final String NO_ACCESS_TO_PLUGIN = BidBundle.get("NO_ACCESS_TO_PLUGIN");
143144

144145
/**

devicehive-frontend/src/main/resources/messages.properties

+1
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,5 @@ PLUGIN_NOT_FOUND=Plugin is not found
137137
PLUGIN_NOT_ACTIVE=Plugin is not active
138138
PLUGIN_ALREADY_EXISTS=Plugin with name %s already exists
139139
ACTIVE_PLUGIN_UPDATED=Plugin's subscription filter can't be updated if plugin is ACTIVE
140+
PLUGIN_SUBSCRIPTION_NOT_VALID=Requested subscription is not valid. Please, set at least one 'return*' parameter to true.
140141
NO_ACCESS_TO_PLUGIN=No access to plugin

devicehive-plugin/src/main/java/com/devicehive/model/query/PluginReqisterQuery.java

+21-19
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
*/
2222

2323
import com.devicehive.model.FilterEntity;
24+
import com.devicehive.model.eventbus.Filter;
2425
import com.devicehive.model.rpc.PluginSubscribeRequest;
2526
import com.devicehive.service.FilterService;
2627
import io.swagger.annotations.ApiParam;
2728

2829
import javax.ws.rs.QueryParam;
2930

3031
import java.util.Optional;
32+
import java.util.Set;
3133
import java.util.StringJoiner;
3234

3335
import static com.devicehive.configuration.Constants.*;
@@ -119,12 +121,18 @@ public void setReturnNotifications(Boolean returnNotifications) {
119121
this.returnNotifications = returnNotifications;
120122
}
121123

122-
public PluginSubscribeRequest toRequest(FilterService filterService) {
124+
public PluginSubscribeRequest toRequest(Set<Filter> filters) {
123125
PluginSubscribeRequest request = new PluginSubscribeRequest();
124-
request.setFilters(filterService.createFilters(this));
125-
request.setReturnCommands(returnCommands);
126-
request.setReturnUpdatedCommands(returnUpdatedCommands);
127-
request.setReturnNotifications(returnNotifications);
126+
request.setFilters(filters);
127+
if (returnCommands != null) {
128+
request.setReturnCommands(returnCommands);
129+
}
130+
if (returnUpdatedCommands != null) {
131+
request.setReturnUpdatedCommands(returnUpdatedCommands);
132+
}
133+
if (returnNotifications != null) {
134+
request.setReturnNotifications(returnNotifications);
135+
}
128136

129137
return request;
130138
}
@@ -133,23 +141,17 @@ public PluginSubscribeRequest toRequest(FilterService filterService) {
133141
// TODO - change to embedded entity for better code readability
134142
public String constructFilterString() {
135143
StringJoiner sj = new StringJoiner("/");
136-
returnCommands = Optional.ofNullable(returnCommands).orElse(false);
137-
returnUpdatedCommands = Optional.ofNullable(returnUpdatedCommands).orElse(false);
138-
returnNotifications = Optional.ofNullable(returnNotifications).orElse(false);
139144

140-
if (returnCommands || returnUpdatedCommands || returnNotifications) {
141-
StringJoiner dataSj = new StringJoiner(",");
142-
if (returnCommands) {
143-
dataSj.add(COMMAND);
144-
}
145+
boolean commandChanges = returnCommands != null && returnCommands;
146+
boolean commandUpdatedChanges = returnUpdatedCommands != null && returnUpdatedCommands;
147+
boolean notificationsChanges = returnNotifications != null && returnNotifications;
145148

146-
if (returnUpdatedCommands) {
147-
dataSj.add(COMMAND_UPDATE);
148-
}
149+
if (commandChanges || commandUpdatedChanges || notificationsChanges) {
150+
StringJoiner dataSj = new StringJoiner(",");
151+
if (commandChanges) dataSj.add(COMMAND);
152+
if (commandUpdatedChanges) dataSj.add(COMMAND_UPDATE);
153+
if (notificationsChanges) dataSj.add(NOTIFICATION);
149154

150-
if (returnNotifications) {
151-
dataSj.add(NOTIFICATION);
152-
}
153155
sj.add(dataSj.toString());
154156
} else {
155157
sj.add(ANY);

devicehive-plugin/src/main/java/com/devicehive/service/FilterService.java

+17-15
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
*/
2222

2323
import com.devicehive.auth.HivePrincipal;
24+
import com.devicehive.model.FilterEntity;
2425
import com.devicehive.model.eventbus.Filter;
2526
import com.devicehive.model.query.PluginReqisterQuery;
2627
import com.devicehive.vo.DeviceVO;
28+
import com.devicehive.vo.PluginVO;
2729
import org.slf4j.Logger;
2830
import org.slf4j.LoggerFactory;
2931
import org.springframework.beans.factory.annotation.Autowired;
@@ -50,44 +52,44 @@ public FilterService(BaseDeviceService deviceService) {
5052
this.deviceService = deviceService;
5153
}
5254

53-
public Set<Filter> createFilters(PluginReqisterQuery query) {
55+
public Set<Filter> createFilters(FilterEntity filterEntity) {
5456
HivePrincipal principal = (HivePrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
5557
Set<Filter> filters;
56-
if (query.getDeviceId() != null) {
57-
DeviceVO device = deviceService.findByIdWithPermissionsCheck(query.getDeviceId(), principal);
58+
if (filterEntity.getDeviceId() != null) {
59+
DeviceVO device = deviceService.findByIdWithPermissionsCheck(filterEntity.getDeviceId(), principal);
5860
if (device == null) {
59-
logger.error("Could not find device with id={}", query.getDeviceId());
61+
logger.error("Could not find device with id={}", filterEntity.getDeviceId());
6062
}
61-
if (query.getNames() != null) {
62-
filters = toStringSet(query.getNames()).stream().map(name ->
63-
new Filter(device.getNetworkId(), device.getDeviceTypeId(), query.getDeviceId(), null, name))
63+
if (filterEntity.getNames() != null) {
64+
filters = toStringSet(filterEntity.getNames()).stream().map(name ->
65+
new Filter(device.getNetworkId(), device.getDeviceTypeId(), filterEntity.getDeviceId(), null, name))
6466
.collect(Collectors.toSet());
6567
} else {
66-
filters = Collections.singleton(new Filter(device.getNetworkId(), device.getDeviceTypeId(), query.getDeviceId(), null, null));
68+
filters = Collections.singleton(new Filter(device.getNetworkId(), device.getDeviceTypeId(), filterEntity.getDeviceId(), null, null));
6769
}
6870
} else {
69-
if (query.getNetworkIds() == null && query.getDeviceTypeIds() == null) {
70-
if (query.getNames() != null) {
71-
filters = toStringSet(query.getNames()).stream().map(name ->
71+
if (filterEntity.getNetworkIds() == null && filterEntity.getDeviceTypeIds() == null) {
72+
if (filterEntity.getNames() != null) {
73+
filters = toStringSet(filterEntity.getNames()).stream().map(name ->
7274
new Filter(null, null, null, null, name))
7375
.collect(Collectors.toSet());
7476
} else {
7577
filters = Collections.singleton(new Filter());
7678
}
7779
} else {
78-
Set<Long> networks = toLongSet(query.getNetworkIds());
80+
Set<Long> networks = toLongSet(filterEntity.getNetworkIds());
7981
if (networks.isEmpty()) {
8082
networks = principal.getNetworkIds();
8183
}
82-
Set<Long> deviceTypes = toLongSet(query.getDeviceTypeIds());
84+
Set<Long> deviceTypes = toLongSet(filterEntity.getDeviceTypeIds());
8385
if (deviceTypes.isEmpty()) {
8486
deviceTypes = principal.getDeviceTypeIds();
8587
}
8688
final Set<Long> finalDeviceTypes = deviceTypes;
8789
filters = networks.stream()
8890
.flatMap(network -> finalDeviceTypes.stream().flatMap(deviceType -> {
89-
if (query.getNames() != null) {
90-
return toStringSet(query.getNames()).stream().map(name ->
91+
if (filterEntity.getNames() != null) {
92+
return toStringSet(filterEntity.getNames()).stream().map(name ->
9193
new Filter(network, deviceType, null, null, name)
9294
);
9395
} else {

devicehive-plugin/src/main/java/com/devicehive/service/PluginRegisterService.java

+18-6
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.springframework.stereotype.Component;
5454
import org.springframework.transaction.annotation.Transactional;
5555

56+
import javax.servlet.Filter;
5657
import javax.ws.rs.ServiceUnavailableException;
5758
import javax.ws.rs.core.Response;
5859
import java.util.Collections;
@@ -206,6 +207,13 @@ private CompletableFuture<PluginVO> updatePlugin(PluginVO existingPlugin, Plugin
206207
throw new IllegalArgumentException("Cannot change status of existing plugin to Created.");
207208
}
208209

210+
if (pluginUpdateQuery.isReturnCommands() != null && !pluginUpdateQuery.isReturnCommands() &&
211+
pluginUpdateQuery.isReturnUpdatedCommands() != null && !pluginUpdateQuery.isReturnUpdatedCommands() &&
212+
pluginUpdateQuery.isReturnNotifications() != null && !pluginUpdateQuery.isReturnNotifications()) {
213+
logger.error("Requested subscription is not valid. Please, set at least one 'return*' parameter to true.");
214+
throw new HiveException(Messages.PLUGIN_SUBSCRIPTION_NOT_VALID, BAD_REQUEST.getStatusCode());
215+
}
216+
209217
if (pluginUpdateQuery.getName() != null) {
210218
existingPlugin.setName(pluginUpdateQuery.getName());
211219
}
@@ -218,15 +226,16 @@ private CompletableFuture<PluginVO> updatePlugin(PluginVO existingPlugin, Plugin
218226
existingPlugin.setParameters(pluginUpdateQuery.getParameters());
219227
}
220228

221-
final boolean isFilterChanges = pluginUpdateQuery.getDeviceId() != null || pluginUpdateQuery.getNetworkIds() != null ||
229+
final boolean isFilterUpdated = pluginUpdateQuery.getDeviceId() != null || pluginUpdateQuery.getNetworkIds() != null ||
222230
pluginUpdateQuery.getDeviceTypeIds() != null || pluginUpdateQuery.getNames() != null ||
223-
pluginUpdateQuery.isReturnCommands() != null || pluginUpdateQuery.isReturnUpdatedCommands() != null ||
231+
pluginUpdateQuery.isReturnCommands() != null ||
232+
pluginUpdateQuery.isReturnUpdatedCommands() != null ||
224233
pluginUpdateQuery.isReturnNotifications() != null;
225234

226235
final boolean isStatusUpdated = pluginUpdateQuery.getStatus() != null &&
227236
!pluginUpdateQuery.getStatus().equals(existingPlugin.getStatus());
228237

229-
if (isFilterChanges && !isStatusUpdated && existingPlugin.getStatus().equals(PluginStatus.ACTIVE)) {
238+
if (isFilterUpdated && !isStatusUpdated && existingPlugin.getStatus().equals(PluginStatus.ACTIVE)) {
230239
logger.error("Plugin's subscription filter can't be updated if plugin is ACTIVE");
231240
throw new HiveException(Messages.ACTIVE_PLUGIN_UPDATED, BAD_REQUEST.getStatusCode());
232241
}
@@ -235,15 +244,18 @@ private CompletableFuture<PluginVO> updatePlugin(PluginVO existingPlugin, Plugin
235244
existingPlugin.setStatus(pluginUpdateQuery.getStatus());
236245
}
237246

238-
existingPlugin.setFilter(pluginUpdateQuery.constructFilterString());
247+
if (isFilterUpdated) {
248+
existingPlugin.setFilter(pluginUpdateQuery.constructFilterString());
249+
}
239250

240251
CompletableFuture<com.devicehive.shim.api.Response> future = new CompletableFuture<>();
241252

242253
BasePluginRequest request = null;
243254
if (isStatusUpdated) {
244255
if (pluginUpdateQuery.getStatus().equals(PluginStatus.ACTIVE) && existingPlugin.getSubscriptionId() == null) {
245-
final Long subscriptionId = idGenerator.generate();
246-
request = pluginUpdateQuery.toRequest(filterService);
256+
Long subscriptionId = idGenerator.generate();
257+
FilterEntity filterEntity = new FilterEntity(existingPlugin.getFilter());
258+
request = pluginUpdateQuery.toRequest(filterService.createFilters(filterEntity));
247259
request.setSubscriptionId(subscriptionId);
248260
existingPlugin.setSubscriptionId(subscriptionId);
249261
((PluginSubscribeRequest) request).setTopicName(existingPlugin.getTopicName());

devicehive-plugin/src/main/resources/messages.properties

+1
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,5 @@ DEVICE_TYPE_CREATION_NOT_ALLOWED=No permissions to create device type!
137137
DEVICE_TYPE_ASSIGNMENT_NOT_ALLOWED=Cannot assign device type to user with all device types allowed
138138
NO_ACCESS_TO_DEVICE_TYPES_OR_NETWORKS=No access to any device type or network
139139
ACTIVE_PLUGIN_UPDATED=Plugin's subscription filter can't be updated if plugin is ACTIVE
140+
PLUGIN_SUBSCRIPTION_NOT_VALID=Requested subscription is not valid. Please, set at least one 'return*' parameter to true.
140141
NO_ACCESS_TO_PLUGIN=No access to plugin

devicehive-proxy-ws-kafka-impl/src/main/java/com/devicehive/proxy/FrontendProxyClient.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private void pingServer() {
9898
boolean connected = false;
9999
int attempts = 10;
100100
for (int i = 0; i < attempts; i++) {
101-
logger.info("Ping Backend Server attempt {}", i);
101+
logger.info("Ping WebSocket Proxy attempt {}", i);
102102

103103
CompletableFuture<Response> pingFuture = new CompletableFuture<>();
104104

0 commit comments

Comments
 (0)