annotate Resources/Samples/Lua/CallWebService.js @ 1055:6f923d52a46c

call Web services from Lua
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 24 Jul 2014 11:37:02 +0200
parents
children 6e7e5ed91c2d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1055
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Belgium
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 *
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * Permission is hereby granted, free of charge, to any person
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * obtaining a copy of this software and associated documentation
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * files (the "Software"), to deal in the Software without
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * restriction, including without limitation the rights to use, copy,
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * modify, merge, publish, distribute, sublicense, and/or sell copies
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * of the Software, and to permit persons to whom the Software is
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * furnished to do so, subject to the following conditions:
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 *
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * The above copyright notice and this permission notice shall be
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * included in all copies or substantial portions of the Software.
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * SOFTWARE.
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 **/
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 /**
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * This file is a simple echo Web service implemented using
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 * "node.js". Whenever it receives a POST HTTP query, it echoes its
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 * body both to stdout and to the client. Credentials are checked.
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 **/
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 // Parameters of the ECHO server
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 var port = 8000;
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 var username = 'alice';
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 var password = 'alicePassword';
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 var http = require('http');
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 var authorization = 'Basic ' + new Buffer(username + ':' + password).toString('base64')
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 var server = http.createServer(function(req, response) {
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 // Check the credentials
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 if (req.headers.authorization != authorization)
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 {
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 console.log('Bad credentials, access not allowed');
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 response.writeHead(401);
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 response.end();
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 return;
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 }
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 switch (req.method)
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 {
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 case 'POST':
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 {
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 var body = '';
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 req.on('data', function (data) {
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 response.write(data);
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 body += data;
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 });
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 req.on('end', function () {
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 console.log('Message received: ' + body);
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 response.end();
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 });
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 break;
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 }
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 default:
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 console.log('Method ' + req.method + ' is not supported by this ECHO Web service');
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 response.writeHead(405, {'Allow': 'POST'});
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 response.end();
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 }
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 });
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 server.listen(port);