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.EnumSet;
26 import java.util.List;
30 * OcResource represents an OC resource. A resource could be a light controller, temperature sensor,
31 * smoke detector, etc. A resource comes with a well-defined contract or interface onto which you
32 * can perform different operations, such as turning on the light, getting the current temperature
33 * or subscribing for event notifications from the smoke detector. A resource can be composed of
34 * one or more resources.
36 public class OcResource {
38 private OcResource(long nativeHandle) {
39 this.mNativeHandle = nativeHandle;
43 * Method to get the attributes of a resource.
45 * @param queryParamsMap map which can have the query parameter name and value
46 * @param onGetListener The event handler will be invoked with a map of attribute name and
47 * values. The event handler will also have the result from this Get
48 * operation This will have error codes
51 public native void get(Map<String, String> queryParamsMap,
52 OnGetListener onGetListener) throws OcException;
55 * Method to get the attributes of a resource.
57 * @param queryParamsMap map which can have the query parameter name and value
58 * @param onGetListener The event handler will be invoked with a map of attribute name and
59 * values. The event handler will also have the result from this Get
60 * operation This will have error codes
61 * @param qualityOfService the quality of communication
64 public void get(Map<String, String> queryParamsMap,
65 OnGetListener onGetListener,
66 QualityOfService qualityOfService) throws OcException {
67 this.get1(queryParamsMap, onGetListener, qualityOfService.getValue());
70 private native void get1(Map<String, String> queryParamsMap,
71 OnGetListener onGetListener,
72 int qualityOfService) throws OcException;
75 * Method to get the attributes of a resource.
77 * @param resourceType resourceType of the resource to operate on
78 * @param resourceInterface interface type of the resource to operate on
79 * @param queryParamsMap map which can have the query parameter name and value
80 * @param onGetListener The event handler will be invoked with a map of attribute name and
81 * values. The event handler will also have the result from this Get
82 * operation This will have error codes
85 public void get(String resourceType,
86 String resourceInterface,
87 Map<String, String> queryParamsMap,
88 OnGetListener onGetListener) throws OcException {
96 private native void get2(String resourceType,
97 String resourceInterface,
98 Map<String, String> queryParamsMap,
99 OnGetListener onGetListener) throws OcException;
102 * Method to get the attributes of a resource.
104 * @param resourceType resourceType of the resource to operate on
105 * @param resourceInterface interface type of the resource to operate on
106 * @param queryParamsMap map which can have the query parameter name and value
107 * @param onGetListener The event handler will be invoked with a map of attribute name and
108 * values. The event handler will also have the result from this Get
109 * operation This will have error codes
110 * @param qualityOfService the quality of communication
111 * @throws OcException
113 public void get(String resourceType,
114 String resourceInterface,
115 Map<String, String> queryParamsMap,
116 OnGetListener onGetListener,
117 QualityOfService qualityOfService) throws OcException {
123 qualityOfService.getValue());
126 private native void get3(String resourceType,
127 String resourceInterface,
128 Map<String, String> queryParamsMap,
129 OnGetListener onGetListener,
130 int qualityOfService) throws OcException;
133 * Method to set the representation of a resource (via PUT)
135 * @param representation representation of the resource
136 * @param queryParamsMap Map which can have the query parameter name and value
137 * @param onPutListener event handler The event handler will be invoked with a map of attribute
139 * @throws OcException
141 public native void put(OcRepresentation representation,
142 Map<String, String> queryParamsMap,
143 OnPutListener onPutListener) throws OcException;
146 * Method to set the representation of a resource (via PUT)
148 * @param ocRepresentation representation of the resource
149 * @param queryParamsMap Map which can have the query parameter name and value
150 * @param onPutListener event handler The event handler will be invoked with a map of
151 * attribute name and values.
152 * @param qualityOfService the quality of communication
153 * @throws OcException
155 public void put(OcRepresentation ocRepresentation,
156 Map<String, String> queryParamsMap,
157 OnPutListener onPutListener,
158 QualityOfService qualityOfService) throws OcException {
163 qualityOfService.getValue());
166 private native void put1(OcRepresentation ocRepresentation,
167 Map<String, String> queryParamsMap,
168 OnPutListener onPutListener,
169 int qualityOfService) throws OcException;
172 * Method to set the representation of a resource (via PUT)
174 * @param resourceType resource type of the resource to operate on
175 * @param resourceInterface interface type of the resource to operate on
176 * @param ocRepresentation representation of the resource
177 * @param queryParamsMap Map which can have the query parameter name and value
178 * @param onPutListener event handler The event handler will be invoked with a map of
179 * attribute name and values.
180 * @throws OcException
182 public void put(String resourceType,
183 String resourceInterface,
184 OcRepresentation ocRepresentation,
185 Map<String, String> queryParamsMap,
186 OnPutListener onPutListener) throws OcException {
195 private native void put2(String resourceType,
196 String resourceInterface,
197 OcRepresentation ocRepresentation,
198 Map<String, String> queryParamsMap,
199 OnPutListener onPutListener) throws OcException;
202 * Method to set the representation of a resource (via PUT)
204 * @param resourceType resource type of the resource to operate on
205 * @param resourceInterface interface type of the resource to operate on
206 * @param ocRepresentation representation of the resource
207 * @param queryParamsMap Map which can have the query parameter name and value
208 * @param onPutListener event handler The event handler will be invoked with a map of
209 * attribute name and values.
210 * @param qualityOfService the quality of communication
211 * @throws OcException
213 public void put(String resourceType,
214 String resourceInterface,
215 OcRepresentation ocRepresentation,
216 Map<String, String> queryParamsMap,
217 OnPutListener onPutListener,
218 QualityOfService qualityOfService) throws OcException {
225 qualityOfService.getValue());
228 private native void put3(String resourceType,
229 String resourceInterface,
230 OcRepresentation ocRepresentation,
231 Map<String, String> queryParamsMap,
232 OnPutListener onPutListener,
233 int qualityOfService) throws OcException;
236 * Method to POST on a resource
238 * @param ocRepresentation representation of the resource
239 * @param queryParamsMap Map which can have the query parameter name and value
240 * @param onPostListener event handler The event handler will be invoked with a map of
241 * attribute name and values.
242 * @throws OcException
244 public native void post(OcRepresentation ocRepresentation,
245 Map<String, String> queryParamsMap,
246 OnPostListener onPostListener) throws OcException;
249 * Method to POST on a resource
251 * @param ocRepresentation representation of the resource
252 * @param queryParamsMap Map which can have the query parameter name and value
253 * @param onPostListener event handler The event handler will be invoked with a map of
254 * attribute name and values.
255 * @param qualityOfService the quality of communication
256 * @throws OcException
258 public void post(OcRepresentation ocRepresentation,
259 Map<String, String> queryParamsMap,
260 OnPostListener onPostListener,
261 QualityOfService qualityOfService) throws OcException {
266 qualityOfService.getValue());
269 private native void post1(OcRepresentation ocRepresentation,
270 Map<String, String> queryParamsMap,
271 OnPostListener onPostListener,
272 int qualityOfService) throws OcException;
275 * Method to POST on a resource
277 * @param resourceType resource type of the resource to operate on
278 * @param resourceInterface interface type of the resource to operate on
279 * @param ocRepresentation representation of the resource
280 * @param queryParamsMap Map which can have the query parameter name and value
281 * @param onPostListener event handler The event handler will be invoked with a map of
282 * attribute name and values.
283 * @throws OcException
285 public void post(String resourceType,
286 String resourceInterface,
287 OcRepresentation ocRepresentation,
288 Map<String, String> queryParamsMap,
289 OnPostListener onPostListener) throws OcException {
298 private native void post2(String resourceType,
299 String resourceInterface,
300 OcRepresentation ocRepresentation,
301 Map<String, String> queryParamsMap,
302 OnPostListener onPostListener) throws OcException;
305 * Method to POST on a resource
307 * @param resourceType resource type of the resource to operate on
308 * @param resourceInterface interface type of the resource to operate on
309 * @param ocRepresentation representation of the resource
310 * @param queryParamsMap Map which can have the query parameter name and value
311 * @param onPostListener event handler The event handler will be invoked with a map of
312 * attribute name and values.
313 * @param qualityOfService the quality of communication
314 * @throws OcException
316 public void post(String resourceType,
317 String resourceInterface,
318 OcRepresentation ocRepresentation,
319 Map<String, String> queryParamsMap,
320 OnPostListener onPostListener,
321 QualityOfService qualityOfService) throws OcException {
328 qualityOfService.getValue());
331 private native void post3(String resourceType,
332 String resourceInterface,
333 OcRepresentation ocRepresentation,
334 Map<String, String> queryParamsMap,
335 OnPostListener onPostListener,
336 int qualityOfService) throws OcException;
339 * Method to perform DELETE operation
341 * @param onDeleteListener event handler The event handler will have headerOptionList
343 public native void deleteResource(OnDeleteListener onDeleteListener) throws OcException;
346 * Method to perform DELETE operation
348 * @param onDeleteListener event handler The event handler will have headerOptionList
349 * @param qualityOfService the quality of communication
351 public void deleteResource(OnDeleteListener onDeleteListener,
352 QualityOfService qualityOfService) throws OcException {
353 this.deleteResource1(onDeleteListener,
354 qualityOfService.getValue());
357 private native void deleteResource1(OnDeleteListener onDeleteListener,
358 int qualityOfService) throws OcException;
361 * Method to set observation on the resource
363 * @param observeType allows the client to specify how it wants to observe
364 * @param queryParamsMap map which can have the query parameter name and value
365 * @param onObserveListener event handler The handler method will be invoked with a map
366 * of attribute name and values.
367 * @throws OcException
369 public void observe(ObserveType observeType,
370 Map<String, String> queryParamsMap,
371 OnObserveListener onObserveListener) throws OcException {
373 observeType.getValue(),
378 private synchronized native void observe(int observeType,
379 Map<String, String> queryParamsMap,
380 OnObserveListener onObserveListener) throws OcException;
383 * Method to set observation on the resource
385 * @param observeType allows the client to specify how it wants to observe
386 * @param queryParamsMap map which can have the query parameter name and value
387 * @param onObserveListener event handler The handler method will be invoked with a map
388 * of attribute name and values.
389 * @param qualityOfService the quality of communication
390 * @throws OcException
392 public void observe(ObserveType observeType,
393 Map<String, String> queryParamsMap,
394 OnObserveListener onObserveListener,
395 QualityOfService qualityOfService) throws OcException {
397 observeType.getValue(),
400 qualityOfService.getValue());
403 private synchronized native void observe1(int observeType,
404 Map<String, String> queryParamsMap,
405 OnObserveListener onObserveListener,
406 int qualityOfService) throws OcException;
409 * Method to cancel the observation on the resource
411 * @throws OcException
413 public native void cancelObserve() throws OcException;
416 * Method to cancel the observation on the resource
418 * @param qualityOfService the quality of communication
419 * @throws OcException
421 public void cancelObserve(QualityOfService qualityOfService) throws OcException {
422 this.cancelObserve1(qualityOfService.getValue());
425 private native void cancelObserve1(int qualityOfService) throws OcException;
428 * Method to set header options
430 * @param headerOptionList List<OcHeaderOption> where header information(header optionID and
431 * optionData is passed
433 public void setHeaderOptions(List<OcHeaderOption> headerOptionList) {
434 this.setHeaderOptions(headerOptionList.toArray(
435 new OcHeaderOption[headerOptionList.size()])
439 private native void setHeaderOptions(OcHeaderOption[] headerOptionList);
442 * Method to unset header options
444 public native void unsetHeaderOptions();
447 * Method to get the host address of this resource
449 * @return host address NOTE: This might or might not be exposed in future due to
452 public native String getHost();
455 * Method to get the URI for this resource
457 * @return resource URI
459 public native String getUri();
462 * Method to get the connectivity type of this resource
464 * @return EnumSet<OcConnectivityType></OcConnectivityType> connectivity type set
466 public EnumSet<OcConnectivityType> getConnectivityTypeSet() {
467 return OcConnectivityType.convertToEnumSet(
468 this.getConnectivityTypeN()
472 private native int getConnectivityTypeN();
475 * Method to provide ability to check if this resource is observable or not
477 * @return true indicates resource is observable; false indicates resource is not observable
479 public native boolean isObservable();
482 * Method to get the list of resource types
484 * @return List of resource types
486 public native List<String> getResourceTypes();
489 * Method to get the list of resource interfaces
491 * @return List of resource interface
493 public native List<String> getResourceInterfaces();
496 * Method to get a unique identifier for this resource across network interfaces. This will
497 * be guaranteed unique for every resource-per-server independent of how this was discovered.
499 * @return OcResourceIdentifier object, which can be used for all comparison and hashing
501 public native OcResourceIdentifier getUniqueIdentifier();
504 * Method to get a string representation of the resource's server ID.
505 * * This is unique per- server independent on how it was discovered.
509 public native String getServerId();
512 * An OnGetListener can be registered via the resource get call.
513 * Event listeners are notified asynchronously
515 public interface OnGetListener {
516 public void onGetCompleted(List<OcHeaderOption> headerOptionList,
517 OcRepresentation ocRepresentation);
519 public void onGetFailed(Throwable ex);
523 * An OnPutListener can be registered via the resource put call.
524 * Event listeners are notified asynchronously
526 public interface OnPutListener {
527 public void onPutCompleted(List<OcHeaderOption> headerOptionList,
528 OcRepresentation ocRepresentation);
530 public void onPutFailed(Throwable ex);
534 * An OnPostListener can be registered via the resource post call.
535 * Event listeners are notified asynchronously
537 public interface OnPostListener {
538 public void onPostCompleted(List<OcHeaderOption> headerOptionList,
539 OcRepresentation ocRepresentation);
541 public void onPostFailed(Throwable ex);
545 * An OnDeleteListener can be registered via the resource delete call.
546 * Event listeners are notified asynchronously
548 public interface OnDeleteListener {
549 public void onDeleteCompleted(List<OcHeaderOption> headerOptionList);
551 public void onDeleteFailed(Throwable ex);
555 * An OnObserveListener can be registered via the resource observe call.
556 * Event listeners are notified asynchronously
558 public interface OnObserveListener {
559 public void onObserveCompleted(List<OcHeaderOption> headerOptionList,
560 OcRepresentation ocRepresentation,
563 public void onObserveFailed(Throwable ex);
567 protected void finalize() throws Throwable {
573 private native void dispose();
575 private long mNativeHandle;