[Simulator] Minor UI changes fixing the IOT-1087.
[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         List<String> segments = new ArrayList<String>();
70         if (uri_path != null) {
71             for (byte[] pathSegment : uri_path) {
72                 segments.add(new String(pathSegment, StandardCharsets.UTF_8));
73             }
74         }
75         return segments;
76     }
77
78     public void setUriQuery(String query) {
79         if (uri_query == null) {
80             uri_query = new ArrayList<byte[]>();
81         }
82         String[] querySegments = query.split("&");
83         for (String querySegment : querySegments) {
84             uri_query.add(querySegment.getBytes(StandardCharsets.UTF_8));
85         }
86     }
87
88     public String getUriQuery() {
89         if (uri_query == null) {
90             return null;
91         }
92         StringBuilder uriQuery = new StringBuilder();
93         int nItem = uri_query.size();
94         for (byte[] querySegment : uri_query) {
95             uriQuery.append(new String(querySegment, StandardCharsets.UTF_8));
96             if (--nItem > 0) {
97                 uriQuery.append('&');
98             }
99         }
100         return uriQuery.toString();
101     }
102
103     public List<String> getUriQuerySegments() {
104         List<String> segments = new ArrayList<String>();
105         for (byte[] querySegment : uri_query) {
106             segments.add(new String(querySegment, StandardCharsets.UTF_8));
107         }
108         return segments;
109     }
110
111     public void clearUriPath() {
112         uri_path.clear();
113     }
114
115     public void setAccept(byte[] accepts) {
116         accept = accepts;
117     }
118 }