2009-09-23 23:26:14 +02:00
|
|
|
// HTTPDFileHandler.java
|
2009-07-19 22:37:44 +02:00
|
|
|
// -----------------------
|
|
|
|
// (C) by Michael Peter Christen; mc@yacy.net
|
|
|
|
// first published on http://www.anomic.de
|
|
|
|
// Frankfurt, Germany, 2004, 2005
|
|
|
|
//
|
2009-09-23 23:26:14 +02:00
|
|
|
// $LastChangedDate$
|
|
|
|
// $LastChangedRevision$
|
|
|
|
// $LastChangedBy$
|
2009-07-19 22:37:44 +02:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
/*
|
|
|
|
Class documentation:
|
|
|
|
this class provides a file servlet and CGI interface
|
|
|
|
for the httpd server.
|
|
|
|
Whenever this server is addressed to load a local file,
|
|
|
|
this class searches for the file in the local path as
|
|
|
|
configured in the setting property 'rootPath'
|
|
|
|
The servlet loads the file and returns it to the client.
|
|
|
|
Every file can also act as an template for the built-in
|
|
|
|
CGI interface. There is no specific path for CGI functions.
|
|
|
|
CGI functionality is triggered, if for the file to-be-served
|
|
|
|
'template.html' also a file 'template.class' exists. Then,
|
|
|
|
the class file is called with the GET/POST properties that
|
|
|
|
are attached to the http call.
|
|
|
|
Possible variable hand-over are:
|
|
|
|
- form method GET
|
|
|
|
- form method POST, enctype text/plain
|
|
|
|
- form method POST, enctype multipart/form-data
|
|
|
|
The class that creates the CGI respond must have at least one
|
|
|
|
static method of the form
|
|
|
|
public static java.util.Hashtable respond(java.util.HashMap, serverSwitch)
|
|
|
|
In the HashMap, the GET/POST variables are handed over.
|
|
|
|
The return value is a Property object that contains replacement
|
|
|
|
key/value pairs for the patterns in the template file.
|
|
|
|
The templates must have the form
|
|
|
|
either '#['<name>']#' for single attributes, or
|
|
|
|
'#{'<enumname>'}#' and '#{/'<enumname>'}#' for enumerations of
|
|
|
|
values '#['<value>']#'.
|
|
|
|
A single value in repetitions/enumerations in the template has
|
|
|
|
the property key '_'<enumname><count>'_'<value>
|
|
|
|
Please see also the example files 'test.html' and 'test.java'
|
|
|
|
*/
|
|
|
|
|
2012-09-21 15:48:16 +02:00
|
|
|
package net.yacy.server.http;
|
2009-07-19 22:37:44 +02:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
2012-11-21 18:46:49 +01:00
|
|
|
import net.yacy.cora.document.analysis.Classification;
|
2013-07-09 14:28:25 +02:00
|
|
|
import net.yacy.cora.util.ConcurrentLog;
|
2011-09-25 18:59:06 +02:00
|
|
|
import net.yacy.search.Switchboard;
|
|
|
|
import net.yacy.search.SwitchboardConstants;
|
2012-09-21 15:48:16 +02:00
|
|
|
import net.yacy.server.serverSwitch;
|
2009-07-19 22:37:44 +02:00
|
|
|
|
|
|
|
public final class HTTPDFileHandler {
|
2011-06-21 00:53:43 +02:00
|
|
|
|
2009-07-19 22:37:44 +02:00
|
|
|
// create a class loader
|
|
|
|
private static serverSwitch switchboard = null;
|
2011-06-21 00:53:43 +02:00
|
|
|
|
2013-12-23 02:06:33 +01:00
|
|
|
public static File htDocsPath = null;
|
2013-12-26 23:59:04 +01:00
|
|
|
public static String[] defaultFiles = null;
|
2009-07-19 22:37:44 +02:00
|
|
|
private static File htDefaultPath = null;
|
|
|
|
private static File htLocalePath = null;
|
2010-12-08 11:50:23 +01:00
|
|
|
public static String indexForward = "";
|
2011-06-21 00:53:43 +02:00
|
|
|
|
2009-07-19 22:37:44 +02:00
|
|
|
//private Properties connectionProperties = null;
|
|
|
|
// creating a logger
|
2011-06-21 00:53:43 +02:00
|
|
|
|
2009-07-19 22:37:44 +02:00
|
|
|
static {
|
|
|
|
final serverSwitch theSwitchboard = Switchboard.getSwitchboard();
|
2011-06-21 00:53:43 +02:00
|
|
|
|
2009-07-19 22:37:44 +02:00
|
|
|
if (switchboard == null) {
|
|
|
|
switchboard = theSwitchboard;
|
2009-09-23 23:26:14 +02:00
|
|
|
|
2011-09-01 18:05:00 +02:00
|
|
|
if (Classification.countMimes() == 0) {
|
2009-07-19 22:37:44 +02:00
|
|
|
// load the mime table
|
2009-10-14 15:29:09 +02:00
|
|
|
final String mimeTablePath = theSwitchboard.getConfig("mimeTable","");
|
2013-07-09 14:28:25 +02:00
|
|
|
ConcurrentLog.config("HTTPDFiles", "Loading mime mapping file " + mimeTablePath);
|
2011-09-01 18:05:00 +02:00
|
|
|
Classification.init(new File(theSwitchboard.getAppPath(), mimeTablePath));
|
2009-07-19 22:37:44 +02:00
|
|
|
}
|
2011-06-21 00:53:43 +02:00
|
|
|
|
2009-07-19 22:37:44 +02:00
|
|
|
// create default files array
|
|
|
|
initDefaultPath();
|
2011-06-21 00:53:43 +02:00
|
|
|
|
2009-07-19 22:37:44 +02:00
|
|
|
// create a htDocsPath: user defined pages
|
|
|
|
if (htDocsPath == null) {
|
2010-09-02 21:24:22 +02:00
|
|
|
htDocsPath = theSwitchboard.getDataPath(SwitchboardConstants.HTDOCS_PATH, SwitchboardConstants.HTDOCS_PATH_DEFAULT);
|
2009-07-19 22:37:44 +02:00
|
|
|
if (!(htDocsPath.exists())) htDocsPath.mkdirs();
|
|
|
|
}
|
2011-06-21 00:53:43 +02:00
|
|
|
|
2009-07-19 22:37:44 +02:00
|
|
|
// create htLocaleDefault, htLocalePath
|
2014-02-27 00:23:34 +01:00
|
|
|
if (htDefaultPath == null) htDefaultPath = theSwitchboard.getAppPath("htDefaultPath", SwitchboardConstants.HTROOT_PATH_DEFAULT);
|
2010-09-02 21:24:22 +02:00
|
|
|
if (htLocalePath == null) htLocalePath = theSwitchboard.getDataPath("locale.translated_html", "DATA/LOCALE/htroot");
|
2009-07-19 22:37:44 +02:00
|
|
|
}
|
|
|
|
}
|
2011-06-21 00:53:43 +02:00
|
|
|
|
2009-07-19 22:37:44 +02:00
|
|
|
public static final void initDefaultPath() {
|
|
|
|
// create default files array
|
2012-02-26 20:53:32 +01:00
|
|
|
defaultFiles = switchboard.getConfig(SwitchboardConstants.BROWSER_DEFAULT,"index.html").split(",");
|
2009-07-19 22:37:44 +02:00
|
|
|
if (defaultFiles.length == 0) defaultFiles = new String[] {"index.html"};
|
2011-02-03 11:43:09 +01:00
|
|
|
indexForward = switchboard.getConfig(SwitchboardConstants.INDEX_FORWARD, "");
|
2010-12-08 11:50:23 +01:00
|
|
|
if (indexForward.startsWith("/")) indexForward = indexForward.substring(1);
|
2009-07-19 22:37:44 +02:00
|
|
|
}
|
2011-06-21 00:53:43 +02:00
|
|
|
|
2009-07-19 22:37:44 +02:00
|
|
|
/** Returns a path to the localized or default file according to the locale.language (from he switchboard)
|
|
|
|
* @param path relative from htroot */
|
|
|
|
public static File getLocalizedFile(final String path){
|
2013-12-23 01:23:40 +01:00
|
|
|
String localeSelection = switchboard.getConfig("locale.language","default");
|
2009-07-19 22:37:44 +02:00
|
|
|
if (!(localeSelection.equals("default"))) {
|
|
|
|
final File localePath = new File(htLocalePath, localeSelection + '/' + path);
|
|
|
|
if (localePath.exists()) return localePath; // avoid "NoSuchFile" troubles if the "localeSelection" is misspelled
|
|
|
|
}
|
2013-12-23 01:23:40 +01:00
|
|
|
|
2011-06-21 00:53:43 +02:00
|
|
|
final File docsPath = new File(htDocsPath, path);
|
2009-07-19 22:37:44 +02:00
|
|
|
if (docsPath.exists()) return docsPath;
|
|
|
|
return new File(htDefaultPath, path);
|
|
|
|
}
|
2011-06-21 00:53:43 +02:00
|
|
|
|
2009-07-19 22:37:44 +02:00
|
|
|
public static final File getOverlayedFile(final String path) {
|
|
|
|
File targetFile;
|
|
|
|
targetFile = getLocalizedFile(path);
|
|
|
|
if (!targetFile.exists()) {
|
|
|
|
targetFile = new File(htDocsPath, path);
|
|
|
|
}
|
|
|
|
return targetFile;
|
|
|
|
}
|
2011-06-21 00:53:43 +02:00
|
|
|
|
2009-07-19 22:37:44 +02:00
|
|
|
}
|