Fixed dispatching rule.
This commit is contained in:
parent
896b9c8578
commit
94d55f65d2
1 changed files with 420 additions and 435 deletions
|
@ -1,12 +1,11 @@
|
||||||
/**
|
/**
|
||||||
* @author Christian Momon, June 2008.
|
* @author Christian Momon, June 2008. This file is free software under the
|
||||||
* This file is free software under the terms of the GNU Library General Public License
|
* terms of the GNU Library General Public License as published by the
|
||||||
* as published by the Free Software Foundation version 2 or any later version.
|
* Free Software Foundation version 2 or any later version.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package fr.devinsy.kiss4web;
|
package fr.devinsy.kiss4web;
|
||||||
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -22,28 +21,151 @@ import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import fr.devinsy.util.StringList;
|
import fr.devinsy.util.StringList;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class SimpleServletDispatcher extends HttpServlet
|
public class SimpleServletDispatcher extends HttpServlet
|
||||||
{
|
{
|
||||||
private static final long serialVersionUID = -3471226305721330069L;
|
private static final long serialVersionUID = -3471226305721330069L;
|
||||||
static protected org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger (ServletDispatcher.class);
|
static protected org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(ServletDispatcher.class);
|
||||||
|
|
||||||
static final protected Pattern SHORT_REWRITED_URL_CLASS = Pattern.compile("^([^-]+)-.+\\.xhtml$");
|
static final protected Pattern SHORT_REWRITED_URL_CLASS = Pattern.compile("^([^-]+)-.+\\.xhtml$");
|
||||||
static final protected Pattern SHORT_REWRITED_URL_PARAMETERS = Pattern.compile("^[^-]+-(.+)\\.xhtml$");
|
static final protected Pattern SHORT_REWRITED_URL_PARAMETERS = Pattern.compile("^[^-]+-(.+)\\.xhtml$");
|
||||||
static final protected Pattern LONG_REWRITED_URL_CLASS = Pattern.compile("^([^-]+)-/.*$");
|
static final protected Pattern LONG_REWRITED_URL_CLASS = Pattern.compile("^([^-]+)-/.*$");
|
||||||
//static final protected Pattern LONG_REWRITED_URL_PARAMETERS = Pattern.compile("^.+-/(.)+*$");
|
// static final protected Pattern LONG_REWRITED_URL_PARAMETERS =
|
||||||
|
// Pattern.compile("^.+-/(.)+*$");
|
||||||
static final protected Pattern REWRITE_PARAMETER = Pattern.compile("[^%\\w\\d]");
|
static final protected Pattern REWRITE_PARAMETER = Pattern.compile("[^%\\w\\d]");
|
||||||
|
|
||||||
protected String webclassesRootPath;
|
protected String webclassesRootPath;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* "Code can be shortest, speedest and memory smallest, but not the three in same time, only two"
|
||||||
|
* , unknow citation.
|
||||||
|
*
|
||||||
|
* Note: characters array avalaible here
|
||||||
|
* http://fr.wikipedia.org/wiki/Table_des_caract
|
||||||
|
* %C3%A8res_Unicode_%280000-0FFF%29
|
||||||
|
*/
|
||||||
|
static protected char NONE = (char) 0;
|
||||||
|
|
||||||
|
static protected int[] rewritingParameterMapping = {
|
||||||
|
/* 00 */NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
|
||||||
|
/* 10 */NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
|
||||||
|
/* 20 */'-', NONE, NONE, '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-',
|
||||||
|
/* 30 */'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '-', '-', '-', '-', '-',
|
||||||
|
/* 40 */'\u0040', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
|
||||||
|
/* 50 */'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '-', '-', '-', '-', '-',
|
||||||
|
/* 60 */'-', '\u0061', '\u0062', '\u0063', '\u0064', '\u0065', '\u0066', '\u0067', '\u0068', '\u0069', '\u006A', '\u006B', '\u006C', '\u006D', '\u006E',
|
||||||
|
'\u006F',
|
||||||
|
/* 70 */'\u0070', '\u0071', '\u0072', '\u0073', '\u0074', '\u0075', '\u0076', '\u0077', '\u0078', '\u0079', '\u007A', '\u007B', '\u007C', '\u007D',
|
||||||
|
'-', '-',
|
||||||
|
/* 80 */NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
|
||||||
|
/* 90 */NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
|
||||||
|
/* A0 */'\u00A0', '\u00A1', '\u00A2', '\u00A3', '\u00A4', '\u00A5', '\u00A6', '\u00A7', '\u00A8', '\u00A9', '\u00AA', '\u00AB', '\u00AC', '\u00AD',
|
||||||
|
'\u00AE', '\u00AF',
|
||||||
|
/* B0 */'-', '\u00B1', '\u00B2', '\u00B3', '\u00B4', '\u00B5', '\u00B6', '\u00B7', '\u00B8', '\u00B9', '\u00BA', '\u00BB', '\u00BC', '\u00BD',
|
||||||
|
'\u00BE', '\u00BF',
|
||||||
|
/* C0 */'a', 'a', 'a', 'a', 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i',
|
||||||
|
/* D0 */'\u00D0', '\u00D1', 'o', 'o', 'o', 'o', 'o', 'o', '\u00D8', 'u', 'u', 'u', 'u', 'y', '\u00DE', '\u00DF',
|
||||||
|
/* E0 */'a', 'a', 'a', 'a', 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i',
|
||||||
|
/* F0 */'o', 'n', 'o', 'o', 'o', 'o', 'o', '\u00F7', '-', 'u', 'u', 'u', 'u', 'y', '-', 'y' };
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public void init () throws ServletException
|
@Override
|
||||||
|
public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException
|
||||||
|
{
|
||||||
|
doIt(request, response);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public void doIt(final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException
|
||||||
|
{
|
||||||
|
logger.info("==================================================");
|
||||||
|
logger.info("getContextPath=[" + request.getContextPath() + "]");
|
||||||
|
logger.info("getPathInfo=[" + request.getPathInfo() + "]");
|
||||||
|
logger.info("getPathTranslated=[" + request.getPathTranslated() + "]");
|
||||||
|
logger.info("getQueryString=[" + request.getQueryString() + "]");
|
||||||
|
logger.info("getRequestURI=[" + request.getRequestURI() + "]");
|
||||||
|
logger.info("getRequestURL=[" + request.getRequestURL() + "]");
|
||||||
|
logger.info("getServletPath=[" + request.getServletPath() + "]");
|
||||||
|
|
||||||
|
//
|
||||||
|
/*
|
||||||
|
* In past, possibility to use the servlet path was enable. It is too
|
||||||
|
* complexe, not kiss mind. String path; if (request.getPathInfo() ==
|
||||||
|
* null) { // web.xml url-pattern= *.xhtml path =
|
||||||
|
* request.getServletPath(); } else { // web.xml url-pattern = /* path =
|
||||||
|
* request.getPathInfo(); }
|
||||||
|
*/
|
||||||
|
// String path = request.getRequestURI();
|
||||||
|
String path = request.getPathInfo();
|
||||||
|
|
||||||
|
if ((!path.endsWith("/")) && (!path.endsWith(".xhtml")) && (!path.contains("-/")))
|
||||||
|
{
|
||||||
|
// path = getServletContext().getRealPath("/") +
|
||||||
|
// request.getRequestURI();
|
||||||
|
path = getServletContext().getRealPath("/") + request.getPathInfo();
|
||||||
|
|
||||||
|
returnInlineFile(response, new File(path), getServletContext().getMimeType(path));
|
||||||
|
logger.info("File returned directly [" + path + "] with mimetype [" + getServletContext().getMimeType(path) + "].");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
String className = pathInfoToClassName(path, this.webclassesRootPath);
|
||||||
|
logger.info("className=[" + className + "]");
|
||||||
|
|
||||||
|
HttpServlet servlet = instanciateServlet(className);
|
||||||
|
|
||||||
|
// servlet.getServletContext().setAttribute(arg0, arg1);
|
||||||
|
|
||||||
|
if (servlet == null)
|
||||||
|
{
|
||||||
|
response.setContentType("text/html");
|
||||||
|
PrintWriter out = response.getWriter();
|
||||||
|
|
||||||
|
out.println("<html><head></head><body>");
|
||||||
|
out.println("Unknow page.");
|
||||||
|
out.println("</body></html>");
|
||||||
|
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
else if (isAuthorized(request, response))
|
||||||
|
{
|
||||||
|
servlet.init(this.getServletConfig());
|
||||||
|
servlet.service(request, response);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* TODO // response.setContentType ("text/html"); PrintWriter
|
||||||
|
* out = response.getWriter();
|
||||||
|
*
|
||||||
|
* out.println ("<html><head></head><body>"); out.println
|
||||||
|
* ("Not authorized page."); out.println ("</body></html>");
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException
|
||||||
|
{
|
||||||
|
doIt(request, response);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void init() throws ServletException
|
||||||
{
|
{
|
||||||
super.init();
|
super.init();
|
||||||
this.webclassesRootPath = getInitParameter("webClassesRootPath");
|
this.webclassesRootPath = getInitParameter("webClassesRootPath");
|
||||||
|
@ -55,7 +177,7 @@ public class SimpleServletDispatcher extends HttpServlet
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
System.out.println("Log configuration found (" + logFilepathname + "), will use it.");
|
System.out.println("Log configuration found (" + logFilepathname + "), will use it.");
|
||||||
org.apache.log4j.PropertyConfigurator.configure (getServletContext().getRealPath("/") + logFilepathname);
|
org.apache.log4j.PropertyConfigurator.configure(getServletContext().getRealPath("/") + logFilepathname);
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
|
@ -68,18 +190,91 @@ public class SimpleServletDispatcher extends HttpServlet
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public boolean isAuthorized(final HttpServletRequest request, final HttpServletResponse response)
|
||||||
|
{
|
||||||
|
boolean result;
|
||||||
|
|
||||||
|
result = true;
|
||||||
|
|
||||||
|
//
|
||||||
|
return (result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
static public HttpServlet instanciateServlet(final String className)
|
||||||
|
{
|
||||||
|
HttpServlet result;
|
||||||
|
|
||||||
|
Class<HttpServlet> servletClass = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
servletClass = (Class<HttpServlet>) Class.forName(className);
|
||||||
|
}
|
||||||
|
catch (java.lang.ClassNotFoundException exception)
|
||||||
|
{
|
||||||
|
result = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("class=" + servletClass);
|
||||||
|
|
||||||
|
if (servletClass == null)
|
||||||
|
{
|
||||||
|
result = null;
|
||||||
|
logger.error("Unknow page: (" + className + ")");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
result = servletClass.newInstance();
|
||||||
|
}
|
||||||
|
catch (java.lang.InstantiationException exception)
|
||||||
|
{
|
||||||
|
logger.error("Can't instanciate servlet (" + className + ")");
|
||||||
|
result = null;
|
||||||
|
}
|
||||||
|
catch (java.lang.IllegalAccessException exception)
|
||||||
|
{
|
||||||
|
logger.error("(2) Can't instanciate servlet (" + className + ")");
|
||||||
|
result = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return (result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract values from a path. Example: "/article-/123/doors/open.xhtml" =>
|
||||||
|
* "123", "doors" and "open".
|
||||||
|
*/
|
||||||
|
static public String[] longRewritedUrlParameters(final String path)
|
||||||
|
{
|
||||||
|
String[] result;
|
||||||
|
|
||||||
|
result = path.substring(path.indexOf("-/") + 2).split("/");
|
||||||
|
|
||||||
|
//
|
||||||
|
return (result);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a path in a class name, using easy conventions.
|
* Convert a path in a class name, using easy conventions.
|
||||||
*
|
*
|
||||||
* "/" => "Index_xhtml"
|
* "/" => "Index_xhtml" "/good/" => "good.Good_xhtml" "/good/morning.xhtml"
|
||||||
* "/good/" => "good.Good_xhtml"
|
* => "good.Morning_xhtml" "/good/morning_girl.xhtml" =>
|
||||||
* "/good/morning.xhtml" => "good.Morning_xhtml"
|
* "good.Morning_girl_xhtml" "/good/morning-123.xhtml" =>
|
||||||
* "/good/morning_girl.xhtml" => "good.Morning_girl_xhtml"
|
* "good.Morning_xhtml" ('123' is detected as a parameter, it will be
|
||||||
* "/good/morning-123.xhtml" => "good.Morning_xhtml" ('123' is detected as a parameter, it will be decoded in the class called later).
|
* decoded in the class called later). "/good/morning-/12/toto.jpg" =>
|
||||||
* "/good/morning-/12/toto.jpg" => "good.Morning" ('12' and 'toto.jpg" are detected as a parameter, they will be decoded in the class called later).
|
* "good.Morning" ('12' and 'toto.jpg" are detected as a parameter, they
|
||||||
|
* will be decoded in the class called later).
|
||||||
*/
|
*/
|
||||||
static public String pathInfoToClassName (String pathInfo)
|
static public String pathInfoToClassName(final String pathInfo)
|
||||||
{
|
{
|
||||||
String result;
|
String result;
|
||||||
|
|
||||||
|
@ -89,7 +284,7 @@ public class SimpleServletDispatcher extends HttpServlet
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if ( pathInfo.equals ("/"))
|
if (pathInfo.equals("/"))
|
||||||
{
|
{
|
||||||
result = "Index_xhtml";
|
result = "Index_xhtml";
|
||||||
}
|
}
|
||||||
|
@ -103,31 +298,33 @@ public class SimpleServletDispatcher extends HttpServlet
|
||||||
String[] tokens = pathInfo.substring(0, keywordIndex).split("/");
|
String[] tokens = pathInfo.substring(0, keywordIndex).split("/");
|
||||||
|
|
||||||
StringList name = new StringList();
|
StringList name = new StringList();
|
||||||
// Note: as pathInfo starts always with a '/', the first good token index is 1.
|
// Note: as pathInfo starts always with a '/', the first
|
||||||
for (int tokenCounter = 1; tokenCounter < tokens.length - 1; tokenCounter++)
|
// good token index is 1.
|
||||||
|
for (int tokenCounter = 1; tokenCounter < (tokens.length - 1); tokenCounter++)
|
||||||
{
|
{
|
||||||
name.append (tokens[tokenCounter]);
|
name.append(tokens[tokenCounter]);
|
||||||
name.append ('.');
|
name.append('.');
|
||||||
}
|
}
|
||||||
|
|
||||||
String lastToken = tokens[tokens.length - 1];
|
String lastToken = tokens[tokens.length - 1];
|
||||||
name.append (lastToken.substring(0, 1).toUpperCase()).append (lastToken.substring(1).replace('.', '_'));
|
name.append(lastToken.substring(0, 1).toUpperCase()).append(lastToken.substring(1).replace('.', '_'));
|
||||||
result = name.toString ();
|
result = name.toString();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
String[] tokens = pathInfo.split ("/");
|
String[] tokens = pathInfo.split("/");
|
||||||
|
|
||||||
StringList name = new StringList();
|
StringList name = new StringList();
|
||||||
// Note: as pathInfo starts always with a '/', the first good token index is 1.
|
// Note: as pathInfo starts always with a '/', the first
|
||||||
for (int tokenCounter = 1; tokenCounter < tokens.length - 1; tokenCounter++)
|
// good token index is 1.
|
||||||
|
for (int tokenCounter = 1; tokenCounter < (tokens.length - 1); tokenCounter++)
|
||||||
{
|
{
|
||||||
name.append (tokens[tokenCounter]);
|
name.append(tokens[tokenCounter]);
|
||||||
name.append ('.');
|
name.append('.');
|
||||||
}
|
}
|
||||||
|
|
||||||
String lastToken = tokens[tokens.length - 1];
|
String lastToken = tokens[tokens.length - 1];
|
||||||
if (pathInfo.endsWith ("/"))
|
if (pathInfo.endsWith("/"))
|
||||||
{
|
{
|
||||||
name.append(lastToken).append(".").append(lastToken.substring(0, 1).toUpperCase()).append(lastToken.substring(1)).append("_xhtml");
|
name.append(lastToken).append(".").append(lastToken.substring(0, 1).toUpperCase()).append(lastToken.substring(1)).append("_xhtml");
|
||||||
}
|
}
|
||||||
|
@ -137,30 +334,30 @@ public class SimpleServletDispatcher extends HttpServlet
|
||||||
if (matcher.matches())
|
if (matcher.matches())
|
||||||
{
|
{
|
||||||
// Short rewrited URL case.
|
// Short rewrited URL case.
|
||||||
//logger.debug("group 1=[" + matcher.group(1) + "]");
|
// logger.debug("group 1=[" + matcher.group(1) +
|
||||||
|
// "]");
|
||||||
lastToken = matcher.group(1) + ".xhtml";
|
lastToken = matcher.group(1) + ".xhtml";
|
||||||
}
|
}
|
||||||
|
|
||||||
name.append (lastToken.substring(0, 1).toUpperCase()).append (lastToken.substring(1).replace('.', '_'));
|
name.append(lastToken.substring(0, 1).toUpperCase()).append(lastToken.substring(1).replace('.', '_'));
|
||||||
}
|
}
|
||||||
|
|
||||||
result = name.toString ();
|
result = name.toString();
|
||||||
logger.debug ("==>[" + tokens[tokens.length - 1] + "]");
|
logger.debug("==>[" + tokens[tokens.length - 1] + "]");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info ("[" + pathInfo + "] => [" + result + "]");
|
logger.info("[" + pathInfo + "] => [" + result + "]");
|
||||||
|
|
||||||
//
|
//
|
||||||
return (result);
|
return (result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
static public String pathInfoToClassName (String pathInfo, String prefix)
|
static public String pathInfoToClassName(final String pathInfo, final String prefix)
|
||||||
{
|
{
|
||||||
String result;
|
String result;
|
||||||
|
|
||||||
|
@ -183,90 +380,18 @@ public class SimpleServletDispatcher extends HttpServlet
|
||||||
return (result);
|
return (result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
static public HttpServlet instanciateServlet (String className)
|
static public void returnAttachmentFile(final HttpServletResponse response, final File file, final String mimeType) throws IOException
|
||||||
{
|
|
||||||
HttpServlet result;
|
|
||||||
|
|
||||||
Class<HttpServlet> servletClass = null;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
servletClass = (Class<HttpServlet>) Class.forName (className);
|
|
||||||
}
|
|
||||||
catch (java.lang.ClassNotFoundException exception)
|
|
||||||
{
|
|
||||||
result = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info ("class=" + servletClass);
|
|
||||||
|
|
||||||
if (servletClass == null)
|
|
||||||
{
|
|
||||||
result = null;
|
|
||||||
logger.error ("Unknow page: (" + className + ")");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
result = servletClass.newInstance ();
|
|
||||||
}
|
|
||||||
catch (java.lang.InstantiationException exception)
|
|
||||||
{
|
|
||||||
logger.error ("Can't instanciate servlet (" + className + ")");
|
|
||||||
result = null;
|
|
||||||
}
|
|
||||||
catch (java.lang.IllegalAccessException exception)
|
|
||||||
{
|
|
||||||
logger.error ("(2) Can't instanciate servlet (" + className + ")");
|
|
||||||
result = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
return (result);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public boolean isAuthorized(HttpServletRequest request, HttpServletResponse response)
|
|
||||||
{
|
|
||||||
boolean result;
|
|
||||||
|
|
||||||
result = true;
|
|
||||||
|
|
||||||
//
|
|
||||||
return(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
static public void returnInlineFile(HttpServletResponse response, File file, String mimeType) throws IOException
|
|
||||||
{
|
|
||||||
returnFile(response, file, mimeType, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
static public void returnAttachmentFile(HttpServletResponse response, File file, String mimeType) throws IOException
|
|
||||||
{
|
{
|
||||||
returnFile(response, file, mimeType, true);
|
returnFile(response, file, mimeType, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
static public void returnFile(HttpServletResponse response, File file, String mimeType, boolean isAttachment) throws IOException
|
static public void returnFile(final HttpServletResponse response, final File file, final String mimeType, final boolean isAttachment) throws IOException
|
||||||
{
|
{
|
||||||
|
|
||||||
if ((file == null) || (!file.exists()))
|
if ((file == null) || (!file.exists()))
|
||||||
|
@ -276,23 +401,25 @@ public class SimpleServletDispatcher extends HttpServlet
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
response.setContentType("application/" + extension);
|
* response.setContentType("application/" + extension);
|
||||||
response.setContentLength((int) data.length);
|
* response.setContentLength((int) data.length);
|
||||||
response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
|
* response.setHeader("Content-Disposition"
|
||||||
response.flushBuffer();
|
* ,"attachment; filename=\"" + filename + "\"");
|
||||||
|
* response.flushBuffer();
|
||||||
*/
|
*/
|
||||||
response.reset();
|
response.reset();
|
||||||
response.setContentType(mimeType);
|
response.setContentType(mimeType);
|
||||||
response.setContentLength((int) file.length());
|
response.setContentLength((int) file.length());
|
||||||
response.setHeader("Content-Disposition","inline; filename=\"" + file.getName()+ "\"");
|
response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");
|
||||||
response.flushBuffer();
|
response.flushBuffer();
|
||||||
|
|
||||||
ServletOutputStream out = response.getOutputStream();
|
ServletOutputStream out = response.getOutputStream();
|
||||||
|
|
||||||
FileInputStream in = null;
|
FileInputStream in = null;
|
||||||
try // Only for the in.
|
try
|
||||||
|
// Only for the in.
|
||||||
{
|
{
|
||||||
byte[] buffer = new byte[64*1024];
|
byte[] buffer = new byte[64 * 1024];
|
||||||
|
|
||||||
in = new FileInputStream(file);
|
in = new FileInputStream(file);
|
||||||
boolean ended = false;
|
boolean ended = false;
|
||||||
|
@ -326,114 +453,64 @@ public class SimpleServletDispatcher extends HttpServlet
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
static public void returnInlineFile(final HttpServletResponse response, final File file, final String mimeType) throws IOException
|
||||||
|
{
|
||||||
|
returnFile(response, file, mimeType, false);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public void doIt (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
|
static public String[] rewritedUrlParameters(final HttpServletRequest request)
|
||||||
{
|
{
|
||||||
logger.info ("==================================================");
|
String[] result;
|
||||||
logger.info ("getContextPath=[" + request.getContextPath () + "]");
|
|
||||||
logger.info ("getPathInfo=[" + request.getPathInfo () + "]");
|
|
||||||
logger.info ("getPathTranslated=[" + request.getPathTranslated () + "]");
|
|
||||||
logger.info ("getQueryString=[" + request.getQueryString () + "]");
|
|
||||||
logger.info ("getRequestURI=[" + request.getRequestURI () + "]");
|
|
||||||
logger.info ("getRequestURL=[" + request.getRequestURL () + "]");
|
|
||||||
logger.info ("getServletPath=[" + request.getServletPath () + "]");
|
|
||||||
|
|
||||||
|
result = longRewritedUrlParameters(request.getRequestURI());
|
||||||
|
|
||||||
//
|
//
|
||||||
/* In past, possibility to use the servlet path was enable. It is too complexe, not kiss mind.
|
return (result);
|
||||||
String path;
|
|
||||||
if (request.getPathInfo() == null)
|
|
||||||
{
|
|
||||||
// web.xml url-pattern= *.xhtml
|
|
||||||
path = request.getServletPath();
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
// web.xml url-pattern = /*
|
|
||||||
path = request.getPathInfo();
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
String path = request.getRequestURI();
|
|
||||||
|
|
||||||
if ((!path.endsWith("/")) && (!path.endsWith(".xhtml")) && (!path.contains("-/")))
|
|
||||||
{
|
|
||||||
path = getServletContext ().getRealPath("/") + request.getRequestURI();
|
|
||||||
|
|
||||||
returnInlineFile(response, new File(path), getServletContext().getMimeType(path));
|
|
||||||
logger.info("File returned directly [" + path + "] with mimetype [" + getServletContext().getMimeType(path) + "].");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
String className = pathInfoToClassName (path, this.webclassesRootPath);
|
|
||||||
logger.info ("className=[" + className + "]");
|
|
||||||
|
|
||||||
HttpServlet servlet = instanciateServlet (className);
|
|
||||||
|
|
||||||
//servlet.getServletContext().setAttribute(arg0, arg1);
|
|
||||||
|
|
||||||
if (servlet == null)
|
|
||||||
{
|
|
||||||
response.setContentType ("text/html");
|
|
||||||
PrintWriter out = response.getWriter();
|
|
||||||
|
|
||||||
out.println ("<html><head></head><body>");
|
|
||||||
out.println ("Unknow page.");
|
|
||||||
out.println ("</body></html>");
|
|
||||||
|
|
||||||
out.close();
|
|
||||||
}
|
|
||||||
else if (isAuthorized(request, response))
|
|
||||||
{
|
|
||||||
servlet.init(this.getServletConfig());
|
|
||||||
servlet.service(request, response);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/*
|
|
||||||
* TODO
|
|
||||||
//
|
|
||||||
response.setContentType ("text/html");
|
|
||||||
PrintWriter out = response.getWriter();
|
|
||||||
|
|
||||||
out.println ("<html><head></head><body>");
|
|
||||||
out.println ("Not authorized page.");
|
|
||||||
out.println ("</body></html>");
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* "Code can be shortest, speedest and memory smallest, but not the three in same time, only two", unknow citation.
|
* This method gives a way for a long rewriting URL format. Long as in REST.
|
||||||
|
*
|
||||||
|
* Sometimes, URL has to be rewrited because we need to put parameter in the
|
||||||
|
* page name.
|
||||||
|
*
|
||||||
|
* Example: "/good/give_file?id=123&filename=foo.jpg" =>
|
||||||
|
* rewriteShorturl("/good/give_file", "123", "foo.jpg"); =>
|
||||||
|
* "/good/give_file-/123/foo.jpg"
|
||||||
|
*
|
||||||
|
* Note: "-/" is used to indicate the start of parameters.
|
||||||
*
|
*
|
||||||
* Note: characters array avalaible here http://fr.wikipedia.org/wiki/Table_des_caract%C3%A8res_Unicode_%280000-0FFF%29
|
|
||||||
*/
|
*/
|
||||||
static protected char NONE = (char) 0;
|
static public String rewriteLongUrl(final String uri, final String... parameters)
|
||||||
static protected int[] rewritingParameterMapping =
|
|
||||||
{
|
{
|
||||||
/* 00 */ NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE ,
|
String result;
|
||||||
/* 10 */ NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE ,
|
|
||||||
/* 20 */ '-' , NONE , NONE , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' ,
|
|
||||||
/* 30 */ '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '-' , '-' , '-' , '-' , '-' , '-' ,
|
|
||||||
/* 40 */ '\u0040', 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , 'o' ,
|
|
||||||
/* 50 */ 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z' , '-' , '-' , '-' , '-' , '-' ,
|
|
||||||
/* 60 */ '-' , '\u0061', '\u0062', '\u0063', '\u0064', '\u0065', '\u0066', '\u0067', '\u0068', '\u0069', '\u006A', '\u006B', '\u006C', '\u006D', '\u006E', '\u006F',
|
|
||||||
/* 70 */ '\u0070', '\u0071', '\u0072', '\u0073', '\u0074', '\u0075', '\u0076', '\u0077', '\u0078', '\u0079', '\u007A', '\u007B', '\u007C', '\u007D', '-' , '-' ,
|
|
||||||
/* 80 */ NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE ,
|
|
||||||
/* 90 */ NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE , NONE ,
|
|
||||||
/* A0 */ '\u00A0', '\u00A1', '\u00A2', '\u00A3', '\u00A4', '\u00A5', '\u00A6', '\u00A7', '\u00A8', '\u00A9', '\u00AA', '\u00AB', '\u00AC', '\u00AD', '\u00AE', '\u00AF',
|
|
||||||
/* B0 */ '-' , '\u00B1', '\u00B2', '\u00B3', '\u00B4', '\u00B5', '\u00B6', '\u00B7', '\u00B8', '\u00B9', '\u00BA', '\u00BB', '\u00BC', '\u00BD', '\u00BE', '\u00BF',
|
|
||||||
/* C0 */ 'a' , 'a' , 'a' , 'a' , 'a' , 'a' , 'a' , 'c' , 'e' , 'e' , 'e' , 'e' , 'i' , 'i' , 'i' , 'i' ,
|
|
||||||
/* D0 */ '\u00D0', '\u00D1', 'o' , 'o' , 'o' , 'o' , 'o' , 'o' , '\u00D8', 'u' , 'u' , 'u' , 'u' , 'y' , '\u00DE', '\u00DF',
|
|
||||||
/* E0 */ 'a' , 'a' , 'a' , 'a' , 'a' , 'a' , 'a' , 'c' , 'e' , 'e' , 'e' , 'e' , 'i' , 'i' , 'i' , 'i' ,
|
|
||||||
/* F0 */ 'o' , 'n' , 'o' , 'o' , 'o' , 'o' , 'o' , '\u00F7', '-' , 'u' , 'u' , 'u' , 'u' , 'y' , '-' , 'y'
|
|
||||||
};
|
|
||||||
|
|
||||||
|
StringList string = new StringList();
|
||||||
|
|
||||||
|
string.append(uri).append("-");
|
||||||
|
if ((parameters == null) || (parameters.length == 0))
|
||||||
|
{
|
||||||
|
string.append("/");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (String parameter : parameters)
|
||||||
|
{
|
||||||
|
string.append("/").append(parameter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result = string.toString();
|
||||||
|
|
||||||
|
//
|
||||||
|
return (result);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -441,7 +518,7 @@ public class SimpleServletDispatcher extends HttpServlet
|
||||||
* @param parameter
|
* @param parameter
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
static String rewriteParameter (String parameter)
|
static String rewriteParameter(final String parameter)
|
||||||
{
|
{
|
||||||
String result;
|
String result;
|
||||||
|
|
||||||
|
@ -450,7 +527,8 @@ public class SimpleServletDispatcher extends HttpServlet
|
||||||
char previousCar = NONE;
|
char previousCar = NONE;
|
||||||
for (int charIndex = 0; charIndex < parameter.length(); charIndex++)
|
for (int charIndex = 0; charIndex < parameter.length(); charIndex++)
|
||||||
{
|
{
|
||||||
//logger.info("" + charIndex + " " + parameter.charAt(charIndex) + " " + (char) tab[parameter.charAt(charIndex)]);
|
// logger.info("" + charIndex + " " + parameter.charAt(charIndex) +
|
||||||
|
// " " + (char) tab[parameter.charAt(charIndex)]);
|
||||||
char car = (char) rewritingParameterMapping[parameter.charAt(charIndex)];
|
char car = (char) rewritingParameterMapping[parameter.charAt(charIndex)];
|
||||||
if (car != NONE)
|
if (car != NONE)
|
||||||
{
|
{
|
||||||
|
@ -473,18 +551,17 @@ public class SimpleServletDispatcher extends HttpServlet
|
||||||
return (result);
|
return (result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method gives a way for a short rewriting URL format.
|
* This method gives a way for a short rewriting URL format.
|
||||||
*
|
*
|
||||||
* Sometimes, URL has to be rewrited because we need to put parameter in the page name.
|
* Sometimes, URL has to be rewrited because we need to put parameter in the
|
||||||
|
* page name.
|
||||||
*
|
*
|
||||||
* Example:
|
* Example: "/good/article.xhtm?id=123&class=today&title=story's about me"
|
||||||
* "/good/article.xhtm?id=123&class=today&title=story's about me"
|
|
||||||
* => rewriteShorturl("/good/article", "xhtml", "123", "Story's aboute me");
|
* => rewriteShorturl("/good/article", "xhtml", "123", "Story's aboute me");
|
||||||
* => "/good/article-123-today-story-s-about-me.xhtml"
|
* => "/good/article-123-today-story-s-about-me.xhtml"
|
||||||
*/
|
*/
|
||||||
static public String rewriteShortUrl(String uri, String extension, String... parameters)
|
static public String rewriteShortUrl(final String uri, final String extension, final String... parameters)
|
||||||
{
|
{
|
||||||
String result;
|
String result;
|
||||||
|
|
||||||
|
@ -494,8 +571,9 @@ public class SimpleServletDispatcher extends HttpServlet
|
||||||
|
|
||||||
for (String parameter : parameters)
|
for (String parameter : parameters)
|
||||||
{
|
{
|
||||||
// Not use of String.replaceAll() method in goal to optimize Pattern compile action.
|
// Not use of String.replaceAll() method in goal to optimize Pattern
|
||||||
//string.append("-").append(REWRITE_PARAMETER.matcher(parameter.toLowerCase()).replaceAll("-"));
|
// compile action.
|
||||||
|
// string.append("-").append(REWRITE_PARAMETER.matcher(parameter.toLowerCase()).replaceAll("-"));
|
||||||
string.append("-").append(rewriteParameter(parameter));
|
string.append("-").append(rewriteParameter(parameter));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -507,16 +585,61 @@ public class SimpleServletDispatcher extends HttpServlet
|
||||||
result = string.toString();
|
result = string.toString();
|
||||||
|
|
||||||
//
|
//
|
||||||
return(result);
|
return (result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
static public String shortRewritedUrlParameter(final HttpServletRequest request)
|
||||||
|
{
|
||||||
|
String result;
|
||||||
|
|
||||||
|
result = shortRewritedUrlParameter(request.getRequestURI());
|
||||||
|
|
||||||
|
//
|
||||||
|
return (result);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract value from a path.
|
* Return value of the first parameter.
|
||||||
* Example:
|
|
||||||
* "/article-123.xhtml" => "123".
|
|
||||||
*/
|
*/
|
||||||
static public String[] shortRewritedUrlParameters(String path)
|
static public String shortRewritedUrlParameter(final String path)
|
||||||
|
{
|
||||||
|
String result;
|
||||||
|
|
||||||
|
String[] results = shortRewritedUrlParameters(path);
|
||||||
|
|
||||||
|
if ((results == null) || (results.length == 0))
|
||||||
|
{
|
||||||
|
result = null;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result = results[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return (result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
static public String[] shortRewritedUrlParameters(final HttpServletRequest request)
|
||||||
|
{
|
||||||
|
String[] result;
|
||||||
|
|
||||||
|
result = shortRewritedUrlParameters(request.getRequestURI());
|
||||||
|
|
||||||
|
//
|
||||||
|
return (result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract value from a path. Example: "/article-123.xhtml" => "123".
|
||||||
|
*/
|
||||||
|
static public String[] shortRewritedUrlParameters(final String path)
|
||||||
{
|
{
|
||||||
String[] result;
|
String[] result;
|
||||||
|
|
||||||
|
@ -538,144 +661,6 @@ public class SimpleServletDispatcher extends HttpServlet
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
return(result);
|
return (result);
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
static public String[] shortRewritedUrlParameters(HttpServletRequest request)
|
|
||||||
{
|
|
||||||
String[] result;
|
|
||||||
|
|
||||||
result = shortRewritedUrlParameters(request.getRequestURI());
|
|
||||||
|
|
||||||
//
|
|
||||||
return(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return value of the first parameter.
|
|
||||||
*/
|
|
||||||
static public String shortRewritedUrlParameter(String path)
|
|
||||||
{
|
|
||||||
String result;
|
|
||||||
|
|
||||||
String[] results = shortRewritedUrlParameters(path);
|
|
||||||
|
|
||||||
if ((results == null) || (results.length == 0))
|
|
||||||
{
|
|
||||||
result = null;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
result = results[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
return(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
static public String shortRewritedUrlParameter(HttpServletRequest request)
|
|
||||||
{
|
|
||||||
String result;
|
|
||||||
|
|
||||||
result = shortRewritedUrlParameter(request.getRequestURI());
|
|
||||||
|
|
||||||
//
|
|
||||||
return(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method gives a way for a long rewriting URL format. Long as in REST.
|
|
||||||
*
|
|
||||||
* Sometimes, URL has to be rewrited because we need to put parameter in the page name.
|
|
||||||
*
|
|
||||||
* Example:
|
|
||||||
* "/good/give_file?id=123&filename=foo.jpg"
|
|
||||||
* => rewriteShorturl("/good/give_file", "123", "foo.jpg");
|
|
||||||
* => "/good/give_file-/123/foo.jpg"
|
|
||||||
*
|
|
||||||
* Note: "-/" is used to indicate the start of parameters.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
static public String rewriteLongUrl(String uri, String... parameters)
|
|
||||||
{
|
|
||||||
String result;
|
|
||||||
|
|
||||||
StringList string = new StringList();
|
|
||||||
|
|
||||||
string.append(uri).append("-");
|
|
||||||
if ((parameters == null) || (parameters.length == 0))
|
|
||||||
{
|
|
||||||
string.append("/");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for (String parameter : parameters)
|
|
||||||
{
|
|
||||||
string.append("/").append(parameter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
result = string.toString();
|
|
||||||
|
|
||||||
//
|
|
||||||
return(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Extract values from a path.
|
|
||||||
* Example:
|
|
||||||
* "/article-/123/doors/open.xhtml" => "123", "doors" and "open".
|
|
||||||
*/
|
|
||||||
static public String[] longRewritedUrlParameters(String path)
|
|
||||||
{
|
|
||||||
String[] result;
|
|
||||||
|
|
||||||
result = path.substring(path.indexOf("-/") + 2).split("/");
|
|
||||||
|
|
||||||
//
|
|
||||||
return(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
static public String[] rewritedUrlParameters(HttpServletRequest request)
|
|
||||||
{
|
|
||||||
String[] result;
|
|
||||||
|
|
||||||
result = longRewritedUrlParameters(request.getRequestURI());
|
|
||||||
|
|
||||||
//
|
|
||||||
return(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
|
|
||||||
{
|
|
||||||
doIt (request, response);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
|
||||||
{
|
|
||||||
doIt (request, response);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue