Refactor logging to use slf4j
[iotivity.git] / cloud / resourcedirectory / src / main / java / org / iotivity / cloud / rdserver / resources / directory / rd / ResourceDirectoryResource.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.rdserver.resources.directory.rd;
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.List;
28
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.iotivity.cloud.base.device.Device;
32 import org.iotivity.cloud.base.exception.ServerException;
33 import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
34 import org.iotivity.cloud.base.exception.ServerException.PreconditionFailedException;
35 import org.iotivity.cloud.base.protocols.IRequest;
36 import org.iotivity.cloud.base.protocols.IResponse;
37 import org.iotivity.cloud.base.protocols.MessageBuilder;
38 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
39 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
40 import org.iotivity.cloud.base.resource.Resource;
41 import org.iotivity.cloud.rdserver.Constants;
42 import org.iotivity.cloud.rdserver.resources.directory.RDManager;
43 import org.iotivity.cloud.rdserver.resources.presence.PresenceManager;
44 import org.iotivity.cloud.util.Cbor;
45
46 /**
47  * 
48  * This class provides a set of APIs handle requests about resource directory
49  *
50  */
51 public class ResourceDirectoryResource extends Resource {
52     private final static Logger Log                  = LoggerFactory.getLogger(ResourceDirectoryResource.class);
53     private Cbor<HashMap<String, Object>> mCbor      = new Cbor<>();
54     private RDManager                     mRdManager = new RDManager();
55
56     public ResourceDirectoryResource() {
57         super(Arrays.asList(Constants.PREFIX_OIC, Constants.RD_URI));
58     }
59
60     @Override
61     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
62             throws ServerException {
63
64         IResponse response = null;
65
66         switch (request.getMethod()) {
67             case POST:
68                 response = handlePostRequest(request);
69                 break;
70
71             case DELETE:
72                 response = handleDeleteRequest(request);
73                 break;
74
75             default:
76                 throw new BadRequestException(
77                         request.getMethod() + " request type is not supported");
78         }
79
80         srcDevice.sendResponse(response);
81
82         PresenceManager.getInstance()
83                 .notifyToObservers(mRdManager.getmResourcePresence());
84
85     }
86
87     private void checkLinksProperty(HashMap<String, Object> payload) {
88
89         Object links = payload.get(Constants.LINKS);
90         if (links == null) {
91             throw new BadRequestException("links property is null");
92         }
93         ArrayList<HashMap<String, Object>> linksList = (ArrayList<HashMap<String, Object>>) links;
94         for (HashMap<String, Object> link : linksList) {
95             checkPayloadException(
96                     Arrays.asList(Constants.HREF, Constants.RESOURCE_TYPE,
97                             Constants.INS, Constants.INTERFACE),
98                     link);
99         }
100     }
101
102     private IResponse handlePostRequest(IRequest request)
103             throws ServerException {
104
105         HashMap<String, Object> payload = mCbor
106                 .parsePayloadFromCbor(request.getPayload(), HashMap.class);
107
108         Log.debug("publish payload : " + payload);
109
110         checkPayloadException(
111                 Arrays.asList(Constants.DEVICE_ID, Constants.LINKS), payload);
112
113         // check mandatory property
114         checkLinksProperty(payload);
115
116         HashMap<String, Object> response = mRdManager.publishResource(payload);
117
118         Log.debug("publish response : " + response);
119
120         return MessageBuilder.createResponse(request, ResponseStatus.CHANGED,
121                 ContentFormat.APPLICATION_CBOR,
122                 mCbor.encodingPayloadToCbor(response));
123
124     }
125
126     private IResponse handleDeleteRequest(IRequest request)
127             throws ServerException {
128
129         HashMap<String, List<String>> queryMap = request.getUriQueryMap();
130
131         checkQueryException(Arrays.asList(Constants.DEVICE_ID), queryMap);
132
133         List<String> diList = queryMap.get(Constants.DEVICE_ID);
134         List<String> insList = queryMap.get(Constants.INS);
135
136         mRdManager.deleteResource(diList.get(0), insList);
137
138         IResponse response = MessageBuilder.createResponse(request,
139                 ResponseStatus.DELETED);
140
141         return response;
142     }
143
144 }