2 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.oic.simulator.client;
20 import java.util.Vector;
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 import org.oic.simulator.SimulatorResult;
30 * SimulatorRemoteResource represents a Resource running in the remote Simulator
31 * Server. It comes with a well-defined contract or interface onto which you can
32 * perform different operations or subscribe for event notifications.
34 public final class SimulatorRemoteResource {
36 private long mNativeHandle;
38 private int mConnType;
41 private Vector<String> mResTypes;
42 private Vector<String> mResInterfaces;
43 private boolean mIsObservable;
45 private native void dispose();
47 private SimulatorRemoteResource(long nativeHandle) {
48 mNativeHandle = nativeHandle;
52 protected void finalize() throws Throwable {
55 } catch (Throwable t) {
62 public enum VerificationType {
63 GET, PUT, POST, DELETE;
67 * API to get the URI for this resource.
69 * @return Resource URI
71 public String getURI() {
76 * API to get the connectivity type for this resource.
78 * @return Connectivity type.
80 public SimulatorConnectivityType getConnectivityType() {
81 return SimulatorConnectivityType.getConnectivityType(mConnType);
85 * API to get the list of resource types.
87 * @return Array of resource types.
89 public Vector<String> getResourceTypes() {
94 * API to get the list of resource interfaces.
96 * @return Array of resource interfaces.
98 public Vector<String> getResourceInterfaces() {
99 return mResInterfaces;
103 * API to get host address and port information of the resource.
105 * @return Host address.
107 public String getHost() {
112 * API to get a unique Id of the resource .
116 public String getId() {
121 * API to get the observe policy of this resource.
123 * @return True if the resource is observable, otherwise false.
125 public boolean isObservable() {
126 return mIsObservable;
130 * API to send GET request to the resource. Response will be notified
131 * asynchronously via callback set for {@link IGetListener}.
134 * Map which can have the query parameter name and value.
135 * @param onGetListener
136 * Event handler which will be invoked with the response for GET
137 * request with a map of attribute name and values.
139 * @throws InvalidArgsException
140 * This exception will be thrown if any parameter has invalid
142 * @throws SimulatorException
143 * This exception will be thrown for other errors.
145 public void get(Map<String, String> queryParams,
146 GetResponseListener onGetListener) throws InvalidArgsException,
148 nativeGet(null, queryParams, onGetListener);
152 * API to send GET request to the resource. Response will be notified
153 * asynchronously via callback set for {@link IGetListener}.
155 * @param resourceInterface
156 * Interface type of the resource to operate on.
158 * Map which can have the query parameter name and value.
159 * @param onGetListener
160 * Event handler which will be invoked with the response for GET
161 * request with a map of attribute name and values.
163 * @throws InvalidArgsException
164 * This exception will be thrown if any parameter has invalid
166 * @throws SimulatorException
167 * This exception will be thrown for other errors.
169 public void get(String resourceInterface, Map<String, String> queryParams,
170 GetResponseListener onGetListener) throws InvalidArgsException,
172 if (null == resourceInterface || resourceInterface.isEmpty())
173 throw new InvalidArgsException(
174 SimulatorResult.SIMULATOR_INVALID_PARAM,
175 "Invalid resource interface!");
176 nativeGet(resourceInterface, queryParams, onGetListener);
180 * API to send PUT request to the resource. Response will be notified
181 * asynchronously via callback set for {@link IPutListener}.
184 * Map which can have the query parameter name and value.
185 * @param representation
186 * {@link SimulatorResourceModel} holding the representation of
188 * @param onPutListener
189 * Event handler which will be invoked with the response for PUT
190 * request with a map of attribute name and values.
192 * @throws InvalidArgsException
193 * This exception will be thrown if any parameter has invalid
195 * @throws SimulatorException
196 * This exception will be thrown for other errors.
198 public void put(Map<String, String> queryParams,
199 SimulatorResourceModel representation,
200 PutResponseListener onPutListener) throws InvalidArgsException,
202 nativePut(null, queryParams, representation, onPutListener);
206 * API to send PUT request to the resource. Response will be notified
207 * asynchronously via callback set for {@link IPutListener}.
209 * @param resourceInterface
210 * Interface type of the resource to operate on.
212 * Map which can have the query parameter name and value.
213 * @param representation
214 * {@link SimulatorResourceModel} holding the representation of
216 * @param onPutListener
217 * Event handler which will be invoked with the response for PUT
218 * request with a map of attribute name and values.
220 * @throws InvalidArgsException
221 * This exception will be thrown if any parameter has invalid
223 * @throws SimulatorException
224 * This exception will be thrown for other errors.
226 public void put(String resourceInterface, Map<String, String> queryParams,
227 SimulatorResourceModel representation,
228 PutResponseListener onPutListener) throws InvalidArgsException,
230 if (null == resourceInterface || resourceInterface.isEmpty())
231 throw new InvalidArgsException(
232 SimulatorResult.SIMULATOR_INVALID_PARAM,
233 "Invalid resource interface!");
234 nativePut(resourceInterface, queryParams, representation, onPutListener);
238 * API to send POST request to the resource. Response will be notified
239 * asynchronously via callback set for {@link IPostListener}.
242 * Map which can have the query parameter name and value.
243 * @param representation
244 * {@link SimulatorResourceModel} holding the representation of
246 * @param onPostListener
247 * Event handler which will be invoked with the response for POST
248 * request with a map of attribute name and values.
250 * @throws InvalidArgsException
251 * This exception will be thrown if any parameter has invalid
253 * @throws SimulatorException
254 * This exception will be thrown for other errors.
256 public void post(Map<String, String> queryParams,
257 SimulatorResourceModel representation,
258 PostResponseListener onPostListener) throws InvalidArgsException,
260 nativePost(null, queryParams, representation, onPostListener);
264 * API to send POST request to the resource. Response will be notified
265 * asynchronously via callback set for {@link IPostListener}.
267 * @param resourceInterface
268 * Interface type of the resource to operate on.
270 * Map which can have the query parameter name and value.
271 * @param representation
272 * {@link SimulatorResourceModel} holding the representation of
274 * @param onPostListener
275 * Event handler which will be invoked with the response for POST
276 * request with a map of attribute name and values.
278 * @throws InvalidArgsException
279 * This exception will be thrown if any parameter has invalid
281 * @throws SimulatorException
282 * This exception will be thrown for other errors.
284 public void post(String resourceInterface, Map<String, String> queryParams,
285 SimulatorResourceModel representation,
286 PostResponseListener onPostListener) throws InvalidArgsException,
288 if (null == resourceInterface || resourceInterface.isEmpty())
289 throw new InvalidArgsException(
290 SimulatorResult.SIMULATOR_INVALID_PARAM,
291 "Invalid resource interface!");
292 nativePost(resourceInterface, queryParams, representation,
297 * API to start observing the resource.
300 * Map which can have the query parameter names and values.
301 * @param onObserveListener
302 * The handler method which will be invoked with a map of
303 * attribute names and values whenever there is a change in
304 * resource model of the remote resource.
306 * @throws InvalidArgsException
307 * This exception will be thrown if any parameter has invalid
309 * @throws SimulatorException
310 * This exception will be thrown for other errors.
312 public native void startObserve(Map<String, String> queryParams,
313 ObserveNotificationListener onObserveListener)
314 throws InvalidArgsException, SimulatorException;
317 * API to stop observing the resource.
319 * @throws InvalidArgsException
320 * This exception will be thrown if the native remote resource
321 * object is unavailable.
322 * @throws SimulatorException
323 * This exception will be thrown for other errors.
325 public native void stopObserve() throws InvalidArgsException,
329 * API to provide remote resource configure information, which is required
330 * for using automation feature.
335 * @return representation {@link SimulatorResourceModel} holding the
336 * representation of the remote resource.
338 * @throws InvalidArgsException
339 * Thrown if the RAML configuration file path is invalid.
340 * @throws SimulatorException
341 * Thrown for other errors.
343 public native SimulatorResourceModel setConfigInfo(String path)
344 throws InvalidArgsException, SimulatorException;
347 * API to send multiple requests for the resource, based on the configure
348 * file provided from {@link setConfigInfo}. This verifies response received
352 * Request type to verify.
353 * @param onVerifyListener
354 * This event handler will be invoked with the current status of
357 * @return Automation ID.
359 * @throws InvalidArgsException
360 * This exception will be thrown if any parameter has invalid
362 * @throws NoSupportException
363 * Thrown either if the resource does not support the request
364 * type or the resource is not configured with RAML.
365 * @throws OperationInProgressException
366 * Thrown if another request generation session is already in
368 * @throws SimulatorException
369 * This exception will be thrown for other errors.
371 public int startVerification(VerificationType type,
372 VerificationListener onVerifyListener) throws InvalidArgsException,
373 NoSupportException, OperationInProgressException,
375 return startVerification(type.ordinal(), onVerifyListener);
379 * API to stop sending requests which has been started using
380 * {@link setConfigInfo}.
385 * @throws InvalidArgsException
386 * Thrown if the automation ID is invalid.
387 * @throws SimulatorException
388 * Thrown for other errors.
390 public native void stopVerification(int id) throws InvalidArgsException,
394 * Listener for receiving asynchronous response for GET request.
396 public interface GetResponseListener {
398 * Method will be called when response for GET request arrives.
401 * Unique Id of the resource.
403 * Error code {@link SimulatorResult}.
404 * @param resourceModel
405 * {@link SimulatorResourceModel}.
407 public void onGetResponse(String uid, SimulatorResult result,
408 SimulatorResourceModel resourceModel);
412 * Listener for receiving asynchronous response for PUT request.
414 public interface PutResponseListener {
416 * Method will be called when response for PUT request arrives.
419 * Unique Id of the resource.
421 * Error code {@link SimulatorResult}.
422 * @param resourceModel
423 * {@link SimulatorResourceModel}.
425 public void onPutResponse(String uid, SimulatorResult result,
426 SimulatorResourceModel resourceModel);
430 * Listener for receiving asynchronous response for POST request.
432 public interface PostResponseListener {
434 * Method will be called when response for POST request arrives.
437 * Unique Id of the resource.
439 * Error code {@link SimulatorResult}.
440 * @param resourceModel
441 * {@link SimulatorResourceModel}.
443 public void onPostResponse(String uid, SimulatorResult result,
444 SimulatorResourceModel resourceModel);
448 * Listener for getting asynchronous notification whenever remote resource's
449 * representation gets changed.
451 public interface ObserveNotificationListener {
453 * This method will be called when there is a change in the resource
454 * model of the remote resource.
457 * Unique Id of the resource.
458 * @param resourceModel
459 * {@link SimulatorResourceModel}.
460 * @param sequenceNumber
461 * Sequential number for ordering the model change
464 public void onObserveNotification(String uid,
465 SimulatorResourceModel resourceModel, int sequenceNumber);
469 * Listener for receiving the verification session status.
471 public interface VerificationListener {
473 * Called when the verification request is accepted and started.
476 * Unique Id of the resource.
480 public void onVerificationStarted(String uid, int id);
483 * Called when the verification is stopped before its completion.
486 * Unique Id of the resource.
490 public void onVerificationAborted(String uid, int id);
493 * Called when the verification is done.
496 * Unique Id of the resource.
500 public void onVerificationCompleted(String uid, int id);
503 private native void nativeGet(String resourceInterface,
504 Map<String, String> queryParamsMap,
505 GetResponseListener onGetListener);
507 private native void nativePut(String resourceInterface,
508 Map<String, String> queryParams,
509 SimulatorResourceModel representation,
510 PutResponseListener onPutListener);
512 private native void nativePost(String resourceInterface,
513 Map<String, String> queryParams,
514 SimulatorResourceModel representation,
515 PostResponseListener onPostListener);
517 private native int startVerification(int type,
518 VerificationListener onVerifyListener);