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 {
35 private long mNativeHandle;
37 private int mConnType;
40 private Vector<String> mResTypes;
41 private Vector<String> mResInterfaces;
42 private boolean mIsObservable;
45 protected void finalize() throws Throwable {
48 } catch (Throwable t) {
56 * Enum to represent the verification types which can be used by the client
57 * to verify the resource model of the remote resource.
59 public enum RequestType {
60 UKNOWN, GET, PUT, POST, DELETE;
64 * API to get the URI for this resource.
66 * @return Resource URI.
68 public String getURI() {
73 * API to get the address detail of the resource.
75 * @return Host address.
77 public String getHost() {
82 * API to get a unique Id of the resource.
86 public String getId() {
91 * API to get the connectivity type for this resource.
93 * @return Connectivity type.
95 public SimulatorConnectivityType getConnectivityType() {
96 return SimulatorConnectivityType.getConnectivityType(mConnType);
100 * API to get the list of resource types.
102 * @return Resource types.
104 public Vector<String> getResourceTypes() {
109 * API to get the list of resource interfaces.
111 * @return Resource interfaces.
113 public Vector<String> getResourceInterfaces() {
114 return mResInterfaces;
118 * API to get the observe policy of this resource.
120 * @return True if the resource is observable, otherwise false.
122 public boolean isObservable() {
123 return mIsObservable;
127 * API to send GET request to the resource. Response will be notified
128 * asynchronously via callback set for {@link GetResponseListener}.
131 * Map which can have the query parameter name and value.
132 * @param onGetListener
133 * Event handler which will be invoked with the response for GET
134 * request with a map of attribute name and values.
136 * @throws InvalidArgsException
137 * This exception will be thrown if any parameter has invalid
139 * @throws SimulatorException
140 * This exception will be thrown for other errors.
142 public void get(Map<String, String> queryParams,
143 GetResponseListener onGetListener) throws InvalidArgsException,
145 nativeGet(null, queryParams, onGetListener);
149 * API to send GET request to the resource. Response will be notified
150 * asynchronously via callback set for {@link GetResponseListener}.
152 * @param resourceInterface
153 * Interface type of the resource to operate on.
155 * Map which can have the query parameter name and value.
156 * @param onGetListener
157 * Event handler which will be invoked with the response for GET
158 * request with a map of attribute name and values.
160 * @throws InvalidArgsException
161 * This exception will be thrown if any parameter has invalid
163 * @throws SimulatorException
164 * This exception will be thrown for other errors.
166 public void get(String resourceInterface, Map<String, String> queryParams,
167 GetResponseListener onGetListener) throws InvalidArgsException,
169 if (null == resourceInterface || resourceInterface.isEmpty())
170 throw new InvalidArgsException(
171 SimulatorResult.SIMULATOR_INVALID_PARAM,
172 "Invalid resource interface!");
173 nativeGet(resourceInterface, queryParams, onGetListener);
177 * API to send PUT request to the resource. Response will be notified
178 * asynchronously via callback set for {@link PutResponseListener}.
181 * Map which can have the query parameter name and value.
182 * @param representation
183 * {@link SimulatorResourceModel} holding the representation of
185 * @param onPutListener
186 * Event handler which will be invoked with the response for PUT
187 * request with a map of attribute name and values.
189 * @throws InvalidArgsException
190 * This exception will be thrown if any parameter has invalid
192 * @throws SimulatorException
193 * This exception will be thrown for other errors.
195 public void put(Map<String, String> queryParams,
196 SimulatorResourceModel representation,
197 PutResponseListener onPutListener) throws InvalidArgsException,
199 nativePut(null, queryParams, representation, onPutListener);
203 * API to send PUT request to the resource. Response will be notified
204 * asynchronously via callback set for {@link PutResponseListener}.
206 * @param resourceInterface
207 * Interface type of the resource to operate on.
209 * Map which can have the query parameter name and value.
210 * @param representation
211 * {@link SimulatorResourceModel} holding the representation of
213 * @param onPutListener
214 * Event handler which will be invoked with the response for PUT
215 * request with a map of attribute name and values.
217 * @throws InvalidArgsException
218 * This exception will be thrown if any parameter has invalid
220 * @throws SimulatorException
221 * This exception will be thrown for other errors.
223 public void put(String resourceInterface, Map<String, String> queryParams,
224 SimulatorResourceModel representation,
225 PutResponseListener onPutListener) throws InvalidArgsException,
227 if (null == resourceInterface || resourceInterface.isEmpty())
228 throw new InvalidArgsException(
229 SimulatorResult.SIMULATOR_INVALID_PARAM,
230 "Invalid resource interface!");
231 nativePut(resourceInterface, queryParams, representation, onPutListener);
235 * API to send POST request to the resource. Response will be notified
236 * asynchronously via callback set for {@link PostResponseListener}.
239 * Map which can have the query parameter name and value.
240 * @param representation
241 * {@link SimulatorResourceModel} holding the representation of
243 * @param onPostListener
244 * Event handler which will be invoked with the response for POST
245 * request with a map of attribute name and values.
247 * @throws InvalidArgsException
248 * This exception will be thrown if any parameter has invalid
250 * @throws SimulatorException
251 * This exception will be thrown for other errors.
253 public void post(Map<String, String> queryParams,
254 SimulatorResourceModel representation,
255 PostResponseListener onPostListener) throws InvalidArgsException,
257 nativePost(null, queryParams, representation, onPostListener);
261 * API to send POST request to the resource. Response will be notified
262 * asynchronously via callback set for {@link PostResponseListener}.
264 * @param resourceInterface
265 * Interface type of the resource to operate on.
267 * Map which can have the query parameter name and value.
268 * @param representation
269 * {@link SimulatorResourceModel} holding the representation of
271 * @param onPostListener
272 * Event handler which will be invoked with the response for POST
273 * request with a map of attribute name and values.
275 * @throws InvalidArgsException
276 * This exception will be thrown if any parameter has invalid
278 * @throws SimulatorException
279 * This exception will be thrown for other errors.
281 public void post(String resourceInterface, Map<String, String> queryParams,
282 SimulatorResourceModel representation,
283 PostResponseListener onPostListener) throws InvalidArgsException,
285 if (null == resourceInterface || resourceInterface.isEmpty())
286 throw new InvalidArgsException(
287 SimulatorResult.SIMULATOR_INVALID_PARAM,
288 "Invalid resource interface!");
289 nativePost(resourceInterface, queryParams, representation,
294 * API to start observing the resource.
297 * Type of observation.
298 * @param onObserveListener
299 * The handler method which will be invoked with a map of
300 * attribute names and values whenever there is a change in
301 * resource model of the remote resource.
303 * @throws InvalidArgsException
304 * This exception will be thrown if any parameter has invalid
306 * @throws SimulatorException
307 * This exception will be thrown for other errors.
309 public void observe(ObserveNotificationListener onObserveListener)
310 throws InvalidArgsException, SimulatorException {
311 nativeStartObserve(null, onObserveListener);
315 * API to start observing the resource.
318 * Type of observation.
320 * Map which can have the query parameter names and values.
321 * @param onObserveListener
322 * The handler method which will be invoked with a map of
323 * attribute names and values whenever there is a change in
324 * resource model of the remote resource.
326 * @throws InvalidArgsException
327 * This exception will be thrown if any parameter has invalid
329 * @throws SimulatorException
330 * This exception will be thrown for other errors.
332 public void observe(Map<String, String> queryParams,
333 ObserveNotificationListener onObserveListener)
334 throws InvalidArgsException, SimulatorException {
335 if (null == queryParams)
336 throw new InvalidArgsException(
337 SimulatorResult.SIMULATOR_INVALID_PARAM,
338 "Invalid Query Parameters!");
339 nativeStartObserve(queryParams, onObserveListener);
343 * API to stop observing the resource.
345 * @throws InvalidArgsException
346 * This exception will be thrown if the native remote resource
347 * object is unavailable.
348 * @throws SimulatorException
349 * This exception will be thrown for other errors.
351 public void stopObserve() throws InvalidArgsException, SimulatorException {
356 * API to provide remote resource configure information, which is required
357 * for using automation feature.
362 * @return Representation {@link SimulatorResourceModel} holding the
363 * representation of the remote resource.
365 * @throws InvalidArgsException
366 * Thrown if the RAML configuration file path is invalid.
367 * @throws SimulatorException
368 * Thrown for other errors.
370 public Map<RequestType, SimulatorRequestModel> setConfigInfo(String path)
371 throws InvalidArgsException, SimulatorException {
372 return nativeSetConfigInfo(path);
376 * API to send multiple requests for the resource, based on the configure
377 * file provided from {@link setConfigInfo}. This verifies response received
381 * Request type to verify.
382 * @param onVerifyListener
383 * This event handler will be invoked with the current status of
386 * @return Automation ID.
388 * @throws InvalidArgsException
389 * This exception will be thrown if any parameter has invalid
391 * @throws NoSupportException
392 * Thrown either if the resource does not support the request
393 * type or the resource is not configured with RAML.
394 * @throws OperationInProgressException
395 * Thrown if another request generation session is already in
397 * @throws SimulatorException
398 * This exception will be thrown for other errors.
400 public int startVerification(RequestType type,
401 VerificationListener onVerifyListener) throws InvalidArgsException,
402 NoSupportException, OperationInProgressException,
404 return nativeStartAutoRequesting(type, onVerifyListener);
408 * API to stop sending requests which has been started using
409 * {@link startVerification}.
414 * @throws InvalidArgsException
415 * Thrown if the automation ID is invalid.
416 * @throws SimulatorException
417 * Thrown for other errors.
419 public void stopVerification(int id) throws InvalidArgsException,
421 nativeStopAutoRequesting(id);
425 * Listener for receiving asynchronous response for GET request.
427 public interface GetResponseListener {
429 * Method will be called when response for GET request arrives.
432 * Unique Id of the resource.
434 * Error code {@link SimulatorResult}.
435 * @param resourceModel
436 * {@link SimulatorResourceModel}.
438 public void onGetResponse(String uid, SimulatorResult result,
439 SimulatorResourceModel resourceModel);
443 * Listener for receiving asynchronous response for PUT request.
445 public interface PutResponseListener {
447 * Method will be called when response for PUT request arrives.
450 * Unique Id of the resource.
452 * Error code {@link SimulatorResult}.
453 * @param resourceModel
454 * {@link SimulatorResourceModel}.
456 public void onPutResponse(String uid, SimulatorResult result,
457 SimulatorResourceModel resourceModel);
461 * Listener for receiving asynchronous response for POST request.
463 public interface PostResponseListener {
465 * Method will be called when response for POST request arrives.
468 * Unique Id of the resource.
470 * Error code {@link SimulatorResult}.
471 * @param resourceModel
472 * {@link SimulatorResourceModel}.
474 public void onPostResponse(String uid, SimulatorResult result,
475 SimulatorResourceModel resourceModel);
479 * Listener for getting asynchronous notification whenever remote resource's
480 * representation gets changed.
482 public interface ObserveNotificationListener {
484 * This method will be called when there is a change in the resource
485 * model of the remote resource.
488 * Unique Id of the resource.
489 * @param resourceModel
490 * {@link SimulatorResourceModel}.
491 * @param sequenceNumber
492 * Sequential number for ordering the model change
495 public void onObserveNotification(String uid,
496 SimulatorResourceModel resourceModel, int sequenceNumber);
500 * Listener for receiving the verification session status.
502 public interface VerificationListener {
504 * Called when the verification request is accepted and started.
507 * Unique Id of the resource.
511 public void onVerificationStarted(String uid, int id);
514 * Called when the verification is stopped before its completion.
517 * Unique Id of the resource.
521 public void onVerificationAborted(String uid, int id);
524 * Called when the verification is done.
527 * Unique Id of the resource.
531 public void onVerificationCompleted(String uid, int id);
534 private SimulatorRemoteResource(long nativeHandle) {
535 mNativeHandle = nativeHandle;
538 private native void nativeGet(String resourceInterface,
539 Map<String, String> queryParamsMap,
540 GetResponseListener onGetListener);
542 private native void nativePut(String resourceInterface,
543 Map<String, String> queryParams,
544 SimulatorResourceModel representation,
545 PutResponseListener onPutListener);
547 private native void nativePost(String resourceInterface,
548 Map<String, String> queryParams,
549 SimulatorResourceModel representation,
550 PostResponseListener onPostListener);
552 private native void nativeStartObserve(Map<String, String> queryParams,
553 ObserveNotificationListener onObserveListener);
555 private native void nativeStopObserve();
557 private native Map<RequestType, SimulatorRequestModel> nativeSetConfigInfo(
560 private native int nativeStartAutoRequesting(RequestType type,
561 VerificationListener onVerifyListener);
563 private native void nativeStopAutoRequesting(int id);
565 private native void nativeDispose();