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