Imported Upstream version 1.2.0
[platform/upstream/iotivity.git] / cloud / account / src / main / java / org / iotivity / cloud / accountserver / resources / credprov / cert / CertificateManager.java
1 /*
2  * //******************************************************************
3  * //
4  * // Copyright 2016 Samsung Electronics All Rights Reserved.
5  * //
6  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  * //
8  * // Licensed under the Apache License, Version 2.0 (the "License");
9  * // you may not use this file except in compliance with the License.
10  * // You may obtain a copy of the License at
11  * //
12  * //      http://www.apache.org/licenses/LICENSE-2.0
13  * //
14  * // Unless required by applicable law or agreed to in writing, software
15  * // distributed under the License is distributed on an "AS IS" BASIS,
16  * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * // See the License for the specific language governing permissions and
18  * // limitations under the License.
19  * //
20  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22 package org.iotivity.cloud.accountserver.resources.credprov.cert;
23
24 import org.iotivity.cloud.accountserver.db.CertificateTable;
25 import org.iotivity.cloud.accountserver.util.TypeCastingManager;
26 import org.iotivity.cloud.accountserver.x509.cert.Utility;
27
28 import java.math.BigInteger;
29 import java.util.Date;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33
34 import static org.iotivity.cloud.accountserver.Constants.*;
35 import static org.iotivity.cloud.accountserver.resources.credprov.cert.CertificateConstants.ACCOUNT_DB_MANAGER;
36
37
38 /**
39  * This class is used as DB manager for CertificateTable.
40  * With help of this class we can save certificate info to DB,
41  * retrieve it from DB by specified device id,
42  * update X509 certificate, also it helps us to get user Id from Token
43  * Table by specified device id.
44  */
45 final class CertificateManager {
46
47     /**
48      * Type casting manager for converting CertificateTable object tot map
49      */
50     private static final TypeCastingManager<CertificateTable> CERTIFICATE_TABLE_TYPE_CASTING_MANAGER =
51             new TypeCastingManager<>();
52
53     /**
54      * Class attribute as payload for response.
55      */
56     private final Map<String, Object> payLoad;
57
58     /**
59      * Class attribute to store device id.
60      */
61     private final String deviceId;
62
63     /**
64      * Constructs certificateMananger with specified device id. Initialize payload
65      * as new hash map instance.
66      *
67      * @param deviceId specified device identifier for this CertificateManager.
68      */
69     CertificateManager(String deviceId) {
70         payLoad = new HashMap<>();
71         this.deviceId = deviceId;
72     }
73
74     /**
75      * Returns payload for response.
76      */
77     Map<String, Object> getPayLoad() {
78         return payLoad;
79     }
80
81     /**
82      * Puts specified value for specified key to payload;
83      *
84      * @param key   specified String key value
85      * @param value specified Object value
86      */
87     public void put(String key, Object value) {
88         payLoad.put(key, value);
89     }
90
91     /**
92      * Saves certificate information: serial number, not after, to DB with specified columns.
93      *
94      * @param serialNumber specified certificate serial number
95      * @param notAfter     validation date not after
96      * @param notBefore    validation date not before
97      */
98     void save(BigInteger serialNumber, Date notAfter, Date notBefore) {
99         ACCOUNT_DB_MANAGER.insertRecord(CERTIFICATE_TABLE,
100                 CERTIFICATE_TABLE_TYPE_CASTING_MANAGER.convertObjectToMap(
101                         new CertificateTable(serialNumber.toString(), notAfter,
102                                 notBefore, deviceId, Utility.getUserID(deviceId), false)));
103     }
104
105     /**
106      * Updates certificate table by revoked column.
107      *
108      * @param certificateTable certificate to be updated.
109      * @param revoked          specified value for revoke
110      */
111     void update(CertificateTable certificateTable, boolean revoked) {
112         certificateTable.setRevoked(revoked);
113         ACCOUNT_DB_MANAGER.updateRecord(CERTIFICATE_TABLE,
114                 CERTIFICATE_TABLE_TYPE_CASTING_MANAGER.convertObjectToMap(certificateTable));
115     }
116
117     /**
118      * Returns certificate from database for specified in constructor
119      * device id.
120      */
121     public CertificateTable getCertificate() {
122         HashMap<String, Object> condition = new HashMap<>();
123         condition.put(KEYFIELD_DID, deviceId);
124         condition.put(KEYFIELD_REVOKED, false);
125         List<HashMap<String, Object>> listMap = ACCOUNT_DB_MANAGER.selectRecord(
126                 CERTIFICATE_TABLE, condition);
127         if (listMap != null && !listMap.isEmpty()) {
128             return CERTIFICATE_TABLE_TYPE_CASTING_MANAGER
129                     .convertMaptoObject(
130                             listMap.get(0),
131                             new CertificateTable());
132         } else {
133             return null;
134         }
135     }
136 }