2 * //******************************************************************
4 * // Copyright 2016 Samsung Electronics All Rights Reserved.
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
12 * // http://www.apache.org/licenses/LICENSE-2.0
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.
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 package org.iotivity.cloud.accountserver.resources.credprov.cert;
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;
28 import java.math.BigInteger;
29 import java.util.Date;
30 import java.util.HashMap;
31 import java.util.List;
34 import static org.iotivity.cloud.accountserver.Constants.*;
35 import static org.iotivity.cloud.accountserver.resources.credprov.cert.CertificateConstants.ACCOUNT_DB_MANAGER;
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.
45 final class CertificateManager {
48 * Type casting manager for converting CertificateTable object tot map
50 private static final TypeCastingManager<CertificateTable> CERTIFICATE_TABLE_TYPE_CASTING_MANAGER =
51 new TypeCastingManager<>();
54 * Class attribute as payload for response.
56 private final Map<String, Object> payLoad;
59 * Class attribute to store device id.
61 private final String deviceId;
64 * Constructs certificateMananger with specified device id. Initialize payload
65 * as new hash map instance.
67 * @param deviceId specified device identifier for this CertificateManager.
69 CertificateManager(String deviceId) {
70 payLoad = new HashMap<>();
71 this.deviceId = deviceId;
75 * Returns payload for response.
77 Map<String, Object> getPayLoad() {
82 * Puts specified value for specified key to payload;
84 * @param key specified String key value
85 * @param value specified Object value
87 public void put(String key, Object value) {
88 payLoad.put(key, value);
92 * Saves certificate information: serial number, not after, to DB with specified columns.
94 * @param serialNumber specified certificate serial number
95 * @param notAfter validation date not after
96 * @param notBefore validation date not before
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)));
106 * Updates certificate table by revoked column.
108 * @param certificateTable certificate to be updated.
109 * @param revoked specified value for revoke
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));
118 * Returns certificate from database for specified in constructor
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
131 new CertificateTable());