cewebs-servicesbase 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/AUTHORS +1 -0
- data/LICENSE.GPL +674 -0
- data/README.md +18 -0
- data/Rakefile +25 -0
- data/cewebs-servicesbase.gemspec +28 -0
- data/contracts/administration.xml +55 -0
- data/contracts/delivery.xml +59 -0
- data/contracts/notification.xml +20 -0
- data/contracts/report.xml +42 -0
- data/contracts/repository.xml +64 -0
- data/lib/cewebs-servicesbase/configuration.rb +93 -0
- data/lib/cewebs-servicesbase/password.rb +39 -0
- data/lib/cewebs-servicesbase/scaffold.rb +27 -0
- data/lib/cewebs-servicesbase/service.rb +379 -0
- data/lib/cewebs-servicesbase/strings.rb +36 -0
- data/lib/cewebs-servicesbase/view.rb +73 -0
- data/server/data/Default/aman.xml +10 -0
- data/server/data/Default/custom.rng +11 -0
- data/server/data/Default/custom.xml +3 -0
- data/server/data/repositories.xml +3 -0
- data/server/service +54 -0
- data/server/service.riddl.xml +14 -0
- data/server/service.xml +10 -0
- data/test/tc_configuration.rb +29 -0
- data/tools/cewebs-servicesbase +26 -0
- metadata +138 -0
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
# Please see the LICENSE.GPL file for copyright and distribution information
|
|
2
|
+
require 'thin'
|
|
3
|
+
require 'time'
|
|
4
|
+
require 'cgi'
|
|
5
|
+
require 'uri'
|
|
6
|
+
require 'fileutils'
|
|
7
|
+
require 'securerandom'
|
|
8
|
+
require 'riddl/server'
|
|
9
|
+
require 'riddl/client'
|
|
10
|
+
require 'xml/smart'
|
|
11
|
+
require 'redel'
|
|
12
|
+
require_relative 'view'
|
|
13
|
+
require_relative 'strings'
|
|
14
|
+
require_relative 'configuration'
|
|
15
|
+
require_relative 'password'
|
|
16
|
+
|
|
17
|
+
# Services call each other while they are answering a request (engine -> aman ->
|
|
18
|
+
# engine), so a request must never block the whole server.
|
|
19
|
+
module CEWebS
|
|
20
|
+
module Threaded
|
|
21
|
+
def threaded?; true; end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
Thin::Connection.prepend CEWebS::Threaded
|
|
25
|
+
|
|
26
|
+
module CEWebS
|
|
27
|
+
CONTRACTS = File.expand_path(File.join(__dir__,'..','..','contracts'))
|
|
28
|
+
|
|
29
|
+
# Declaration of a service. It includes the contracts of this gem with contracts/{name}.xml,
|
|
30
|
+
# everything else is relative to the file.
|
|
31
|
+
def self::declaration(file) #{{{
|
|
32
|
+
doc = XML::Smart.string(File.read(file))
|
|
33
|
+
doc.register_namespace 'xi', 'http://www.w3.org/2001/XInclude'
|
|
34
|
+
doc.find('//xi:include/@href').each do |h|
|
|
35
|
+
h.value = h.value.start_with?('contracts/') ? File.join(CONTRACTS,File.basename(h.value)) : File.expand_path(h.value,File.dirname(file))
|
|
36
|
+
end
|
|
37
|
+
doc
|
|
38
|
+
end #}}}
|
|
39
|
+
|
|
40
|
+
class << self
|
|
41
|
+
attr_reader :data_dir
|
|
42
|
+
# contains keys and users, so it is not for other users of the server
|
|
43
|
+
def data_dir=(dir)
|
|
44
|
+
File.chmod(0700,dir)
|
|
45
|
+
@data_dir = dir
|
|
46
|
+
end
|
|
47
|
+
def data(*path); File.join(data_dir,*path.map(&:to_s)); end
|
|
48
|
+
def key; SecureRandom.hex(16); end
|
|
49
|
+
def key?(key); key.to_s =~ /\A\h{32}\z/ ? true : false; end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
class Error < StandardError
|
|
53
|
+
attr_reader :status
|
|
54
|
+
def initialize(message='not possible',status=400)
|
|
55
|
+
super(message)
|
|
56
|
+
@status = status
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
### messages
|
|
61
|
+
|
|
62
|
+
# value is safe to be used in markup (and in attributes with double quotes), raw
|
|
63
|
+
# is what the user typed. ' is kept, because it is part of the wiki syntax.
|
|
64
|
+
Parameter = Struct.new(:name,:raw,:data) do
|
|
65
|
+
def value; raw.to_s.gsub(/[&<>"]/,'&' => '&','<' => '<','>' => '>','"' => '"'); end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
Document = Struct.new(:content,:modified,:js,:headers,:links,:meta,:access,:breadcrumbs,:search,:keyword_init => true)
|
|
69
|
+
File_ = Struct.new(:content,:name,:mime,:modified,:transform,:keyword_init => true)
|
|
70
|
+
Prepare = Struct.new(:cachekey,:authenticate,:keyword_init => true)
|
|
71
|
+
|
|
72
|
+
# the lists of a Document consist of hashes
|
|
73
|
+
LISTS = [:headers,:links,:meta,:access,:breadcrumbs]
|
|
74
|
+
|
|
75
|
+
class Messages < MarkUS
|
|
76
|
+
template :prepare do
|
|
77
|
+
prepare_ do
|
|
78
|
+
cachekey_ @data.cachekey.to_s
|
|
79
|
+
authenticate_ @data.authenticate ? 'true' : 'false'
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
template :document do
|
|
83
|
+
document_ do
|
|
84
|
+
content_ @data.content.to_s
|
|
85
|
+
modified_ @data.modified.iso8601(6) if @data.modified
|
|
86
|
+
# library the content needs, only jquery is available
|
|
87
|
+
js_ @data.js.to_s
|
|
88
|
+
LISTS.each do |l|
|
|
89
|
+
(@data[l] || []).each { |e| template_! :fields, l, e }
|
|
90
|
+
end
|
|
91
|
+
template_! :fields, :search, @data.search if @data.search
|
|
92
|
+
nil
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
# list of hashes
|
|
96
|
+
template :list do
|
|
97
|
+
list_ do
|
|
98
|
+
@data.each { |e| template_! :fields, :item, e }
|
|
99
|
+
nil
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
template :fields do |name,fields|
|
|
103
|
+
element_! name.to_s do
|
|
104
|
+
fields.each { |k,v| element_! k.to_s, v.to_s unless v.nil? }
|
|
105
|
+
nil
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def self::create(template,data)
|
|
110
|
+
new.xml_!(template,:data => data)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def self::fields(node)
|
|
114
|
+
node.find('*').to_h { |e| [e.qname.name.to_sym,e.to_s] }
|
|
115
|
+
end
|
|
116
|
+
def self::parse_list(xml)
|
|
117
|
+
XML::Smart.string(xml).find('/list/item').map { |e| fields(e) }
|
|
118
|
+
end
|
|
119
|
+
def self::parse_prepare(xml)
|
|
120
|
+
f = fields(XML::Smart.string(xml).root)
|
|
121
|
+
Prepare.new(:cachekey => f[:cachekey].to_s, :authenticate => f[:authenticate] == 'true')
|
|
122
|
+
end
|
|
123
|
+
def self::parse_document(xml)
|
|
124
|
+
root = XML::Smart.string(xml).root
|
|
125
|
+
ret = Document.new(
|
|
126
|
+
:content => root.sfind('content'),
|
|
127
|
+
:modified => (Time.iso8601(root.sfind('modified')) rescue nil),
|
|
128
|
+
:js => root.sfind('js')
|
|
129
|
+
)
|
|
130
|
+
LISTS.each { |l| ret[l] = root.find(l.to_s).map { |e| fields(e) } }
|
|
131
|
+
search = root.find('search').first
|
|
132
|
+
ret.search = fields(search) if search
|
|
133
|
+
ret
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
### client
|
|
138
|
+
|
|
139
|
+
class Remote
|
|
140
|
+
class Unavailable < Error; end
|
|
141
|
+
|
|
142
|
+
# posted: the user has sent a form, and not just opened a link
|
|
143
|
+
def initialize(url,key,user: nil,language: nil,posted: false)
|
|
144
|
+
@url = url.to_s.sub(/\/+\z/,'')
|
|
145
|
+
@headers = { 'CEWEBS-KEY' => key, 'CEWEBS-USER' => user, 'CEWEBS-LANGUAGE' => language && language.join(','), 'CEWEBS-POSTED' => posted ? 'true' : nil }
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# returns status, body, headers. 304 is not an error
|
|
149
|
+
def call(method,path='',parameters=[],headers={}) #{{{
|
|
150
|
+
paras = parameters.map { |p| p.is_a?(::Array) ? Riddl::Parameter::Simple.new(p[0].to_s,p[1].to_s) : p }
|
|
151
|
+
paras += @headers.merge(headers).reject { |_,v| v.nil? || v.to_s == '' }.map { |k,v| Riddl::Header.new(k,v.to_s) }
|
|
152
|
+
status, response, rheaders = Riddl::Client.new(@url).resource(path).request(method => paras)
|
|
153
|
+
body = response && response[0] ? response[0].value : ''
|
|
154
|
+
body = body.read if body.respond_to?(:read)
|
|
155
|
+
body = body.to_s
|
|
156
|
+
raise Unavailable.new("#{@url} not reachable",502) if status == 0
|
|
157
|
+
raise Error.new(body == '' ? "#{@url} returned #{status}" : body.dup.force_encoding('UTF-8'),status) unless status == 304 || (200..299).include?(status)
|
|
158
|
+
[status,body,rheaders]
|
|
159
|
+
end #}}}
|
|
160
|
+
|
|
161
|
+
def list(path,parameters=[]); Messages::parse_list(call(:get,path,parameters)[1]); end
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
### requests
|
|
165
|
+
|
|
166
|
+
class Request
|
|
167
|
+
attr_reader :instance, :command, :key, :user, :language, :modified, :parameters
|
|
168
|
+
|
|
169
|
+
def initialize(h,p,instance,command) #{{{
|
|
170
|
+
@instance = instance
|
|
171
|
+
@command = command
|
|
172
|
+
@key = h['CEWEBS_KEY'].to_s
|
|
173
|
+
@user = h['CEWEBS_USER'].to_s
|
|
174
|
+
@language = h['CEWEBS_LANGUAGE'].to_s.split(',')
|
|
175
|
+
@posted = h['CEWEBS_POSTED'] == 'true'
|
|
176
|
+
@modified = Time.iso8601(h['CEWEBS_MODIFIED'].to_s) rescue Time.at(0)
|
|
177
|
+
@parameters = p.map do |e|
|
|
178
|
+
if e.is_a?(Riddl::Parameter::Complex)
|
|
179
|
+
data = e.value.respond_to?(:read) ? e.value.read : e.value
|
|
180
|
+
Parameter.new(e.name,e.filename.to_s.dup.force_encoding('UTF-8'),data)
|
|
181
|
+
else
|
|
182
|
+
Parameter.new(e.name,e.value.to_s.dup.force_encoding('UTF-8').scrub)
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
end #}}}
|
|
186
|
+
|
|
187
|
+
def posted?; @posted; end
|
|
188
|
+
def parameter(name); @parameters.reverse.find { |p| p.name == name }; end
|
|
189
|
+
# safe for markup
|
|
190
|
+
def [](name); (p = parameter(name)) && p.value; end
|
|
191
|
+
def raw(name); (p = parameter(name)) && p.raw; end
|
|
192
|
+
def decode(name); (v = raw(name)) && v.unpack1('m').force_encoding('UTF-8'); end
|
|
193
|
+
|
|
194
|
+
# newest modification, nil if the engine already has a current copy
|
|
195
|
+
def newer(*files) #{{{
|
|
196
|
+
newest = files.select { |f| File.exist?(f) }.map { |f| File.mtime(f) }.max || Time.now
|
|
197
|
+
newest.floor(6) > @modified ? newest : nil
|
|
198
|
+
end #}}}
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
module Respond
|
|
202
|
+
def respond #{{{
|
|
203
|
+
yield
|
|
204
|
+
rescue Error => e
|
|
205
|
+
@status = e.status
|
|
206
|
+
Riddl::Parameter::Complex.new('error','text/plain',e.message)
|
|
207
|
+
rescue => e
|
|
208
|
+
puts e.full_message
|
|
209
|
+
@status = 500
|
|
210
|
+
Riddl::Parameter::Complex.new('error','text/plain',e.message)
|
|
211
|
+
end #}}}
|
|
212
|
+
def xml(template,data)
|
|
213
|
+
Riddl::Parameter::Complex.new('result','text/xml',Messages::create(template,data))
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
### delivery
|
|
218
|
+
|
|
219
|
+
# Subclasses implement prepare, document, file and chunk. All get a Request.
|
|
220
|
+
# document and file return nil if the copy of the engine is current.
|
|
221
|
+
class Delivery
|
|
222
|
+
def authorized?(r)
|
|
223
|
+
r.instance =~ /\A\d+\z/ && CEWebS::key?(r.key) && File.exist?(CEWebS::data(r.instance,"key:#{r.key}"))
|
|
224
|
+
end
|
|
225
|
+
# Commands that change something. They need a form, or a link with the attribute
|
|
226
|
+
# data-post (post in the access list). Otherwise links of others could change something.
|
|
227
|
+
def changes; []; end
|
|
228
|
+
def prepare(r); unknown(r); end
|
|
229
|
+
def document(r); unknown(r); end
|
|
230
|
+
def file(r); unknown(r); end
|
|
231
|
+
def chunk(r); unknown(r); end
|
|
232
|
+
def unknown(r); raise Error.new("Command #{CGI.escapeHTML(r.command)} not found!",404); end
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
class DoDelivery < Riddl::Implementation #{{{
|
|
236
|
+
include Respond
|
|
237
|
+
def response
|
|
238
|
+
respond do
|
|
239
|
+
service, mode = @a
|
|
240
|
+
r = Request.new(@h,@p,@r[0],@r[2])
|
|
241
|
+
raise Error.new('not allowed',403) unless service.authorized?(r)
|
|
242
|
+
raise Error.new('not possible with a link',405) if service.changes.include?(r.command) && !r.posted?
|
|
243
|
+
ret = service.send(mode,r)
|
|
244
|
+
if ret.nil?
|
|
245
|
+
@status = 304
|
|
246
|
+
next []
|
|
247
|
+
end
|
|
248
|
+
case ret
|
|
249
|
+
when Prepare; xml(:prepare,ret)
|
|
250
|
+
when Document; xml(:document,ret)
|
|
251
|
+
when File_
|
|
252
|
+
@headers << Riddl::Header.new('CEWEBS-NAME',URI.encode_www_form_component(ret.name)) if ret.name
|
|
253
|
+
@headers << Riddl::Header.new('CEWEBS-MODIFIED',ret.modified.iso8601(6)) if ret.modified
|
|
254
|
+
@headers << Riddl::Header.new('CEWEBS-TRANSFORM','true') if ret.transform
|
|
255
|
+
Riddl::Parameter::Complex.new('result',ret.mime,ret.content.to_s)
|
|
256
|
+
end
|
|
257
|
+
end
|
|
258
|
+
end
|
|
259
|
+
end #}}}
|
|
260
|
+
|
|
261
|
+
def self::delivery(service) #{{{
|
|
262
|
+
Proc.new do
|
|
263
|
+
on resource do
|
|
264
|
+
%w{prepare document file chunk}.each do |mode|
|
|
265
|
+
on resource mode do
|
|
266
|
+
on resource do
|
|
267
|
+
run DoDelivery, service, mode if get 'command'
|
|
268
|
+
run DoDelivery, service, mode if post 'command'
|
|
269
|
+
end
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
end
|
|
273
|
+
end
|
|
274
|
+
end #}}}
|
|
275
|
+
|
|
276
|
+
### administration
|
|
277
|
+
|
|
278
|
+
# Subclasses implement dynamic (new elements) and entry_points.
|
|
279
|
+
class Administration
|
|
280
|
+
def authorized?(instance,key)
|
|
281
|
+
instance =~ /\A\d+\z/ && CEWebS::key?(key) && File.exist?(CEWebS::data(instance,"key:#{key}"))
|
|
282
|
+
end
|
|
283
|
+
def directory(instance); CEWebS::data(instance); end
|
|
284
|
+
|
|
285
|
+
# what a new element looks like
|
|
286
|
+
def dynamic(instance,part,path); nil; end
|
|
287
|
+
def entry_points(instance,language); []; end
|
|
288
|
+
|
|
289
|
+
def create(authentication,key,description) #{{{
|
|
290
|
+
raise Error.new('invalid key',403) unless CEWebS::key?(key)
|
|
291
|
+
repo = XML::Smart.open(CEWebS::data('repositories.xml')).find('/repositories/repository').find { |r| r.sfind('key') == authentication }
|
|
292
|
+
raise Error.new('repository not known',403) if repo.nil? || authentication == ''
|
|
293
|
+
instance = nil
|
|
294
|
+
XML::Smart.modify(CEWebS::data('descriptions.xml'),'<descriptions/>') do |doc|
|
|
295
|
+
instance = (1..).find { |i| !File.exist?(CEWebS::data(i)) }
|
|
296
|
+
doc.root.add('instance',{ 'id' => instance, 'repository' => repo.attributes['id'] },description)
|
|
297
|
+
FileUtils.cp_r(CEWebS::data('Default'),CEWebS::data(instance),:preserve => true)
|
|
298
|
+
FileUtils.touch(CEWebS::data(instance,"key:#{key}"))
|
|
299
|
+
end
|
|
300
|
+
instance
|
|
301
|
+
end #}}}
|
|
302
|
+
|
|
303
|
+
def delete(instance) #{{{
|
|
304
|
+
FileUtils.rm_r(CEWebS::data(instance))
|
|
305
|
+
XML::Smart.modify(CEWebS::data('descriptions.xml'),'<descriptions/>') do |doc|
|
|
306
|
+
doc.find("/descriptions/instance[@id='#{instance}']").delete_all!
|
|
307
|
+
end
|
|
308
|
+
end #}}}
|
|
309
|
+
|
|
310
|
+
def parts(instance) #{{{
|
|
311
|
+
dir = directory(instance)
|
|
312
|
+
XML::Smart.open(File.join(dir,'aman.xml')).find('/parts/part').map do |e|
|
|
313
|
+
{
|
|
314
|
+
:identifier => e.attributes['id'],
|
|
315
|
+
:share => e.attributes['share'],
|
|
316
|
+
:schema => File.read(File.join(dir,e.attributes['schema'])),
|
|
317
|
+
:xml => File.read(File.join(dir,e.sfind('xml'))),
|
|
318
|
+
:description => e.find('description').map(&:dump).join
|
|
319
|
+
}
|
|
320
|
+
end
|
|
321
|
+
end #}}}
|
|
322
|
+
|
|
323
|
+
# returns the xml that has been used
|
|
324
|
+
def change(instance,part,operation,path,xml) #{{{
|
|
325
|
+
dir = directory(instance)
|
|
326
|
+
node = XML::Smart.open(File.join(dir,'aman.xml')).find('/parts/part').find { |e| e.attributes['id'] == part }
|
|
327
|
+
raise Error.new('part not known',404) if node.nil?
|
|
328
|
+
if operation == 'treeAddDynamic'
|
|
329
|
+
# without conditions, and without positions before |
|
|
330
|
+
xml = dynamic(instance,part,Configuration::plain(path)) || xml
|
|
331
|
+
end
|
|
332
|
+
Configuration::apply(File.join(dir,node.sfind('xml')),operation,path,xml)
|
|
333
|
+
xml
|
|
334
|
+
end #}}}
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
class DoAdministration < Riddl::Implementation #{{{
|
|
338
|
+
include Respond
|
|
339
|
+
def response
|
|
340
|
+
respond do
|
|
341
|
+
admin, operation = @a
|
|
342
|
+
if operation != :create
|
|
343
|
+
raise Error.new('not allowed',403) unless admin.authorized?(@r[0],@h['CEWEBS_KEY'].to_s)
|
|
344
|
+
end
|
|
345
|
+
case operation
|
|
346
|
+
when :create
|
|
347
|
+
xml :list, [{ :instance => admin.create(@p[0].value,@p[1].value,@p[2].value) }]
|
|
348
|
+
when :delete
|
|
349
|
+
admin.delete(@r[0])
|
|
350
|
+
[]
|
|
351
|
+
when :parts
|
|
352
|
+
xml :list, admin.parts(@r[0])
|
|
353
|
+
when :change
|
|
354
|
+
xml :list, [{ :xml => admin.change(@r[0],@r[2],*@p.map { |e| e.value.to_s.dup.force_encoding('UTF-8') }) }]
|
|
355
|
+
when :entrypoints
|
|
356
|
+
xml :list, admin.entry_points(@r[0],@h['CEWEBS_LANGUAGE'].to_s.split(','))
|
|
357
|
+
end
|
|
358
|
+
end
|
|
359
|
+
end
|
|
360
|
+
end #}}}
|
|
361
|
+
|
|
362
|
+
def self::administration(admin) #{{{
|
|
363
|
+
Proc.new do
|
|
364
|
+
run DoAdministration, admin, :create if post 'create'
|
|
365
|
+
on resource do
|
|
366
|
+
run DoAdministration, admin, :delete if delete 'key'
|
|
367
|
+
on resource 'parts' do
|
|
368
|
+
run DoAdministration, admin, :parts if get 'key'
|
|
369
|
+
on resource do
|
|
370
|
+
run DoAdministration, admin, :change if put 'change'
|
|
371
|
+
end
|
|
372
|
+
end
|
|
373
|
+
on resource 'entrypoints' do
|
|
374
|
+
run DoAdministration, admin, :entrypoints if get 'key'
|
|
375
|
+
end
|
|
376
|
+
end
|
|
377
|
+
end
|
|
378
|
+
end #}}}
|
|
379
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# Please see the LICENSE.GPL file for copyright and distribution information
|
|
2
|
+
require 'xml/smart'
|
|
3
|
+
require 'redel'
|
|
4
|
+
|
|
5
|
+
module CEWebS
|
|
6
|
+
# language aware and text finders for xml-smart
|
|
7
|
+
module Find
|
|
8
|
+
def lfind(what,language=['en']) #{{{
|
|
9
|
+
(language || []).each do |l|
|
|
10
|
+
[l,l.sub(/-.*/,'')].uniq.each do |e|
|
|
11
|
+
next unless e =~ /\A[\w-]+\z/
|
|
12
|
+
ret = find("#{what}[@xml:lang='#{e}']").first
|
|
13
|
+
return ret if ret
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
find(what).first
|
|
17
|
+
end #}}}
|
|
18
|
+
# text
|
|
19
|
+
def sfind(what) #{{{
|
|
20
|
+
find(what).first.to_s
|
|
21
|
+
end #}}}
|
|
22
|
+
# text in wiki syntax, converted to markup
|
|
23
|
+
def sffind(what) #{{{
|
|
24
|
+
node = find(what).first
|
|
25
|
+
return '' if node.nil? || node.to_s == ''
|
|
26
|
+
ReDel::Simple.parse(node.to_s).output(ReDel::HTML.new)
|
|
27
|
+
end #}}}
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
class XML::Smart::Dom
|
|
32
|
+
include CEWebS::Find
|
|
33
|
+
end
|
|
34
|
+
class XML::Smart::Dom::Element
|
|
35
|
+
include CEWebS::Find
|
|
36
|
+
end
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# Please see the LICENSE.GPL file for copyright and distribution information
|
|
2
|
+
require 'redel'
|
|
3
|
+
|
|
4
|
+
module CEWebS
|
|
5
|
+
# Base for everything that produces markup.
|
|
6
|
+
#
|
|
7
|
+
# Subclasses implement plain methods that use the markus syntax (div_ ...) and
|
|
8
|
+
# are rendered with
|
|
9
|
+
#
|
|
10
|
+
# SomeView.new(...).render(:document)
|
|
11
|
+
#
|
|
12
|
+
# See ReDel::Helpers for further methods.
|
|
13
|
+
class View < MarkUS
|
|
14
|
+
include ReDel::Helpers
|
|
15
|
+
|
|
16
|
+
def self::inherited(subclass) #{{{
|
|
17
|
+
super
|
|
18
|
+
subclass.template :view do
|
|
19
|
+
@__calls.each { |c| send(*c) }
|
|
20
|
+
nil
|
|
21
|
+
end
|
|
22
|
+
end #}}}
|
|
23
|
+
|
|
24
|
+
def render(*calls) #{{{
|
|
25
|
+
html_! :view, :__calls => calls
|
|
26
|
+
end #}}}
|
|
27
|
+
|
|
28
|
+
def a_peg_(content,attrs={}) #{{{
|
|
29
|
+
text_ '['
|
|
30
|
+
a_ content, attrs
|
|
31
|
+
text_ ']'
|
|
32
|
+
end #}}}
|
|
33
|
+
|
|
34
|
+
def small_img_(name,attrs={}) #{{{
|
|
35
|
+
attrs = {:alt => ''}.merge(attrs)
|
|
36
|
+
attrs = {:title => attrs[:alt]}.merge(attrs)
|
|
37
|
+
img_ attrs.merge(
|
|
38
|
+
:class => ['small align-top',attrs[:class]].compact.join(' '),
|
|
39
|
+
:src => "CEWebS://small/icon/#{name}",
|
|
40
|
+
:width => '16',
|
|
41
|
+
:height => '16'
|
|
42
|
+
)
|
|
43
|
+
end #}}}
|
|
44
|
+
|
|
45
|
+
def date_(name,p={}) #{{{
|
|
46
|
+
p = {:value => Time.now.strftime('%Y-%m-%d')}.merge(p.compact)
|
|
47
|
+
span_ :class => 'margin-right-big' do
|
|
48
|
+
input_ :id => name, :name => name, :value => p[:value], :type => 'date'
|
|
49
|
+
end
|
|
50
|
+
end #}}}
|
|
51
|
+
|
|
52
|
+
def time_(name,p={}) #{{{
|
|
53
|
+
p = {:hour => Time.now.hour, :minute => Time.now.min}.merge(p.compact)
|
|
54
|
+
span_ :class => 'margin-right-big' do
|
|
55
|
+
[['hour','00'..'23'],['minute','00'..'59']].each do |part,range|
|
|
56
|
+
select_ :name => "#{name}_#{part}" do
|
|
57
|
+
range.each do |n|
|
|
58
|
+
option_ n, :selected => (n.to_i == p[part.to_sym].to_i ? 'selected' : nil)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end #}}}
|
|
64
|
+
|
|
65
|
+
# url for links between services, resolved by the engine
|
|
66
|
+
def self::link(mode,command,parameters={},amp='&',target='self') #{{{
|
|
67
|
+
"CEWebS://#{target}/#{mode}/#{command}" + parameters.reject{ |_,v| v.nil? || v == '' }.map { |k,v|
|
|
68
|
+
"#{amp}CEWebS_#{k}=#{URI.encode_www_form_component(v.to_s)}"
|
|
69
|
+
}.join
|
|
70
|
+
end #}}}
|
|
71
|
+
def link(*args); View::link(*args); end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
<!-- what can be configured with the AMan -->
|
|
2
|
+
<parts>
|
|
3
|
+
<part id="custom" share="" schema="custom.rng">
|
|
4
|
+
<description xml:lang="en">
|
|
5
|
+
<short>Settings</short>
|
|
6
|
+
<long>The text that is shown.</long>
|
|
7
|
+
</description>
|
|
8
|
+
<xml>custom.xml</xml>
|
|
9
|
+
</part>
|
|
10
|
+
</parts>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
<element cewebs:uiversion="1.2" name="custom" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes" xmlns="http://relaxng.org/ns/structure/1.0" xmlns:cewebs="http://www.cewebs.org">
|
|
2
|
+
<cewebs:ui>
|
|
3
|
+
<cewebs:heading>Settings</cewebs:heading>
|
|
4
|
+
</cewebs:ui>
|
|
5
|
+
<element name="text">
|
|
6
|
+
<cewebs:ui><cewebs:label>Text:</cewebs:label></cewebs:ui>
|
|
7
|
+
<data type="string">
|
|
8
|
+
<param name="pattern">.*</param>
|
|
9
|
+
</data>
|
|
10
|
+
</element>
|
|
11
|
+
</element>
|
data/server/service
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
#!/usr/bin/ruby -W0
|
|
2
|
+
require 'digest/md5'
|
|
3
|
+
require 'cewebs-servicesbase/service'
|
|
4
|
+
|
|
5
|
+
# everything that produces markup, see CEWebS::View
|
|
6
|
+
class HelloView < CEWebS::View
|
|
7
|
+
def initialize(text)
|
|
8
|
+
super()
|
|
9
|
+
@text = text
|
|
10
|
+
end
|
|
11
|
+
def document
|
|
12
|
+
p_ @text
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# pages of the service, the user and the parameters are in r (see CEWebS::Request)
|
|
17
|
+
class Hello < CEWebS::Delivery
|
|
18
|
+
def prepare(r)
|
|
19
|
+
r.command == 'show' ? CEWebS::Prepare.new(:cachekey => Digest::MD5.hexdigest('show'), :authenticate => false) : unknown(r)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def document(r)
|
|
23
|
+
return unknown(r) unless r.command == 'show'
|
|
24
|
+
custom = CEWebS::data(r.instance,'custom.xml')
|
|
25
|
+
# nil: the engine already has a current copy
|
|
26
|
+
modified = r.newer(custom) or return nil
|
|
27
|
+
CEWebS::Document.new(
|
|
28
|
+
:content => HelloView.new(XML::Smart.open(custom).find('string(/custom/text)')).render(:document),
|
|
29
|
+
:modified => modified
|
|
30
|
+
)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# pages menu points can link to
|
|
35
|
+
class HelloAdministration < CEWebS::Administration
|
|
36
|
+
def entry_points(instance,language)
|
|
37
|
+
[{ :command => 'show', :parameters => '', :description => 'Hello' }]
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
options = {
|
|
42
|
+
:bind => '127.0.0.1',
|
|
43
|
+
:host => 'localhost',
|
|
44
|
+
:port => 9030
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
Riddl::Server.new(CEWebS::declaration(File.join(__dir__,'service.riddl.xml')), options) do
|
|
48
|
+
accessible_description true
|
|
49
|
+
|
|
50
|
+
CEWebS::data_dir = File.join(__dir__,'data')
|
|
51
|
+
|
|
52
|
+
interface 'delivery', &CEWebS::delivery(Hello.new)
|
|
53
|
+
interface 'administration', &CEWebS::administration(HelloAdministration.new)
|
|
54
|
+
end.loop!
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<declaration xmlns="http://riddl.org/ns/declaration/1.0" xmlns:xi="http://www.w3.org/2001/XInclude">
|
|
2
|
+
<interface name="delivery">
|
|
3
|
+
<xi:include href="contracts/delivery.xml"/>
|
|
4
|
+
</interface>
|
|
5
|
+
<interface name="administration">
|
|
6
|
+
<xi:include href="contracts/administration.xml"/>
|
|
7
|
+
</interface>
|
|
8
|
+
<facade>
|
|
9
|
+
<tile>
|
|
10
|
+
<layer name="delivery"><apply-to>/delivery</apply-to></layer>
|
|
11
|
+
<layer name="administration"><apply-to>/administration</apply-to></layer>
|
|
12
|
+
</tile>
|
|
13
|
+
</facade>
|
|
14
|
+
</declaration>
|
data/server/service.xml
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
<!-- what cewebs-aman service tells the AMan -->
|
|
2
|
+
<service>
|
|
3
|
+
<identifier>hello</identifier>
|
|
4
|
+
<delivery>http://127.0.0.1:9030/delivery</delivery>
|
|
5
|
+
<administration>http://127.0.0.1:9030/administration</administration>
|
|
6
|
+
<description xml:lang="en">
|
|
7
|
+
<name>Hello</name>
|
|
8
|
+
<details>Sample service, shows a text.</details>
|
|
9
|
+
</description>
|
|
10
|
+
</service>
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require 'minitest/autorun'
|
|
2
|
+
require_relative '../lib/cewebs-servicesbase/configuration'
|
|
3
|
+
|
|
4
|
+
class TestConfiguration < Minitest::Test
|
|
5
|
+
C = CEWebS::Configuration
|
|
6
|
+
|
|
7
|
+
def test_names
|
|
8
|
+
doc = XML::Smart.string('<a><b/></a>')
|
|
9
|
+
['Plain',"O'Neill",'Say "hi"','a [b','a ] b',%q{both " and '},'a/b|c','"',''].each do |name|
|
|
10
|
+
doc.root.find('b').first.text = name
|
|
11
|
+
path = "a/b[.=#{C::literal(name)}]"
|
|
12
|
+
assert_equal 1, doc.find(path).length, name
|
|
13
|
+
assert_equal 'a', C::parent(path), name
|
|
14
|
+
assert_equal 'a/b', C::plain(path), name
|
|
15
|
+
assert_equal path, C::parent(path + '/c'), name
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def test_parent
|
|
20
|
+
assert_equal '/', C::parent('/a')
|
|
21
|
+
assert_equal '.', C::parent('a')
|
|
22
|
+
assert_equal 'a/x|a/y|a', C::parent('a/x|a/y|a/z')
|
|
23
|
+
assert_equal 'a/b[c[@d="e/f"]]', C::parent('a/b[c[@d="e/f"]]/g')
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def test_plain
|
|
27
|
+
assert_equal 'a/z', C::plain('/a/x|/a[@b="c"]/z')
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#!/usr/bin/ruby -W0
|
|
2
|
+
# Please see the LICENSE.GPL file for copyright and distribution information
|
|
3
|
+
require 'optparse'
|
|
4
|
+
require 'cewebs-servicesbase/scaffold'
|
|
5
|
+
|
|
6
|
+
ARGV.options { |opt|
|
|
7
|
+
opt.summary_indent = ' ' * 2
|
|
8
|
+
opt.banner = "Usage:\n#{opt.summary_indent}#{File.basename($0)} new DIR\n"
|
|
9
|
+
opt.on('Options:')
|
|
10
|
+
opt.on('--help', '-h', 'This text') { puts opt; exit }
|
|
11
|
+
opt.separator <<~END
|
|
12
|
+
|
|
13
|
+
new DIR scaffolds a sample service, as a start for your own. Change identifier and port in service.xml
|
|
14
|
+
and service, and tell the AMan about it with: cewebs-aman service AMAN DIR
|
|
15
|
+
END
|
|
16
|
+
opt.parse!
|
|
17
|
+
}
|
|
18
|
+
command, dir = ARGV
|
|
19
|
+
unless command == 'new' && ARGV.length == 2
|
|
20
|
+
puts ARGV.options
|
|
21
|
+
exit 1
|
|
22
|
+
end
|
|
23
|
+
# the sample is there to be changed, so it is never updated
|
|
24
|
+
abort 'Directory already exists.' if File.exist?(dir)
|
|
25
|
+
|
|
26
|
+
CEWebS::Scaffold::create(File.join(__dir__,'..','server'),dir)
|