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:
hns 2002-09-26 16:33:35 +00:00
parent d09006f985
commit dbd45f573c
5 changed files with 78 additions and 100 deletions

View file

@ -26,10 +26,11 @@ public abstract class AbstractServletClient extends HttpServlet {
String host = null; String host = null;
// port of Helma RMI server // port of Helma RMI server
int port = 0; int port = 0;
// limit to HTTP uploads in kB
int uploadLimit = 1024;
// RMI url of Helma app // RMI url of Helma app
String hopUrl; String hopUrl;
// limit to HTTP uploads in kB
int uploadLimit = 1024;
// cookie domain to use // cookie domain to use
String cookieDomain; String cookieDomain;
// default encoding for requests // default encoding for requests
@ -44,35 +45,19 @@ public abstract class AbstractServletClient extends HttpServlet {
public void init (ServletConfig init) throws ServletException { public void init (ServletConfig init) throws ServletException {
super.init (init); super.init (init);
// get max size for file uploads
host = init.getInitParameter ("host");
if (host == null) host = "localhost";
String portstr = init.getInitParameter ("port");
port = portstr == null ? 5055 : Integer.parseInt (portstr);
String upstr = init.getInitParameter ("uploadLimit"); String upstr = init.getInitParameter ("uploadLimit");
uploadLimit = upstr == null ? 1024 : Integer.parseInt (upstr); uploadLimit = upstr == null ? 1024 : Integer.parseInt (upstr);
// get cookie domain
cookieDomain = init.getInitParameter ("cookieDomain"); cookieDomain = init.getInitParameter ("cookieDomain");
// get default encoding
hopUrl = "//" + host + ":" + port + "/";
defaultEncoding = init.getInitParameter ("charset"); defaultEncoding = init.getInitParameter ("charset");
debug = ("true".equalsIgnoreCase (init.getInitParameter ("debug"))); debug = ("true".equalsIgnoreCase (init.getInitParameter ("debug")));
caching = ! ("false".equalsIgnoreCase (init.getInitParameter ("caching"))); caching = ! ("false".equalsIgnoreCase (init.getInitParameter ("caching")));
} }
abstract IRemoteApp getApp (String appID) throws Exception; abstract ResponseTrans execute (RequestTrans req, String reqPath) throws Exception;
abstract void invalidateApp (String appID);
abstract String getAppID (String reqpath);
abstract String getRequestPath (String reqpath);
public void doGet (HttpServletRequest request, HttpServletResponse response) public void doGet (HttpServletRequest request, HttpServletResponse response)
@ -90,11 +75,11 @@ public abstract class AbstractServletClient extends HttpServlet {
String protocol = request.getProtocol (); String protocol = request.getProtocol ();
Cookie[] cookies = request.getCookies(); 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); 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 { try {
@ -126,10 +111,13 @@ public abstract class AbstractServletClient extends HttpServlet {
} }
} }
} catch (Exception upx) { } 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) if (uploadErr == null || uploadErr.length () == 0)
uploadErr = upx.toString (); uploadErr = upx.toString ();
reqtrans.set ("uploadError", uploadErr); reqtrans.set ("uploadError", uploadErr); */
} }
} }
@ -187,30 +175,18 @@ public abstract class AbstractServletClient extends HttpServlet {
if ( authorization != null ) if ( authorization != null )
reqtrans.set ("authorization", authorization ); reqtrans.set ("authorization", authorization );
// get RMI ref to application and execute request String pathInfo = request.getPathInfo ();
IRemoteApp app = getApp (appID); ResponseTrans restrans = execute (reqtrans, pathInfo);
ResponseTrans restrans = null;
try {
restrans = app.execute (reqtrans);
} catch (RemoteException cnx) {
invalidateApp (appID);
app = getApp (appID);
app.ping ();
restrans = app.execute (reqtrans);
}
writeResponse (response, restrans, cookies, protocol); writeResponse (response, restrans, cookies, protocol);
} catch (Exception x) { } catch (Exception x) {
invalidateApp (appID); // invalidateApp (appID);
try { response.setStatus (HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.setContentType ("text/html");
Writer out = response.getWriter ();
if (debug) if (debug)
out.write ("<b>Error:</b><br>" +x); writeError (response, "<b>Error:</b><br>" +x);
else else
out.write ("This server is temporarily unavailable. Please check back later."); writeError (response, "This server is temporarily unavailable. Please check back later.");
out.flush ();
} catch (Exception io_e) {}
} }
} }
@ -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 (); int contentLength = request.getContentLength ();
BufferedInputStream in = new BufferedInputStream (request.getInputStream ()); BufferedInputStream in = new BufferedInputStream (request.getInputStream ());
if (contentLength > uploadLimit*1024) { if (contentLength > uploadLimit*1024) {
// consume all input to make Apache happy // consume all input to make Apache happy
byte b[] = new byte[4096]; /* byte b[] = new byte[4096];
int read = 0; int read = 0;
int sum = 0; int sum = 0;
while (read > -1 && sum < contentLength) { while (read > -1 && sum < contentLength) {
read = in.read (b, 0, 4096); read = in.read (b, 0, 4096);
if (read > 0) if (read > 0)
sum += read; sum += read;
} } */
throw new RuntimeException ("Upload exceeds limit of "+uploadLimit+" kb."); throw new RuntimeException ("Upload exceeds limit of "+uploadLimit+" kb.");
} }
String contentType = request.getContentType (); 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); return upload.getParts().get(name);
} }

