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.
296 * @param onObserveListener
297 * The handler method which will be invoked with a map of
298 * attribute names and values whenever there is a change in
299 * resource model of the remote resource.
301 * @throws InvalidArgsException
302 * This exception will be thrown if any parameter has invalid
304 * @throws SimulatorException
305 * This exception will be thrown for other errors.
307 public void observe(ObserveNotificationListener onObserveListener)
308 throws InvalidArgsException, SimulatorException {
309 nativeStartObserve(null, onObserveListener);
313 * API to start observing the resource.
316 * Map which can have the query parameter names and values.
317 * @param onObserveListener
318 * The handler method which will be invoked with a map of
319 * attribute names and values whenever there is a change in
320 * resource model of the remote resource.
322 * @throws InvalidArgsException
323 * This exception will be thrown if any parameter has invalid
325 * @throws SimulatorException
326 * This exception will be thrown for other errors.
328 public void observe(Map<String, String> queryParams,
329 ObserveNotificationListener onObserveListener)
330 throws InvalidArgsException, SimulatorException {
331 if (null == queryParams)
332 throw new InvalidArgsException(
333 SimulatorResult.SIMULATOR_INVALID_PARAM,
334 "Invalid Query Parameters!");
335 nativeStartObserve(queryParams, onObserveListener);
339 * API to stop observing the resource.
341 * @throws InvalidArgsException
342 * This exception will be thrown if the native remote resource
343 * object is unavailable.
344 * @throws SimulatorException
345 * This exception will be thrown for other errors.
347 public void stopObserve() throws InvalidArgsException, SimulatorException {
352 * API to provide remote resource configure information, which is required
353 * for using automation feature.
358 * @return Representation {@link SimulatorResourceModel} holding the
359 * representation of the remote resource.
361 * @throws InvalidArgsException
362 * Thrown if the RAML configuration file path is invalid.
363 * @throws SimulatorException
364 * Thrown for other errors.
366 public Map<RequestType, SimulatorRequestModel> setConfigInfo(String path)
367 throws InvalidArgsException, SimulatorException {
368 return nativeSetConfigInfo(path);
372 * API to send multiple requests for the resource, based on the configure
373 * file provided from {@link setConfigInfo}. This verifies response received
377 * Request type to verify.
378 * @param onVerifyListener
379 * This event handler will be invoked with the current status of
382 * @return Automation ID.
384 * @throws InvalidArgsException
385 * This exception will be thrown if any parameter has invalid
387 * @throws NoSupportException
388 * Thrown either if the resource does not support the request
389 * type or the resource is not configured with RAML.
390 * @throws OperationInProgressException
391 * Thrown if another request generation session is already in
393 * @throws SimulatorException
394 * This exception will be thrown for other errors.
396 public int startVerification(RequestType type,
397 VerificationListener onVerifyListener) throws InvalidArgsException,
398 NoSupportException, OperationInProgressException,
400 return nativeStartAutoRequesting(type, onVerifyListener);
404 * API to stop sending requests which has been started using
405 * {@link startVerification}.
410 * @throws InvalidArgsException
411 * Thrown if the automation ID is invalid.
412 * @throws SimulatorException
413 * Thrown for other errors.
415 public void stopVerification(int id) throws InvalidArgsException,
417 nativeStopAutoRequesting(id);
421 * Listener for receiving asynchronous response for GET request.
423 public interface GetResponseListener {
425 * Method will be called when response for GET request arrives.
428 * Unique Id of the resource.
430 * Error code {@link SimulatorResult}.
431 * @param resourceModel
432 * {@link SimulatorResourceModel}.
434 public void onGetResponse(String uid, SimulatorResult result,
435 SimulatorResourceModel resourceModel);
439 * Listener for receiving asynchronous response for PUT request.
441 public interface PutResponseListener {
443 * Method will be called when response for PUT request arrives.
446 * Unique Id of the resource.
448 * Error code {@link SimulatorResult}.
449 * @param resourceModel
450 * {@link SimulatorResourceModel}.
452 public void onPutResponse(String uid, SimulatorResult result,
453 SimulatorResourceModel resourceModel);
457 * Listener for receiving asynchronous response for POST request.
459 public interface PostResponseListener {
461 * Method will be called when response for POST request arrives.
464 * Unique Id of the resource.
466 * Error code {@link SimulatorResult}.
467 * @param resourceModel
468 * {@link SimulatorResourceModel}.
470 public void onPostResponse(String uid, SimulatorResult result,
471 SimulatorResourceModel resourceModel);
475 * Listener for getting asynchronous notification whenever remote resource's
476 * representation gets changed.
478 public interface ObserveNotificationListener {
480 * This method will be called when there is a change in the resource
481 * model of the remote resource.
484 * Unique Id of the resource.
485 * @param resourceModel
486 * {@link SimulatorResourceModel}.
487 * @param sequenceNumber
488 * Sequential number for ordering the model change
491 public void onObserveNotification(String uid,
492 SimulatorResourceModel resourceModel, int sequenceNumber);
496 * Listener for receiving the verification session status.
498 public interface VerificationListener {
500 * Called when the verification request is accepted and started.
503 * Unique Id of the resource.
507 public void onVerificationStarted(String uid, int id);
510 * Called when the verification is stopped before its completion.
513 * Unique Id of the resource.
517 public void onVerificationAborted(String uid, int id);
520 * Called when the verification is done.
523 * Unique Id of the resource.
527 public void onVerificationCompleted(String uid, int id);
530 private SimulatorRemoteResource(long nativeHandle) {
531 mNativeHandle = nativeHandle;
534 private native void nativeGet(String resourceInterface,
535 Map<String, String> queryParamsMap,
536 GetResponseListener onGetListener);
538 private native void nativePut(String resourceInterface,
539 Map<String, String> queryParams,
540 SimulatorResourceModel representation,
541 PutResponseListener onPutListener);
543 private native void nativePost(String resourceInterface,
544 Map<String, String> queryParams,
545 SimulatorResourceModel representation,
546 PostResponseListener onPostListener);
548 private native void nativeStartObserve(Map<String, String> queryParams,
549 ObserveNotificationListener onObserveListener);
551 private native void nativeStopObserve();
553 private native Map<RequestType, SimulatorRequestModel> nativeSetConfigInfo(
556 private native int nativeStartAutoRequesting(RequestType type,
557 VerificationListener onVerifyListener);
559 private native void nativeStopAutoRequesting(int id);
561 private native void nativeDispose();