2 |
26 Feb 07 |
jari |
1 |
/* |
2 |
26 Feb 07 |
jari |
Copyright @ 1999-2003, The Institute for Genomic Research (TIGR). |
2 |
26 Feb 07 |
jari |
All rights reserved. |
2 |
26 Feb 07 |
jari |
4 |
*/ |
2 |
26 Feb 07 |
jari |
5 |
/* |
2 |
26 Feb 07 |
jari |
* $RCSfile: MEVRequestAcceptStrategy2.java,v $ |
2 |
26 Feb 07 |
jari |
* $Revision: 1.3 $ |
2 |
26 Feb 07 |
jari |
* $Date: 2005/03/10 15:30:16 $ |
2 |
26 Feb 07 |
jari |
* $Author: braistedj $ |
2 |
26 Feb 07 |
jari |
* $State: Exp $ |
2 |
26 Feb 07 |
jari |
11 |
*/ |
2 |
26 Feb 07 |
jari |
12 |
package org.tigr.remote.gateway; |
2 |
26 Feb 07 |
jari |
13 |
|
2 |
26 Feb 07 |
jari |
14 |
import java.io.BufferedInputStream; |
2 |
26 Feb 07 |
jari |
15 |
import java.io.ByteArrayOutputStream; |
2 |
26 Feb 07 |
jari |
16 |
import java.io.IOException; |
2 |
26 Feb 07 |
jari |
17 |
|
2 |
26 Feb 07 |
jari |
18 |
import javax.servlet.http.HttpServletRequest; |
2 |
26 Feb 07 |
jari |
19 |
import javax.servlet.http.HttpServletResponse; |
2 |
26 Feb 07 |
jari |
20 |
|
2 |
26 Feb 07 |
jari |
21 |
import org.tigr.remote.gateway.util.IQueue; |
2 |
26 Feb 07 |
jari |
22 |
|
2 |
26 Feb 07 |
jari |
23 |
|
2 |
26 Feb 07 |
jari |
24 |
class MEVRequestAcceptStrategy2 { |
2 |
26 Feb 07 |
jari |
25 |
private static final int MAX_MESSAGES_IN_QUEUE = 10; |
2 |
26 Feb 07 |
jari |
26 |
|
2 |
26 Feb 07 |
jari |
27 |
/** |
2 |
26 Feb 07 |
jari |
* Constructs a <code>MEVRequestAcceptStrategy2</code> with specified session. |
2 |
26 Feb 07 |
jari |
29 |
*/ |
2 |
26 Feb 07 |
jari |
30 |
public MEVRequestAcceptStrategy2( SessionState s ) { |
2 |
26 Feb 07 |
jari |
31 |
m_session = s; |
2 |
26 Feb 07 |
jari |
32 |
} |
2 |
26 Feb 07 |
jari |
33 |
|
2 |
26 Feb 07 |
jari |
34 |
/** |
2 |
26 Feb 07 |
jari |
* Accepts a MEV request. |
2 |
26 Feb 07 |
jari |
36 |
*/ |
2 |
26 Feb 07 |
jari |
37 |
public void acceptMEVRequest( HttpServletRequest req, HttpServletResponse resp ) |
2 |
26 Feb 07 |
jari |
38 |
throws IOException { |
2 |
26 Feb 07 |
jari |
39 |
int packetSize = req.getContentLength(); |
2 |
26 Feb 07 |
jari |
40 |
if (packetSize < 1) |
2 |
26 Feb 07 |
jari |
41 |
throw new IOException("Bad content length: " + req.getContentLength() ); |
2 |
26 Feb 07 |
jari |
42 |
ByteArrayOutputStream tmp = new ByteArrayOutputStream( packetSize ); |
2 |
26 Feb 07 |
jari |
43 |
BufferedInputStream in = new BufferedInputStream( req.getInputStream() ); |
2 |
26 Feb 07 |
jari |
44 |
int counter = 0; |
2 |
26 Feb 07 |
jari |
45 |
byte[] b = new byte[1024*100]; |
2 |
26 Feb 07 |
jari |
46 |
int cnt; |
2 |
26 Feb 07 |
jari |
47 |
while (((cnt = in.read(b)) >= 0) && ( counter < packetSize )) { |
2 |
26 Feb 07 |
jari |
48 |
tmp.write(b, 0, cnt); |
2 |
26 Feb 07 |
jari |
49 |
counter += cnt; |
2 |
26 Feb 07 |
jari |
50 |
} |
2 |
26 Feb 07 |
jari |
51 |
String response = tmp.toString(); |
2 |
26 Feb 07 |
jari |
52 |
IQueue queue = m_session.getRequestQueue(); |
2 |
26 Feb 07 |
jari |
53 |
if (queue.getSize() >= MAX_MESSAGES_IN_QUEUE) queue.getHead(); |
2 |
26 Feb 07 |
jari |
54 |
queue.addTail( response ); |
2 |
26 Feb 07 |
jari |
55 |
} |
2 |
26 Feb 07 |
jari |
56 |
|
2 |
26 Feb 07 |
jari |
57 |
private SessionState m_session; |
2 |
26 Feb 07 |
jari |
58 |
} |