From 0bba8c08d29aa16cdba91e25fcf3ba754800993b Mon Sep 17 00:00:00 2001 From: tkellner Date: Thu, 18 Aug 2011 15:04:09 +0000 Subject: Help update: Show specific 404 pages * Locale-dependent * 404h.html if page not found was a help page * 404.html else git-svn-id: https://joinup.ec.europa.eu/svn/mocca/trunk@956 8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4 --- .../java/at/gv/egiz/bku/webstart/Container.java | 108 ++++++++++++++++++--- .../java/at/gv/egiz/bku/webstart/Launcher.java | 2 +- .../gv/egiz/bku/webstart/WebappErrorHandler.java | 80 +++++++++++++++ 3 files changed, 177 insertions(+), 13 deletions(-) create mode 100644 BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/WebappErrorHandler.java (limited to 'BKUWebStart/src') diff --git a/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/Container.java b/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/Container.java index 216c6228..07c5d792 100644 --- a/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/Container.java +++ b/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/Container.java @@ -41,6 +41,8 @@ import java.security.AllPermission; import java.security.KeyStore; import java.security.Permissions; import java.security.cert.Certificate; +import java.util.Locale; + import org.mortbay.jetty.Connector; import org.mortbay.jetty.Server; import org.mortbay.jetty.nio.SelectChannelConnector; @@ -66,8 +68,17 @@ public class Container { } } private Server server; - private WebAppContext webapp; - private Certificate caCertificate; + private WebAppContext webapp; + private WebappErrorHandler errorHandler; + private Certificate caCertificate; + private File tempDir; + + private Locale locale = null; + + public void init(Locale locale) throws IOException { + this.locale = locale; + init(); + } public void init() throws IOException { // System.setProperty("DEBUG", "true"); @@ -103,7 +114,7 @@ public class Container { sslConnector.setKeyPassword(passwd); //avoid jetty's ClassCastException: iaik.security.ecc.ecdsa.ECPublicKey cannot be cast to java.security.interfaces.ECPublicKey - String[] RFC4492CipherSuites = new String[]{ + String[] RFC4492CipherSuites = new String[] { "TLS_ECDH_ECDSA_WITH_NULL_SHA", "TLS_ECDH_ECDSA_WITH_RC4_128_SHA", "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA", @@ -139,10 +150,14 @@ public class Container { webapp.setLogUrlOnStart(true); webapp.setContextPath("/"); webapp.setExtractWAR(true); - webapp.setParentLoaderPriority(false); - - webapp.setWar(copyWebapp(webapp.getTempDirectory())); -// webapp.setPermissions(getPermissions(webapp.getTempDirectory())); + webapp.setParentLoaderPriority(false); + + errorHandler = new WebappErrorHandler(locale); + webapp.setErrorHandler(errorHandler); + + tempDir = webapp.getTempDirectory(); + webapp.setWar(copyWebapp(tempDir)); +// webapp.setPermissions(getPermissions(tempDir)); server.setHandler(webapp); server.setGracefulShutdown(1000 * 3); @@ -186,7 +201,70 @@ public class Container { new StreamCopier(is, os).copyStream(); os.close(); return webapp.getPath(); - } + } + +// private boolean deleteRecursive(File f) +// { +// if (f.isDirectory()) +// { +// String[] children = f.list(); +// for (String child : children) { +// if (!deleteRecursive(new File(f, child))) +// return false; +// } +// } +// return f.delete(); +// } +// +// private void deleteOnExitRecursive(File f) +// { +// if (f.isDirectory()) +// { +// String[] children = f.list(); +// for (String child : children) +// deleteOnExitRecursive(new File(f, child)); +// } +// f.deleteOnExit(); +// } +// +// /** +// * Workaround for Jetty problem where temporary directory +// * doesn't get deleted under Windows due to locking issues. +// * +// * @param tempDir Temporary directory to delete +// */ +// private void deleteTempDir(final File tempDir) { +// try { +// if (tempDir.exists()) { +// log.debug("Temp directory still exists - trying to delete"); +// if (!deleteRecursive(tempDir)) +// { +// log.debug("Deleting temp directory failed - adding shutdown hook"); +// Runtime.getRuntime().addShutdownHook( +// new Thread() { +// public void run() { +// log.debug("Shutdown hook executing"); +// if (tempDir.exists()) { +// log.debug("Temp directory still exists - trying to delete"); +// if (!deleteRecursive(tempDir)) +// { +// log.debug("Deleting temp directory failed"); +// deleteOnExitRecursive(tempDir); +// } +// else +// log.debug("Successfully deleted temp directory"); +// } +// } +// } +// ); +// } +// else +// log.debug("Successfully deleted temp directory"); +// } +// } catch (SecurityException ex) { +// log.debug("Temp directory access failed", ex); +// } +// } /** * grant all permissions, since we need read/write access to save signature data files anywhere (JFileChooser) in the local filesystem @@ -221,13 +299,19 @@ public class Container { return server.isRunning(); } - public void stop() throws Exception { - server.stop(); + public void stop() throws Exception { +// log.debug("Container: stop called"); + server.stop(); + +// deleteTempDir(tempDir); } public void destroy() { +// log.debug("Container: destroy called"); server.destroy(); - } + +// deleteTempDir(tempDir); +} public void join() throws InterruptedException { server.join(); @@ -248,5 +332,5 @@ public class Container { log.error("Failed to load local ca certificate", ex); log.warn("automated web certificate installation will not be available"); } - } + } } diff --git a/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/Launcher.java b/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/Launcher.java index 95d76a4a..f9500f8c 100644 --- a/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/Launcher.java +++ b/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/Launcher.java @@ -201,7 +201,7 @@ public class Launcher implements BKUControllerInterface { private void startServer() throws Exception { log.info("init servlet container and MOCCA webapp"); server = new Container(); - server.init(); + server.init(status.getLocale()); server.start(); } diff --git a/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/WebappErrorHandler.java b/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/WebappErrorHandler.java new file mode 100644 index 00000000..c11f5345 --- /dev/null +++ b/BKUWebStart/src/main/java/at/gv/egiz/bku/webstart/WebappErrorHandler.java @@ -0,0 +1,80 @@ +/* + * Copyright 2011 by Graz University of Technology, Austria + * MOCCA has been developed by the E-Government Innovation Center EGIZ, a joint + * initiative of the Federal Chancellery Austria and Graz University of Technology. + * + * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by + * the European Commission - subsequent versions of the EUPL (the "Licence"); + * You may not use this work except in compliance with the Licence. + * You may obtain a copy of the Licence at: + * http://www.osor.eu/eupl/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the Licence is distributed on an "AS IS" basis, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the Licence for the specific language governing permissions and + * limitations under the Licence. + * + * This product combines work with different licenses. See the "NOTICE" text + * file for details on the various modules and licenses. + * The "NOTICE" text file is part of the distribution. Any derivative works + * that you distribute must include a readable copy of the "NOTICE" text file. + */ + +package at.gv.egiz.bku.webstart; + +import java.io.IOException; +import java.util.Locale; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.mortbay.jetty.HttpConnection; +import org.mortbay.jetty.HttpMethods; +import org.mortbay.jetty.servlet.ErrorPageErrorHandler; +import org.mortbay.jetty.servlet.ServletHandler; + +/** + * @author tkellner_local + * + */ +public class WebappErrorHandler extends ErrorPageErrorHandler { + private String link404, link404help; + + public WebappErrorHandler(Locale locale) { + super(); + + link404 = "/help/404.html"; + link404help = "/help/404h.html"; + if (locale != null) + { + String language = locale.getLanguage(); + if (!language.isEmpty()) + { + link404 = "/help/" + language + "/404.html"; + link404help = "/help/" + language + "/404h.html"; + } + } + } + + @Override + public void handle(String target, HttpServletRequest request, + HttpServletResponse response, int dispatch) throws IOException { + String method = request.getMethod(); + if(!method.equals(HttpMethods.GET) && !method.equals(HttpMethods.POST)) + { + HttpConnection.getCurrentConnection().getRequest().setHandled(true); + return; + } + + Integer code=(Integer)request.getAttribute(ServletHandler.__J_S_ERROR_STATUS_CODE); + if (code.equals(404)) + { + HttpConnection.getCurrentConnection().getRequest().setHandled(true); + response.sendRedirect(request.getRequestURI().startsWith("/help") ? link404help : link404); + return; + } + + super.handle(target, request, response, dispatch); + } +} -- cgit v1.2.3