Trace:
Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
jvx:common:util:rest [2018/02/08 08:08] admin [Call actions] |
jvx:common:util:rest [2020/06/29 13:38] cduncan |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ~~Title: REST service~~ | + | ~~Title: REST Services~~ |
- | We define the business logic with [[jvx:server:lco:objects|life-cycle objects]] on the server side. The access authorization of an application is checked by a [[jvx:server:security:manager|security manager]]. The business logic is usually available via master- or subconnections from the client. | + | We define the business logic with [[jvx:server:lco:objects|life cycle objects]] on the server side. The access authorization of an application is checked by a [[jvx:server:security:manager|security manager]]. The business logic is usually available via master- or subconnections from the client. |
In order to complete the technology independence, the complete business logic of an application is also available via REST. | In order to complete the technology independence, the complete business logic of an application is also available via REST. | ||
Line 7: | Line 7: | ||
For the use of the REST services, the authentication with username and password is necessary. [[https://en.wikipedia.org/wiki/Basic_access_authentication|BASIC]] is used as the authentication mechanism. The credentials are checked by the security manager of the application as usual. You do not need to change a source code line to integrate the REST services. | For the use of the REST services, the authentication with username and password is necessary. [[https://en.wikipedia.org/wiki/Basic_access_authentication|BASIC]] is used as the authentication mechanism. The credentials are checked by the security manager of the application as usual. You do not need to change a source code line to integrate the REST services. | ||
- | =====How it works===== | + | =====How It Works===== |
- | The REST implementation in JVx has been implemented with Restlet. To use the REST services, the deployment descriptor must be configured as follows: | + | The REST implementation in JVx has been implemented with [[http://www.restlet.org/|Restlet]]. To use the REST services, the deployment descriptor must be configured as follows: |
<file xml> | <file xml> | ||
Line 30: | Line 30: | ||
With this configuration, the following services are available: | With this configuration, the following services are available: | ||
- | ===== Available services ===== | + | ===== Available Services ===== |
- | ====Storage access (CRUD, Meta Data)==== | + | ==== Administration ==== |
- | * Select | + | Various services are available for the administration. These can only be used by POST requests in the standard case. However, if certain custom services have been registered, they can also be addressed via GET request. |
- | * Insert | + | |
- | * Update | + | The following services are available by default: |
- | * Delete | + | |
- | * Meta Data | + | * [[#test_authentication|Test authentication]] |
+ | * [[#change_password|Change password]] | ||
+ | |||
+ | === Test Authentication === | ||
+ | |||
+ | Test URL:\\ | ||
+ | ''<nowiki>http://server:port/webapp/services/rest/</nowiki>**<fc #BF0000>APPLICATION_NAME</fc>**/**<fc #BF0000>LIFECYCLE_CLASS</fc>**/_admin/**<fc #BF0000>testAuthentication</fc>**'' \\ | ||
+ | or \\ | ||
+ | ''<nowiki>http://server:port/webapp/services/rest/</nowiki>**<fc #BF0000>APPLICATION_NAME</fc>**/**<fc #BF0000>LIFECYCLE_CLASS</fc>**/_admin/**<fc #BF0000>testAuthentication</fc>**/**<fc #BF0000>parameter</fc>**'' | ||
+ | |||
+ | The request requires a HashMap in JSON format. | ||
+ | |||
+ | Example: | ||
+ | |||
+ | <file json> | ||
+ | { "username" : "admin", | ||
+ | "password" : "adminpassword" | ||
+ | } | ||
+ | </file> | ||
+ | |||
+ | The username can also be omitted. In this case, the parameter from the URL will be used as the username. | ||
+ | |||
+ | == POST-Response == | ||
+ | |||
+ | If the login was successful, no response is generated and the status code is 204 (SUCCESS_NO_CONTENT). | ||
+ | |||
+ | === Change Password === | ||
+ | |||
+ | Test URL:\\ | ||
+ | ''<nowiki>http://server:port/webapp/services/rest/</nowiki>**<fc #BF0000>APPLICATION_NAME</fc>**/**<fc #BF0000>LIFECYCLE_CLASS</fc>**/_admin/**<fc #BF0000>changePassword</fc>**'' \\ | ||
+ | or \\ | ||
+ | ''<nowiki>http://server:port/webapp/services/rest/</nowiki>**<fc #BF0000>APPLICATION_NAME</fc>**/**<fc #BF0000>LIFECYCLE_CLASS</fc>**/_admin/**<fc #BF0000>changePassword</fc>**/**<fc #BF0000>parameter</fc>**'' | ||
+ | |||
+ | The request requires a HashMap in JSON format. | ||
+ | |||
+ | Example: | ||
+ | |||
+ | <file json> | ||
+ | { "username" : "admin", | ||
+ | "oldpassword" : "oldpassword", | ||
+ | "newpassword" : "newpassword" | ||
+ | } | ||
+ | </file> | ||
+ | |||
+ | The username can also be omitted. In this case, the parameter from the URL will be used as the username. | ||
+ | |||
+ | == POST-Response == | ||
+ | |||
+ | If the password has been changed, no response is generated and the status code is 204 (SUCCESS_NO_CONTENT). | ||
+ | |||
+ | === Custom Services === | ||
+ | |||
+ | If you want to register your own service at runtime, this can be done by | ||
+ | |||
+ | <code java> | ||
+ | AdminService.register(String pApplicationName, String pAction, IAdminServiceDelegate pDelegate); | ||
+ | AdminService.unregister(String pApplicationName, String pAction, Class<? extends IAdminServiceDelegate> pClass) | ||
+ | </code> | ||
+ | |||
+ | The service can be addressed either via GET or POST request, depending on whether ** // IAdminServiceGetDelegate // ** or ** // IAdminServicePostDelegate // ** is used. | ||
+ | |||
+ | Test URL: | ||
+ | |||
+ | ''<nowiki>http://server:port/webapp/services/rest/</nowiki>**<fc #BF0000>APPLICATION_NAME</fc>**/**<fc #BF0000>LIFECYCLE_CLASS</fc>**/_admin/**<fc #BF0000>ACTION</fc>**'' \\ | ||
+ | or \\ | ||
+ | ''<nowiki>http://server:port/webapp/services/rest/</nowiki>**<fc #BF0000>APPLICATION_NAME</fc>**/**<fc #BF0000>LIFECYCLE_CLASS</fc>**/_admin/**<fc #BF0000>ACTION</fc>**/**<fc #BF0000>parameter</fc>**'' | ||
+ | |||
+ | ====Storage Access (CRUD, Metadata)==== | ||
+ | |||
+ | * [[#get-request_select|Select]] | ||
+ | * [[#post-request_insert|Insert]] | ||
+ | * [[#put-request_update|Update]] | ||
+ | * [[#delete-request_delete|Delete]] | ||
+ | * [[#options-request_meta_data|Meta Data]] | ||
==GET-Request (Select)== | ==GET-Request (Select)== | ||
- | Query all data:\\ | + | Query all data: |
''<nowiki>http://server:port/webapp/services/rest/</nowiki>**<fc #BF0000>APPLICATION_NAME</fc>**/**<fc #BF0000>LIFECYCLE_CLASS</fc>**/data/**<fc #BF0000>STORAGE_NAME</fc>**'' | ''<nowiki>http://server:port/webapp/services/rest/</nowiki>**<fc #BF0000>APPLICATION_NAME</fc>**/**<fc #BF0000>LIFECYCLE_CLASS</fc>**/data/**<fc #BF0000>STORAGE_NAME</fc>**'' | ||
Line 52: | Line 125: | ||
The query parameters can also be used to perform filtering with columns that are not PK columns. | The query parameters can also be used to perform filtering with columns that are not PK columns. | ||
+ | |||
+ | Read more about [[https://blog.sibvisions.com/2017/10/30/jvx-rest-interface-update/|complex query parameters]]? | ||
==GET-Response== | ==GET-Response== | ||
Line 320: | Line 395: | ||
\\ | \\ | ||
- | ==== Call actions ==== | + | ==== Call Actions ==== |
- | The server-side actions can be called directly from the life-cycle object as well as from available business objects. It's also possible to use parameters. | + | The [[jvx:communication:calling_server_action|server-side actions]] can be called directly from the life-cycle object as well as from available business objects. It's also possible to use parameters. |
- | * Action without parameter | + | * [[#get-request|Action without parameter]] |
- | * Action with parameter | + | * [[#postput-Request|Action with parameter]] |
== GET-Request == | == GET-Request == | ||
Call a server-side action (without parameter): | Call a server-side action (without parameter): | ||
+ | ''<nowiki>http://server:port/webapp/services/rest/</nowiki>**<fc #BF0000>APPLICATION_NAME</fc>**/**<fc #BF0000>LIFECYCLE_CLASS</fc>**/action/**<fc #BF0000>ACTION_NAME</fc>**'' | ||
+ | Call a method from a business object (without parameter): | ||
+ | ''<nowiki>http://server:port/webapp/services/rest/</nowiki>**<fc #BF0000>APPLICATION_NAME</fc>**/**<fc #BF0000>LIFECYCLE_CLASS</fc>**/object/**<fc #BF0000>OBJECT_NAME</fc>**/**<fc #BF0000>ACTION_NAME</fc>**'' | ||
+ | == GET-Response == | ||
+ | The response returns the return value of the action in JSON format. | ||
+ | == POST/PUT-Request == | ||
+ | Call a serer-side action (with parameter): | ||
+ | ''<nowiki>http://server:port/webapp/services/rest/</nowiki>**<fc #BF0000>APPLICATION_NAME</fc>**/**<fc #BF0000>LIFECYCLE_CLASS</fc>**/action/**<fc #BF0000>ACTION_NAME</fc>**'' | ||
+ | Call a method from a business object (with parameter): | ||
+ | ''<nowiki>http://server:port/webapp/services/rest/</nowiki>**<fc #BF0000>APPLICATION_NAME</fc>**/**<fc #BF0000>LIFECYCLE_CLASS</fc>**/object/**<fc #BF0000>OBJECT_NAME</fc>**/**<fc #BF0000>ACTION_NAME</fc>**'' | ||
+ | The request requires an array of objects populated with the parameters for the action. | ||
+ | |||
+ | Example: | ||
+ | |||
+ | Action: | ||
+ | |||
+ | <file java> | ||
+ | public String calculate(Number pFirst, Number pSecond) | ||
+ | { | ||
+ | return "" + pFirst.intValue() + pSecond.intValue(); | ||
+ | } | ||
+ | </file> | ||
+ | |||
+ | JSON Request: | ||
+ | |||
+ | <file json> | ||
+ | [123,1] | ||
+ | </file> | ||
+ | |||
+ | == POST/PUT-Response == | ||
+ | |||
+ | The response returns the return value of the action in JSON format. | ||
+ | |||
+ | |||
+ | ===== Examples ===== | ||
+ | |||
+ | ==== Integration ==== | ||
+ | |||
+ | Using php: | ||
+ | |||
+ | <file php> | ||
+ | $ch = curl_init(); | ||
+ | curl_setopt($ch, CURLOPT_URL,'https://<server>/DB/services/rest/League/Standings/data/All'); | ||
+ | | ||
+ | curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); | ||
+ | curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); | ||
+ | curl_setopt($ch, CURLOPT_USERPWD, "user:password"); | ||
+ | //curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); | ||
+ | |||
+ | $json = json_decode(curl_exec($ch), true); | ||
+ | |||
+ | curl_close($ch); | ||
+ | </file> | ||
+ | |||
+ | Using Javascript: | ||
+ | |||
+ | <file html rest.html> | ||
+ | <html> | ||
+ | <head> | ||
+ | <script> | ||
+ | function doRest() { | ||
+ | const http = new XMLHttpRequest(); | ||
+ | const url='https://<server>/DB/services/rest/League/Standings/action/getResults'; | ||
+ | |||
+ | http.open("POST", url, true, 'user', 'password'); | ||
+ | http.withCredentials = true; | ||
+ | http.send("[88]"); | ||
+ | |||
+ | http.onreadystatechange=(e)=> | ||
+ | { | ||
+ | if (http.readyState == 4) | ||
+ | { | ||
+ | console.log(atob(eval(http.responseText))); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </script> | ||
+ | </head> | ||
+ | <body> | ||
+ | <button type="button" onclick="doRest()">REST call</button> | ||
+ | </body> | ||
+ | </html> | ||
+ | </file> | ||
+ | |||
+ | \\ | ||
+ | [[https://blog.sibvisions.com/2017/10/23/angularjs-4-with-visionx-and-jvx-rest-services/|AngularJS 4 with VisionX and JVx REST services]] \\ | ||
+ | [[https://blog.sibvisions.com/2015/06/15/angularjs-with-jvx-in-action/|AngularJS with JVx in action]] \\ | ||
+ | [[https://blog.sibvisions.com/2016/02/29/using-oracle-jet-with-visionxjvx/|Oracle JET with VisionX/JVx]] \\ | ||
+ | |||
+ | ==== JUnit Tests ==== | ||
+ | |||
+ | [[https://sourceforge.net/p/jvx/code/HEAD/tree/trunk/java/library/test/com/sibvisions/rad/server/http/rest/TestCallService.java|TestCallService]] for the Lifecycle objects: [[https://sourceforge.net/p/jvx/code/HEAD/tree/trunk/java/library/rad/apps/demo/src.server/demo/Session.java|Session]] and [[https://sourceforge.net/p/jvx/code/HEAD/tree/trunk/java/library/rad/apps/demo/src.server/demo/special/Address.java|Address]] | ||
+ | |||
+ | \\ | ||
**<fs 20px>Note</fs>** | **<fs 20px>Note</fs>** | ||
- | https://blog.sibvisions.com/2017/10/30/jvx-rest-interface-update/ | + | For action calls, the correct data types must be used! In general, it is best to dispense with primitive data types, as parameters, and instead of using arrays, the List Interface should be used. It is also recommended to use Number for all numerical values. This avoids problems due to JSON serialization. |
+ | |||
+ | The life-cycle name should be the fully qualified class name, with package. If only the simple class name is used, JVx will try to find a matching class. If several classes are considered, then no class is used. You can optionally define a search path in the config.xml of the application: | ||
+ | |||
+ | <file xml> | ||
+ | <application> | ||
+ | .. | ||
+ | <rest> | ||
+ | <search> | ||
+ | <path>/com/sibvisions/app/myapp</app> | ||
+ | <path>/com/sibvisions/app/myapp/screens/sub/</app> | ||
+ | </search> | ||
+ | </rest> | ||
+ | </application> | ||
+ | </file> | ||
+ | |||
+ | Additional information about this feature is available in our [[https://oss.sibvisions.com/index.php?do=details&task_id=534|Support System]]. |