View file

@ -39,21 +39,14 @@ public final class EmbeddedServletClient extends AbstractServletClient {
mountpoint = "/"+appName; mountpoint = "/"+appName;
} }
IRemoteApp getApp (String appID) { ResponseTrans execute (RequestTrans req, String reqPath) throws Exception {
req.path = getRequestPath (reqPath);
if (app == null) if (app == null)
app = Server.getServer().getApplication (appName); 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) { String getRequestPath (String path) {
if (path == null) if (path == null)
return ""; return "";

View file

@ -10,7 +10,7 @@ import java.io.*;
import java.rmi.Naming; import java.rmi.Naming;
import java.rmi.RemoteException; import java.rmi.RemoteException;
import java.util.HashMap; import java.util.HashMap;
import helma.framework.IRemoteApp; import helma.framework.*;
/** /**
* This is the HOP servlet adapter. This class communicates with any * 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 { public void init (ServletConfig init) throws ServletException {
super.init (init); super.init (init);
apps = new HashMap (); 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 { IRemoteApp getApp (String appID) throws Exception {
@ -89,7 +101,7 @@ public class MultiServletClient extends AbstractServletClient {
// for testing // for testing
public static void main (String args[]) { public static void main (String args[]) {
AbstractServletClient client = new MultiServletClient (); MultiServletClient client = new MultiServletClient ();
// String path = "///appname/do/it/for/me///"; // String path = "///appname/do/it/for/me///";
String path = "appname"; String path = "appname";
System.out.println (client.getAppID (path)); System.out.println (client.getAppID (path));
@ -98,21 +110,3 @@ public class MultiServletClient extends AbstractServletClient {
} }

View file

@ -24,9 +24,21 @@ public class ServletClient extends AbstractServletClient {
public void init (ServletConfig init) throws ServletException { public void init (ServletConfig init) throws ServletException {
super.init (init); super.init (init);
appName = init.getInitParameter ("application"); 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) if (app != null)
return app; return app;
if (appName == null) if (appName == null)
@ -35,13 +47,6 @@ public class ServletClient extends AbstractServletClient {
return app; return app;
} }
void invalidateApp (String appID) {
app = null;
}
String getAppID (String path) {
return appName;
}
String getRequestPath (String path) { String getRequestPath (String path) {
// get request path // get request path
@ -67,9 +72,9 @@ public class ServletClient extends AbstractServletClient {
// for testing // for testing
public static void main (String args[]) { public static void main (String args[]) {
AbstractServletClient client = new ServletClient (); ServletClient client = new ServletClient ();
String path = "///appname/some/random/path///"; String path = "///appname/some/random/path///";
System.out.println (client.getAppID (path)); // System.out.println (client.getAppID (path));
System.out.println (client.getRequestPath (path)); System.out.println (client.getRequestPath (path));
} }

View file

@ -47,12 +47,14 @@ public final class StandaloneServletClient extends AbstractServletClient {
throw new ServletException ("dbdir parameter not specified"); 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) if (app == null)
createApp (); createApp ();
return app; return app.execute (req);
} }
/** /**
* Create the application. Since we are synchronized only here, we * Create the application. Since we are synchronized only here, we
* do another check if the app already exists and immediately return if it does. * 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; app = null;
} }
void invalidateApp (String appID) {
// app = null;
}
String getAppID (String path) { String getAppID (String path) {
return appName; return appName;
@ -121,7 +120,7 @@ public final class StandaloneServletClient extends AbstractServletClient {
// for testing // for testing
public static void main (String args[]) { public static void main (String args[]) {
AbstractServletClient client = new ServletClient (); StandaloneServletClient client = new StandaloneServletClient ();
String path = "///appname/some/random/path///"; String path = "///appname/some/random/path///";
System.out.println (client.getAppID (path)); System.out.println (client.getAppID (path));
System.out.println (client.getRequestPath (path)); System.out.println (client.getRequestPath (path));