Skip to content

Package: DeviceKeystoreManagementServiceImpl$7

DeviceKeystoreManagementServiceImpl$7

nameinstructionbranchcomplexitylinemethod
getResponseClass()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
{...}
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2021, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.service.device.management.keystore.internal;
14:
15: import com.google.common.base.Strings;
16: import org.eclipse.kapua.KapuaEntityNotFoundException;
17: import org.eclipse.kapua.KapuaException;
18: import org.eclipse.kapua.KapuaIllegalArgumentException;
19: import org.eclipse.kapua.commons.util.ArgumentValidator;
20: import org.eclipse.kapua.locator.KapuaLocator;
21: import org.eclipse.kapua.locator.KapuaProvider;
22: import org.eclipse.kapua.model.domain.Actions;
23: import org.eclipse.kapua.model.id.KapuaId;
24: import org.eclipse.kapua.service.certificate.info.CertificateInfo;
25: import org.eclipse.kapua.service.certificate.info.CertificateInfoFactory;
26: import org.eclipse.kapua.service.certificate.info.CertificateInfoService;
27: import org.eclipse.kapua.service.device.management.DeviceManagementDomains;
28: import org.eclipse.kapua.service.device.management.commons.AbstractDeviceManagementServiceImpl;
29: import org.eclipse.kapua.service.device.management.commons.call.DeviceCallBuilder;
30: import org.eclipse.kapua.service.device.management.exception.DeviceManagementRequestContentException;
31: import org.eclipse.kapua.service.device.management.keystore.DeviceKeystoreManagementService;
32: import org.eclipse.kapua.service.device.management.keystore.internal.message.request.KeystoreCertificateRequestMessage;
33: import org.eclipse.kapua.service.device.management.keystore.internal.message.request.KeystoreCsrRequestMessage;
34: import org.eclipse.kapua.service.device.management.keystore.internal.message.request.KeystoreKeypairRequestMessage;
35: import org.eclipse.kapua.service.device.management.keystore.internal.message.request.KeystoreQueryRequestMessage;
36: import org.eclipse.kapua.service.device.management.keystore.internal.message.request.KeystoreRequestChannel;
37: import org.eclipse.kapua.service.device.management.keystore.internal.message.request.KeystoreRequestPayload;
38: import org.eclipse.kapua.service.device.management.keystore.internal.message.response.KeystoreCsrResponseMessage;
39: import org.eclipse.kapua.service.device.management.keystore.internal.message.response.KeystoreItemResponseMessage;
40: import org.eclipse.kapua.service.device.management.keystore.internal.message.response.KeystoreItemsResponseMessage;
41: import org.eclipse.kapua.service.device.management.keystore.internal.message.response.KeystoreNoContentResponseMessage;
42: import org.eclipse.kapua.service.device.management.keystore.internal.message.response.KeystoreResponseMessage;
43: import org.eclipse.kapua.service.device.management.keystore.internal.message.response.KeystoresResponseMessage;
44: import org.eclipse.kapua.service.device.management.keystore.model.DeviceKeystoreCSR;
45: import org.eclipse.kapua.service.device.management.keystore.model.DeviceKeystoreCSRInfo;
46: import org.eclipse.kapua.service.device.management.keystore.model.DeviceKeystoreCertificate;
47: import org.eclipse.kapua.service.device.management.keystore.model.DeviceKeystoreItem;
48: import org.eclipse.kapua.service.device.management.keystore.model.DeviceKeystoreItemQuery;
49: import org.eclipse.kapua.service.device.management.keystore.model.DeviceKeystoreItems;
50: import org.eclipse.kapua.service.device.management.keystore.model.DeviceKeystoreKeypair;
51: import org.eclipse.kapua.service.device.management.keystore.model.DeviceKeystores;
52: import org.eclipse.kapua.service.device.management.keystore.model.internal.DeviceKeystoreCertificateImpl;
53: import org.eclipse.kapua.service.device.management.keystore.model.internal.DeviceKeystoreItemQueryImpl;
54: import org.eclipse.kapua.service.device.management.message.KapuaMethod;
55: import org.slf4j.Logger;
56: import org.slf4j.LoggerFactory;
57:
58: import java.util.Date;
59:
60: /**
61: * {@link DeviceKeystoreManagementService} implementation.
62: *
63: * @since 1.5.0
64: */
65: @KapuaProvider
66: public class DeviceKeystoreManagementServiceImpl extends AbstractDeviceManagementServiceImpl implements DeviceKeystoreManagementService {
67:
68: private static final Logger LOG = LoggerFactory.getLogger(DeviceKeystoreManagementServiceImpl.class);
69:
70: private static final String SCOPE_ID = "scopeId";
71: private static final String DEVICE_ID = "deviceId";
72:
73: private static final KapuaLocator LOCATOR = KapuaLocator.getInstance();
74:
75: private static final CertificateInfoService CERTIFICATE_INFO_SERVICE = LOCATOR.getService(CertificateInfoService.class);
76: private static final CertificateInfoFactory CERTIFICATE_INFO_FACTORY = LOCATOR.getFactory(CertificateInfoFactory.class);
77:
78: @Override
79: public DeviceKeystores getKeystores(KapuaId scopeId, KapuaId deviceId, Long timeout)
80: throws KapuaException {
81: //
82: // Argument Validation
83: ArgumentValidator.notNull(scopeId, SCOPE_ID);
84: ArgumentValidator.notNull(deviceId, DEVICE_ID);
85:
86: //
87: // Check Access
88: AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(DeviceManagementDomains.DEVICE_MANAGEMENT_DOMAIN, Actions.read, scopeId));
89:
90: //
91: // Prepare the request
92: KeystoreRequestChannel keystoreRequestChannel = new KeystoreRequestChannel();
93: keystoreRequestChannel.setAppName(DeviceKeystoreAppProperties.APP_NAME);
94: keystoreRequestChannel.setVersion(DeviceKeystoreAppProperties.APP_VERSION);
95: keystoreRequestChannel.setMethod(KapuaMethod.READ);
96:
97: KeystoreRequestPayload keystoreRequestPayload = new KeystoreRequestPayload();
98:
99: KeystoreQueryRequestMessage keystoreRequestMessage = new KeystoreQueryRequestMessage() {
100: @Override
101: public Class<KeystoresResponseMessage> getResponseClass() {
102: return KeystoresResponseMessage.class;
103: }
104: };
105:
106: keystoreRequestMessage.setScopeId(scopeId);
107: keystoreRequestMessage.setDeviceId(deviceId);
108: keystoreRequestMessage.setCapturedOn(new Date());
109: keystoreRequestMessage.setPayload(keystoreRequestPayload);
110: keystoreRequestMessage.setChannel(keystoreRequestChannel);
111:
112: //
113: // Build request
114: DeviceCallBuilder<KeystoreRequestChannel, KeystoreRequestPayload, KeystoreQueryRequestMessage, KeystoreResponseMessage> keystoreDeviceCallBuilder =
115: DeviceCallBuilder
116: .newBuilder()
117: .withRequestMessage(keystoreRequestMessage)
118: .withTimeoutOrDefault(timeout);
119:
120: //
121: // Do get
122: KeystoreResponseMessage responseMessage;
123: try {
124: responseMessage = keystoreDeviceCallBuilder.send();
125: } catch (Exception e) {
126: LOG.error("Error while getting DeviceKeystores for Device {}. Error: {}", deviceId, e.getMessage(), e);
127: throw e;
128: }
129:
130: //
131: // Create event
132: createDeviceEvent(scopeId, deviceId, keystoreRequestMessage, responseMessage);
133:
134: //
135: // Check response
136: return checkResponseAcceptedOrThrowError(responseMessage, () -> responseMessage.getPayload().getKeystores());
137: }
138:
139: @Override
140: public DeviceKeystoreItems getKeystoreItems(KapuaId scopeId, KapuaId deviceId, Long timeout) throws KapuaException {
141: return getKeystoreItems(scopeId, deviceId, new DeviceKeystoreItemQueryImpl(), timeout);
142: }
143:
144: @Override
145: public DeviceKeystoreItems getKeystoreItems(KapuaId scopeId, KapuaId deviceId, DeviceKeystoreItemQuery itemQuery, Long timeout) throws KapuaException {
146: //
147: // Argument Validation
148: ArgumentValidator.notNull(scopeId, SCOPE_ID);
149: ArgumentValidator.notNull(deviceId, DEVICE_ID);
150: ArgumentValidator.notNull(itemQuery, "itemQuery");
151:
152: // Only one of the filter can be specified.
153: if (itemQuery.hasFilters() &&
154: !Strings.isNullOrEmpty(itemQuery.getKeystoreId()) &&
155: !Strings.isNullOrEmpty(itemQuery.getAlias())
156: ) {
157: throw new KapuaIllegalArgumentException("itemQuery.alias", itemQuery.getAlias());
158: }
159:
160: //
161: // Check Access
162: AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(DeviceManagementDomains.DEVICE_MANAGEMENT_DOMAIN, Actions.read, scopeId));
163:
164: //
165: // Prepare the request
166: KeystoreRequestChannel keystoreRequestChannel = new KeystoreRequestChannel();
167: keystoreRequestChannel.setAppName(DeviceKeystoreAppProperties.APP_NAME);
168: keystoreRequestChannel.setVersion(DeviceKeystoreAppProperties.APP_VERSION);
169: keystoreRequestChannel.setMethod(KapuaMethod.READ);
170: keystoreRequestChannel.setResource("items");
171:
172: KeystoreRequestPayload keystoreRequestPayload = new KeystoreRequestPayload();
173: try {
174: keystoreRequestPayload.setItemQuery(itemQuery);
175: } catch (Exception e) {
176: throw new DeviceManagementRequestContentException(e, itemQuery);
177: }
178:
179: KeystoreQueryRequestMessage keystoreRequestMessage = new KeystoreQueryRequestMessage() {
180: @Override
181: public Class<KeystoreItemsResponseMessage> getResponseClass() {
182: return KeystoreItemsResponseMessage.class;
183: }
184: };
185:
186: keystoreRequestMessage.setScopeId(scopeId);
187: keystoreRequestMessage.setDeviceId(deviceId);
188: keystoreRequestMessage.setCapturedOn(new Date());
189: keystoreRequestMessage.setPayload(keystoreRequestPayload);
190: keystoreRequestMessage.setChannel(keystoreRequestChannel);
191:
192: //
193: // Build request
194: DeviceCallBuilder<KeystoreRequestChannel, KeystoreRequestPayload, KeystoreQueryRequestMessage, KeystoreResponseMessage> keystoreDeviceCallBuilder =
195: DeviceCallBuilder
196: .newBuilder()
197: .withRequestMessage(keystoreRequestMessage)
198: .withTimeoutOrDefault(timeout);
199:
200: //
201: // Do get
202: KeystoreResponseMessage responseMessage;
203: try {
204: responseMessage = keystoreDeviceCallBuilder.send();
205: } catch (Exception e) {
206: LOG.error("Error while getting DeviceKeystoreItems with DeviceKeystoreQuery {} for Device {}. Error: {}", itemQuery, deviceId, e.getMessage(), e);
207: throw e;
208: }
209:
210: //
211: // Create event
212: createDeviceEvent(scopeId, deviceId, keystoreRequestMessage, responseMessage);
213:
214: //
215: // Check response
216: return checkResponseAcceptedOrThrowError(responseMessage, () -> responseMessage.getPayload().getKeystoreItems());
217: }
218:
219: @Override
220: public DeviceKeystoreItem getKeystoreItem(KapuaId scopeId, KapuaId deviceId, String keystoreId, String alias, Long timeout) throws KapuaException {
221: //
222: // Argument Validation
223: ArgumentValidator.notNull(scopeId, SCOPE_ID);
224: ArgumentValidator.notNull(deviceId, DEVICE_ID);
225: ArgumentValidator.notEmptyOrNull(keystoreId, "keystoreId");
226: ArgumentValidator.notEmptyOrNull(alias, "alias");
227:
228: //
229: // Check Access
230: AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(DeviceManagementDomains.DEVICE_MANAGEMENT_DOMAIN, Actions.read, scopeId));
231:
232: //
233: // Prepare the request
234: KeystoreRequestChannel keystoreRequestChannel = new KeystoreRequestChannel();
235: keystoreRequestChannel.setAppName(DeviceKeystoreAppProperties.APP_NAME);
236: keystoreRequestChannel.setVersion(DeviceKeystoreAppProperties.APP_VERSION);
237: keystoreRequestChannel.setMethod(KapuaMethod.READ);
238: keystoreRequestChannel.setResource("item");
239:
240: // Build query
241: DeviceKeystoreItemQuery itemQuery = new DeviceKeystoreItemQueryImpl();
242: itemQuery.setKeystoreId(keystoreId);
243: itemQuery.setAlias(alias);
244:
245: KeystoreRequestPayload keystoreRequestPayload = new KeystoreRequestPayload();
246: try {
247: keystoreRequestPayload.setItemQuery(itemQuery);
248: } catch (Exception e) {
249: throw new DeviceManagementRequestContentException(e, itemQuery);
250: }
251:
252: KeystoreQueryRequestMessage keystoreRequestMessage = new KeystoreQueryRequestMessage() {
253: @Override
254: public Class<KeystoreItemResponseMessage> getResponseClass() {
255: return KeystoreItemResponseMessage.class;
256: }
257: };
258:
259: keystoreRequestMessage.setScopeId(scopeId);
260: keystoreRequestMessage.setDeviceId(deviceId);
261: keystoreRequestMessage.setCapturedOn(new Date());
262: keystoreRequestMessage.setPayload(keystoreRequestPayload);
263: keystoreRequestMessage.setChannel(keystoreRequestChannel);
264:
265: //
266: // Build request
267: DeviceCallBuilder<KeystoreRequestChannel, KeystoreRequestPayload, KeystoreQueryRequestMessage, KeystoreResponseMessage> keystoreDeviceCallBuilder =
268: DeviceCallBuilder
269: .newBuilder()
270: .withRequestMessage(keystoreRequestMessage)
271: .withTimeoutOrDefault(timeout);
272:
273: //
274: // Do get
275: KeystoreResponseMessage responseMessage;
276: try {
277: responseMessage = keystoreDeviceCallBuilder.send();
278: } catch (Exception e) {
279: LOG.error("Error while getting DeviceKeystoreItem with alias {} in DeviceKeystore {} for Device {}. Error: {}", alias, keystoreId, deviceId, e.getMessage(), e);
280: throw e;
281: }
282:
283: //
284: // Create event
285: createDeviceEvent(scopeId, deviceId, keystoreRequestMessage, responseMessage);
286:
287: //
288: // Check response
289: return checkResponseAcceptedOrThrowError(responseMessage, () -> responseMessage.getPayload().getKeystoreItem());
290: }
291:
292: @Override
293: public void createKeystoreCertificate(KapuaId scopeId, KapuaId deviceId, String keystoreId, String alias, KapuaId certificateId, Long timeout) throws KapuaException {
294: ArgumentValidator.notNull(scopeId, SCOPE_ID);
295: ArgumentValidator.notNull(deviceId, DEVICE_ID);
296: ArgumentValidator.notEmptyOrNull(keystoreId, "keystoreId");
297: ArgumentValidator.notEmptyOrNull(alias, "alias");
298: ArgumentValidator.notNull(certificateId, "certificateId");
299:
300: //
301: // Check Certificate Info existence
302: CertificateInfo certificateInfo;
303: try {
304: certificateInfo = CERTIFICATE_INFO_SERVICE.find(scopeId, certificateId);
305: } catch (UnsupportedOperationException e) {
306: LOG.warn("Unable to get the certificate {} since the implementation does not support CertificateInfoService.find(scopeId, certificateId)... Returning KapuaEntityNotFoundException!", certificateId);
307: throw new KapuaEntityNotFoundException(CertificateInfo.TYPE, certificateId);
308: }
309:
310: if (certificateInfo == null) {
311: throw new KapuaEntityNotFoundException(CertificateInfo.TYPE, certificateId);
312: }
313:
314: //
315: // Build DeviceKeystoreCertificate create
316: DeviceKeystoreCertificate deviceKeystoreCertificate = new DeviceKeystoreCertificateImpl();
317: deviceKeystoreCertificate.setKeystoreId(keystoreId);
318: deviceKeystoreCertificate.setAlias(alias);
319: deviceKeystoreCertificate.setCertificate(certificateInfo.getCertificate());
320:
321: createKeystoreCertificate(scopeId, deviceId, deviceKeystoreCertificate, timeout);
322: }
323:
324: @Override
325: public void createKeystoreCertificate(KapuaId scopeId, KapuaId deviceId, DeviceKeystoreCertificate keystoreCertificate, Long timeout) throws KapuaException {
326: //
327: // Argument Validation
328: ArgumentValidator.notNull(scopeId, SCOPE_ID);
329: ArgumentValidator.notNull(deviceId, DEVICE_ID);
330: ArgumentValidator.notNull(keystoreCertificate, "keystoreCertificate");
331:
332: //
333: // Check Access
334: AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(DeviceManagementDomains.DEVICE_MANAGEMENT_DOMAIN, Actions.write, scopeId));
335:
336: //
337: // Prepare the request
338: KeystoreRequestChannel keystoreRequestChannel = new KeystoreRequestChannel();
339: keystoreRequestChannel.setAppName(DeviceKeystoreAppProperties.APP_NAME);
340: keystoreRequestChannel.setVersion(DeviceKeystoreAppProperties.APP_VERSION);
341: keystoreRequestChannel.setMethod(KapuaMethod.CREATE);
342: keystoreRequestChannel.setResource("certificate");
343:
344: KeystoreRequestPayload keystoreRequestPayload = new KeystoreRequestPayload();
345: try {
346: keystoreRequestPayload.setCertificate(keystoreCertificate);
347: } catch (Exception e) {
348: throw new DeviceManagementRequestContentException(e, keystoreCertificate);
349: }
350:
351: KeystoreCertificateRequestMessage keystoreRequestMessage = new KeystoreCertificateRequestMessage() {
352: @Override
353: public Class<KeystoreNoContentResponseMessage> getResponseClass() {
354: return KeystoreNoContentResponseMessage.class;
355: }
356: };
357:
358: keystoreRequestMessage.setScopeId(scopeId);
359: keystoreRequestMessage.setDeviceId(deviceId);
360: keystoreRequestMessage.setCapturedOn(new Date());
361: keystoreRequestMessage.setPayload(keystoreRequestPayload);
362: keystoreRequestMessage.setChannel(keystoreRequestChannel);
363:
364: //
365: // Build request
366: DeviceCallBuilder<KeystoreRequestChannel, KeystoreRequestPayload, KeystoreCertificateRequestMessage, KeystoreResponseMessage> keystoreDeviceCallBuilder =
367: DeviceCallBuilder
368: .newBuilder()
369: .withRequestMessage(keystoreRequestMessage)
370: .withTimeoutOrDefault(timeout);
371:
372: //
373: // Do get
374: KeystoreResponseMessage responseMessage;
375: try {
376: responseMessage = keystoreDeviceCallBuilder.send();
377: } catch (Exception e) {
378: LOG.error("Error while creating DeviceKeystoreCertificate {} for Device {}. Error: {}", keystoreCertificate, deviceId, e.getMessage(), e);
379: throw e;
380: }
381:
382: //
383: // Create event
384: createDeviceEvent(scopeId, deviceId, keystoreRequestMessage, responseMessage);
385:
386: //
387: // Check response
388: checkResponseAcceptedOrThrowError(responseMessage);
389: }
390:
391: @Override
392: public void createKeystoreKeypair(KapuaId scopeId, KapuaId deviceId, DeviceKeystoreKeypair keystoreKeypair, Long timeout) throws KapuaException {
393: //
394: // Argument Validation
395: ArgumentValidator.notNull(scopeId, SCOPE_ID);
396: ArgumentValidator.notNull(deviceId, DEVICE_ID);
397: ArgumentValidator.notNull(keystoreKeypair, "keystoreKeypair");
398:
399: //
400: // Check Access
401: AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(DeviceManagementDomains.DEVICE_MANAGEMENT_DOMAIN, Actions.write, scopeId));
402:
403: //
404: // Prepare the request
405: KeystoreRequestChannel keystoreRequestChannel = new KeystoreRequestChannel();
406: keystoreRequestChannel.setAppName(DeviceKeystoreAppProperties.APP_NAME);
407: keystoreRequestChannel.setVersion(DeviceKeystoreAppProperties.APP_VERSION);
408: keystoreRequestChannel.setMethod(KapuaMethod.CREATE);
409: keystoreRequestChannel.setResource("keypair");
410:
411: KeystoreRequestPayload keystoreRequestPayload = new KeystoreRequestPayload();
412: try {
413: keystoreRequestPayload.setKeypair(keystoreKeypair);
414: } catch (Exception e) {
415: throw new DeviceManagementRequestContentException(e, keystoreKeypair);
416: }
417:
418: KeystoreKeypairRequestMessage keystoreRequestMessage = new KeystoreKeypairRequestMessage() {
419: @Override
420: public Class<KeystoreNoContentResponseMessage> getResponseClass() {
421: return KeystoreNoContentResponseMessage.class;
422: }
423: };
424:
425: keystoreRequestMessage.setScopeId(scopeId);
426: keystoreRequestMessage.setDeviceId(deviceId);
427: keystoreRequestMessage.setCapturedOn(new Date());
428: keystoreRequestMessage.setPayload(keystoreRequestPayload);
429: keystoreRequestMessage.setChannel(keystoreRequestChannel);
430:
431: //
432: // Build request
433: DeviceCallBuilder<KeystoreRequestChannel, KeystoreRequestPayload, KeystoreKeypairRequestMessage, KeystoreResponseMessage> keystoreDeviceCallBuilder =
434: DeviceCallBuilder
435: .newBuilder()
436: .withRequestMessage(keystoreRequestMessage)
437: .withTimeoutOrDefault(timeout);
438:
439: //
440: // Do get
441: KeystoreResponseMessage responseMessage;
442: try {
443: responseMessage = keystoreDeviceCallBuilder.send();
444: } catch (Exception e) {
445: LOG.error("Error while creating DeviceKeystoreKeypair {} for Device {}. Error: {}", keystoreKeypair, deviceId, e.getMessage(), e);
446: throw e;
447: }
448:
449: //
450: // Create event
451: createDeviceEvent(scopeId, deviceId, keystoreRequestMessage, responseMessage);
452:
453: //
454: // Check response
455: checkResponseAcceptedOrThrowError(responseMessage);
456: }
457:
458: @Override
459: public DeviceKeystoreCSR createKeystoreCSR(KapuaId scopeId, KapuaId deviceId, DeviceKeystoreCSRInfo keystoreCSRInfo, Long timeout) throws KapuaException {
460: //
461: // Argument Validation
462: ArgumentValidator.notNull(scopeId, SCOPE_ID);
463: ArgumentValidator.notNull(deviceId, DEVICE_ID);
464: ArgumentValidator.notNull(keystoreCSRInfo, "keystoreCSRInfo");
465:
466: //
467: // Check Access
468: AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(DeviceManagementDomains.DEVICE_MANAGEMENT_DOMAIN, Actions.read, scopeId));
469:
470: //
471: // Prepare the request
472: KeystoreRequestChannel keystoreRequestChannel = new KeystoreRequestChannel();
473: keystoreRequestChannel.setAppName(DeviceKeystoreAppProperties.APP_NAME);
474: keystoreRequestChannel.setVersion(DeviceKeystoreAppProperties.APP_VERSION);
475: keystoreRequestChannel.setMethod(KapuaMethod.CREATE);
476: keystoreRequestChannel.setResource("csr");
477:
478: KeystoreRequestPayload keystoreRequestPayload = new KeystoreRequestPayload();
479: try {
480: keystoreRequestPayload.setCsrInfo(keystoreCSRInfo);
481: } catch (Exception e) {
482: throw new DeviceManagementRequestContentException(e, keystoreCSRInfo);
483: }
484:
485: KeystoreCsrRequestMessage keystoreRequestMessage = new KeystoreCsrRequestMessage() {
486: @Override
487: public Class<KeystoreCsrResponseMessage> getResponseClass() {
488: return KeystoreCsrResponseMessage.class;
489: }
490: };
491:
492: keystoreRequestMessage.setScopeId(scopeId);
493: keystoreRequestMessage.setDeviceId(deviceId);
494: keystoreRequestMessage.setCapturedOn(new Date());
495: keystoreRequestMessage.setPayload(keystoreRequestPayload);
496: keystoreRequestMessage.setChannel(keystoreRequestChannel);
497:
498: //
499: // Build request
500: DeviceCallBuilder<KeystoreRequestChannel, KeystoreRequestPayload, KeystoreCsrRequestMessage, KeystoreResponseMessage> keystoreDeviceCallBuilder =
501: DeviceCallBuilder
502: .newBuilder()
503: .withRequestMessage(keystoreRequestMessage)
504: .withTimeoutOrDefault(timeout);
505:
506: //
507: // Do get
508: KeystoreResponseMessage responseMessage;
509: try {
510: responseMessage = keystoreDeviceCallBuilder.send();
511: } catch (Exception e) {
512: LOG.error("Error while creating DeviceKeystoreCsrInfo {} for Device {}. Error: {}", keystoreCSRInfo, deviceId, e.getMessage(), e);
513: throw e;
514: }
515:
516: //
517: // Create event
518: createDeviceEvent(scopeId, deviceId, keystoreRequestMessage, responseMessage);
519:
520: //
521: // Check response
522: return checkResponseAcceptedOrThrowError(responseMessage, () -> responseMessage.getPayload().getCSR());
523: }
524:
525: @Override
526: public void deleteKeystoreItem(KapuaId scopeId, KapuaId deviceId, String keystoreId, String alias, Long timeout) throws KapuaException {
527: //
528: // Argument Validation
529: ArgumentValidator.notNull(scopeId, SCOPE_ID);
530: ArgumentValidator.notNull(deviceId, DEVICE_ID);
531: ArgumentValidator.notEmptyOrNull(keystoreId, "keystoreId");
532: ArgumentValidator.notEmptyOrNull(alias, "alias");
533:
534: //
535: // Check Access
536: AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(DeviceManagementDomains.DEVICE_MANAGEMENT_DOMAIN, Actions.delete, scopeId));
537:
538: //
539: // Prepare the request
540: KeystoreRequestChannel keystoreRequestChannel = new KeystoreRequestChannel();
541: keystoreRequestChannel.setAppName(DeviceKeystoreAppProperties.APP_NAME);
542: keystoreRequestChannel.setVersion(DeviceKeystoreAppProperties.APP_VERSION);
543: keystoreRequestChannel.setMethod(KapuaMethod.DELETE);
544: keystoreRequestChannel.setResource("items");
545:
546: // Build query
547: DeviceKeystoreItemQuery itemQuery = new DeviceKeystoreItemQueryImpl();
548: itemQuery.setKeystoreId(keystoreId);
549: itemQuery.setAlias(alias);
550:
551: KeystoreRequestPayload keystoreRequestPayload = new KeystoreRequestPayload();
552: try {
553: keystoreRequestPayload.setItemQuery(itemQuery);
554: } catch (Exception e) {
555: throw new DeviceManagementRequestContentException(e, itemQuery);
556: }
557:
558: KeystoreQueryRequestMessage keystoreRequestMessage = new KeystoreQueryRequestMessage() {
559: @Override
560: public Class<KeystoreNoContentResponseMessage> getResponseClass() {
561: return KeystoreNoContentResponseMessage.class;
562: }
563: };
564:
565: keystoreRequestMessage.setScopeId(scopeId);
566: keystoreRequestMessage.setDeviceId(deviceId);
567: keystoreRequestMessage.setCapturedOn(new Date());
568: keystoreRequestMessage.setPayload(keystoreRequestPayload);
569: keystoreRequestMessage.setChannel(keystoreRequestChannel);
570:
571: //
572: // Build request
573: DeviceCallBuilder<KeystoreRequestChannel, KeystoreRequestPayload, KeystoreQueryRequestMessage, KeystoreResponseMessage> keystoreDeviceCallBuilder =
574: DeviceCallBuilder
575: .newBuilder()
576: .withRequestMessage(keystoreRequestMessage)
577: .withTimeoutOrDefault(timeout);
578:
579: //
580: // Do get
581: KeystoreResponseMessage responseMessage;
582: try {
583: responseMessage = keystoreDeviceCallBuilder.send();
584: } catch (Exception e) {
585: LOG.error("Error while deleting DeviceKeystoreItem with alias {} in DeviceKeystore {} for Device {}. Error: {}", alias, keystoreId, deviceId, e.getMessage(), e);
586: throw e;
587: }
588:
589: //
590: // Create event
591: createDeviceEvent(scopeId, deviceId, keystoreRequestMessage, responseMessage);
592:
593: //
594: // Check response
595: checkResponseAcceptedOrThrowError(responseMessage);
596: }
597: }