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: MEVRequestAcceptStrategy.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.BufferedOutputStream; |
2 |
26 Feb 07 |
jari |
16 |
import java.io.BufferedReader; |
2 |
26 Feb 07 |
jari |
17 |
import java.io.IOException; |
2 |
26 Feb 07 |
jari |
18 |
import java.io.InputStream; |
2 |
26 Feb 07 |
jari |
19 |
import java.io.InputStreamReader; |
2 |
26 Feb 07 |
jari |
20 |
import java.io.OutputStream; |
2 |
26 Feb 07 |
jari |
21 |
|
2 |
26 Feb 07 |
jari |
22 |
import javax.servlet.http.HttpServletRequest; |
2 |
26 Feb 07 |
jari |
23 |
import javax.servlet.http.HttpServletResponse; |
2 |
26 Feb 07 |
jari |
24 |
import javax.servlet.http.HttpUtils; |
2 |
26 Feb 07 |
jari |
25 |
|
2 |
26 Feb 07 |
jari |
26 |
import org.tigr.util.ConfMap; |
2 |
26 Feb 07 |
jari |
27 |
|
2 |
26 Feb 07 |
jari |
28 |
class MEVRequestAcceptStrategy { |
2 |
26 Feb 07 |
jari |
29 |
private static final int MAX_PACKET_SIZE = 1024*1024; // 1M chunk |
2 |
26 Feb 07 |
jari |
30 |
|
2 |
26 Feb 07 |
jari |
31 |
/** |
2 |
26 Feb 07 |
jari |
* Constructs a <code>MEVRequestAcceptStrategy</code> with specified configuration. |
2 |
26 Feb 07 |
jari |
33 |
*/ |
2 |
26 Feb 07 |
jari |
34 |
public MEVRequestAcceptStrategy( ConfMap cfg ) { |
2 |
26 Feb 07 |
jari |
35 |
m_pvmProxyName = cfg.getString("gateway.pvm.proxy-name"); |
2 |
26 Feb 07 |
jari |
36 |
m_pvmSlaveName = cfg.getString("gateway.pvm.slave-name"); |
2 |
26 Feb 07 |
jari |
37 |
m_pvmURL = cfg.getProperty("gateway.pvm.slave.http","").trim(); |
2 |
26 Feb 07 |
jari |
38 |
m_pvmRoot = cfg.getProperty("gateway.pvm.root-path","").trim(); |
2 |
26 Feb 07 |
jari |
39 |
} |
2 |
26 Feb 07 |
jari |
40 |
|
2 |
26 Feb 07 |
jari |
41 |
/** |
2 |
26 Feb 07 |
jari |
* Checkes if specified input stream contains an error. |
2 |
26 Feb 07 |
jari |
43 |
*/ |
2 |
26 Feb 07 |
jari |
44 |
private static void checkError( InputStream in ) throws IOException { |
2 |
26 Feb 07 |
jari |
45 |
BufferedReader reader = new BufferedReader( new InputStreamReader( in ) ); |
2 |
26 Feb 07 |
jari |
46 |
String result = reader.readLine(); |
2 |
26 Feb 07 |
jari |
47 |
if (result != null) { |
2 |
26 Feb 07 |
jari |
48 |
if (!result.startsWith("OK")) { |
2 |
26 Feb 07 |
jari |
49 |
result = reader.readLine(); |
2 |
26 Feb 07 |
jari |
50 |
throw new IOException("Cannot send to PVM: " + result ); |
2 |
26 Feb 07 |
jari |
51 |
} |
2 |
26 Feb 07 |
jari |
52 |
} else |
2 |
26 Feb 07 |
jari |
53 |
throw new IOException("Bad pvmproxy response."); |
2 |
26 Feb 07 |
jari |
54 |
} |
2 |
26 Feb 07 |
jari |
55 |
|
2 |
26 Feb 07 |
jari |
56 |
/** |
2 |
26 Feb 07 |
jari |
* Constructs PVM url to execute post response. |
2 |
26 Feb 07 |
jari |
58 |
*/ |
2 |
26 Feb 07 |
jari |
59 |
private void constructURL(HttpServletRequest req) throws IOException { |
2 |
26 Feb 07 |
jari |
60 |
if (m_pvmURL == null || ( m_pvmURL.trim().equals("") )) { |
2 |
26 Feb 07 |
jari |
61 |
StringBuffer sb = HttpUtils.getRequestURL( req ); |
2 |
26 Feb 07 |
jari |
62 |
sb.append("?post-response"); |
2 |
26 Feb 07 |
jari |
63 |
m_pvmURL = sb.toString(); |
2 |
26 Feb 07 |
jari |
64 |
} |
2 |
26 Feb 07 |
jari |
65 |
} |
2 |
26 Feb 07 |
jari |
66 |
|
2 |
26 Feb 07 |
jari |
67 |
/** |
2 |
26 Feb 07 |
jari |
* Starts pvmproxy and waits for it to finish. |
2 |
26 Feb 07 |
jari |
* The exact protocol is defined at TIGR-MEV/remote/src/pvmproxy/pvmproxy.cpp file |
2 |
26 Feb 07 |
jari |
70 |
*/ |
2 |
26 Feb 07 |
jari |
71 |
public void acceptMEVRequest( HttpServletRequest req, HttpServletResponse resp ) |
2 |
26 Feb 07 |
jari |
72 |
throws IOException, InterruptedException { |
2 |
26 Feb 07 |
jari |
73 |
if (req.getContentLength() < 1) |
2 |
26 Feb 07 |
jari |
74 |
throw new IOException("Bad content length: " + req.getContentLength() ); |
2 |
26 Feb 07 |
jari |
75 |
constructURL( req ); |
2 |
26 Feb 07 |
jari |
76 |
Runtime runtime = Runtime.getRuntime(); |
2 |
26 Feb 07 |
jari |
77 |
String cmdarray[] = {m_pvmProxyName, m_pvmSlaveName, m_pvmURL, getSessionIdString( req )}; |
2 |
26 Feb 07 |
jari |
78 |
Process process; |
2 |
26 Feb 07 |
jari |
79 |
if (!m_pvmRoot.equals("")) { |
2 |
26 Feb 07 |
jari |
80 |
String envp[] = { "PVM_ROOT=" + m_pvmRoot}; |
2 |
26 Feb 07 |
jari |
81 |
process = runtime.exec( cmdarray, envp ); |
2 |
26 Feb 07 |
jari |
82 |
} else |
2 |
26 Feb 07 |
jari |
83 |
process = runtime.exec( cmdarray ); |
2 |
26 Feb 07 |
jari |
84 |
|
2 |
26 Feb 07 |
jari |
// ad hoc: |
2 |
26 Feb 07 |
jari |
86 |
OutputStream out = new BufferedOutputStream( process.getOutputStream(), 1024*1024 ); |
2 |
26 Feb 07 |
jari |
87 |
InputStream in = new BufferedInputStream( process.getInputStream() ); |
2 |
26 Feb 07 |
jari |
88 |
InputStream wwwIn = new BufferedInputStream( req.getInputStream() ); |
2 |
26 Feb 07 |
jari |
89 |
|
2 |
26 Feb 07 |
jari |
90 |
checkError( in ); |
2 |
26 Feb 07 |
jari |
91 |
|
2 |
26 Feb 07 |
jari |
92 |
int i = 0; |
2 |
26 Feb 07 |
jari |
93 |
byte[] b = new byte[1024*100]; |
2 |
26 Feb 07 |
jari |
94 |
int cnt; |
2 |
26 Feb 07 |
jari |
95 |
while ((cnt = wwwIn.read(b)) >= 0) { |
2 |
26 Feb 07 |
jari |
96 |
out.write(b, 0, cnt); |
2 |
26 Feb 07 |
jari |
97 |
i += cnt; |
2 |
26 Feb 07 |
jari |
98 |
} |
2 |
26 Feb 07 |
jari |
99 |
if (i < 10) throw new IOException("read only : " + i); |
2 |
26 Feb 07 |
jari |
100 |
out.flush(); |
2 |
26 Feb 07 |
jari |
101 |
out.close(); |
2 |
26 Feb 07 |
jari |
102 |
checkError( in ); |
2 |
26 Feb 07 |
jari |
103 |
|
2 |
26 Feb 07 |
jari |
104 |
process.waitFor(); |
2 |
26 Feb 07 |
jari |
105 |
int code = process.exitValue(); |
2 |
26 Feb 07 |
jari |
106 |
|
2 |
26 Feb 07 |
jari |
107 |
if (code != 0) |
2 |
26 Feb 07 |
jari |
108 |
throw new IOException("pvmproxy exit code != 0"); |
2 |
26 Feb 07 |
jari |
109 |
} |
2 |
26 Feb 07 |
jari |
110 |
|
2 |
26 Feb 07 |
jari |
111 |
/** |
2 |
26 Feb 07 |
jari |
* Create a string, which identifies this session among others. |
2 |
26 Feb 07 |
jari |
* Since we are not sure, what cookie is used exactly to identify this session, |
2 |
26 Feb 07 |
jari |
* we will use all of them. |
2 |
26 Feb 07 |
jari |
115 |
* |
2 |
26 Feb 07 |
jari |
* New: |
2 |
26 Feb 07 |
jari |
* We use the folllowing: getSessionId as string. |
2 |
26 Feb 07 |
jari |
* This should be used as JSESSIONID cookie value |
2 |
26 Feb 07 |
jari |
119 |
*/ |
2 |
26 Feb 07 |
jari |
120 |
private final String getSessionIdString(HttpServletRequest req) throws IOException { |
2 |
26 Feb 07 |
jari |
121 |
return "JSESSIONID=" + req.getSession().getId(); |
2 |
26 Feb 07 |
jari |
122 |
} |
2 |
26 Feb 07 |
jari |
123 |
|
2 |
26 Feb 07 |
jari |
124 |
private String m_pvmProxyName; |
2 |
26 Feb 07 |
jari |
125 |
private String m_pvmSlaveName; |
2 |
26 Feb 07 |
jari |
126 |
private String m_pvmURL; |
2 |
26 Feb 07 |
jari |
127 |
private String m_pvmRoot; |
2 |
26 Feb 07 |
jari |
128 |
} |