From 27ed0a72faaa85ba456a24c8292735bbe2fa82ab Mon Sep 17 00:00:00 2001 From: Tobias Kellner Date: Mon, 12 Jan 2015 18:59:30 +0100 Subject: JarLocation fix --- .../at/gv/egiz/bku/online/applet/BKUApplet.java | 8 +++- .../java/at/gv/egiz/bku/utils/JarLocation.java | 49 ++++++++++++++++++++++ .../java/at/gv/egiz/bku/webstart/Launcher.java | 29 ++++++++++++- .../egiz/bku/binding/HTTPBindingProcessorImpl.java | 2 +- .../egiz/bku/spring/ConfigurationFactoryBean.java | 9 ++-- 5 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 BKUCommonGUI/src/main/java/at/gv/egiz/bku/utils/JarLocation.java diff --git a/BKUApplet/src/main/java/at/gv/egiz/bku/online/applet/BKUApplet.java b/BKUApplet/src/main/java/at/gv/egiz/bku/online/applet/BKUApplet.java index 8ae1471b..46ee4102 100644 --- a/BKUApplet/src/main/java/at/gv/egiz/bku/online/applet/BKUApplet.java +++ b/BKUApplet/src/main/java/at/gv/egiz/bku/online/applet/BKUApplet.java @@ -31,6 +31,7 @@ import java.awt.Container; import java.awt.Point; import java.io.IOException; import java.net.MalformedURLException; +import java.net.URISyntaxException; import java.net.URL; import java.security.AccessController; import java.security.AllPermission; @@ -58,6 +59,7 @@ import at.gv.egiz.bku.gui.BKUGUIImpl; import at.gv.egiz.bku.gui.HelpListener; import at.gv.egiz.bku.gui.viewer.FontProvider; import at.gv.egiz.bku.online.applet.viewer.URLFontLoader; +import at.gv.egiz.bku.utils.JarLocation; import at.gv.egiz.smcc.SignatureCardFactory; import at.gv.egiz.stal.service.STALPortType; import at.gv.egiz.stal.service.STALService; @@ -107,8 +109,8 @@ public class BKUApplet extends JApplet { String tmp = UNKNOWN_VERSION; Logger log = LoggerFactory.getLogger(BKUApplet.class); try { - String BKUAppletJar = BKUApplet.class.getProtectionDomain().getCodeSource().getLocation().toString(); - URL manifestURL = new URL("jar:" + BKUAppletJar + "!/META-INF/MANIFEST.MF"); + String bKUAppletJar = JarLocation.get(BKUApplet.class); + URL manifestURL = new URL("jar:" + bKUAppletJar + "!/META-INF/MANIFEST.MF"); log.trace("Read version information from {}.", manifestURL); Manifest manifest = new Manifest(manifestURL.openStream()); Attributes atts = manifest.getMainAttributes(); @@ -117,6 +119,8 @@ public class BKUApplet extends JApplet { } } catch (IOException ex) { log.error("Failed to read version.", ex); + } catch (URISyntaxException ex) { + log.error("Failed to read version.", ex); } finally { VERSION = tmp; log.debug("BKU Applet {}.", VERSION); diff --git a/BKUCommonGUI/src/main/java/at/gv/egiz/bku/utils/JarLocation.java b/BKUCommonGUI/src/main/java/at/gv/egiz/bku/utils/JarLocation.java new file mode 100644 index 00000000..c26ac3dd --- /dev/null +++ b/BKUCommonGUI/src/main/java/at/gv/egiz/bku/utils/JarLocation.java @@ -0,0 +1,49 @@ +/* + * 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.utils; + +import java.io.UnsupportedEncodingException; +import java.net.URISyntaxException; +import java.net.URLDecoder; +import java.security.CodeSource; + +public class JarLocation { + public static String get(Class c) throws URISyntaxException, + UnsupportedEncodingException { + CodeSource codeSource = c.getProtectionDomain().getCodeSource(); + + String loc; + + if (codeSource.getLocation() != null) { + loc = codeSource.getLocation().toString(); + } else { + loc = c.getResource(c.getSimpleName() + ".class").toString(); + } + if (loc.startsWith("jar:")) { + loc = loc.substring(loc.indexOf(":") + 1, loc.indexOf("!")); + loc = URLDecoder.decode(loc, "UTF-8"); + } + return loc; + } +} 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 287e3b9d..30a6ce3d 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 @@ -29,23 +29,30 @@ import at.gv.egiz.bku.webstart.gui.StatusNotifier; import at.gv.egiz.bku.webstart.gui.BKUControllerInterface; import at.gv.egiz.bku.webstart.gui.MOCCAIcon; import iaik.asn1.CodingException; + import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.net.URISyntaxException; import java.util.Locale; import javax.jnlp.UnavailableServiceException; + //import com.sun.javaws.security.JavaWebStartSecurity; import java.awt.Desktop; import java.awt.SplashScreen; import java.net.BindException; import java.net.MalformedURLException; import java.net.URL; +import java.net.URLDecoder; +import java.security.CodeSource; import java.security.GeneralSecurityException; import java.util.jar.Attributes; import java.util.jar.Manifest; + import javax.jnlp.BasicService; import javax.jnlp.ServiceManager; + import org.mortbay.util.MultiException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -106,7 +113,7 @@ public class Launcher implements BKUControllerInterface { static { String tmp = Configurator.UNKOWN_VERSION; try { - String bkuWebStartJar = Launcher.class.getProtectionDomain().getCodeSource().getLocation().toString(); + String bkuWebStartJar = getJarLocation(Launcher.class); URL manifestURL = new URL("jar:" + bkuWebStartJar + "!/META-INF/MANIFEST.MF"); if (log.isTraceEnabled()) { log.trace("read version information from " + manifestURL); @@ -118,6 +125,8 @@ public class Launcher implements BKUControllerInterface { } } catch (IOException ex) { log.error("failed to read version", ex); + } catch (URISyntaxException ex) { + log.error("failed to read version", ex); } finally { version = tmp; log.info("BKU Web Start " + version); @@ -188,6 +197,24 @@ public class Launcher implements BKUControllerInterface { throw new IOException("current platform does not support Java Desktop API"); } + private static String getJarLocation(Class c) throws URISyntaxException, + UnsupportedEncodingException { + CodeSource codeSource = c.getProtectionDomain().getCodeSource(); + + String loc; + + if (codeSource.getLocation() != null) { + loc = codeSource.getLocation().toString(); + } else { + loc = c.getResource(c.getSimpleName() + ".class").toString(); + } + if (loc.startsWith("jar:")) { + loc = loc.substring(loc.indexOf(":") + 1, loc.indexOf("!")); + loc = URLDecoder.decode(loc, "UTF-8"); + } + return loc; + } + private void initStart() { try { status.info(StatusNotifier.MESSAGE_START); diff --git a/bkucommon/src/main/java/at/gv/egiz/bku/binding/HTTPBindingProcessorImpl.java b/bkucommon/src/main/java/at/gv/egiz/bku/binding/HTTPBindingProcessorImpl.java index fcaa1be6..98218e52 100644 --- a/bkucommon/src/main/java/at/gv/egiz/bku/binding/HTTPBindingProcessorImpl.java +++ b/bkucommon/src/main/java/at/gv/egiz/bku/binding/HTTPBindingProcessorImpl.java @@ -143,7 +143,7 @@ public class HTTPBindingProcessorImpl extends AbstractBindingProcessor implement String signatureLayout = configuration .getString(ConfigurationFactoryBean.SIGNATURE_LAYOUT_PROPERTY); - if (getUseXAdES14() && signatureLayout.equals("1.0")) + if (getUseXAdES14() && "1.0".equals(signatureLayout)) signatureLayout = "1.1"; //bump SignatureLayout version to prevent PDF-AS from generating invalid signatures return signatureLayout; diff --git a/bkucommon/src/main/java/at/gv/egiz/bku/spring/ConfigurationFactoryBean.java b/bkucommon/src/main/java/at/gv/egiz/bku/spring/ConfigurationFactoryBean.java index ce318876..47e1320a 100644 --- a/bkucommon/src/main/java/at/gv/egiz/bku/spring/ConfigurationFactoryBean.java +++ b/bkucommon/src/main/java/at/gv/egiz/bku/spring/ConfigurationFactoryBean.java @@ -46,6 +46,7 @@ import org.springframework.core.io.ResourceLoader; import at.gv.egiz.bku.conf.MoccaConfigurationException; import at.gv.egiz.bku.slcommands.impl.CreateXMLSignatureCommandImpl; +import at.gv.egiz.bku.utils.JarLocation; /** * This is a {@link FactoryBean} for the creation of a {@link Configuration}. @@ -140,10 +141,10 @@ public class ConfigurationFactoryBean implements FactoryBean, ResourceLoaderAwar // signature layout try { - String classContainer = CreateXMLSignatureCommandImpl.class.getProtectionDomain() - .getCodeSource().getLocation().toString(); + String classContainer = JarLocation.get(CreateXMLSignatureCommandImpl.class); URL manifestUrl = new URL("jar:" + classContainer + "!/META-INF/MANIFEST.MF"); + log.debug(manifestUrl.toString()); Manifest manifest = new Manifest(manifestUrl.openStream()); Attributes attributes = manifest.getMainAttributes(); String signatureLayout = attributes.getValue("SignatureLayout"); @@ -153,10 +154,8 @@ public class ConfigurationFactoryBean implements FactoryBean, ResourceLoaderAwar } catch (Exception e) { log.warn("Failed to get signature layout from manifest.", e); } - - + return new MapConfiguration(map); - } @Override -- cgit v1.2.3