Lots of code cleanup and little fixes.
redirect() now sends a 303 status code instead of 302 for HTTP/1.1.
This commit is contained in:
parent
0db8fd2bbd
commit
d0aa1a72b5
1 changed files with 94 additions and 41 deletions
|
@ -71,8 +71,9 @@ public abstract class AbstractServletClient extends HttpServlet {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected void execute (HttpServletRequest request, HttpServletResponse response, byte method) {
|
protected void execute (HttpServletRequest request,
|
||||||
String protocol = request.getProtocol ();
|
HttpServletResponse response,
|
||||||
|
byte method) {
|
||||||
Cookie[] cookies = request.getCookies();
|
Cookie[] cookies = request.getCookies();
|
||||||
|
|
||||||
RequestTrans reqtrans = new RequestTrans (method);
|
RequestTrans reqtrans = new RequestTrans (method);
|
||||||
|
@ -111,13 +112,11 @@ public abstract class AbstractServletClient extends HttpServlet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception upx) {
|
} catch (Exception upx) {
|
||||||
response.setStatus (HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE);
|
sendError (
|
||||||
writeError (response, "Sorry, upload size exceeds limit of "+uploadLimit+"kB.");
|
response,
|
||||||
|
response.SC_REQUEST_ENTITY_TOO_LARGE,
|
||||||
|
"Sorry, upload size exceeds limit of "+uploadLimit+"kB.");
|
||||||
return;
|
return;
|
||||||
/* String uploadErr = upx.getMessage ();
|
|
||||||
if (uploadErr == null || uploadErr.length () == 0)
|
|
||||||
uploadErr = upx.toString ();
|
|
||||||
reqtrans.set ("uploadError", uploadErr); */
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -178,20 +177,33 @@ public abstract class AbstractServletClient extends HttpServlet {
|
||||||
String pathInfo = request.getPathInfo ();
|
String pathInfo = request.getPathInfo ();
|
||||||
ResponseTrans restrans = execute (reqtrans, pathInfo);
|
ResponseTrans restrans = execute (reqtrans, pathInfo);
|
||||||
|
|
||||||
writeResponse (response, restrans, cookies, protocol);
|
writeResponse (request, response, restrans, cookies);
|
||||||
|
|
||||||
} catch (Exception x) {
|
} catch (Exception x) {
|
||||||
// invalidateApp (appID);
|
try {
|
||||||
response.setStatus (HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
if (debug)
|
||||||
if (debug)
|
sendError (
|
||||||
writeError (response, "<b>Error:</b><br>" +x);
|
response,
|
||||||
else
|
response.SC_INTERNAL_SERVER_ERROR,
|
||||||
writeError (response, "This server is temporarily unavailable. Please check back later.");
|
"Error in request handler:" +x);
|
||||||
|
else
|
||||||
|
sendError (
|
||||||
|
response,
|
||||||
|
response.SC_INTERNAL_SERVER_ERROR,
|
||||||
|
"The server encountered an error while processing your request. "+
|
||||||
|
"Please check back later.");
|
||||||
|
log ("Exception in execute: "+x);
|
||||||
|
} catch (IOException io_e) {
|
||||||
|
log ("Exception in sendError: "+io_e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void writeResponse (HttpServletResponse res, ResponseTrans trans, Cookie[] cookies, String protocol) {
|
void writeResponse (HttpServletRequest req,
|
||||||
|
HttpServletResponse res,
|
||||||
|
ResponseTrans trans,
|
||||||
|
Cookie[] cookies) {
|
||||||
|
|
||||||
for (int i = 0; i < trans.countCookies(); i++) try {
|
for (int i = 0; i < trans.countCookies(); i++) try {
|
||||||
Cookie c = new Cookie(trans.getKeyAt(i), trans.getValueAt(i));
|
Cookie c = new Cookie(trans.getKeyAt(i), trans.getValueAt(i));
|
||||||
|
@ -205,19 +217,20 @@ public abstract class AbstractServletClient extends HttpServlet {
|
||||||
} catch (Exception ign) {}
|
} catch (Exception ign) {}
|
||||||
|
|
||||||
if (trans.getRedirect () != null) {
|
if (trans.getRedirect () != null) {
|
||||||
try {
|
sendRedirect(req, res, trans.getRedirect ());
|
||||||
res.sendRedirect(trans.getRedirect ());
|
|
||||||
} catch(Exception io_e) {}
|
|
||||||
} else if (trans.getNotModified ()) {
|
} else if (trans.getNotModified ()) {
|
||||||
res.setStatus (HttpServletResponse.SC_NOT_MODIFIED);
|
res.setStatus (HttpServletResponse.SC_NOT_MODIFIED);
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
if (!trans.cache || ! caching) {
|
if (!trans.cache || ! caching) {
|
||||||
// Disable caching of response.
|
// Disable caching of response.
|
||||||
if (protocol == null || !protocol.endsWith ("1.1"))
|
if (isOneDotOne (req.getProtocol ())) {
|
||||||
res.setHeader ("Pragma", "no-cache"); // for HTTP 1.0
|
// for HTTP 1.0
|
||||||
else
|
res.setHeader ("Pragma", "no-cache");
|
||||||
res.setHeader ("Cache-Control", "no-cache"); // for HTTP 1.1
|
} else {
|
||||||
|
// for HTTP 1.1
|
||||||
|
res.setHeader ("Cache-Control", "no-cache");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if ( trans.realm!=null )
|
if ( trans.realm!=null )
|
||||||
res.setHeader( "WWW-Authenticate", "Basic realm=\"" + trans.realm + "\"" );
|
res.setHeader( "WWW-Authenticate", "Basic realm=\"" + trans.realm + "\"" );
|
||||||
|
@ -239,7 +252,7 @@ public abstract class AbstractServletClient extends HttpServlet {
|
||||||
try {
|
try {
|
||||||
OutputStream out = res.getOutputStream ();
|
OutputStream out = res.getOutputStream ();
|
||||||
out.write (trans.getContent ());
|
out.write (trans.getContent ());
|
||||||
out.close ();
|
out.flush ();
|
||||||
} catch(Exception io_e) {
|
} catch(Exception io_e) {
|
||||||
log ("Exception in writeResponse: "+io_e);
|
log ("Exception in writeResponse: "+io_e);
|
||||||
}
|
}
|
||||||
|
@ -247,30 +260,62 @@ public abstract class AbstractServletClient extends HttpServlet {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void writeError (HttpServletResponse res, String message) {
|
void sendError (HttpServletResponse response, int code, String message)
|
||||||
try {
|
throws IOException {
|
||||||
res.setContentType ("text/html");
|
response.reset ();
|
||||||
Writer out = res.getWriter ();
|
response.setStatus (code);
|
||||||
out.write (message);
|
response.setContentType ("text/html");
|
||||||
out.flush ();
|
Writer writer = response.getWriter ();
|
||||||
} catch (Exception io_e) {
|
writer.write (message);
|
||||||
// ignore
|
writer.flush ();
|
||||||
|
}
|
||||||
|
|
||||||
|
void sendRedirect (HttpServletRequest req, HttpServletResponse res, String url) {
|
||||||
|
String location = url;
|
||||||
|
if (url.indexOf ("://") == -1)
|
||||||
|
{
|
||||||
|
// need to transform a relative URL into an absolute one
|
||||||
|
String scheme = req.getScheme ();
|
||||||
|
StringBuffer loc = new StringBuffer(scheme);
|
||||||
|
loc.append ("://");
|
||||||
|
loc.append (req.getServerName ());
|
||||||
|
int p = req.getServerPort ();
|
||||||
|
// check if we need to include server port
|
||||||
|
if (p > 0 &&
|
||||||
|
(("http".equals(scheme) && p != 80) ||
|
||||||
|
("https".equals(scheme) && p != 443)))
|
||||||
|
{
|
||||||
|
loc.append (":");
|
||||||
|
loc.append (p);
|
||||||
|
}
|
||||||
|
if (!url.startsWith ("/"))
|
||||||
|
{
|
||||||
|
String requri = req.getRequestURI ();
|
||||||
|
int lastSlash = requri.lastIndexOf ("/");
|
||||||
|
if (lastSlash == requri.length()-1)
|
||||||
|
loc.append (requri);
|
||||||
|
else if (lastSlash > -1)
|
||||||
|
loc.append (requri.substring (0, lastSlash+1));
|
||||||
|
else
|
||||||
|
loc.append ("/");
|
||||||
|
}
|
||||||
|
loc.append (url);
|
||||||
|
location = loc.toString ();
|
||||||
}
|
}
|
||||||
|
res.reset ();
|
||||||
|
// send status code 303 for HTTP 1.1, 302 otherwise
|
||||||
|
if (isOneDotOne (req.getProtocol ()))
|
||||||
|
res.setStatus (res.SC_SEE_OTHER);
|
||||||
|
else
|
||||||
|
res.setStatus (res.SC_MOVED_TEMPORARILY);
|
||||||
|
res.setContentType ("text/html");
|
||||||
|
res.setHeader ("Location", location);
|
||||||
}
|
}
|
||||||
|
|
||||||
FileUpload getUpload (HttpServletRequest request) throws Exception {
|
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
|
|
||||||
/* 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.");
|
throw new RuntimeException ("Upload exceeds limit of "+uploadLimit+" kb.");
|
||||||
}
|
}
|
||||||
String contentType = request.getContentType ();
|
String contentType = request.getContentType ();
|
||||||
|
@ -424,6 +469,14 @@ public abstract class AbstractServletClient extends HttpServlet {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean isOneDotOne (String protocol) {
|
||||||
|
if (protocol == null)
|
||||||
|
return false;
|
||||||
|
if (protocol.endsWith ("1.1"))
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public String getServletInfo(){
|
public String getServletInfo(){
|
||||||
return new String("Helma Servlet Client");
|
return new String("Helma Servlet Client");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue