Rewrote servlet classes to be independent from IRemoteApp interface, i.e. the
AbstractServletClient now does not mind whether a remote application or a local reference to an application are used. If the upload limit is exceeded, a HTTP error is returned immediately instead of going into the scripting environment.
This commit is contained in:
parent
d09006f985
commit
dbd45f573c
5 changed files with 78 additions and 100 deletions
|
@ -26,10 +26,11 @@ public abstract class AbstractServletClient extends HttpServlet {
|
|||
String host = null;
|
||||
// port of Helma RMI server
|
||||
int port = 0;
|
||||
// limit to HTTP uploads in kB
|
||||
int uploadLimit = 1024;
|
||||
// RMI url of Helma app
|
||||
String hopUrl;
|
||||
|
||||
// limit to HTTP uploads in kB
|
||||
int uploadLimit = 1024;
|
||||
// cookie domain to use
|
||||
String cookieDomain;
|
||||
// default encoding for requests
|
||||
|
@ -44,35 +45,19 @@ public abstract class AbstractServletClient extends HttpServlet {
|
|||
|
||||
public void init (ServletConfig init) throws ServletException {
|
||||
super.init (init);
|
||||
|
||||
host = init.getInitParameter ("host");
|
||||
if (host == null) host = "localhost";
|
||||
|
||||
String portstr = init.getInitParameter ("port");
|
||||
port = portstr == null ? 5055 : Integer.parseInt (portstr);
|
||||
|
||||
// get max size for file uploads
|
||||
String upstr = init.getInitParameter ("uploadLimit");
|
||||
uploadLimit = upstr == null ? 1024 : Integer.parseInt (upstr);
|
||||
|
||||
// get cookie domain
|
||||
cookieDomain = init.getInitParameter ("cookieDomain");
|
||||
|
||||
hopUrl = "//" + host + ":" + port + "/";
|
||||
|
||||
// get default encoding
|
||||
defaultEncoding = init.getInitParameter ("charset");
|
||||
|
||||
debug = ("true".equalsIgnoreCase (init.getInitParameter ("debug")));
|
||||
|
||||
caching = ! ("false".equalsIgnoreCase (init.getInitParameter ("caching")));
|
||||
}
|
||||
|
||||
|
||||
abstract IRemoteApp getApp (String appID) throws Exception;
|
||||
|
||||
abstract void invalidateApp (String appID);
|
||||
|
||||
abstract String getAppID (String reqpath);
|
||||
|
||||
abstract String getRequestPath (String reqpath);
|
||||
abstract ResponseTrans execute (RequestTrans req, String reqPath) throws Exception;
|
||||
|
||||
|
||||
public void doGet (HttpServletRequest request, HttpServletResponse response)
|
||||
|
@ -90,11 +75,11 @@ public abstract class AbstractServletClient extends HttpServlet {
|
|||
String protocol = request.getProtocol ();
|
||||
Cookie[] cookies = request.getCookies();
|
||||
|
||||
// get app and path from original request path
|
||||
String pathInfo = request.getPathInfo ();
|
||||
String appID = getAppID (pathInfo);
|
||||
RequestTrans reqtrans = new RequestTrans (method);
|
||||
reqtrans.path = getRequestPath (pathInfo);
|
||||
// get app and path from original request path
|
||||
// String pathInfo = request.getPathInfo ();
|
||||
// String appID = getAppID (pathInfo);
|
||||
// reqtrans.path = getRequestPath (pathInfo);
|
||||
|
||||
try {
|
||||
|
||||
|
@ -126,10 +111,13 @@ public abstract class AbstractServletClient extends HttpServlet {
|
|||
}
|
||||
}
|
||||
} catch (Exception upx) {
|
||||
String uploadErr = upx.getMessage ();
|
||||
response.setStatus (HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE);
|
||||
writeError (response, "Sorry, upload size exceeds limit of "+uploadLimit+"kB.");
|
||||
return;
|
||||
/* String uploadErr = upx.getMessage ();
|
||||
if (uploadErr == null || uploadErr.length () == 0)
|
||||
uploadErr = upx.toString ();
|
||||
reqtrans.set ("uploadError", uploadErr);
|
||||
reqtrans.set ("uploadError", uploadErr); */
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -187,30 +175,18 @@ public abstract class AbstractServletClient extends HttpServlet {
|
|||
if ( authorization != null )
|
||||
reqtrans.set ("authorization", authorization );
|
||||
|
||||
// get RMI ref to application and execute request
|
||||
IRemoteApp app = getApp (appID);
|
||||
ResponseTrans restrans = null;
|
||||
try {
|
||||
restrans = app.execute (reqtrans);
|
||||
} catch (RemoteException cnx) {
|
||||
invalidateApp (appID);
|
||||
app = getApp (appID);
|
||||
app.ping ();
|
||||
restrans = app.execute (reqtrans);
|
||||
}
|
||||
String pathInfo = request.getPathInfo ();
|
||||
ResponseTrans restrans = execute (reqtrans, pathInfo);
|
||||
|
||||
writeResponse (response, restrans, cookies, protocol);
|
||||
|
||||
} catch (Exception x) {
|
||||
invalidateApp (appID);
|
||||
try {
|
||||
response.setContentType ("text/html");
|
||||
Writer out = response.getWriter ();
|
||||
// invalidateApp (appID);
|
||||
response.setStatus (HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
||||
if (debug)
|
||||
out.write ("<b>Error:</b><br>" +x);
|
||||
writeError (response, "<b>Error:</b><br>" +x);
|
||||
else
|
||||
out.write ("This server is temporarily unavailable. Please check back later.");
|
||||
out.flush ();
|
||||
} catch (Exception io_e) {}
|
||||
writeError (response, "This server is temporarily unavailable. Please check back later.");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -271,19 +247,30 @@ public abstract class AbstractServletClient extends HttpServlet {
|
|||
}
|
||||
|
||||
|
||||
public FileUpload getUpload (HttpServletRequest request) throws Exception {
|
||||
void writeError (HttpServletResponse res, String message) {
|
||||
try {
|
||||
res.setContentType ("text/html");
|
||||
Writer out = res.getWriter ();
|
||||
out.write (message);
|
||||
out.flush ();
|
||||
} catch (Exception io_e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
FileUpload getUpload (HttpServletRequest request) throws Exception {
|
||||
int contentLength = request.getContentLength ();
|
||||
BufferedInputStream in = new BufferedInputStream (request.getInputStream ());
|
||||
if (contentLength > uploadLimit*1024) {
|
||||
// consume all input to make Apache happy
|
||||
byte b[] = new byte[4096];
|
||||
/* byte b[] = new byte[4096];
|
||||
int read = 0;
|
||||
int sum = 0;
|
||||
while (read > -1 && sum < contentLength) {
|
||||
read = in.read (b, 0, 4096);
|
||||
if (read > 0)
|
||||
sum += read;
|
||||
}
|
||||
} */
|
||||
throw new RuntimeException ("Upload exceeds limit of "+uploadLimit+" kb.");
|
||||
}
|
||||
String contentType = request.getContentType ();
|
||||
|
@ -293,7 +280,7 @@ public abstract class AbstractServletClient extends HttpServlet {
|
|||
}
|
||||
|
||||
|
||||
public Object getUploadPart(FileUpload upload, String name) {
|
||||
Object getUploadPart(FileUpload upload, String name) {
|
||||
return upload.getParts().get(name);
|
||||
}
|
||||
|
||||
|
|
|
@ -39,21 +39,14 @@ public final class EmbeddedServletClient extends AbstractServletClient {
|
|||
mountpoint = "/"+appName;
|
||||
}
|
||||
|
||||
IRemoteApp getApp (String appID) {
|
||||
ResponseTrans execute (RequestTrans req, String reqPath) throws Exception {
|
||||
req.path = getRequestPath (reqPath);
|
||||
if (app == null)
|
||||
app = Server.getServer().getApplication (appName);
|
||||
return app;
|
||||
return app.execute (req);
|
||||
}
|
||||
|
||||
|
||||
void invalidateApp (String appID) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
String getAppID (String path) {
|
||||
return appName;
|
||||
}
|
||||
|
||||
String getRequestPath (String path) {
|
||||
if (path == null)
|
||||
return "";
|
||||
|
|
|
@ -10,7 +10,7 @@ import java.io.*;
|
|||
import java.rmi.Naming;
|
||||
import java.rmi.RemoteException;
|
||||
import java.util.HashMap;
|
||||
import helma.framework.IRemoteApp;
|
||||
import helma.framework.*;
|
||||
|
||||
/**
|
||||
* This is the HOP servlet adapter. This class communicates with any
|
||||
|
@ -25,7 +25,19 @@ public class MultiServletClient extends AbstractServletClient {
|
|||
public void init (ServletConfig init) throws ServletException {
|
||||
super.init (init);
|
||||
apps = new HashMap ();
|
||||
super.init (init);
|
||||
host = init.getInitParameter ("host");
|
||||
if (host == null)
|
||||
host = "localhost";
|
||||
String portstr = init.getInitParameter ("port");
|
||||
port = portstr == null ? 5055 : Integer.parseInt (portstr);
|
||||
hopUrl = "//" + host + ":" + port + "/";
|
||||
}
|
||||
|
||||
ResponseTrans execute (RequestTrans req, String reqPath) throws Exception {
|
||||
String appID = getAppID (reqPath);
|
||||
IRemoteApp app = getApp (appID);
|
||||
req.path = getRequestPath (reqPath);
|
||||
return app.execute (req);
|
||||
}
|
||||
|
||||
IRemoteApp getApp (String appID) throws Exception {
|
||||
|
@ -89,7 +101,7 @@ public class MultiServletClient extends AbstractServletClient {
|
|||
|
||||
// for testing
|
||||
public static void main (String args[]) {
|
||||
AbstractServletClient client = new MultiServletClient ();
|
||||
MultiServletClient client = new MultiServletClient ();
|
||||
// String path = "///appname/do/it/for/me///";
|
||||
String path = "appname";
|
||||
System.out.println (client.getAppID (path));
|
||||
|
@ -98,21 +110,3 @@ public class MultiServletClient extends AbstractServletClient {
|
|||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -24,9 +24,21 @@ public class ServletClient extends AbstractServletClient {
|
|||
public void init (ServletConfig init) throws ServletException {
|
||||
super.init (init);
|
||||
appName = init.getInitParameter ("application");
|
||||
host = init.getInitParameter ("host");
|
||||
if (host == null)
|
||||
host = "localhost";
|
||||
String portstr = init.getInitParameter ("port");
|
||||
port = portstr == null ? 5055 : Integer.parseInt (portstr);
|
||||
hopUrl = "//" + host + ":" + port + "/";
|
||||
}
|
||||
|
||||
IRemoteApp getApp (String appID) throws Exception {
|
||||
ResponseTrans execute (RequestTrans req, String reqPath) throws Exception {
|
||||
IRemoteApp app = getApp ();
|
||||
req.path = getRequestPath (reqPath);
|
||||
return app.execute (req);
|
||||
}
|
||||
|
||||
IRemoteApp getApp () throws Exception {
|
||||
if (app != null)
|
||||
return app;
|
||||
if (appName == null)
|
||||
|
@ -35,13 +47,6 @@ public class ServletClient extends AbstractServletClient {
|
|||
return app;
|
||||
}
|
||||
|
||||
void invalidateApp (String appID) {
|
||||
app = null;
|
||||
}
|
||||
|
||||
String getAppID (String path) {
|
||||
return appName;
|
||||
}
|
||||
|
||||
String getRequestPath (String path) {
|
||||
// get request path
|
||||
|
@ -67,9 +72,9 @@ public class ServletClient extends AbstractServletClient {
|
|||
|
||||
// for testing
|
||||
public static void main (String args[]) {
|
||||
AbstractServletClient client = new ServletClient ();
|
||||
ServletClient client = new ServletClient ();
|
||||
String path = "///appname/some/random/path///";
|
||||
System.out.println (client.getAppID (path));
|
||||
// System.out.println (client.getAppID (path));
|
||||
System.out.println (client.getRequestPath (path));
|
||||
}
|
||||
|
||||
|
|
|
@ -47,12 +47,14 @@ public final class StandaloneServletClient extends AbstractServletClient {
|
|||
throw new ServletException ("dbdir parameter not specified");
|
||||
}
|
||||
|
||||
IRemoteApp getApp (String appID) {
|
||||
ResponseTrans execute (RequestTrans req, String reqPath) throws Exception {
|
||||
req.path = getRequestPath (reqPath);
|
||||
if (app == null)
|
||||
createApp ();
|
||||
return app;
|
||||
return app.execute (req);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create the application. Since we are synchronized only here, we
|
||||
* do another check if the app already exists and immediately return if it does.
|
||||
|
@ -89,9 +91,6 @@ public final class StandaloneServletClient extends AbstractServletClient {
|
|||
app = null;
|
||||
}
|
||||
|
||||
void invalidateApp (String appID) {
|
||||
// app = null;
|
||||
}
|
||||
|
||||
String getAppID (String path) {
|
||||
return appName;
|
||||
|
@ -121,7 +120,7 @@ public final class StandaloneServletClient extends AbstractServletClient {
|
|||
|
||||
// for testing
|
||||
public static void main (String args[]) {
|
||||
AbstractServletClient client = new ServletClient ();
|
||||
StandaloneServletClient client = new StandaloneServletClient ();
|
||||
String path = "///appname/some/random/path///";
|
||||
System.out.println (client.getAppID (path));
|
||||
System.out.println (client.getRequestPath (path));
|
||||
|
|
Loading…
Add table
Reference in a new issue