|
41 | 41 | import com.devicehive.shim.api.client.RpcClient;
|
42 | 42 | import com.devicehive.shim.kafka.topic.KafkaTopicService;
|
43 | 43 | import com.devicehive.util.HiveValidator;
|
| 44 | +import com.devicehive.vo.ApiInfoVO; |
44 | 45 | import com.devicehive.vo.JwtTokenVO;
|
45 | 46 | import com.devicehive.vo.PluginVO;
|
46 | 47 | import com.google.gson.Gson;
|
@@ -111,6 +112,8 @@ public CompletableFuture<Response> register(Long userId, PluginReqisterQuery plu
|
111 | 112 | throw new HiveException(String.format(Messages.PLUGIN_ALREADY_EXISTS, pluginUpdate.getName()), BAD_REQUEST.getStatusCode());
|
112 | 113 | }
|
113 | 114 |
|
| 115 | + checkAuthServiceAvailable(); |
| 116 | + |
114 | 117 | return persistPlugin(pluginUpdate, pluginReqisterQuery.constructFilterString(), userId).thenApply(pluginVO -> {
|
115 | 118 | JwtTokenVO jwtTokenVO = createPluginTokens(pluginVO.getTopicName(), authorization);
|
116 | 119 | JsonObject response = createTokenResponse(pluginVO.getTopicName(), jwtTokenVO);
|
@@ -215,46 +218,29 @@ private CompletableFuture<PluginVO> updatePlugin(PluginVO existingPlugin, Plugin
|
215 | 218 | existingPlugin.setParameters(pluginUpdateQuery.getParameters());
|
216 | 219 | }
|
217 | 220 |
|
218 |
| - if (pluginUpdateQuery.getStatus() != null) { |
219 |
| - existingPlugin.setStatus(pluginUpdateQuery.getStatus()); |
220 |
| - } |
221 |
| - |
222 |
| - // if no new information about filters is provided in PluginUpdateQuery, we should keep the same filters |
223 |
| - FilterEntity filterEntity = new FilterEntity(existingPlugin.getFilter()); |
224 |
| - if (pluginUpdateQuery.getDeviceId() == null) { |
225 |
| - pluginUpdateQuery.setDeviceId(filterEntity.getDeviceId()); |
226 |
| - } |
227 |
| - |
228 |
| - if (pluginUpdateQuery.getNetworkIds() == null) { |
229 |
| - pluginUpdateQuery.setNetworkIds(filterEntity.getNetworkIds()); |
230 |
| - } |
231 |
| - |
232 |
| - if (pluginUpdateQuery.getDeviceTypeIds() == null) { |
233 |
| - pluginUpdateQuery.setDeviceTypeIds(filterEntity.getDeviceTypeIds()); |
234 |
| - } |
235 |
| - |
236 |
| - if (pluginUpdateQuery.getNames() == null) { |
237 |
| - pluginUpdateQuery.setNames(filterEntity.getNames()); |
238 |
| - } |
| 221 | + final boolean isFilterChanges = pluginUpdateQuery.getDeviceId() != null || pluginUpdateQuery.getNetworkIds() != null || |
| 222 | + pluginUpdateQuery.getDeviceTypeIds() != null || pluginUpdateQuery.getNames() != null || |
| 223 | + pluginUpdateQuery.isReturnCommands() != null || pluginUpdateQuery.isReturnUpdatedCommands() != null || |
| 224 | + pluginUpdateQuery.isReturnNotifications() != null; |
239 | 225 |
|
240 |
| - if (pluginUpdateQuery.isReturnCommands() == null) { |
241 |
| - pluginUpdateQuery.setReturnCommands(filterEntity.isReturnCommands()); |
242 |
| - } |
| 226 | + final boolean isStatusUpdated = pluginUpdateQuery.getStatus() != null && |
| 227 | + !pluginUpdateQuery.getStatus().equals(existingPlugin.getStatus()); |
243 | 228 |
|
244 |
| - if (pluginUpdateQuery.isReturnUpdatedCommands() == null) { |
245 |
| - pluginUpdateQuery.setReturnUpdatedCommands(filterEntity.isReturnUpdatedCommands()); |
| 229 | + if (isFilterChanges && !isStatusUpdated && existingPlugin.getStatus().equals(PluginStatus.ACTIVE)) { |
| 230 | + logger.error("Plugin's subscription filter can't be updated if plugin is ACTIVE"); |
| 231 | + throw new HiveException(Messages.ACTIVE_PLUGIN_UPDATED, BAD_REQUEST.getStatusCode()); |
246 | 232 | }
|
247 | 233 |
|
248 |
| - if (pluginUpdateQuery.isReturnNotifications() == null) { |
249 |
| - pluginUpdateQuery.setReturnNotifications(filterEntity.isReturnNotifications()); |
| 234 | + if (isStatusUpdated) { |
| 235 | + existingPlugin.setStatus(pluginUpdateQuery.getStatus()); |
250 | 236 | }
|
251 | 237 |
|
252 | 238 | existingPlugin.setFilter(pluginUpdateQuery.constructFilterString());
|
253 | 239 |
|
254 | 240 | CompletableFuture<com.devicehive.shim.api.Response> future = new CompletableFuture<>();
|
255 | 241 |
|
256 | 242 | BasePluginRequest request = null;
|
257 |
| - if (pluginUpdateQuery.getStatus() != null) { |
| 243 | + if (isStatusUpdated) { |
258 | 244 | if (pluginUpdateQuery.getStatus().equals(PluginStatus.ACTIVE) && existingPlugin.getSubscriptionId() == null) {
|
259 | 245 | final Long subscriptionId = idGenerator.generate();
|
260 | 246 | request = pluginUpdateQuery.toRequest(filterService);
|
@@ -283,19 +269,22 @@ private CompletableFuture<PluginVO> updatePlugin(PluginVO existingPlugin, Plugin
|
283 | 269 |
|
284 | 270 | }
|
285 | 271 |
|
| 272 | + private void checkAuthServiceAvailable() { |
| 273 | + httpRestHelper.get(authBaseUrl + "/info", ApiInfoVO.class, null); |
| 274 | + } |
| 275 | + |
286 | 276 | private JwtTokenVO createPluginTokens(String topicName, String authorization) {
|
287 | 277 | JwtPluginPayload jwtPluginPayload = new JwtPluginPayload(Collections.singleton(MANAGE_PLUGIN.getId()), topicName, null, null);
|
288 | 278 |
|
289 | 279 | JwtTokenVO jwtToken = null;
|
290 | 280 | try {
|
291 | 281 | jwtToken = httpRestHelper.post(authBaseUrl + "/token/plugin/create", gson.toJson(jwtPluginPayload), JwtTokenVO.class, authorization);
|
292 | 282 | } catch (ServiceUnavailableException e) {
|
293 |
| - logger.warn("Service is not available"); |
294 |
| - throw new HiveException(e.getMessage(), SC_SERVICE_UNAVAILABLE); |
| 283 | + logger.error("Authentication service is not available"); |
| 284 | + throw new HiveException(e.getMessage(), SERVICE_UNAVAILABLE.getStatusCode()); |
295 | 285 | }
|
296 | 286 |
|
297 | 287 | return jwtToken;
|
298 |
| - |
299 | 288 | }
|
300 | 289 |
|
301 | 290 | private JsonObject createTokenResponse(String topicName, JwtTokenVO jwtTokenVO) {
|
|
0 commit comments