Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / cloud / stack / src / main / java / org / iotivity / cloud / base / protocols / coap / CoapRequest.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.base.protocols.coap;
23
24 import java.nio.charset.StandardCharsets;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import org.iotivity.cloud.base.protocols.coap.enums.CoapMethod;
29
30 public class CoapRequest extends CoapMessage {
31     public CoapRequest(CoapMethod method) {
32         this.code = method.getCode();
33     }
34
35     public CoapMethod getRequestMethod() {
36         return CoapMethod.valueOf(code);
37     }
38
39     public void setUriPath(String path) {
40         if (uri_path == null) {
41             uri_path = new ArrayList<byte[]>();
42         }
43         clearUriPath();
44
45         String[] pathSegments = path.split("/");
46         for (String pathSegment : pathSegments) {
47             if (pathSegment.length() == 0)
48                 continue;
49             uri_path.add(pathSegment.getBytes(StandardCharsets.UTF_8));
50         }
51     }
52
53     public String getUriPath() {
54         if (uri_path == null) {
55             return null;
56         }
57         StringBuilder path = new StringBuilder();
58         int nItem = uri_path.size();
59         for (byte[] pathSegment : uri_path) {
60             path.append(new String(pathSegment, StandardCharsets.UTF_8));
61             if (--nItem > 0) {
62                 path.append('/');
63             }
64         }
65         return path.toString();
66     }
67
68     public List<String> getUriPathSegments() {
69         if (uri_path == null) {
70             return null;
71         }
72         List<String> segments = new ArrayList<String>();
73         for (byte[] pathSegment : uri_path) {
74             segments.add(new String(pathSegment, StandardCharsets.UTF_8));
75         }
76         return segments;
77     }
78
79     public void setUriQuery(String query) {
80         if (uri_query == null) {
81             uri_query = new ArrayList<byte[]>();
82         }
83         String[] querySegments = query.split("&");
84         for (String querySegment : querySegments) {
85             uri_query.add(querySegment.getBytes(StandardCharsets.UTF_8));
86         }
87     }
88
89     public String getUriQuery() {
90         if (uri_query == null) {
91             return null;
92         }
93         StringBuilder uriQuery = new StringBuilder();
94         int nItem = uri_query.size();
95         for (byte[] querySegment : uri_query) {
96             uriQuery.append(new String(querySegment, StandardCharsets.UTF_8));
97             if (--nItem > 0) {
98                 uriQuery.append('&');
99             }
100         }
101         return uriQuery.toString();
102     }
103
104     public List<String> getUriQuerySegments() {
105         if (uri_query == null) {
106             return null;
107         }
108         List<String> segments = new ArrayList<String>();
109         for (byte[] querySegment : uri_query) {
110             segments.add(new String(querySegment, StandardCharsets.UTF_8));
111         }
112         return segments;
113     }
114
115     public void clearUriPath() {
116         if (uri_path != null) {
117             uri_path.clear();
118         }
119     }
120 }