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 native void cancelObserve() throws OcException;
417 * Method to cancel the observation on the resource
419 * @param qualityOfService the quality of communication
420 * @throws OcException
422 public void cancelObserve(QualityOfService qualityOfService) throws OcException {
423 this.cancelObserve1(qualityOfService.getValue());
426 private native void cancelObserve1(int qualityOfService) throws OcException;
429 * Method to set header options
431 * @param headerOptionList List<OcHeaderOption> where header information(header optionID and
432 * optionData is passed
434 public void setHeaderOptions(List<OcHeaderOption> headerOptionList) {
435 this.setHeaderOptions(headerOptionList.toArray(
436 new OcHeaderOption[headerOptionList.size()])
440 private native void setHeaderOptions(OcHeaderOption[] headerOptionList);
443 * Method to unset header options
445 public native void unsetHeaderOptions();
448 * Method to get the host address of this resource
450 * @return host address NOTE: This might or might not be exposed in future due to
453 public native String getHost();
456 * Method to get the URI for this resource
458 * @return resource URI
460 public native String getUri();
463 * Method to get the connectivity type of this resource
465 * @return EnumSet<OcConnectivityType></OcConnectivityType> connectivity type set
467 public EnumSet<OcConnectivityType> getConnectivityTypeSet() {
468 return OcConnectivityType.convertToEnumSet(
469 this.getConnectivityTypeN()
473 private native int getConnectivityTypeN();
476 * Method to provide ability to check if this resource is observable or not
478 * @return true indicates resource is observable; false indicates resource is not observable
480 public native boolean isObservable();
483 * Method to get the list of resource types
485 * @return List of resource types
487 public native List<String> getResourceTypes();
490 * Method to get the list of resource interfaces
492 * @return List of resource interface
494 public native List<String> getResourceInterfaces();
497 * Method to get a unique identifier for this resource across network interfaces. This will
498 * be guaranteed unique for every resource-per-server independent of how this was discovered.
500 * @return OcResourceIdentifier object, which can be used for all comparison and hashing
502 public native OcResourceIdentifier getUniqueIdentifier();
505 * Method to get a string representation of the resource's server ID.
507 * This is unique per- server independent on how it was discovered.
512 public native String getServerId();
515 * An OnGetListener can be registered via the resource get call.
516 * Event listeners are notified asynchronously
518 public interface OnGetListener {
519 public void onGetCompleted(List<OcHeaderOption> headerOptionList,
520 OcRepresentation ocRepresentation);
522 public void onGetFailed(Throwable ex);
526 * An OnPutListener can be registered via the resource put call.
527 * Event listeners are notified asynchronously
529 public interface OnPutListener {
530 public void onPutCompleted(List<OcHeaderOption> headerOptionList,
531 OcRepresentation ocRepresentation);
533 public void onPutFailed(Throwable ex);
537 * An OnPostListener can be registered via the resource post call.
538 * Event listeners are notified asynchronously
540 public interface OnPostListener {
541 public void onPostCompleted(List<OcHeaderOption> headerOptionList,
542 OcRepresentation ocRepresentation);
544 public void onPostFailed(Throwable ex);
548 * An OnDeleteListener can be registered via the resource delete call.
549 * Event listeners are notified asynchronously
551 public interface OnDeleteListener {
552 public void onDeleteCompleted(List<OcHeaderOption> headerOptionList);
554 public void onDeleteFailed(Throwable ex);
558 * An OnObserveListener can be registered via the resource observe call.
559 * Event listeners are notified asynchronously
561 public interface OnObserveListener {
565 public static final int REGISTER = 0;
569 public static final int DEREGISTER = 1;
573 public static final int NO_OPTION = 2;
575 public void onObserveCompleted(List<OcHeaderOption> headerOptionList,
576 OcRepresentation ocRepresentation,
579 public void onObserveFailed(Throwable ex);
583 protected void finalize() throws Throwable {
589 private native void dispose();
591 private long mNativeHandle;