-
Notifications
You must be signed in to change notification settings - Fork 6
/
RestBCAdapter.bbj
829 lines (666 loc) · 31.4 KB
/
RestBCAdapter.bbj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
use java.util.HashMap
use java.io.File
use com.basiscomponents.db.ResultSet
use com.basiscomponents.db.DataRow
use ::RestBridge/InputHandler.bbj::InputHandler
seterr execerr
debug=num(stbl("DEBUG",err=*next),err=*next)
if debug then System.out.println("REST: new remote session ")
BBjAPI().getConfig().setOptionSetting("ERROR_UNWINDS", 1)
ses$ = argv(1)
authtype$=argv(2)
auth$ = argv(3)
if debug then System.out.println("REST: WORKDIR "+dir(""))
ses$=stbl("SESSIONID",ses$)
BBjAPI().getGlobalNamespace().setValue(ses$+"_THREAD",Thread.currentThread())
servletParams! = new HashMap()
servletParams! = BBjAPI().getGlobalNamespace().getValue(ses$+"_SERVLET_PARAMETERS",err=*next)
if servletParams!.get("REST_TRACE") <> null() then
tracedir$ = servletParams!.get("REST_TRACE").toString()
tracedir! = new File(tracedir$,err=*endif)
if tracedir!.exists() AND tracedir!.isDirectory() then
TRACE_CH=UNT
OPEN (TRACE_CH,MODE="O_CREATE,O_TRUNC")tracedir$+"/RestBCAdapter_"+ses$+str(System.currentTimeMillis())+"_"+str(dec(INFO(3,1)))+".trace"
SETTRACE (TRACE_CH)
endif
endif
if debug then System.out.println("Got auth header: "+authtype$+" "+auth$)
authtype$=cvs(authtype$,4)
rem Retrieve the auth pgm to call
authpgm$="authenticate.bbj"
if servletParams!.get("REST_AUTHPGM") <> null() then
authpgm$ = servletParams!.get("REST_AUTHPGM").toString()
endif
user$=""
authenticated=0
call authpgm$ + "::AUTHENTICATION", err=*next
if err = 21 AND authenticated = 0 then
rem Label not found --> custom authenticate without new label
rem fallback code
System.out.println("***********************************")
System.out.println("WARNING: Deprecated fallback code was used due to the missing label ""AUTHORIZATION"" in the authentication program")
System.out.println("WARNING: Please consider to update the authentication program, as this fallback code will be removed in a later version")
System.out.println("***********************************")
if authtype$="BASIC" then
auth$ = java.util.Base64.getDecoder().decode(auth$)
user$ = auth$(1,pos(":"=auth$)-1)
password$ = auth$(pos(":"=auth$)+1)
endif
if authtype$="BEARER" then
jwt$ = auth$
endif
authenticated = 0
rem changing the string encoding to the server's encoding
user$ = new String(user$,"utf-8").getBytes(info(1,2))
password$ = new String(password$,"utf-8").getBytes(info(1,2))
authpgm$="authenticate.bbj"
if servletParams!.get("REST_AUTHPGM") <> null() then
authpgm$ = servletParams!.get("REST_AUTHPGM").toString()
endif
call authpgm$
else
if authenticated = 0 then
throw errmes(-1), err
endif
endif
requestSemaphore! = BBjAPI().getGlobalNamespace().getValue(ses$+"_REQUEST_SEM")
responseSemaphore! = BBjAPI().getGlobalNamespace().getValue(ses$+"_RESPONSE_SEM")
done_login:
REST_TIMEOUT=60
if servletParams!.containsKey("REST_TIMEOUT") then
REST_TIMEOUT=num(servletParams!.get("REST_TIMEOUT").toString(),err=*next)
endif
REST_MAX_WORKER_RECYCLECOUNT=0
if servletParams!.containsKey("REST_MAX_WORKER_RECYCLECOUNT") then
REST_MAX_WORKER_RECYCLECOUNT=num(servletParams!.get("REST_MAX_WORKER_RECYCLECOUNT").toString(),err=*next)
endif
main_loop:
CLEAR EXCEPT user$, password$, ses$, requestSemaphore!, responseSemaphore!, ses!, authenticated, REST_TIMEOUT, usecount, debug, authpgm$, servletParams!, REST_MAX_WORKER_RECYCLECOUNT
seterr execerr
dummy$=STBL("!SESSIONINFO",ses$+" - idle "+str(usecount))
rem requestSemaphore!.acquire()
if !requestSemaphore!.tryAcquire(1,REST_TIMEOUT,java.util.concurrent.TimeUnit.SECONDS) then
rem a timeout has occurred while trying to acquire the semaphore
rem remove the sessions' namespace values
namespace! = BBjAPI().getGlobalNamespace()
namespace!.removeValue(ses$+"_REQUEST")
namespace!.removeValue(ses$+"_RESPONSE")
namespace!.removeValue(ses$+"_REQUEST_SEM")
namespace!.removeValue(ses$+"_RESPONSE_SEM")
namespace!.removeValue(ses$+"_SERVLET_PARAMETERS")
namespace!.removeValue(ses$+"_OUTPUT_HANDLER", err=*next)
namespace!.removeValue(ses$+"_INPUT_HANDLER", err=*next)
namespace!.removeValue(ses$+"_THREAD", err=*next)
rem Calling the ON_CLEANUP label to perform possible user defined cleanup operations
call authpgm$+"::ON_CLEANUP",err=*next
release
fi
request!=BBjAPI().getGlobalNamespace().getValue(ses$+"_REQUEST")
if (str(request!) = "PING") then
responseSemaphore!.release()
goto main_loop
fi
call authpgm$+"::ON_REQUEST",err=*next
if debug then System.out.println("REST: remote session running "+ses$)
dummy$=STBL("!SESSIONINFO",ses$+" - working "+str(request!))
rem Creating the InputHandler to map the Request content to the desired format
namespace! = BBjAPI().getGlobalNamespace()
handler! = namespace!.getValue(ses$ + "_INPUT_HANDLER", err=*next)
if handler! <> null() then
inputHandler! = eval(handler!)
else
inputHandler! = new InputHandler()
endif
inputHandler!.setRequestMethod(request!.get("method"))
inputHandler!.setRequestBody(request!.get("body"))
inputHandler!.setRequestURI(request!.get("uri"))
inputHandler!.setRequestParams(request!.get("params"))
inputHandler!.setRequestHeaders(request!.get("headers"))
inputHandler!.setRequestedCustomMethod(request!.get("invoke"))
inputHandler!.setRequestedCustomMethodReturnValueName(request!.get("retvarname"))
inputHandler!.setSession(ses$)
rem Get the BC name to invoke
bcName! = inputHandler!.getBCName()
response! = new HashMap()
params! = new HashMap()
if bcName! = null() then
statuscode=404
throw "Missing BC",12
endif
vec! = request!.get("preferredlocales")
if vec! <> null() and vec!.size() then
void$ = stbl("!LOCALE",vec!.getItem(0).getLanguage())
endif
ns! = BBjAPI().getNamespace("RestBCAdapter_"+ses$,"BCList",1)
bcMap! = ns!.getValue("BCMAP",err=*next)
if bcMap! = null() then
bcMap! = new HashMap()
ns!.setValue("BCMAP",bcMap!)
endif
rem Special Funtionality
if bcName!="Token" then
rs! = new ResultSet()
token_rec! = new DataRow()
rem token_rec! should be created in the customizable authpgm
call authpgm$+"::ISSUE_JWT"
rs!.add(token_rec!)
response!.put("resultset",rs!)
response!.put("BC","Token")
response!.put("single",1)
goto send_response
fi
bcName! = bcName!+"BC"
if bcMap!.containsKey(bcName!) then
bc! = bcMap!.get(bcName!)
else
if servletParams!.containsKey("REST_PGM_PREFIX") then
prefix$ = servletParams!.get("REST_PGM_PREFIX")
void$ = stbl("REST_PGM_PREFIX", prefix$)
endif
if servletParams!.containsKey("REST_PGM_SUFFIX") then
suffix$ = servletParams!.get("REST_PGM_SUFFIX")
void$ = stbl("REST_PGM_SUFFIX", suffix$)
endif
bc! = null()
ok=0; bc! = eval("new ::" + prefix$ + bcName! + suffix$ + "::" + bcName! + "()", err=*next); ok=1
if ok=0 then
statuscode=404
throw "Missing BC", 12
endif
bcMap!.put(bcName!, bc!)
endif
attributesRecord! = bc!.getAttributesRecord()
inputHandler!.setAttributesRecord(attributesRecord!)
if servletParams!.containsKey("USE_GET_ALLOWED_FILTER") and servletParams!.get("USE_GET_ALLOWED_FILTER").toString().equals("true") then
allowedFilterFields! = bc!.getAllowedFilter(err=*next)
inputHandler!.setAllowedFilterFields(allowedFilterFields!)
endif
method$ = inputHandler!.getMethod()
urlExtension! = ""
urlExtensionList! = inputHandler!.getTrailingRoutesList()
it! = urlExtensionList!.iterator()
while it!.hasNext()
extension! = it!.next()
urlExtension! = urlExtension! + "/" + extension!
wend
if !urlExtension!.isEmpty() then
urlExtension! = urlExtension!.substring(1)
endif
BCPrimaryKeyString! = ""
fields! = attributesRecord!.getFieldsHavingAttributeValue("EDITABLE", "2")
it! = fields!.getFieldNames().iterator()
while it!.hasNext()
BCPrimaryKeyString!=BCPrimaryKeyString!+"/"+it!.next()
wend
if !BCPrimaryKeyString!.isEmpty() then
BCPrimaryKeyString! = BCPrimaryKeyString!.substring(1)
endif
customMethod! = inputHandler!.getCustomMethodName()
if customMethod! <> null() AND customMethod! <> "" then
methodParams! = inputHandler!.getCustomMethodParams()
variableNamePrefix! = new String(user$)
variableNamePrefix! = variableNamePrefix!.replaceAll("\W", "_")
invokeCommand! = "bc!." + customMethod! + "("
if methodParams! <> null() AND !methodParams!.isEmpty() then
for i=0 to methodParams!.size() -1
variableName! = variableNamePrefix! + "_" + str(i) + "!"
execute variableName! + " = methodParams!.get(" + str(i) + ")"
parameterList$ = parameterList$ + "," + variableName!
next i
rem removing the leading ,
parameterList$ = parameterList$(2)
invokeCommand! = invokeCommand! + parameterList$ + ")"
else
invokeCommand! = invokeCommand! + ")"
endif
rem return value
returnValue! = inputHandler!.getCustomMethodReturnValueName()
if returnValue! <> null() then
invokeCommand! = "var! = " + invokeCommand!
endif
statuscode=400
execute invokeCommand!, err=*next
statuscode=0
if returnValue! <> null() AND returnValue! <> "" then
className! = var!.getClass().getName()
switch className!
case "com.basiscomponents.db.ResultSet"
rs! = var!
break
case "com.basiscomponents.db.DataRow"
rs! = new ResultSet()
rs!.add(var!)
break
case "java.util.HashMap"
rs! = new ResultSet()
row! = new DataRow(var!)
rs!.add(row!)
break
case "java.lang.String"
case "java.lang.Boolean"
rs! = new ResultSet()
row! = new DataRow()
row!.setFieldValue(returnValue!, var!)
rs!.add(row!)
break
case default
if !className!.contains("BBjNumber") AND !className!.contains("BBjVector") then
rs! = new ResultSet()
endif
rs! = new ResultSet()
row! = new DataRow()
row!.setFieldValue(returnValue!, var!)
rs!.add(row!)
break
swend
response!.put("resultset", rs!)
else
response!.put("resultset","")
response!.put("statuscode","204")
endif
response!.put("request",str(request!))
response!.put("BC",bcName!)
response!.put("sel",urlExtension!)
response!.put("primarykey",BCPrimaryKeyString!)
goto send_response
endif
switch method$
case "POST"
rem normal POST request
errorOccurred = 0
rs! = new ResultSet()
body! = inputHandler!.getBody()
rem Empty RS is invalid
if body!.isEmpty() then
throw "The request was not valid", 400; rem BAD REQUEST
endif
rem Additional check if the body is empty
rem This check is necessary due to the fact that an empty DataRow
rem is created inside the ResultSet having the automatic field "META"
rem The "META" field prevents the isEmpty check to return true.
rem Luckily the DataRow.getFieldNames method doesn't return the "META"
if body!.size() = 1 AND body!.get(0).getFieldNames().size() = 0 then
throw "The request was not valid", 400; rem BAD REQUEST
endif
extensionFilter! = inputHandler!.getTrailingRoutesFilter()
it! = body!.iterator()
while it!.hasNext()
row! = it!.next()
row!.mergeRecord(extensionFilter!, Boolean.TRUE)
wend
ok=0; rs! = bc!.write(body!,err=*next); ok=1
if !ok then
if pos("No match for method" = errmes(-1)) = 0 then
response!.put("statuscode","409")
response!.put("errorcode",str(err))
response!.put("errormsg",errmes(-1))
endif
else
rem Successfull
rem TODO - Check the Created field
REM created! = row!.getAttribute("CREATED")
REM if !errorOccurred then
REM if created! <> null() and created!.equals("true") then
REM response!.put("statuscode","201")
REM else
REM response!.put("statuscode","200")
REM endif
REM endif
response!.put("statuscode","200")
endif
if !ok AND pos("No match for method" = errmes(-1)) > 0 then
rs! = new ResultSet()
it! = body!.iterator()
while it!.hasNext()
row! = it!.next()
ok=0; dr! = bc!.write(row!,err=*next); ok=1
if !ok then
errorOccurred=1
response!.put("statuscode","409")
response!.put("errorcode",str(err))
response!.put("errormsg",errmes(-1))
else
rs!.addItem(dr!)
created! = dr!.getAttribute("CREATED")
if !errorOccurred then
if created! <> null() and created!.equals("true") then
response!.put("statuscode","201")
else
response!.put("statuscode","200")
endif
endif
endif
wend
endif
response!.put("resultset",rs!)
response!.put("request",str(request!))
response!.put("BC", bcName!)
response!.put("sel",urlExtension!)
response!.put("primarykey",BCPrimaryKeyString!)
response!.put("single",rs!.size()>1)
break
case "PUT"
case "DELETE"
body! = inputHandler!.getBody()
if body! = null() then
body! = new ResultSet()
endif
extensionList! = inputHandler!.getTrailingRoutesList()
rem only merge the url fields into the body if the url doesn't contain the ingorekeys extension
if extensionList!.size() <> 1 OR cvs(extensionList!.get(0),4) <> "IGNOREKEYS" then
rem Adding the field from the URL extension to the fields in the body
primaryKeysDR! = attributesRecord!.getFieldsHavingAttributeValue("EDITABLE", "2")
if extensionList!.isEmpty() OR primaryKeysDR!.isEmpty() then
throw "Missing resource ID", 400; rem BAD REQUEST
endif
primaryKeyFieldNames! = primaryKeysDR!.getFieldNames()
rem Only a specific record shall be deleted, therefore the record should be identifed using all primary keys fields
if extensionList!.size() < primaryKeyFieldNames!.size() then
throw "Not enough parameters for " + method$, 400; rem BAD REQUEST
endif
rem if there is no body, merge the fields into the body
if body!.isEmpty() then
row! = new DataRow()
for i=0 to primaryKeyFieldNames!.size() -1
primaryKeyFieldName! = primaryKeyFieldNames!.get(i)
row!.setFieldValue(primaryKeyFieldName!, extensionList!.get(i))
next i
body!.add(row!)
else
it! = body!.iterator()
while it!.hasNext()
row! = it!.next()
for i=0 to primaryKeyFieldNames!.size() -1
primaryKeyFieldName! = primaryKeyFieldNames!.get(i)
rem adding the URL extension field to the body row if it doesn't already exist in the body row
if !row!.contains(primaryKeyFieldName!) then
row!.setFieldValue(primaryKeyFieldName!, extensionList!.get(i))
continue
endif
if row!.getFieldAsString(primaryKeyFieldName!) <> extensionList!.get(i) then
throw "Different resource ID in URL path and request body!", 400; REM BAD REQUEST
endif
next i
wend
endif
endif
rem In order to write / delete, the body RS must contain at least one record
if body!.isEmpty() OR (body!.size() = 1 AND body!.get(0).isEmpty()) then
throw "Not enough parameters for " + method$, 400; rem BAD REQUEST
endif
rem Attempt to write the whole ResultSet
if method$ = "PUT" then
ok=0; rs! = bc!.write(body!, err=*next); ok=1
if !ok then
if pos("No match for method" = errmes(-1)) = 0 then
response!.put("statuscode","409")
throw errmes(-1),err
endif
else
rem TODO - Check the state of the created field ?
created! = dr!.getAttribute("CREATED")
if created! <> null() and created!.equals("true") then
response!.put("statuscode","201")
else
response!.put("statuscode","200")
endif
if rs!.size() = 1 then
response!.put("single",1)
endif
endif
else
rem DELETE
ok=0; bc!.remove(body!, err=*next); ok=1
if !ok then
if pos("No match for method" = errmes(-1)) = 0 then
rem Failed to execute the DELETE Request
statuscode = 409
response!.put("statuscode","409")
throw errmes(-1), err
endif
else
response!.put("statuscode","204")
endif
endif
if !ok AND pos("No match for method" = errmes(-1)) > 0 then
rem The BC doesn't support the write(ResultSet) method yet, use the default process
rs! = new ResultSet()
it! = body!.iterator()
while it!.hasNext()
row! = it!.next()
if method$ = "PUT" then
ok=0; dr! = bc!.write(row!,err=*next); ok=1
if !ok then
response!.put("statuscode","409")
throw errmes(-1),err
endif
created! = dr!.getAttribute("CREATED")
if created! <> null() and created!.equals("true") then
response!.put("statuscode","201")
else
response!.put("statuscode","200")
endif
if body!.size() = 1 then
response!.put("single",1)
endif
rs!.addItem(dr!)
else
ok=0; bc!.remove(row!, err=*next); ok=1
if !ok then
statuscode = 409
response!.put("statuscode","409")
throw errmes(-1), err
endif
response!.put("statuscode","204")
endif
wend
endif
response!.put("resultset",rs!)
response!.put("request",str(request!))
response!.put("BC",bcName!)
response!.put("sel",urlExtension!)
response!.put("primarykey",BCPrimaryKeyString!)
break
case default
restBridgeCommands! = inputHandler!.getRestBridgeCommands()
if restBridgeCommands!.contains("_META") then
rs! = new ResultSet()
rs!.add(attributesRecord!)
response!.put("resultset",rs!)
response!.put("BC",bcName!)
response!.put("sel", urlExtension!)
response!.put("single",1)
goto send_response
endif
filter! = inputHandler!.getFilter()
rem _lookup mechanism
if restBridgeCommands!.contains("_LOOKUP") then
rs! = new ResultSet()
field! = restBridgeCommands!.getFieldAsString("_LOOKUP")
if field! = "" then
throw "Invalid lookup field value: Empty", 400
endif
ok=0; rs! = bc!.getLookupData(field!, filter!, err=*next); ok=1
if !ok then
if pos("No match for method" = errmes(-1)) > 0 then
System.out.println("***********************************")
System.out.println("WARNING: The RestBridge attempted to invoke the non existing getLookupData(String, DataRow) method on the BC: " + bcName! + ".")
System.out.println("WARNING: As a fallback the retrieveLookup(String, DataRow) method will be invoked instead which is marked deprecated.")
System.out.println("WARNING: Please consider changing the retrieveLookup into the getLookupData method as the retrieveLookup fallback will most likely vanish in futre RestBridge versions")
System.out.println("***********************************")
rem fallback for older BC versions
ok=0; rs! = bc!.retrieveLookup(field!, filter!, err=*next); ok=1
if !ok then
if pos("No match for method" = errmes(-1)) > 0 then
throw "Method not found getLookupData", 500
endif
throw errmes(-1), 500
endif
else
throw errmes(-1), 500
endif
endif
response!.put("resultset", rs!)
response!.put("BC", bcName!)
response!.put("single", 0)
goto send_response
endif
response!.put("single",0)
urlExtensionFilter! = inputHandler!.getTrailingRoutesFilter()
if !urlExtensionFilter!.isEmpty() then
response!.put("linkfield", BCPrimaryKeyString!)
fields! = attributesRecord!.getFieldsHavingAttributeValue("EDITABLE", "2")
if fields!.getFieldNames().size() = urlExtensionFilter!.getFieldNames().size() then
response!.put("single",1)
endif
endif
rem applying the merged filter (URL Extension fields + Params)
filter!.mergeRecord(urlExtensionFilter!, Boolean.TRUE)
bc!.setFilter(filter!, err=*next)
rem applying the scope
scope! = ""
if restBridgeCommands!.contains("_SCOPE") then
scope! = restBridgeCommands!.getFieldAsString("_SCOPE")
endif
bc!.setScope(scope!, err=*next)
rem applying the field selection
fieldSel! = new DataRow()
if restBridgeCommands!.contains("_FIELDSEL") then
fieldSel! = cast(DataRow, restBridgeCommands!.getField("_FIELDSEL").getObject())
endif
bc!.setFieldSelection(fieldSel!, err=*next)
first% = 0
last% = 0
limitSearch = 0
if restBridgeCommands!.contains("_PAGE") OR restBridgeCommands!.contains("_PER_PAGE") then
page = 1
page = restBridgeCommands!.getFieldAsNumber("_PAGE", err=*next)
if page < 1 then
throw "Invalid value for _PAGE parameter: must be greater than 0", 400
endif
perPage = restBridgeCommands!.getFieldAsNumber("_PER_PAGE", err=*next)
if perPage < 1 then
throw "Invalid value for _PER_PAGE parameter: must be greater than 0", 400
endif
first% = (int(page)-1)*int(perPage)
last% = first%+int(perPage)-1
limitSearch = 1
endif
if !limitSearch AND restBridgeCommands!.contains("_OFFSET") then
offset = restBridgeCommands!.getFieldAsNumber("_OFFSET", err=*next)
if offset < 0 then
throw "Invalid parameter for _OFFSET parameter: must be greater than or equals 0", 400
endif
limit = 20
if restBridgeCommands!.contains("_LIMIT") then
limit = restBridgeCommands!.getFieldAsNumber("_LIMIT", err=*next)
endif
if limit < 1 then
throw "Invalid parameter for _LIMIT parameter: must be greater than 0", 400
endif
first% = int(offset)
last% = first% + int(limit)-1
limitSearch = 1
endif
if limitSearch then
rs! = bc!.retrieve(first%, last%)
else
rs! = bc!.retrieve()
endif
if restBridgeCommands!.contains("_ATTACHMENT") then
if rs!.size() =1 then
rec! = rs!.getItem(0)
attachfield$ = restBridgeCommands!.getFieldAsString("_ATTACHMENT")
rs! = new ResultSet()
dr! = new DataRow()
filename$=rec!.getFieldAsString(attachfield$)
dr!.setFieldValue("FILEDELIVERY",filename$)
rs!.addItem(dr!)
response!.put("resultset",rs!)
response!.put("BC",bcName!)
response!.put("single",1)
response!.put("file",filename$)
goto send_response
else
throw "file / record not found", 404
endif
endif
response!.put("resultset",rs!)
response!.put("request",str(request!))
response!.put("BC",bcName!)
response!.put("sel",urlExtension!)
response!.put("primarykey",BCPrimaryKeyString!)
response!.put("statuscode","200")
break
swend
send_response:
usecount=usecount+1
BBjAPI().getGlobalNamespace().setValue(ses$+"_RESPONSE",response!)
responseSemaphore!.release()
if debug then
System.out.println("REST: remote session done"+ses$)
System.out.println("REST_MAX_WORKER_RECYCLECOUNT: "+str(REST_MAX_WORKER_RECYCLECOUNT))
System.out.println("CYCLES: "+str(usecount))
fi
IF REST_MAX_WORKER_RECYCLECOUNT>0 AND usecount >= REST_MAX_WORKER_RECYCLECOUNT THEN
if debug then
System.out.println("max cycles reached - releasing worker")
fi
call authpgm$+"::ON_CLEANUP",err=*next
RELEASE
FI
goto main_loop
execerr:
errorcode = err
errormsg$ = errmes(-1)
sw! = new java.io.StringWriter()
pw! = new java.io.PrintWriter(sw!)
BBjAPI().getLastBBjException().printStackTrace(pw!)
stacktrace$ = sw!.toString()
sw!.close()
pw!.close()
if debug then
ch=unt
open (ch,mode="O_CREATE,O_APPEND",err=notrace)"bridgeerrlog.txt"
print (ch,err=*next)date(0:"%Yl-%Mz-%Dz %Hz:%mz:%sz")
print (ch,err=*next)"!ERROR "+str(err)+" "+stacktrace$
print (ch,err=*next)errmes(-1)
close (ch,err=*next)
endif
notrace:
error_out:
if statuscode=0
statuscode = err
fi
response! = new HashMap()
if authenticated>0 then
response!.put("statuscode",str(statuscode))
response!.put("errorcode",errorcode)
response!.put("errormsg",errormsg$)
rem consider: stack trace only with DEBUG flag in config?
response!.put("stacktrace",stacktrace$)
else
response!.put("statuscode","401")
response!.put("errorcode",errorcode)
response!.put("errormsg",errormsg$)
rem no stack trace for unauthenticated users
fi
BBjAPI().getGlobalNamespace().setValue(ses$+"_RESPONSE",response!)
responseSemaphore! = null()
responseSemaphore! = BBjAPI().getGlobalNamespace().getValue(ses$+"_RESPONSE_SEM",err=*next)
if responseSemaphore! <> null() then
responseSemaphore!.release()
fi
wait 10; rem FIN_WAIT
namespace! = BBjAPI().getGlobalNamespace()
namespace!.removeValue(ses$+"_REQUEST",err=*next)
namespace!.removeValue(ses$+"_RESPONSE",err=*next)
namespace!.removeValue(ses$+"_REQUEST_SEM",err=*next)
namespace!.removeValue(ses$+"_RESPONSE_SEM",err=*next)
namespace!.removeValue(ses$+"_SERVLET_PARAMETERS",err=*next)
namespace!.removeValue(ses$+"_OUTPUT_HANDLER", err=*next)
namespace!.removeValue(ses$+"_INPUT_HANDLER", err=*next)
namespace!.removeValue(ses$+"_THREAD", err=*next)
rem Calling the ON_CLEANUP label to perform possible user defined cleanup operations
call authpgm$+"::ON_CLEANUP",err=*next
release