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) {
63 * Enum to represent the verification types which can be used by the client
64 * to verify the resource model of the remote resource.
66 public enum VerificationType {
67 GET, PUT, POST, DELETE;
71 * API to get the URI for this resource.
73 * @return Resource URI.
75 public String getURI() {
80 * API to get the connectivity type for this resource.
82 * @return Connectivity type.
84 public SimulatorConnectivityType getConnectivityType() {
85 return SimulatorConnectivityType.getConnectivityType(mConnType);
89 * API to get the list of resource types.
91 * @return Resource types.
93 public Vector<String> getResourceTypes() {
98 * API to get the list of resource interfaces.
100 * @return Resource interfaces.
102 public Vector<String> getResourceInterfaces() {
103 return mResInterfaces;
107 * API to get the address detail of the resource.
109 * @return Host address.
111 public String getHost() {
116 * API to get a unique Id of the resource.
120 public String getId() {
125 * API to get the observe policy of this resource.
127 * @return True if the resource is observable, otherwise false.
129 public boolean isObservable() {
130 return mIsObservable;
134 * API to send GET request to the resource. Response will be notified
135 * asynchronously via callback set for {@link GetResponseListener}.
138 * Map which can have the query parameter name and value.
139 * @param onGetListener
140 * Event handler which will be invoked with the response for GET
141 * request with a map of attribute name and values.
143 * @throws InvalidArgsException
144 * This exception will be thrown if any parameter has invalid
146 * @throws SimulatorException
147 * This exception will be thrown for other errors.
149 public void get(Map<String, String> queryParams,
150 GetResponseListener onGetListener) throws InvalidArgsException,
152 nativeGet(null, queryParams, onGetListener);
156 * API to send GET request to the resource. Response will be notified
157 * asynchronously via callback set for {@link GetResponseListener}.
159 * @param resourceInterface
160 * Interface type of the resource to operate on.
162 * Map which can have the query parameter name and value.
163 * @param onGetListener
164 * Event handler which will be invoked with the response for GET
165 * request with a map of attribute name and values.
167 * @throws InvalidArgsException
168 * This exception will be thrown if any parameter has invalid
170 * @throws SimulatorException
171 * This exception will be thrown for other errors.
173 public void get(String resourceInterface, Map<String, String> queryParams,
174 GetResponseListener onGetListener) throws InvalidArgsException,
176 if (null == resourceInterface || resourceInterface.isEmpty())
177 throw new InvalidArgsException(
178 SimulatorResult.SIMULATOR_INVALID_PARAM,
179 "Invalid resource interface!");
180 nativeGet(resourceInterface, queryParams, onGetListener);
184 * API to send PUT request to the resource. Response will be notified
185 * asynchronously via callback set for {@link PutResponseListener}.
188 * Map which can have the query parameter name and value.
189 * @param representation
190 * {@link SimulatorResourceModel} holding the representation of
192 * @param onPutListener
193 * Event handler which will be invoked with the response for PUT
194 * request with a map of attribute name and values.
196 * @throws InvalidArgsException
197 * This exception will be thrown if any parameter has invalid
199 * @throws SimulatorException
200 * This exception will be thrown for other errors.
202 public void put(Map<String, String> queryParams,
203 SimulatorResourceModel representation,
204 PutResponseListener onPutListener) throws InvalidArgsException,
206 nativePut(null, queryParams, representation, onPutListener);
210 * API to send PUT request to the resource. Response will be notified
211 * asynchronously via callback set for {@link PutResponseListener}.
213 * @param resourceInterface
214 * Interface type of the resource to operate on.
216 * Map which can have the query parameter name and value.
217 * @param representation
218 * {@link SimulatorResourceModel} holding the representation of
220 * @param onPutListener
221 * Event handler which will be invoked with the response for PUT
222 * request with a map of attribute name and values.
224 * @throws InvalidArgsException
225 * This exception will be thrown if any parameter has invalid
227 * @throws SimulatorException
228 * This exception will be thrown for other errors.
230 public void put(String resourceInterface, Map<String, String> queryParams,
231 SimulatorResourceModel representation,
232 PutResponseListener onPutListener) throws InvalidArgsException,
234 if (null == resourceInterface || resourceInterface.isEmpty())
235 throw new InvalidArgsException(
236 SimulatorResult.SIMULATOR_INVALID_PARAM,
237 "Invalid resource interface!");
238 nativePut(resourceInterface, queryParams, representation, onPutListener);
242 * API to send POST request to the resource. Response will be notified
243 * asynchronously via callback set for {@link PostResponseListener}.
246 * Map which can have the query parameter name and value.
247 * @param representation
248 * {@link SimulatorResourceModel} holding the representation of
250 * @param onPostListener
251 * Event handler which will be invoked with the response for POST
252 * request with a map of attribute name and values.
254 * @throws InvalidArgsException
255 * This exception will be thrown if any parameter has invalid
257 * @throws SimulatorException
258 * This exception will be thrown for other errors.
260 public void post(Map<String, String> queryParams,
261 SimulatorResourceModel representation,
262 PostResponseListener onPostListener) throws InvalidArgsException,
264 nativePost(null, queryParams, representation, onPostListener);
268 * API to send POST request to the resource. Response will be notified
269 * asynchronously via callback set for {@link PostResponseListener}.
271 * @param resourceInterface
272 * Interface type of the resource to operate on.
274 * Map which can have the query parameter name and value.
275 * @param representation
276 * {@link SimulatorResourceModel} holding the representation of
278 * @param onPostListener
279 * Event handler which will be invoked with the response for POST
280 * request with a map of attribute name and values.
282 * @throws InvalidArgsException
283 * This exception will be thrown if any parameter has invalid
285 * @throws SimulatorException
286 * This exception will be thrown for other errors.
288 public void post(String resourceInterface, Map<String, String> queryParams,
289 SimulatorResourceModel representation,
290 PostResponseListener onPostListener) throws InvalidArgsException,
292 if (null == resourceInterface || resourceInterface.isEmpty())
293 throw new InvalidArgsException(
294 SimulatorResult.SIMULATOR_INVALID_PARAM,
295 "Invalid resource interface!");
296 nativePost(resourceInterface, queryParams, representation,
301 * API to start observing the resource.
304 * Map which can have the query parameter names and values.
305 * @param onObserveListener
306 * The handler method which will be invoked with a map of
307 * attribute names and values whenever there is a change in
308 * resource model of the remote resource.
310 * @throws InvalidArgsException
311 * This exception will be thrown if any parameter has invalid
313 * @throws SimulatorException
314 * This exception will be thrown for other errors.
316 public native void startObserve(Map<String, String> queryParams,
317 ObserveNotificationListener onObserveListener)
318 throws InvalidArgsException, SimulatorException;
321 * API to stop observing the resource.
323 * @throws InvalidArgsException
324 * This exception will be thrown if the native remote resource
325 * object is unavailable.
326 * @throws SimulatorException
327 * This exception will be thrown for other errors.
329 public native void stopObserve() throws InvalidArgsException,
333 * API to provide remote resource configure information, which is required
334 * for using automation feature.
339 * @return Representation {@link SimulatorResourceModel} holding the
340 * representation of the remote resource.
342 * @throws InvalidArgsException
343 * Thrown if the RAML configuration file path is invalid.
344 * @throws SimulatorException
345 * Thrown for other errors.
347 public native SimulatorResourceModel setConfigInfo(String path)
348 throws InvalidArgsException, SimulatorException;
351 * API to send multiple requests for the resource, based on the configure
352 * file provided from {@link setConfigInfo}. This verifies response received
356 * Request type to verify.
357 * @param onVerifyListener
358 * This event handler will be invoked with the current status of
361 * @return Automation ID.
363 * @throws InvalidArgsException
364 * This exception will be thrown if any parameter has invalid
366 * @throws NoSupportException
367 * Thrown either if the resource does not support the request
368 * type or the resource is not configured with RAML.
369 * @throws OperationInProgressException
370 * Thrown if another request generation session is already in
372 * @throws SimulatorException
373 * This exception will be thrown for other errors.
375 public int startVerification(VerificationType type,
376 VerificationListener onVerifyListener) throws InvalidArgsException,
377 NoSupportException, OperationInProgressException,
379 return startVerification(type.ordinal(), onVerifyListener);
383 * API to stop sending requests which has been started using
384 * {@link startVerification}.
389 * @throws InvalidArgsException
390 * Thrown if the automation ID is invalid.
391 * @throws SimulatorException
392 * Thrown for other errors.
394 public native void stopVerification(int id) throws InvalidArgsException,
398 * Listener for receiving asynchronous response for GET request.
400 public interface GetResponseListener {
402 * Method will be called when response for GET request arrives.
405 * Unique Id of the resource.
407 * Error code {@link SimulatorResult}.
408 * @param resourceModel
409 * {@link SimulatorResourceModel}.
411 public void onGetResponse(String uid, SimulatorResult result,
412 SimulatorResourceModel resourceModel);
416 * Listener for receiving asynchronous response for PUT request.
418 public interface PutResponseListener {
420 * Method will be called when response for PUT request arrives.
423 * Unique Id of the resource.
425 * Error code {@link SimulatorResult}.
426 * @param resourceModel
427 * {@link SimulatorResourceModel}.
429 public void onPutResponse(String uid, SimulatorResult result,
430 SimulatorResourceModel resourceModel);
434 * Listener for receiving asynchronous response for POST request.
436 public interface PostResponseListener {
438 * Method will be called when response for POST request arrives.
441 * Unique Id of the resource.
443 * Error code {@link SimulatorResult}.
444 * @param resourceModel
445 * {@link SimulatorResourceModel}.
447 public void onPostResponse(String uid, SimulatorResult result,
448 SimulatorResourceModel resourceModel);
452 * Listener for getting asynchronous notification whenever remote resource's
453 * representation gets changed.
455 public interface ObserveNotificationListener {
457 * This method will be called when there is a change in the resource
458 * model of the remote resource.
461 * Unique Id of the resource.
462 * @param resourceModel
463 * {@link SimulatorResourceModel}.
464 * @param sequenceNumber
465 * Sequential number for ordering the model change
468 public void onObserveNotification(String uid,
469 SimulatorResourceModel resourceModel, int sequenceNumber);
473 * Listener for receiving the verification session status.
475 public interface VerificationListener {
477 * Called when the verification request is accepted and started.
480 * Unique Id of the resource.
484 public void onVerificationStarted(String uid, int id);
487 * Called when the verification is stopped before its completion.
490 * Unique Id of the resource.
494 public void onVerificationAborted(String uid, int id);
497 * Called when the verification is done.
500 * Unique Id of the resource.
504 public void onVerificationCompleted(String uid, int id);
507 private native void nativeGet(String resourceInterface,
508 Map<String, String> queryParamsMap,
509 GetResponseListener onGetListener);
511 private native void nativePut(String resourceInterface,
512 Map<String, String> queryParams,
513 SimulatorResourceModel representation,
514 PutResponseListener onPutListener);
516 private native void nativePost(String resourceInterface,
517 Map<String, String> queryParams,
518 SimulatorResourceModel representation,
519 PostResponseListener onPostListener);
521 private native int startVerification(int type,
522 VerificationListener onVerifyListener);