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 {
37 public static final String CREATED_URI_KEY = "createduri";
39 private OcResource(long nativeHandle) {
40 this.mNativeHandle = nativeHandle;
44 * Method to get the attributes of a resource.
46 * @param queryParamsMap map which can have the query parameter name and value
47 * @param onGetListener The event handler will be invoked with a map of attribute name and
48 * values. The event handler will also have the result from this Get
49 * operation This will have error codes
50 * @throws OcException if failure
52 public native void get(Map<String, String> queryParamsMap,
53 OnGetListener onGetListener) throws OcException;
56 * Method to get the attributes of a resource.
58 * @param queryParamsMap map which can have the query parameter name and value
59 * @param onGetListener The event handler will be invoked with a map of attribute name and
60 * values. The event handler will also have the result from this Get
61 * operation This will have error codes
62 * @param qualityOfService the quality of communication
63 * @throws OcException if failure
65 public void get(Map<String, String> queryParamsMap,
66 OnGetListener onGetListener,
67 QualityOfService qualityOfService) throws OcException {
68 this.get1(queryParamsMap, onGetListener, qualityOfService.getValue());
71 private native void get1(Map<String, String> queryParamsMap,
72 OnGetListener onGetListener,
73 int qualityOfService) throws OcException;
76 * Method to get the attributes of a resource.
78 * @param resourceType resourceType of the resource to operate on
79 * @param resourceInterface interface type of the resource to operate on
80 * @param queryParamsMap map which can have the query parameter name and value
81 * @param onGetListener The event handler will be invoked with a map of attribute name and
82 * values. The event handler will also have the result from this Get
83 * operation This will have error codes
84 * @throws OcException if failure
86 public void get(String resourceType,
87 String resourceInterface,
88 Map<String, String> queryParamsMap,
89 OnGetListener onGetListener) throws OcException {
97 private native void get2(String resourceType,
98 String resourceInterface,
99 Map<String, String> queryParamsMap,
100 OnGetListener onGetListener) throws OcException;
103 * Method to get the attributes of a resource.
105 * @param resourceType resourceType of the resource to operate on
106 * @param resourceInterface interface type of the resource to operate on
107 * @param queryParamsMap map which can have the query parameter name and value
108 * @param onGetListener The event handler will be invoked with a map of attribute name and
109 * values. The event handler will also have the result from this Get
110 * operation This will have error codes
111 * @param qualityOfService the quality of communication
112 * @throws OcException if failure
114 public void get(String resourceType,
115 String resourceInterface,
116 Map<String, String> queryParamsMap,
117 OnGetListener onGetListener,
118 QualityOfService qualityOfService) throws OcException {
124 qualityOfService.getValue());
127 private native void get3(String resourceType,
128 String resourceInterface,
129 Map<String, String> queryParamsMap,
130 OnGetListener onGetListener,
131 int qualityOfService) throws OcException;
134 * Method to set the representation of a resource (via PUT)
136 * @param representation representation of the resource
137 * @param queryParamsMap Map which can have the query parameter name and value
138 * @param onPutListener event handler The event handler will be invoked with a map of attribute
140 * @throws OcException if failure
142 public native void put(OcRepresentation representation,
143 Map<String, String> queryParamsMap,
144 OnPutListener onPutListener) throws OcException;
147 * Method to set the representation of a resource (via PUT)
149 * @param ocRepresentation representation of the resource
150 * @param queryParamsMap Map which can have the query parameter name and value
151 * @param onPutListener event handler The event handler will be invoked with a map of
152 * attribute name and values.
153 * @param qualityOfService the quality of communication
154 * @throws OcException if failure
156 public void put(OcRepresentation ocRepresentation,
157 Map<String, String> queryParamsMap,
158 OnPutListener onPutListener,
159 QualityOfService qualityOfService) throws OcException {
164 qualityOfService.getValue());
167 private native void put1(OcRepresentation ocRepresentation,
168 Map<String, String> queryParamsMap,
169 OnPutListener onPutListener,
170 int qualityOfService) throws OcException;
173 * Method to set the representation of a resource (via PUT)
175 * @param resourceType resource type of the resource to operate on
176 * @param resourceInterface interface type of the resource to operate on
177 * @param ocRepresentation representation of the resource
178 * @param queryParamsMap Map which can have the query parameter name and value
179 * @param onPutListener event handler The event handler will be invoked with a map of
180 * attribute name and values.
181 * @throws OcException if failure
183 public void put(String resourceType,
184 String resourceInterface,
185 OcRepresentation ocRepresentation,
186 Map<String, String> queryParamsMap,
187 OnPutListener onPutListener) throws OcException {
196 private native void put2(String resourceType,
197 String resourceInterface,
198 OcRepresentation ocRepresentation,
199 Map<String, String> queryParamsMap,
200 OnPutListener onPutListener) throws OcException;
203 * Method to set the representation of a resource (via PUT)
205 * @param resourceType resource type of the resource to operate on
206 * @param resourceInterface interface type of the resource to operate on
207 * @param ocRepresentation representation of the resource
208 * @param queryParamsMap Map which can have the query parameter name and value
209 * @param onPutListener event handler The event handler will be invoked with a map of
210 * attribute name and values.
211 * @param qualityOfService the quality of communication
212 * @throws OcException if failure
214 public void put(String resourceType,
215 String resourceInterface,
216 OcRepresentation ocRepresentation,
217 Map<String, String> queryParamsMap,
218 OnPutListener onPutListener,
219 QualityOfService qualityOfService) throws OcException {
226 qualityOfService.getValue());
229 private native void put3(String resourceType,
230 String resourceInterface,
231 OcRepresentation ocRepresentation,
232 Map<String, String> queryParamsMap,
233 OnPutListener onPutListener,
234 int qualityOfService) throws OcException;
237 * Method to POST on a resource
239 * @param ocRepresentation representation of the resource
240 * @param queryParamsMap Map which can have the query parameter name and value
241 * @param onPostListener event handler The event handler will be invoked with a map of
242 * attribute name and values.
243 * @throws OcException if failure
245 public native void post(OcRepresentation ocRepresentation,
246 Map<String, String> queryParamsMap,
247 OnPostListener onPostListener) throws OcException;
250 * Method to POST on a resource
252 * @param ocRepresentation representation of the resource
253 * @param queryParamsMap Map which can have the query parameter name and value
254 * @param onPostListener event handler The event handler will be invoked with a map of
255 * attribute name and values.
256 * @param qualityOfService the quality of communication
257 * @throws OcException if failure
259 public void post(OcRepresentation ocRepresentation,
260 Map<String, String> queryParamsMap,
261 OnPostListener onPostListener,
262 QualityOfService qualityOfService) throws OcException {
267 qualityOfService.getValue());
270 private native void post1(OcRepresentation ocRepresentation,
271 Map<String, String> queryParamsMap,
272 OnPostListener onPostListener,
273 int qualityOfService) throws OcException;
276 * Method to POST on a resource
278 * @param resourceType resource type of the resource to operate on
279 * @param resourceInterface interface type of the resource to operate on
280 * @param ocRepresentation representation of the resource
281 * @param queryParamsMap Map which can have the query parameter name and value
282 * @param onPostListener event handler The event handler will be invoked with a map of
283 * attribute name and values.
284 * @throws OcException if failure
286 public void post(String resourceType,
287 String resourceInterface,
288 OcRepresentation ocRepresentation,
289 Map<String, String> queryParamsMap,
290 OnPostListener onPostListener) throws OcException {
299 private native void post2(String resourceType,
300 String resourceInterface,
301 OcRepresentation ocRepresentation,
302 Map<String, String> queryParamsMap,
303 OnPostListener onPostListener) throws OcException;
306 * Method to POST on a resource
308 * @param resourceType resource type of the resource to operate on
309 * @param resourceInterface interface type of the resource to operate on
310 * @param ocRepresentation representation of the resource
311 * @param queryParamsMap Map which can have the query parameter name and value
312 * @param onPostListener event handler The event handler will be invoked with a map of
313 * attribute name and values.
314 * @param qualityOfService the quality of communication
315 * @throws OcException
317 public void post(String resourceType,
318 String resourceInterface,
319 OcRepresentation ocRepresentation,
320 Map<String, String> queryParamsMap,
321 OnPostListener onPostListener,
322 QualityOfService qualityOfService) throws OcException {
329 qualityOfService.getValue());
332 private native void post3(String resourceType,
333 String resourceInterface,
334 OcRepresentation ocRepresentation,
335 Map<String, String> queryParamsMap,
336 OnPostListener onPostListener,
337 int qualityOfService) throws OcException;
340 * Method to perform DELETE operation
342 * @param onDeleteListener event handler The event handler will have headerOptionList
344 public native void deleteResource(OnDeleteListener onDeleteListener) throws OcException;
347 * Method to perform DELETE operation
349 * @param onDeleteListener event handler The event handler will have headerOptionList
350 * @param qualityOfService the quality of communication
352 public void deleteResource(OnDeleteListener onDeleteListener,
353 QualityOfService qualityOfService) throws OcException {
354 this.deleteResource1(onDeleteListener,
355 qualityOfService.getValue());
358 private native void deleteResource1(OnDeleteListener onDeleteListener,
359 int qualityOfService) throws OcException;
362 * Method to set observation on the resource
364 * @param observeType allows the client to specify how it wants to observe
365 * @param queryParamsMap map which can have the query parameter name and value
366 * @param onObserveListener event handler The handler method will be invoked with a map
367 * of attribute name and values.
368 * @throws OcException
370 public void observe(ObserveType observeType,
371 Map<String, String> queryParamsMap,
372 OnObserveListener onObserveListener) throws OcException {
374 observeType.getValue(),
379 private synchronized native void observe(int observeType,
380 Map<String, String> queryParamsMap,
381 OnObserveListener onObserveListener) throws OcException;
384 * Method to set observation on the resource
386 * @param observeType allows the client to specify how it wants to observe
387 * @param queryParamsMap map which can have the query parameter name and value
388 * @param onObserveListener event handler The handler method will be invoked with a map
389 * of attribute name and values.
390 * @param qualityOfService the quality of communication
391 * @throws OcException
393 public void observe(ObserveType observeType,
394 Map<String, String> queryParamsMap,
395 OnObserveListener onObserveListener,
396 QualityOfService qualityOfService) throws OcException {
398 observeType.getValue(),
401 qualityOfService.getValue());
404 private synchronized native void observe1(int observeType,
405 Map<String, String> queryParamsMap,
406 OnObserveListener onObserveListener,
407 int qualityOfService) throws OcException;
410 * Method to cancel the observation on the resource
412 * @throws OcException
414 public void cancelObserve() throws OcException{
415 this.cancelObserve(OcPlatform.getPlatformQualityOfService());
419 * Method to cancel the observation on the resource
421 * @param qualityOfService the quality of communication
422 * @throws OcException
424 public void cancelObserve(QualityOfService qualityOfService) throws OcException {
425 this.cancelObserve1(qualityOfService.getValue());
428 private native void cancelObserve1(int qualityOfService) throws OcException;
431 * Method to set header options
433 * @param headerOptionList List<OcHeaderOption> where header information(header optionID and
434 * optionData is passed
436 public void setHeaderOptions(List<OcHeaderOption> headerOptionList) {
437 this.setHeaderOptions(headerOptionList.toArray(
438 new OcHeaderOption[headerOptionList.size()])
442 private native void setHeaderOptions(OcHeaderOption[] headerOptionList);
445 * Method to unset header options
447 public native void unsetHeaderOptions();
450 * Method to get the host address of this resource
452 * @return host address NOTE: This might or might not be exposed in future due to
455 public native String getHost();
458 * Method to get the URI for this resource
460 * @return resource URI
462 public native String getUri();
465 * Method to get the connectivity type of this resource
467 * @return EnumSet<OcConnectivityType></OcConnectivityType> connectivity type set
469 public EnumSet<OcConnectivityType> getConnectivityTypeSet() {
470 return OcConnectivityType.convertToEnumSet(
471 this.getConnectivityTypeN()
475 private native int getConnectivityTypeN();
478 * Method to provide ability to check if this resource is observable or not
480 * @return true indicates resource is observable; false indicates resource is not observable
482 public native boolean isObservable();
485 * Method to get the list of resource types
487 * @return List of resource types
489 public native List<String> getResourceTypes();
492 * Method to get the list of resource interfaces
494 * @return List of resource interface
496 public native List<String> getResourceInterfaces();
499 * Method to get a unique identifier for this resource across network interfaces. This will
500 * be guaranteed unique for every resource-per-server independent of how this was discovered.
502 * @return OcResourceIdentifier object, which can be used for all comparison and hashing
504 public native OcResourceIdentifier getUniqueIdentifier();
507 * Method to get a string representation of the resource's server ID.
509 * This is unique per- server independent on how it was discovered.
514 public native String getServerId();
517 * An OnGetListener can be registered via the resource get call.
518 * Event listeners are notified asynchronously
520 public interface OnGetListener {
521 public void onGetCompleted(List<OcHeaderOption> headerOptionList,
522 OcRepresentation ocRepresentation);
524 public void onGetFailed(Throwable ex);
528 * An OnPutListener can be registered via the resource put call.
529 * Event listeners are notified asynchronously
531 public interface OnPutListener {
532 public void onPutCompleted(List<OcHeaderOption> headerOptionList,
533 OcRepresentation ocRepresentation);
535 public void onPutFailed(Throwable ex);
539 * An OnPostListener can be registered via the resource post call.
540 * Event listeners are notified asynchronously
542 public interface OnPostListener {
543 public void onPostCompleted(List<OcHeaderOption> headerOptionList,
544 OcRepresentation ocRepresentation);
546 public void onPostFailed(Throwable ex);
550 * An OnDeleteListener can be registered via the resource delete call.
551 * Event listeners are notified asynchronously
553 public interface OnDeleteListener {
554 public void onDeleteCompleted(List<OcHeaderOption> headerOptionList);
556 public void onDeleteFailed(Throwable ex);
560 * An OnObserveListener can be registered via the resource observe call.
561 * Event listeners are notified asynchronously
563 public interface OnObserveListener {
567 public static final int REGISTER = 0;
571 public static final int DEREGISTER = 1;
575 public static final int NO_OPTION = 2;
577 public void onObserveCompleted(List<OcHeaderOption> headerOptionList,
578 OcRepresentation ocRepresentation,
581 public void onObserveFailed(Throwable ex);
585 protected void finalize() throws Throwable {
591 private native void dispose();
593 private long mNativeHandle;