Fix for prevent tool reported defects.
[contrib/iotivity.git] / service / simulator / java / sdk / src / org / oic / simulator / clientcontroller / SimulatorRemoteResource.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.oic.simulator.clientcontroller;
18
19 import java.util.LinkedList;
20 import java.util.Map;
21
22 import org.oic.simulator.InvalidArgsException;
23 import org.oic.simulator.NoSupportException;
24 import org.oic.simulator.OperationInProgressException;
25 import org.oic.simulator.SimulatorException;
26 import org.oic.simulator.SimulatorResourceModel;
27
28 /**
29  * SimulatorRemoteResource represents a Resource running in the remote Simulator
30  * Server. It comes with a well-defined contract or interface onto which you can
31  * perform different operations or subscribe for event notifications.
32  */
33 public class SimulatorRemoteResource {
34
35     private SimulatorRemoteResource(long nativeHandle) {
36         this.nativeHandle = nativeHandle;
37     }
38
39     /**
40      * API to get the URI for this resource.
41      *
42      * @return Resource URI
43      */
44     public String getUri() {
45         return mUri;
46     }
47
48     /**
49      * API to get the observe policy of this resource.
50      *
51      * @return True if the resource is observable, otherwise false.
52      */
53     public boolean getIsObservable() {
54         return mIsObservable;
55     }
56
57     /**
58      * API to get the connectivity type for this resource.
59      *
60      * @return Connectivity type.
61      */
62     public SimulatorConnectivityType getConnectivityType() {
63         return SimulatorConnectivityType.getConnectivityType(mConnType);
64     }
65
66     /**
67      * API to get the list of resource types.
68      *
69      * @return List of resource types.
70      */
71     public LinkedList<String> getResourceTypes() {
72         return mResTypes;
73     }
74
75     /**
76      * API to get the list of resource interfaces.
77      *
78      * @return List of resource interfaces.
79      */
80     public LinkedList<String> getResourceInterfaces() {
81         return mResInterfaces;
82     }
83
84     /**
85      * API to get host address and port information of the resource.
86      *
87      * @return Host address.
88      */
89     public String getHost() {
90         return mHost;
91     }
92
93     /**
94      * API to get a unique Id of the resource .
95      *
96      * @return Unique ID.
97      */
98     public String getId() {
99         return mId;
100     }
101
102     /**
103      * API to start observing the resource.
104      *
105      * @param observeType
106      *            Allows the client to specify how it wants to observe.
107      * @param queryParamsMap
108      *            Map which can have the query parameter names and values.
109      * @param onObserveListener
110      *            The handler method which will be invoked with a map of
111      *            attribute names and values whenever there is a change in
112      *            resource model of the remote resource.
113      *
114      * @throws InvalidArgsException
115      *             This exception will be thrown if any parameter has invalid
116      *             values.
117      * @throws SimulatorException
118      *             This exception will be thrown for other errors.
119      */
120     public native void startObserve(SimulatorObserveType observeType,
121             Map<String, String> queryParamsMap,
122             IObserveListener onObserveListener) throws InvalidArgsException,
123             SimulatorException;
124
125     /**
126      * API to stop observing the resource.
127      *
128      * @throws InvalidArgsException
129      *             This exception will be thrown if the native remote resource
130      *             object is unavailable.
131      * @throws SimulatorException
132      *             This exception will be thrown for other errors.
133      */
134     public native void stopObserve() throws InvalidArgsException,
135             SimulatorException;
136
137     /**
138      * API to send GET request to the resource. Response will be notified
139      * asynchronously via callback set for {@link IGetListener}.
140      *
141      * @param queryParamsMap
142      *            Map which can have the query parameter name and value.
143      * @param onGetListener
144      *            Event handler which will be invoked with the response for GET
145      *            request with a map of attribute name and values.
146      *
147      * @throws InvalidArgsException
148      *             This exception will be thrown if any parameter has invalid
149      *             values.
150      * @throws NoSupportException
151      *             This exception will be thrown if we cannot send GET request
152      *             to the remote resource.
153      * @throws SimulatorException
154      *             This exception will be thrown for other errors.
155      */
156     public void get(Map<String, String> queryParamsMap,
157             IGetListener onGetListener) throws InvalidArgsException,
158             NoSupportException, SimulatorException {
159         this.get(null, queryParamsMap, onGetListener);
160     }
161
162     /**
163      * API to send GET request to the resource. Response will be notified
164      * asynchronously via callback set for {@link IGetListener}.
165      *
166      * @param resourceInterface
167      *            Interface type of the resource to operate on.
168      * @param queryParamsMap
169      *            Map which can have the query parameter name and value.
170      * @param onGetListener
171      *            Event handler which will be invoked with the response for GET
172      *            request with a map of attribute name and values.
173      *
174      * @throws InvalidArgsException
175      *             This exception will be thrown if any parameter has invalid
176      *             values.
177      * @throws NoSupportException
178      *             This exception will be thrown if we cannot send GET request
179      *             to the remote resource.
180      * @throws SimulatorException
181      *             This exception will be thrown for other errors.
182      */
183     public native void get(String resourceInterface,
184             Map<String, String> queryParamsMap, IGetListener onGetListener)
185             throws InvalidArgsException, NoSupportException, SimulatorException;
186
187     /**
188      * API to send PUT request to the resource. Response will be notified
189      * asynchronously via callback set for {@link IPutListener}.
190      *
191      * @param representation
192      *            {@link SimulatorResourceModel} holding the representation of
193      *            the resource.
194      * @param queryParamsMap
195      *            Map which can have the query parameter name and value.
196      * @param onPutListener
197      *            Event handler which will be invoked with the response for PUT
198      *            request with a map of attribute name and values.
199      *
200      * @throws InvalidArgsException
201      *             This exception will be thrown if any parameter has invalid
202      *             value.
203      * @throws NoSupportException
204      *             This exception will be thrown if we cannot send PUT request
205      *             to the remote resource.
206      * @throws SimulatorException
207      *             This exception will be thrown for other errors.
208      */
209     public void put(SimulatorResourceModel representation,
210             Map<String, String> queryParamsMap, IPutListener onPutListener)
211             throws InvalidArgsException, NoSupportException, SimulatorException {
212         this.put(null, representation, queryParamsMap, onPutListener);
213     }
214
215     /**
216      * API to send PUT request to the resource. Response will be notified
217      * asynchronously via callback set for {@link IPutListener}.
218      *
219      * @param resourceInterface
220      *            Interface type of the resource to operate on.
221      * @param representation
222      *            {@link SimulatorResourceModel} holding the representation of
223      *            the resource.
224      * @param queryParamsMap
225      *            Map which can have the query parameter name and value.
226      * @param onPutListener
227      *            Event handler which will be invoked with the response for PUT
228      *            request with a map of attribute name and values.
229      *
230      * @throws InvalidArgsException
231      *             This exception will be thrown if any parameter has invalid
232      *             value.
233      * @throws NoSupportException
234      *             This exception will be thrown if we cannot send PUT request
235      *             to the remote resource.
236      * @throws SimulatorException
237      *             This exception will be thrown for other errors.
238      */
239     private native void put(String resourceInterface,
240             SimulatorResourceModel representation,
241             Map<String, String> queryParamsMap, IPutListener onPutListener)
242             throws InvalidArgsException, NoSupportException, SimulatorException;
243
244     /**
245      * API to send POST request to the resource. Response will be notified
246      * asynchronously via callback set for {@link IPostListener}.
247      *
248      * @param representation
249      *            {@link SimulatorResourceModel} holding the representation of
250      *            the resource
251      * @param queryParamsMap
252      *            Map which can have the query parameter name and value
253      * @param onPostListener
254      *            Event handler which will be invoked with the response for POST
255      *            request with a map of attribute name and values.
256      *
257      * @throws InvalidArgsException
258      *             This exception will be thrown if any parameter has invalid
259      *             value.
260      * @throws NoSupportException
261      *             This exception will be thrown if we cannot send POST request
262      *             on the remote resource.
263      * @throws SimulatorException
264      *             This exception will be thrown for other errors.
265      */
266     public void post(SimulatorResourceModel representation,
267             Map<String, String> queryParamsMap, IPostListener onPostListener)
268             throws InvalidArgsException, NoSupportException, SimulatorException {
269         this.post(null, representation, queryParamsMap, onPostListener);
270     }
271
272     /**
273      * API to send POST request to the resource. Response will be notified
274      * asynchronously via callback set for {@link IPostListener}.
275      *
276      * @param resourceInterface
277      *            Interface type of the resource to operate on.
278      * @param representation
279      *            {@link SimulatorResourceModel} holding the representation of
280      *            the resource.
281      * @param queryParamsMap
282      *            Map which can have the query parameter name and value.
283      * @param onPostListener
284      *            Event handler which will be invoked with the response for POST
285      *            request with a map of attribute name and values.
286      *
287      * @throws InvalidArgsException
288      *             This exception will be thrown if any parameter has invalid
289      *             value.
290      * @throws NoSupportException
291      *             This exception will be thrown if we cannot send POST request
292      *             on the remote resource.
293      * @throws SimulatorException
294      *             This exception will be thrown for other errors.
295      */
296     public native void post(String resourceInterface,
297             SimulatorResourceModel representation,
298             Map<String, String> queryParamsMap, IPostListener onPostListener)
299             throws InvalidArgsException, NoSupportException, SimulatorException;
300
301     /**
302      * API to provide remote resource configure information,
303      * which is required for using automation feature.
304      *
305      * @param path
306      *            Path to RAML file.
307      *
308      * @throws InvalidArgsException
309      *             Thrown if the RAML configuration file path is invalid.
310      * @throws SimulatorException
311      *             Thrown for other errors.
312      */
313     public native void setConfigInfo(String path)
314             throws InvalidArgsException, SimulatorException;
315
316     /**
317      * API to send multiple requests for the resource, based on
318      * the configure file provided from {@link setConfigInfo}.
319      * This verifies response received as well.
320      *
321      * @param requestType
322      *            Request type to verify.
323      * @param onVerifyListener
324      *            This event handler will be invoked with the current status of
325      *            the automation.
326      *
327      * @return Automation ID.
328      *
329      * @throws InvalidArgsException
330      *             This exception will be thrown if any parameter has invalid
331      *             value.
332      * @throws NoSupportException
333      *             Thrown either if the resource does not support the request
334      *             type or the resource is not configured with RAML.
335      * @throws OperationInProgressException
336      *             Thrown if another request generation session is already in
337      *             progress.
338      * @throws SimulatorException
339      *             This exception will be thrown for other errors.
340      */
341     public int startVerification(SimulatorVerificationType requestType,
342             IVerificationListener onVerifyListener)
343             throws InvalidArgsException, NoSupportException,
344             OperationInProgressException, SimulatorException {
345         return startVerification(requestType.ordinal(), onVerifyListener);
346     }
347
348     /**
349      * API to stop sending requests which has been started using {@link setConfigInfo}.
350      *
351      * @param id
352      *            Automation ID.
353      *
354      * @throws InvalidArgsException
355      *             Thrown if the automation ID is invalid.
356      * @throws NoSupportException
357      *             Thrown if the resource is not configured with RAML.
358      * @throws SimulatorException
359      *             Thrown for other errors.
360      */
361     public native void stopVerification(int id) throws InvalidArgsException,
362             NoSupportException, SimulatorException;
363
364     private native int startVerification(int requestType,
365             IVerificationListener onVerifyListener)
366             throws InvalidArgsException, NoSupportException,
367             OperationInProgressException, SimulatorException;
368
369     @Override
370     protected void finalize() throws Throwable {
371         super.finalize();
372
373         dispose();
374     }
375
376     private native void dispose();
377
378     private long               nativeHandle;
379     private String             mUri;
380     private int                mConnType;
381     private String             mHost;
382     private String             mId;
383     private LinkedList<String> mResTypes;
384     private LinkedList<String> mResInterfaces;
385     private boolean            mIsObservable;
386 }