Skip to content

Package: TranslatorAppKeystoreCsrKuraKapua

TranslatorAppKeystoreCsrKuraKapua

nameinstructionbranchcomplexitylinemethod
TranslatorAppKeystoreCsrKuraKapua()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
patchForKuraKeystoreCSR(KuraResponsePayload)
M: 31 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
translatePayload(KuraResponsePayload)
M: 35 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 10 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.translator.kura.kapua.keystore;
14:
15: import org.eclipse.kapua.service.device.call.kura.model.keystore.KuraKeystoreCSR;
16: import org.eclipse.kapua.service.device.call.message.kura.app.response.KuraResponsePayload;
17: import org.eclipse.kapua.service.device.management.keystore.internal.message.response.KeystoreCsrResponseMessage;
18: import org.eclipse.kapua.service.device.management.keystore.internal.message.response.KeystoreResponsePayload;
19: import org.eclipse.kapua.translator.Translator;
20: import org.eclipse.kapua.translator.exception.InvalidPayloadException;
21: import org.slf4j.Logger;
22: import org.slf4j.LoggerFactory;
23:
24: /**
25: * {@link Translator} implementation from {@link KeystoreCsrResponseMessage} to {@link KeystoreCsrResponseMessage}
26: *
27: * @since 1.5.0
28: */
29: public class TranslatorAppKeystoreCsrKuraKapua extends AbstractTranslatorAppKeystoreKuraKapua<KeystoreCsrResponseMessage> {
30:
31: private static final Logger LOG = LoggerFactory.getLogger(TranslatorAppKeystoreCsrKuraKapua.class);
32:
33: /**
34: * Constructor.
35: *
36: * @since 1.5.0
37: */
38: public TranslatorAppKeystoreCsrKuraKapua() {
39: super(KeystoreCsrResponseMessage.class);
40: }
41:
42: @Override
43: protected KeystoreResponsePayload translatePayload(KuraResponsePayload kuraResponsePayload) throws InvalidPayloadException {
44: try {
45: KeystoreResponsePayload keystoreResponsePayload = super.translatePayload(kuraResponsePayload);
46:
47:• if (kuraResponsePayload.hasBody()) {
48: KuraKeystoreCSR kuraKeystoreCSR;
49: try {
50: kuraKeystoreCSR = readJsonBodyAs(kuraResponsePayload.getBody(), KuraKeystoreCSR.class);
51: } catch (Exception e) {
52: kuraKeystoreCSR = patchForKuraKeystoreCSR(kuraResponsePayload);
53: }
54:
55: keystoreResponsePayload.setCSR(translate(kuraKeystoreCSR));
56: }
57:
58: return keystoreResponsePayload;
59: } catch (Exception e) {
60: throw new InvalidPayloadException(e, kuraResponsePayload);
61: }
62: }
63:
64: /**
65: * This method fixes the issue with the {@link KuraKeystoreCSR}.
66: * <p>
67: * See https://github.com/eclipse/kura/issues/3387 for more info.
68: *
69: * @param kuraResponsePayload The {@link KuraKeystoreCSR} that cannot be {@link #readJsonBodyAs(byte[], Class)} {@link KuraKeystoreCSR}.
70: * @return The {@link KuraKeystoreCSR} parsed with the alternative format.
71: * @since 1.5.0
72: */
73: private KuraKeystoreCSR patchForKuraKeystoreCSR(KuraResponsePayload kuraResponsePayload) {
74: LOG.warn("KEYS-V1/POST/keystore/entries/csr returned not a JSON body... Trying reading body as a String...");
75:
76: String kuraKeystoreCsrString = new String(kuraResponsePayload.getBody());
77: kuraKeystoreCsrString = kuraKeystoreCsrString.replace("\"", "");
78: kuraKeystoreCsrString = kuraKeystoreCsrString.replace("\\n", "\n");
79:
80: KuraKeystoreCSR kuraKeystoreCSR = new KuraKeystoreCSR();
81: kuraKeystoreCSR.setSigningRequest(kuraKeystoreCsrString);
82:
83: LOG.warn("KEYS-V1/POST/keystore/entries/csr returned not a JSON body... Trying reading body as a String... DONE!");
84: return kuraKeystoreCSR;
85: }
86: }