Refactor logging to use slf4j
[iotivity.git] / cloud / account / src / main / java / org / iotivity / cloud / accountserver / resources / credprov / crl / CrlResource.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.crl;
23
24 import static org.iotivity.cloud.accountserver.resources.credprov.cert.CertificateConstants.BASE_64;
25 import static org.iotivity.cloud.accountserver.resources.credprov.crl.CrlManager.CRL_MANAGER;
26
27 import java.io.IOException;
28 import java.security.cert.CRLException;
29 import java.text.ParseException;
30 import java.text.SimpleDateFormat;
31 import java.util.Arrays;
32 import java.util.Date;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.bouncycastle.operator.OperatorCreationException;
40 import org.bouncycastle.util.encoders.Base64;
41 import org.bouncycastle.util.encoders.DecoderException;
42 import org.iotivity.cloud.accountserver.Constants;
43 import org.iotivity.cloud.base.device.Device;
44 import org.iotivity.cloud.base.exception.ServerException;
45 import org.iotivity.cloud.base.protocols.IRequest;
46 import org.iotivity.cloud.base.protocols.IResponse;
47 import org.iotivity.cloud.base.protocols.MessageBuilder;
48 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
49 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
50 import org.iotivity.cloud.base.resource.Resource;
51 import org.iotivity.cloud.util.Cbor;
52
53 /**
54  * Class is used working with POST and GET requests and handles CRL requests.
55  */
56 public class CrlResource extends Resource {
57     private final static Logger                     Log         = LoggerFactory.getLogger(CrlResource.class);
58
59     /**
60      * CBOR container with help of map presentation.
61      */
62     private static final Cbor<Map<String, Object>>  MAP_CBOR    = new Cbor<>();
63
64     /**
65      * Creates resource for handling CRL requests(GET and POST)
66      */
67     public CrlResource() {
68         super(Arrays.asList(Constants.PREFIX_OIC, Constants.CREDPROV_URI,
69                 Constants.REQ_CRL));
70
71     }
72
73     @Override
74     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
75             throws ServerException {
76         IResponse response;
77         switch (request.getMethod()) {
78             case POST:
79                 response = handlePostRequest(request);
80                 break;
81             case GET:
82                 response = handleGetRequest(request);
83                 break;
84             default:
85                 response = MessageBuilder.createResponse(request,
86                         ResponseStatus.BAD_REQUEST);
87         }
88         srcDevice.sendResponse(response);
89     }
90
91     /**
92      * Method handles GET requests with specified format: GET
93      * /oic/credprov/crl?lu=20170701000000 Checks if “lu” value is not after the
94      * latest update. If so, response with the latest CRL, otherwise response
95      * error (e.g. 4.04 Not Found) And response of next format: 2.05 CONTENTS {
96      * “tu” : “20160711000000”, “nu” : “20161011000000”, “crl” : { “encoding” :
97      * “oic.sec.encoding.base64”, “data” : “<Base64 encoded CRL Binary>” } }
98      */
99     private IResponse handleGetRequest(IRequest request)
100             throws ServerException {
101         HashMap<String, List<String>> queryData = request.getUriQueryMap();
102         IResponse iResponse = MessageBuilder.createResponse(request,
103                 ResponseStatus.NOT_FOUND);
104         if (queryData != null) {
105             List<String> lastUpdateList = queryData
106                     .get(Constants.REQ_LAST_UPDATE);
107             if (lastUpdateList != null && !lastUpdateList.isEmpty()) {
108                 try {
109                     Map<String, Object> payload = CRL_MANAGER
110                             .getPayload(lastUpdateList.get(0));
111                     if (!payload.isEmpty()) {
112                         iResponse = MessageBuilder.createResponse(request,
113                                 ResponseStatus.CONTENT,
114                                 ContentFormat.APPLICATION_CBOR,
115                                 MAP_CBOR.encodingPayloadToCbor(payload));
116                     }
117                 } catch (CRLException e) {
118                     Log.error(e.getMessage());
119                 }
120             }
121         }
122         return iResponse;
123     }
124
125     /**
126      * Handles POST requests of next formats: POST /oic/credprov/crl { “tu” :
127      * “20160727000000”, “nu” : “20161027000000”, “rcsn” : “123456” } AND POST
128      * /oic/credprov/crl { “tu” : “20160727000000”, “nu” : “20161027000000”,
129      * “crl” : { “encoding” : “oic.sec.encoding.base64”, “data” : “<Base64
130      * encoded New CRL Binary>” } } And responds back with 2.04 CHANGED if
131      * everything is ok, and PRECONDITION_FAILED - otherwise
132      */
133     private IResponse handlePostRequest(IRequest request)
134             throws ServerException {
135         byte[] requestPayload = request.getPayload();
136         IResponse response = MessageBuilder.createResponse(request,
137                 ResponseStatus.PRECONDITION_FAILED);
138         if (requestPayload != null) {
139             Map<String, Object> payloadData = MAP_CBOR
140                     .parsePayloadFromCbor(request.getPayload(), HashMap.class);
141             if (payloadData != null) {
142                 Object thisUpdate = payloadData.get(Constants.REQ_THIS_UPDATE);
143                 Object nextUpdate = payloadData.get(Constants.REQ_NEXT_UPDATE);
144                 if (thisUpdate != null && thisUpdate instanceof String
145                         && nextUpdate != null && nextUpdate instanceof String) {
146                     Date thisUpdateDate;
147                     try {
148                         thisUpdateDate = new SimpleDateFormat("yyyyMMddHHmmss")
149                                 .parse(thisUpdate.toString());
150                         Object reqSerialNumber = payloadData
151                                 .get(Constants.REQ_SERIAL_NUMBER);
152                         Object crl = payloadData.get(Constants.REQ_CRL);
153                         if (reqSerialNumber != null
154                                 && reqSerialNumber instanceof List) {
155                             CRL_MANAGER.revoke(((List<String>) reqSerialNumber)
156                                     .toArray(new String[] {}));
157                             response = MessageBuilder.createResponse(request,
158                                     ResponseStatus.CHANGED);
159                         } else if (crl != null && crl instanceof Map) {
160                             Object encoding = ((Map<String, Object>) crl)
161                                     .get(Constants.ENCODING);
162                             Object crlData = ((Map<String, Object>) crl)
163                                     .get(Constants.DATA);
164                             if (encoding != null && encoding instanceof String
165                                     && crlData != null
166                                     && crlData instanceof byte[]) {
167                                 try {
168                                     if (encoding.equals(BASE_64)) {
169                                         crlData = Base64
170                                                 .decode((byte[]) crlData);
171                                     }
172                                     CRL_MANAGER.update(thisUpdateDate,
173                                             (byte[]) crlData);
174                                     response = MessageBuilder.createResponse(
175                                             request, ResponseStatus.CHANGED);
176                                 } catch (DecoderException e) {
177                                     Log.error(e.getMessage() + e.getClass());
178                                 }
179                             }
180                         }
181                     } catch (CRLException | IOException
182                             | OperatorCreationException | ParseException e) {
183                         Log.error(e.getMessage() + e.getClass());
184                     }
185                 }
186             }
187         }
188         return response;
189     }
190 }