2 * //******************************************************************
\r
4 * // Copyright 2015 Intel Corporation.
\r
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
8 * // Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * // you may not use this file except in compliance with the License.
\r
10 * // You may obtain a copy of the License at
\r
12 * // http://www.apache.org/licenses/LICENSE-2.0
\r
14 * // Unless required by applicable law or agreed to in writing, software
\r
15 * // distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * // See the License for the specific language governing permissions and
\r
18 * // limitations under the License.
\r
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
23 package org.iotivity.base;
\r
25 import java.util.List;
\r
26 import java.util.Map;
\r
29 * OcResource represents an OC resource. A resource could be a light controller, temperature sensor,
\r
30 * smoke detector, etc. A resource comes with a well-defined contract or interface onto which you
\r
31 * can perform different operations, such as turning on the light, getting the current temperature
\r
32 * or subscribing for event notifications from the smoke detector. A resource can be composed of
\r
33 * one or more resources.
\r
35 public class OcResource {
\r
37 private OcResource(long nativeHandle) {
\r
38 this.mNativeHandle = nativeHandle;
\r
42 * Method to get the attributes of a resource.
\r
44 * @param queryParamsMap map which can have the query parameter name and value
\r
45 * @param onGetListener The event handler will be invoked with a map of attribute name and
\r
46 * values. The event handler will also have the result from this Get
\r
47 * operation This will have error codes
\r
48 * @throws OcException
\r
50 public native void get(Map<String, String> queryParamsMap,
\r
51 OnGetListener onGetListener) throws OcException;
\r
54 * Method to get the attributes of a resource.
\r
56 * @param queryParamsMap map which can have the query parameter name and value
\r
57 * @param onGetListener The event handler will be invoked with a map of attribute name and
\r
58 * values. The event handler will also have the result from this Get
\r
59 * operation This will have error codes
\r
60 * @param qualityOfService the quality of communication
\r
61 * @throws OcException
\r
63 public void get(Map<String, String> queryParamsMap,
\r
64 OnGetListener onGetListener,
\r
65 QualityOfService qualityOfService) throws OcException {
\r
66 this.get1(queryParamsMap, onGetListener, qualityOfService.getValue());
\r
69 private native void get1(Map<String, String> queryParamsMap,
\r
70 OnGetListener onGetListener,
\r
71 int qualityOfService) throws OcException;
\r
74 * Method to get the attributes of a resource.
\r
76 * @param resourceType resourceType of the resource to operate on
\r
77 * @param resourceInterface interface type of the resource to operate on
\r
78 * @param queryParamsMap map which can have the query parameter name and value
\r
79 * @param onGetListener The event handler will be invoked with a map of attribute name and
\r
80 * values. The event handler will also have the result from this Get
\r
81 * operation This will have error codes
\r
82 * @throws OcException
\r
84 public void get(String resourceType,
\r
85 String resourceInterface,
\r
86 Map<String, String> queryParamsMap,
\r
87 OnGetListener onGetListener) throws OcException {
\r
95 private native void get2(String resourceType,
\r
96 String resourceInterface,
\r
97 Map<String, String> queryParamsMap,
\r
98 OnGetListener onGetListener) throws OcException;
\r
101 * Method to get the attributes of a resource.
\r
103 * @param resourceType resourceType of the resource to operate on
\r
104 * @param resourceInterface interface type of the resource to operate on
\r
105 * @param queryParamsMap map which can have the query parameter name and value
\r
106 * @param onGetListener The event handler will be invoked with a map of attribute name and
\r
107 * values. The event handler will also have the result from this Get
\r
108 * operation This will have error codes
\r
109 * @param qualityOfService the quality of communication
\r
110 * @throws OcException
\r
112 public void get(String resourceType,
\r
113 String resourceInterface,
\r
114 Map<String, String> queryParamsMap,
\r
115 OnGetListener onGetListener,
\r
116 QualityOfService qualityOfService) throws OcException {
\r
122 qualityOfService.getValue());
\r
125 private native void get3(String resourceType,
\r
126 String resourceInterface,
\r
127 Map<String, String> queryParamsMap,
\r
128 OnGetListener onGetListener,
\r
129 int qualityOfService) throws OcException;
\r
132 * Method to set the representation of a resource (via PUT)
\r
134 * @param representation representation of the resource
\r
135 * @param queryParamsMap Map which can have the query parameter name and value
\r
136 * @param onPutListener event handler The event handler will be invoked with a map of attribute
\r
138 * @throws OcException
\r
140 public native void put(OcRepresentation representation,
\r
141 Map<String, String> queryParamsMap,
\r
142 OnPutListener onPutListener) throws OcException;
\r
145 * Method to set the representation of a resource (via PUT)
\r
147 * @param ocRepresentation representation of the resource
\r
148 * @param queryParamsMap Map which can have the query parameter name and value
\r
149 * @param onPutListener event handler The event handler will be invoked with a map of
\r
150 * attribute name and values.
\r
151 * @param qualityOfService the quality of communication
\r
152 * @throws OcException
\r
154 public void put(OcRepresentation ocRepresentation,
\r
155 Map<String, String> queryParamsMap,
\r
156 OnPutListener onPutListener,
\r
157 QualityOfService qualityOfService) throws OcException {
\r
162 qualityOfService.getValue());
\r
165 private native void put1(OcRepresentation ocRepresentation,
\r
166 Map<String, String> queryParamsMap,
\r
167 OnPutListener onPutListener,
\r
168 int qualityOfService) throws OcException;
\r
171 * Method to set the representation of a resource (via PUT)
\r
173 * @param resourceType resource type of the resource to operate on
\r
174 * @param resourceInterface interface type of the resource to operate on
\r
175 * @param ocRepresentation representation of the resource
\r
176 * @param queryParamsMap Map which can have the query parameter name and value
\r
177 * @param onPutListener event handler The event handler will be invoked with a map of
\r
178 * attribute name and values.
\r
179 * @throws OcException
\r
181 public void put(String resourceType,
\r
182 String resourceInterface,
\r
183 OcRepresentation ocRepresentation,
\r
184 Map<String, String> queryParamsMap,
\r
185 OnPutListener onPutListener) throws OcException {
\r
194 private native void put2(String resourceType,
\r
195 String resourceInterface,
\r
196 OcRepresentation ocRepresentation,
\r
197 Map<String, String> queryParamsMap,
\r
198 OnPutListener onPutListener) throws OcException;
\r
201 * Method to set the representation of a resource (via PUT)
\r
203 * @param resourceType resource type of the resource to operate on
\r
204 * @param resourceInterface interface type of the resource to operate on
\r
205 * @param ocRepresentation representation of the resource
\r
206 * @param queryParamsMap Map which can have the query parameter name and value
\r
207 * @param onPutListener event handler The event handler will be invoked with a map of
\r
208 * attribute name and values.
\r
209 * @param qualityOfService the quality of communication
\r
210 * @throws OcException
\r
212 public void put(String resourceType,
\r
213 String resourceInterface,
\r
214 OcRepresentation ocRepresentation,
\r
215 Map<String, String> queryParamsMap,
\r
216 OnPutListener onPutListener,
\r
217 QualityOfService qualityOfService) throws OcException {
\r
224 qualityOfService.getValue());
\r
227 private native void put3(String resourceType,
\r
228 String resourceInterface,
\r
229 OcRepresentation ocRepresentation,
\r
230 Map<String, String> queryParamsMap,
\r
231 OnPutListener onPutListener,
\r
232 int qualityOfService) throws OcException;
\r
235 * Method to POST on a resource
\r
237 * @param ocRepresentation representation of the resource
\r
238 * @param queryParamsMap Map which can have the query parameter name and value
\r
239 * @param onPostListener event handler The event handler will be invoked with a map of
\r
240 * attribute name and values.
\r
241 * @throws OcException
\r
243 public native void post(OcRepresentation ocRepresentation,
\r
244 Map<String, String> queryParamsMap,
\r
245 OnPostListener onPostListener) throws OcException;
\r
248 * Method to POST on a resource
\r
250 * @param ocRepresentation representation of the resource
\r
251 * @param queryParamsMap Map which can have the query parameter name and value
\r
252 * @param onPostListener event handler The event handler will be invoked with a map of
\r
253 * attribute name and values.
\r
254 * @param qualityOfService the quality of communication
\r
255 * @throws OcException
\r
257 public void post(OcRepresentation ocRepresentation,
\r
258 Map<String, String> queryParamsMap,
\r
259 OnPostListener onPostListener,
\r
260 QualityOfService qualityOfService) throws OcException {
\r
265 qualityOfService.getValue());
\r
268 private native void post1(OcRepresentation ocRepresentation,
\r
269 Map<String, String> queryParamsMap,
\r
270 OnPostListener onPostListener,
\r
271 int qualityOfService) throws OcException;
\r
274 * Method to POST on a resource
\r
276 * @param resourceType resource type of the resource to operate on
\r
277 * @param resourceInterface interface type of the resource to operate on
\r
278 * @param ocRepresentation representation of the resource
\r
279 * @param queryParamsMap Map which can have the query parameter name and value
\r
280 * @param onPostListener event handler The event handler will be invoked with a map of
\r
281 * attribute name and values.
\r
282 * @throws OcException
\r
284 public void post(String resourceType,
\r
285 String resourceInterface,
\r
286 OcRepresentation ocRepresentation,
\r
287 Map<String, String> queryParamsMap,
\r
288 OnPostListener onPostListener) throws OcException {
\r
297 private native void post2(String resourceType,
\r
298 String resourceInterface,
\r
299 OcRepresentation ocRepresentation,
\r
300 Map<String, String> queryParamsMap,
\r
301 OnPostListener onPostListener) throws OcException;
\r
304 * Method to POST on a resource
\r
306 * @param resourceType resource type of the resource to operate on
\r
307 * @param resourceInterface interface type of the resource to operate on
\r
308 * @param ocRepresentation representation of the resource
\r
309 * @param queryParamsMap Map which can have the query parameter name and value
\r
310 * @param onPostListener event handler The event handler will be invoked with a map of
\r
311 * attribute name and values.
\r
312 * @param qualityOfService the quality of communication
\r
313 * @throws OcException
\r
315 public void post(String resourceType,
\r
316 String resourceInterface,
\r
317 OcRepresentation ocRepresentation,
\r
318 Map<String, String> queryParamsMap,
\r
319 OnPostListener onPostListener,
\r
320 QualityOfService qualityOfService) throws OcException {
\r
327 qualityOfService.getValue());
\r
330 private native void post3(String resourceType,
\r
331 String resourceInterface,
\r
332 OcRepresentation ocRepresentation,
\r
333 Map<String, String> queryParamsMap,
\r
334 OnPostListener onPostListener,
\r
335 int qualityOfService) throws OcException;
\r
338 * Method to perform DELETE operation
\r
340 * @param onDeleteListener event handler The event handler will have headerOptionList
\r
342 public native void deleteResource(OnDeleteListener onDeleteListener) throws OcException;
\r
345 * Method to perform DELETE operation
\r
347 * @param onDeleteListener event handler The event handler will have headerOptionList
\r
348 * @param qualityOfService the quality of communication
\r
350 public void deleteResource(OnDeleteListener onDeleteListener,
\r
351 QualityOfService qualityOfService) throws OcException {
\r
352 this.deleteResource1(onDeleteListener,
\r
353 qualityOfService.getValue());
\r
356 private native void deleteResource1(OnDeleteListener onDeleteListener,
\r
357 int qualityOfService) throws OcException;
\r
360 * Method to set observation on the resource
\r
362 * @param observeType allows the client to specify how it wants to observe
\r
363 * @param queryParamsMap map which can have the query parameter name and value
\r
364 * @param onObserveListener event handler The handler method will be invoked with a map
\r
365 * of attribute name and values.
\r
366 * @throws OcException
\r
368 public void observe(ObserveType observeType,
\r
369 Map<String, String> queryParamsMap,
\r
370 OnObserveListener onObserveListener) throws OcException {
\r
372 observeType.getValue(),
\r
374 onObserveListener);
\r
377 private synchronized native void observe(int observeType,
\r
378 Map<String, String> queryParamsMap,
\r
379 OnObserveListener onObserveListener) throws OcException;
\r
382 * Method to set observation on the resource
\r
384 * @param observeType allows the client to specify how it wants to observe
\r
385 * @param queryParamsMap map which can have the query parameter name and value
\r
386 * @param onObserveListener event handler The handler method will be invoked with a map
\r
387 * of attribute name and values.
\r
388 * @param qualityOfService the quality of communication
\r
389 * @throws OcException
\r
391 public void observe(ObserveType observeType,
\r
392 Map<String, String> queryParamsMap,
\r
393 OnObserveListener onObserveListener,
\r
394 QualityOfService qualityOfService) throws OcException {
\r
396 observeType.getValue(),
\r
399 qualityOfService.getValue());
\r
402 private synchronized native void observe1(int observeType,
\r
403 Map<String, String> queryParamsMap,
\r
404 OnObserveListener onObserveListener,
\r
405 int qualityOfService) throws OcException;
\r
408 * Method to cancel the observation on the resource
\r
410 * @throws OcException
\r
412 public native void cancelObserve() throws OcException;
\r
415 * Method to cancel the observation on the resource
\r
417 * @param qualityOfService the quality of communication
\r
418 * @throws OcException
\r
420 public void cancelObserve(QualityOfService qualityOfService) throws OcException {
\r
421 this.cancelObserve1(qualityOfService.getValue());
\r
424 private native void cancelObserve1(int qualityOfService) throws OcException;
\r
427 * Method to set header options
\r
429 * @param headerOptionList List<OcHeaderOption> where header information(header optionID and
\r
430 * optionData is passed
\r
432 public void setHeaderOptions(List<OcHeaderOption> headerOptionList) {
\r
433 this.setHeaderOptions(headerOptionList.toArray(
\r
434 new OcHeaderOption[headerOptionList.size()])
\r
438 private native void setHeaderOptions(OcHeaderOption[] headerOptionList);
\r
441 * Method to unset header options
\r
443 public native void unsetHeaderOptions();
\r
446 * Method to get the host address of this resource
\r
448 * @return host address NOTE: This might or might not be exposed in future due to
\r
449 * security concerns
\r
451 public native String getHost();
\r
454 * Method to get the URI for this resource
\r
456 * @return resource URI
\r
458 public native String getUri();
\r
461 * Method to get the connectivity type of this resource
\r
463 * @return OcConnectivityType connectivity type
\r
465 public OcConnectivityType getConnectivityType() {
\r
466 return OcConnectivityType.get(
\r
467 this.getConnectivityTypeN()
\r
471 private native int getConnectivityTypeN();
\r
474 * Method to provide ability to check if this resource is observable or not
\r
476 * @return true indicates resource is observable; false indicates resource is not observable
\r
478 public native boolean isObservable();
\r
481 * Method to get the list of resource types
\r
483 * @return List of resource types
\r
485 public native List<String> getResourceTypes();
\r
488 * Method to get the list of resource interfaces
\r
490 * @return List of resource interface
\r
492 public native List<String> getResourceInterfaces();
\r
495 * Method to get a unique identifier for this resource across network interfaces. This will
\r
496 * be guaranteed unique for every resource-per-server independent of how this was discovered.
\r
498 * @return OcResourceIdentifier object, which can be used for all comparison and hashing
\r
500 public native OcResourceIdentifier getUniqueIdentifier();
\r
503 * Method to get a string representation of the resource's server ID.
\r
504 * * This is unique per- server independent on how it was discovered.
\r
506 * @return server ID
\r
508 public native String getServerId();
\r
511 * An OnGetListener can be registered via the resource get call.
\r
512 * Event listeners are notified asynchronously
\r
514 public interface OnGetListener {
\r
515 public void onGetCompleted(List<OcHeaderOption> headerOptionList,
\r
516 OcRepresentation ocRepresentation);
\r
518 public void onGetFailed(Throwable ex);
\r
522 * An OnPutListener can be registered via the resource put call.
\r
523 * Event listeners are notified asynchronously
\r
525 public interface OnPutListener {
\r
526 public void onPutCompleted(List<OcHeaderOption> headerOptionList,
\r
527 OcRepresentation ocRepresentation);
\r
529 public void onPutFailed(Throwable ex);
\r
533 * An OnPostListener can be registered via the resource post call.
\r
534 * Event listeners are notified asynchronously
\r
536 public interface OnPostListener {
\r
537 public void onPostCompleted(List<OcHeaderOption> headerOptionList,
\r
538 OcRepresentation ocRepresentation);
\r
540 public void onPostFailed(Throwable ex);
\r
544 * An OnDeleteListener can be registered via the resource delete call.
\r
545 * Event listeners are notified asynchronously
\r
547 public interface OnDeleteListener {
\r
548 public void onDeleteCompleted(List<OcHeaderOption> headerOptionList);
\r
550 public void onDeleteFailed(Throwable ex);
\r
554 * An OnObserveListener can be registered via the resource observe call.
\r
555 * Event listeners are notified asynchronously
\r
557 public interface OnObserveListener {
\r
558 public void onObserveCompleted(List<OcHeaderOption> headerOptionList,
\r
559 OcRepresentation ocRepresentation,
\r
560 int sequenceNumber);
\r
562 public void onObserveFailed(Throwable ex);
\r
566 protected void finalize() throws Throwable {
\r
572 private native void dispose();
\r
574 private long mNativeHandle;
\r