forked from micw/php-java-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.java
More file actions
678 lines (652 loc) · 20.8 KB
/
Copy pathRequest.java
File metadata and controls
678 lines (652 loc) · 20.8 KB
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
/*-*- mode: Java; tab-width:8 -*-*/
package php.java.bridge;
/*
* Copyright (C) 2003-2007 Jost Boekemeier
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
/**
* This class is used to handle requests from the front-end.
* @author jostb
*
*/
public final class Request implements IDocHandler {
private Parser parser;
private JavaBridge defaultBridge, bridge;
protected static final IntegerComparator PHP_ARRAY_KEY_COMPARATOR = new IntegerComparator();
// Only used when the async. protocol is enabled.
protected static final class PhpNull {
public String toString () {
return ""; //evaluates to false
}
}
protected static final PhpNull PHPNULL = new PhpNull();
protected Object getGlobalRef(int i) {
Object ref = bridge.globalRef.get(i);
if(ref == PHPNULL) return null;
return ref;
}
static final Object[] ZERO_ARGS = new Object[0];
private class SimpleContext {
public void parseID(ParserString string) {}
public void setID(Response response) {}
}
private class Context extends SimpleContext {
protected long id;
public void parseID(ParserString string) {
this.id = string.getLongValue();
}
public void setID(Response response) {
response.setID(id);
}
}
private SimpleContext contextCache;
SimpleContext getContext() {
if(contextCache!=null) return contextCache;
if(bridge.options.passContext()) {
return contextCache = new Context();
}
return contextCache = new SimpleContext();
}
private abstract class Arg {
protected byte type;
protected Object callObject;
protected String method;
protected SimpleContext id = getContext();
protected byte predicate;
protected Object key;
protected byte composite;
public abstract void add(Object val);
public abstract Object[] getArgs();
public abstract void reset();
}
private final class SimpleArg extends Arg {
private LinkedList list;
public void add(Object val) {
if(list==null) list=new LinkedList();
list.add(val);
}
public Object[] getArgs() {
return (list==null) ? Request.ZERO_ARGS : list.toArray();
}
public void reset() {
list=null;
composite=0;
type=0;
callObject=null;
method=null;
key=null;
}
}
private final class CompositeArg extends Arg {
private PhpArray ht = null;
private int count = 0;
private Arg parent;
public CompositeArg(Arg parent) {
this.parent = parent;
}
public void add(Object val) {
if(ht==null) ht=new PhpArray();
if(key!=null) {
ht.put(key, val);
}
else {
ht.put(new Integer(count++), val);
}
}
protected Arg pop() {
if(ht==null) ht=new PhpArray();
parent.add(ht);
return parent;
}
/* (non-Javadoc)
* @see php.java.bridge.Request.Arg#getArgs()
*/
public Object[] getArgs() {
bridge.logError("Protocol error: getArgs");
return ZERO_ARGS;
}
/* (non-Javadoc)
* @see php.java.bridge.Request.Arg#reset()
*/
public void reset() {
bridge.logError("Protocol error: reset");
}
}
private Arg arg;
/**
* The current response handle or null.
* There is only one response handle for each request object.
* <code>response.reset()</code> or <code>response.flush()</code> must be called at the end of each packet.
*/
Response response = null;
/**
* Creates an empty request object.
* @param bridge The bridge instance.
* @see Request#init(InputStream, OutputStream)
*/
public Request(JavaBridge bridge) {
this.bridge=bridge;
this.parser=new Parser(bridge, this);
}
static final byte[] ZERO={0};
static final Object ZERO_OBJECT=new Object();
/**
* This method must be called with the current header option byte.
* It initializes the request object.
*
* @param ch the current php options.
*/
public void init(byte ch) {
parser.initOptions(ch);
}
/**
* This method must be called with the current input and output streams.
* It reads the protocol header and initializes the request object.
*
* @param in The input stream.
* @param out The output stream.
* @return true, if the protocol header was valid, false otherwise.
* @throws IOException
*/
public boolean init(InputStream in, OutputStream out) throws IOException {
switch(parser.initOptions(in, out)) {
case Parser.PING:
bridge.logDebug("PING - PONG - Closing Request");
out.write(ZERO, 0, 1);
return false;
case Parser.IO_ERROR:
bridge.logDebug("IO_ERROR - Closing Request");
return false;
case Parser.EOF:
bridge.logDebug("EOF - Closing Request");
return false;
}
return true;
}
private long getClassicPhpLong(ParserString st[]) {
return st[0].getClassicLongValue();
}
private Object createClassicExact(ParserString st[]) {
return new Integer(st[0].getClassicIntValue());
}
private long getPhpLong(ParserString st[]) {
long val = st[0].getLongValue();
if(st[1].string[st[1].off]!='O')
val *= -1;
return val;
}
private Object createExact(ParserString st[]) {
{
int val = st[0].getIntValue();
if(st[1].string[st[1].off]!='O')
val *= -1;
return (new Integer(val));
}
}
/**{@inheritDoc}*/
public boolean begin(ParserTag[] tag) {
boolean reply = true;
ParserString[] st=tag[2].strings;
byte ch;
switch (ch=tag[0].strings[0].string[0]) {
case 'G':
case 'Y': {
arg.type=ch;
arg.predicate=st[0].string[st[0].off];
int i= st[1].getIntValue();
arg.callObject=i==0?bridge:getGlobalRef(i);
arg.method=st[2].getCachedStringValue();
arg.id.parseID(st[3]);
break;
}
case 'I': {
arg.type=ch;
int i= st[0].getIntValue();
arg.callObject=i==0?bridge:getGlobalRef(i);
arg.method=st[1].getCachedStringValue();
arg.predicate=st[2].string[st[2].off];
arg.id.parseID(st[3]);
break;
}
case 'H':
case 'K': {
arg.type=ch;
arg.predicate=st[0].string[st[0].off];
arg.callObject=st[1].getCachedStringValue();
arg.id.parseID(st[2]);
break;
}
case 'C': {
arg.type=ch;
arg.callObject=st[0].getCachedStringValue();
arg.predicate=st[1].string[st[1].off];
arg.id.parseID(st[2]);
break;
}
case 'F': {
arg.type=ch;
arg.predicate = st[0].string[st[0].off];
break;
}
case 'R': {
arg.type=ch;
arg.id.parseID(st[0]);
break;
}
case 'X': {
arg = new CompositeArg(arg);
arg.composite=st[0].string[st[0].off];
break;
}
case 'P': {
if(arg.composite=='H') {// hash
if(st[0].string[st[0].off]=='S')
arg.key = st[1].getCachedStringValue();
else {
arg.key = new Integer(st[1].getIntValue());
}
} else // array
arg.key=null;
break;
}
case 'U': {
int i=st[0].getIntValue();
if(Util.logLevel>4) {
Object obj = bridge.globalRef.get(i);
if (!(obj instanceof java.lang.reflect.Proxy))
Util.logDebug("unref: " + obj);
}
bridge.globalRef.remove(i);
reply=false; // U is the only top-level request which doesn't need a reply
break;
}
case 'S':
if(tag[2].n>0) { // handle <S v=... />
if(arg.composite!='H')
arg.add(new PhpParserString(bridge, st[0]));
else // hash has no type information
arg.add(st[0].getStringValue());
}
break;
case 'B': {
arg.add(new Boolean(st[0].string[st[0].off]=='T'));
break;
}
case 'T': {
arg.add(new Boolean(st[0].string[st[0].off]=='1'));
break;
}
case 'L': {
if(arg.composite!='H')
arg.add(new PhpExactNumber(getPhpLong(st)));
else // hash has no type information
arg.add(createExact(st));
break;
}
case 'J': {
if(arg.composite!='H')
arg.add(new PhpExactNumber(getClassicPhpLong(st)));
else // hash has no type information
arg.add(createClassicExact(st));
break;
}
case 'D': {
arg.add(new Double(st[0].getDoubleValue()));
break;
}
case 'E': {
if(0==st[0].length)
arg.callObject=new Exception(st[1].getStringValue());
else {
int i=st[0].getIntValue();
if(0==i) {
arg.callObject=new Exception(st[1].getStringValue());
}
else
arg.callObject=getGlobalRef(i);
}
break;
}
case 'O': {
if(0==st[0].length)
arg.add(null);
else {
int i=st[0].getIntValue();
if(0==i)
arg.add(null);
else
arg.add(getGlobalRef(i));
}
break;
}
}
return reply;
}
/**{@inheritDoc}*/
public void end(ParserString[] string) {
switch(string[0].string[0]) {
case 'X': {
arg = ((CompositeArg)arg).pop();
}
}
}
private static final String SUB_FAILED = "PHP callback execution failed.";
private void setIllegalStateException(String s) {
IllegalStateException ex = new IllegalStateException(s);
// PHP callbacks always generate a round-trip anyway
response.setResultException(bridge.lastException = ex, true);
}
private int handleRequest() throws IOException {
int retval;
if(Parser.OK==(retval=parser.parse(bridge.in))) {
arg.id.setID(response);
switch(arg.type){
case 'I':
try {
switch(arg.predicate) {
case 'P': bridge.GetSetProp(arg.callObject, arg.method, arg.getArgs(), response); break;
case 'I': bridge.Invoke(arg.callObject, arg.method, arg.getArgs(), response); break;
default: throw new IOException("Protocol error");
}
response.flush();
} catch (AbortException sub) {
Util.printStackTrace(sub);
retval = Parser.EOF;
}
break;
case 'G':
try {
switch(arg.predicate) {
case '2': response.setAsyncWriter();
case '1': bridge.GetSetProp(arg.callObject, arg.method, arg.getArgs(), response); break;
case '3': response.setAsyncVoidWriter(); bridge.GetSetProp(arg.callObject, arg.method, arg.getArgs(), response); break;
default: throw new IOException("Protocol error");
}
response.flush();
} catch (AbortException sub) {
Util.printStackTrace(sub);
retval = Parser.EOF;
}
break;
case 'Y':
try {
switch(arg.predicate) {
case '2': response.setAsyncWriter();
case '1': bridge.Invoke(arg.callObject, arg.method, arg.getArgs(), response); break;
case '3': response.setAsyncVoidWriter(); bridge.Invoke(arg.callObject, arg.method, arg.getArgs(), response); break;
default: throw new IOException("Protocol error");
}
response.flush();
} catch (AbortException sub) {
Util.printStackTrace(sub);
retval = Parser.EOF;
}
break;
case 'C':
try {
switch(arg.predicate) {
case 'C': bridge.CreateObject((String)arg.callObject, false, arg.getArgs(), response); break;
case 'I': bridge.CreateObject((String)arg.callObject, true, arg.getArgs(), response); break;
default: throw new IOException("Protocol error");
}
response.flush();
} catch (AbortException sub) {
Util.printStackTrace(sub);
retval = Parser.EOF;
}
break;
case 'H':
try {
switch(arg.predicate) {
case '2': response.setAsyncWriter();
case '1': bridge.CreateObject((String)arg.callObject, false, arg.getArgs(), response); break;
case '3': response.setAsyncVoidWriter(); bridge.CreateObject((String)arg.callObject, false, arg.getArgs(), response); break;
default: throw new IOException("Protocol error");
}
response.flush();
} catch (AbortException sub) {
Util.printStackTrace(sub);
retval = Parser.EOF;
}
break;
case 'K':
try {
switch(arg.predicate) {
case '2': response.setAsyncWriter();
case '1': bridge.CreateObject((String)arg.callObject, true, arg.getArgs(), response); break;
case '3': response.setAsyncVoidWriter(); bridge.CreateObject((String)arg.callObject, true, arg.getArgs(), response); break;
default: throw new IOException("Protocol error");
}
response.flush();
} catch (AbortException sub) {
Util.printStackTrace(sub);
retval = Parser.EOF;
}
break;
case 'F':
IJavaBridgeFactory factory = bridge.getFactory();
if(arg.predicate=='A') { // keep alive
bridge.recycle();
try {
((AppThreadPool.Delegate)Thread.currentThread()).setPersistent();
} catch (ClassCastException ex) {/*no thread pool*/}
response.setFinish(true);
} else { // terminate or terminate keep alive
response.setFinish(false);
retval = Parser.EOF;
}
response.flush();
factory.invalidate();
break;
case 'R': // should not happen
setIllegalStateException(SUB_FAILED);
response.flush();
break;
}
arg.reset();
}
return retval;
}
/**
* Start handling requests until EOF. Creates a response object and handles all packets.
* @throws IOException
*/
public void handleRequests() throws IOException {
if(response==null) response=bridge.createResponse();
this.arg=new SimpleArg();
while(Parser.OK==handleRequest())
;
}
/**
* Start handling one request. Creates a response object and handles one packet.
* @throws IOException
*/
public boolean handleOneRequest() throws IOException {
if(response==null) response=bridge.createResponse();
this.arg=new SimpleArg();
return(Parser.OK==handleRequest());
}
/** This exception isn't an exception but a construct to emulate a one-shot continuation in Java.
* It is used to quickly clear the stack and to jumb back to some top-level loop.
* <p>
* The exception is thrown when the client exits while running a callback,
* it aborts everything and jumps back to the top-level request-handling loop, see handleRequests() above.
* </p>
* <p>Do not catch this exception! If you must catch Exception(), check for AbortException. If
* you invoke using the reflection interface, check for the wrapped exception, unwrap it, if necessary
* and re-throw the exception</p>
* @see Request#handleRequest()
*/
public static class AbortException extends RuntimeException {
private static final long serialVersionUID = 7778150395848350732L;
}
/**
* When within a sub request, handle an IOException as EOF
* @return EOF if an IOException or EOF occurred, otherwise the parser status is returned
*/
private short parseSubRequestUntilEof () {
try {
return parser.parse(bridge.in);
} catch (IOException e) {
bridge.printStackTrace(e);
return Parser.EOF;
}
}
/**
* Handle protocol sub-requests, see <code>R</code> and <code>A</code> in the protocol spec.
* @return An array of one object. The object is the result of the Apply call.
* @throws IOException
* @throws Throwable thrown by the PHP code.
*/
protected Object[] handleSubRequests() throws AbortException, Throwable {
Response currentResponse = response; Arg current = arg;
response = response.copyResponse(); // must keep the current response state, for example coerceWriter for castToString()
arg = new SimpleArg();
while(Parser.OK==parseSubRequestUntilEof()){
arg.id.setID(response);
switch(arg.type){
case 'I':
switch(arg.predicate) {
case 'P': bridge.GetSetProp(arg.callObject, arg.method, arg.getArgs(), response); break;
case 'I': bridge.Invoke(arg.callObject, arg.method, arg.getArgs(), response); break;
default: throw new IOException("Protocol error");
}
response.flush();
break;
case 'G':
switch(arg.predicate) {
case '2': response.setAsyncWriter();
case '1': bridge.GetSetProp(arg.callObject, arg.method, arg.getArgs(), response); break;
case '3': response.setAsyncVoidWriter(); bridge.GetSetProp(arg.callObject, arg.method, arg.getArgs(), response); break;
default: throw new IOException("Protocol error");
}
response.flush();
break;
case 'Y':
switch(arg.predicate) {
case '2': response.setAsyncWriter();
case '1': bridge.Invoke(arg.callObject, arg.method, arg.getArgs(), response); break;
case '3': response.setAsyncVoidWriter(); bridge.Invoke(arg.callObject, arg.method, arg.getArgs(), response); break;
default: throw new IOException("Protocol error");
}
response.flush();
break;
case 'C':
switch(arg.predicate) {
case 'C':bridge.CreateObject((String)arg.callObject, false, arg.getArgs(), response); break;
case 'I': bridge.CreateObject((String)arg.callObject, true, arg.getArgs(), response); break;
default: throw new IOException("Protocol error");
}
response.flush();
break;
case 'H':
switch(arg.predicate) {
case '2':response.setAsyncWriter();
case '1':bridge.CreateObject((String)arg.callObject, false, arg.getArgs(), response); break;
case '3':response.setAsyncVoidWriter(); bridge.CreateObject((String)arg.callObject, false, arg.getArgs(), response); break;
default: throw new IOException("Protocol error");
}
response.flush();
break;
case 'K':
switch(arg.predicate) {
case '2': response.setAsyncWriter();
case '1': bridge.CreateObject((String)arg.callObject, true, arg.getArgs(), response); break;
case '3': response.setAsyncVoidWriter(); bridge.CreateObject((String)arg.callObject, true, arg.getArgs(), response); break;
default: throw new IOException("Protocol error");
}
response.flush();
break;
case 'F': // may happen due to a fatal error in a sub request
response.setFinish(false);
response.flush();
response = currentResponse;
arg = current;
throw new AbortException();
// no factory.invalidate necessary; ContextRunner will terminate and call factory.destroy()
case 'R':
Arg ret = arg;
arg = current;
// remove retval from the output buffer and return it to the parent.
response.reset();
response = currentResponse;
if(ret.callObject!=null)
throw (Throwable)ret.callObject;
return ret.getArgs();
}
arg.reset();
}
// should not happen
response.setFinish(false);
response.flush();
arg = current;
response = currentResponse;
throw new AbortException();
}
/**
* Reset the internal state so that a new input and output stream
* can be used for the next packed. Note that request options
* (from init()) remain valid.
* @see #init(InputStream, OutputStream)
*/
public void reset() {
parser.reset();
}
/**
* Set a temporary bridge into this request. The bridge and its
* associated session-/contextFactory will be automatically destroyed when the request is done.
* @see php.java.bridge.http.IContextFactory#recycle(String)
* @param bridge The fresh bridge and its ContextFactory
*/
public void setBridge(JavaBridge bridge) {
defaultBridge = this.bridge;
bridge.in = this.bridge.in;
bridge.out = this.bridge.out;
this.bridge = bridge;
this.bridge.request = this;
response.setBridge(bridge);
parser.setBridge(bridge);
}
private void resetBridge() {
if(defaultBridge!=null) {
bridge = defaultBridge;
response.setBridge(bridge);
parser.setBridge(bridge);
defaultBridge = null;
}
}
/** re-initialize for new requests */
public void recycle() {
reset();
if(arg != null) arg.reset();
if (response != null) response.recycle();
resetBridge();
}
/**
* {@inheritDoc}
*/
public void parseHeader(InputStream in) throws IOException {
bridge.getFactory().parseHeader(this, in);
}
}