summaryrefslogtreecommitdiff
path: root/utils/src/main/java/at/gv/egiz/bku
diff options
context:
space:
mode:
Diffstat (limited to 'utils/src/main/java/at/gv/egiz/bku')
-rw-r--r--utils/src/main/java/at/gv/egiz/bku/local/ui/BKUControllerInterface.java23
-rw-r--r--utils/src/main/java/at/gv/egiz/bku/local/ui/TrayIconDialog.java202
-rw-r--r--utils/src/main/java/at/gv/egiz/bku/local/ui/TrayIconDialogInterface.java33
-rw-r--r--utils/src/main/java/at/gv/egiz/bku/utils/HexDump.java75
-rw-r--r--utils/src/main/java/at/gv/egiz/bku/utils/StreamUtil.java101
-rw-r--r--utils/src/main/java/at/gv/egiz/bku/utils/binding/Protocol.java41
-rw-r--r--utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/FormDataURLSupplier.java26
-rw-r--r--utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/HTTPURLProtocolHandlerImpl.java76
-rw-r--r--utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/SimpleFormDataContextImpl.java41
-rw-r--r--utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/StreamData.java61
-rw-r--r--utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URIResolverAdapter.java96
-rw-r--r--utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLDereferencer.java90
-rw-r--r--utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLDereferencerContext.java27
-rw-r--r--utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLProtocolHandler.java32
14 files changed, 924 insertions, 0 deletions
diff --git a/utils/src/main/java/at/gv/egiz/bku/local/ui/BKUControllerInterface.java b/utils/src/main/java/at/gv/egiz/bku/local/ui/BKUControllerInterface.java
new file mode 100644
index 00000000..5e191c79
--- /dev/null
+++ b/utils/src/main/java/at/gv/egiz/bku/local/ui/BKUControllerInterface.java
@@ -0,0 +1,23 @@
+/*
+* Copyright 2008 Federal Chancellery Austria and
+* Graz University of Technology
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package at.gv.egiz.bku.local.ui;
+
+public interface BKUControllerInterface {
+
+ public void shutDown();
+
+}
diff --git a/utils/src/main/java/at/gv/egiz/bku/local/ui/TrayIconDialog.java b/utils/src/main/java/at/gv/egiz/bku/local/ui/TrayIconDialog.java
new file mode 100644
index 00000000..5aa74d99
--- /dev/null
+++ b/utils/src/main/java/at/gv/egiz/bku/local/ui/TrayIconDialog.java
@@ -0,0 +1,202 @@
+/*
+* Copyright 2008 Federal Chancellery Austria and
+* Graz University of Technology
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package at.gv.egiz.bku.local.ui;
+
+import java.awt.AWTException;
+import java.awt.Image;
+import java.awt.MenuItem;
+import java.awt.PopupMenu;
+import java.awt.SystemTray;
+import java.awt.TrayIcon;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.io.IOException;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.ResourceBundle;
+
+import javax.imageio.ImageIO;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class TrayIconDialog implements TrayIconDialogInterface {
+
+ private static Log log = LogFactory.getLog(TrayIconDialog.class);
+ private static TrayIconDialogInterface instance;
+ private boolean isSupported;
+ private BKUControllerInterface shutDown;
+ private TrayIcon trayIcon = null;
+ private ResourceBundle resourceBundle = null;
+
+ private TrayIconDialog() {
+ }
+
+ private void displayTrayMsg(String captionID, String messageID,
+ TrayIcon.MessageType type) {
+ if ((isSupported) && (resourceBundle != null)) {
+ try {
+ trayIcon.displayMessage(resourceBundle.getString(captionID),
+ resourceBundle.getString(messageID), type);
+ } catch (Exception ex) {
+ log.error(ex);
+ }
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * at.gv.egiz.bku.local.ui.TrayIconDialogInterface#displayInfo(java.lang.String
+ * , java.lang.String)
+ */
+ public void displayInfo(String captionID, String messageID) {
+ displayTrayMsg(captionID, messageID, TrayIcon.MessageType.INFO);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * at.gv.egiz.bku.local.ui.TrayIconDialogInterface#displayWarning(java.lang
+ * .String, java.lang.String)
+ */
+ public void displayWarning(String captionID, String messageID) {
+ displayTrayMsg(captionID, messageID, TrayIcon.MessageType.WARNING);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * at.gv.egiz.bku.local.ui.TrayIconDialogInterface#displayError(java.lang.
+ * String, java.lang.String)
+ */
+ public void displayError(String captionID, String messageID) {
+ displayTrayMsg(captionID, messageID, TrayIcon.MessageType.ERROR);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * at.gv.egiz.bku.local.ui.TrayIconDialogInterface#init(java.util.ResourceBundle
+ * )
+ */
+ public void init(ResourceBundle resourceBundel) {
+ this.resourceBundle = resourceBundel;
+ isSupported = SystemTray.isSupported();
+ log.info("Trayicon supported: " + isSupported);
+ try {
+ if (isSupported) {
+ SystemTray tray = SystemTray.getSystemTray();
+ Image image = ImageIO.read(getClass().getClassLoader()
+ .getResourceAsStream("at/gv/egiz/bku/local/ui/chipperling.png"));
+ PopupMenu popup = new PopupMenu();
+ MenuItem exitItem = new MenuItem(resourceBundel
+ .getString("TrayMenu.Shutdown"));
+ popup.add(exitItem);
+ exitItem.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ log.info("Calling Shutdown");
+ if (shutDown != null) {
+ shutDown.shutDown();
+ }
+ }
+ });
+
+ trayIcon = new TrayIcon(image, "BKULogo", popup);
+ trayIcon.setImageAutoSize(true);
+ trayIcon.setToolTip(resourceBundel.getString("TrayMenu.Tooltip"));
+ try {
+ tray.add(trayIcon);
+ } catch (AWTException e) {
+ log.error("TrayIcon could not be added.", e);
+ isSupported = false;
+ }
+ }
+ } catch (IOException e) {
+ log.error(e);
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * at.gv.egiz.bku.local.ui.TrayIconDialogInterface#setShutdownHook(at.gv.egiz
+ * .bku.local.ui.BKUControllerInterface)
+ */
+ public void setShutdownHook(BKUControllerInterface shutDown) {
+ this.shutDown = shutDown;
+ }
+
+ @SuppressWarnings("unchecked")
+ public synchronized static TrayIconDialogInterface getInstance() {
+ ClassLoader cl = TrayIconDialog.class.getClassLoader();
+ if (instance == null) {
+ if (cl.toString().startsWith("sun.")) {
+ instance = new TrayIconDialog();
+ return instance;
+ }
+ ClassLoader parent = cl;
+ while (!parent.toString().startsWith("sun.")) {
+ parent = parent.getParent();
+ }
+ try {
+ Class<TrayIconDialog> otherClassInstance = (Class<TrayIconDialog>) parent
+ .loadClass(TrayIconDialog.class.getName());
+ Method getInstanceMethod = otherClassInstance.getDeclaredMethod(
+ "getInstance", new Class[] {});
+ Object otherSingleton = getInstanceMethod.invoke(null, new Object[] {});
+ instance = (TrayIconDialogInterface) Proxy.newProxyInstance(cl,
+ new Class[] { TrayIconDialogInterface.class },
+ new PassThroughProxyHandler(otherSingleton));
+ } catch (ClassNotFoundException ce) {
+ instance = new TrayIconDialog();
+ } catch (Exception e) {
+ log.error(e);
+ instance = new TrayIconDialog();
+ }
+ return instance;
+ }
+ return instance;
+ }
+
+ /**
+ *
+ * Only works for public methods
+ *
+ */
+ static class PassThroughProxyHandler implements InvocationHandler {
+ private final Object delegate;
+
+ public PassThroughProxyHandler(Object delegate) {
+ this.delegate = delegate;
+ }
+
+ public Object invoke(Object proxy, Method method, Object[] args)
+ throws Throwable {
+ Method delegateMethod = delegate.getClass().getMethod(method.getName(),
+ method.getParameterTypes());
+ return delegateMethod.invoke(delegate, args);
+ }
+ }
+
+}
diff --git a/utils/src/main/java/at/gv/egiz/bku/local/ui/TrayIconDialogInterface.java b/utils/src/main/java/at/gv/egiz/bku/local/ui/TrayIconDialogInterface.java
new file mode 100644
index 00000000..87c64102
--- /dev/null
+++ b/utils/src/main/java/at/gv/egiz/bku/local/ui/TrayIconDialogInterface.java
@@ -0,0 +1,33 @@
+/*
+* Copyright 2008 Federal Chancellery Austria and
+* Graz University of Technology
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package at.gv.egiz.bku.local.ui;
+
+import java.util.ResourceBundle;
+
+public interface TrayIconDialogInterface {
+
+ public abstract void displayInfo(String captionID, String messageID);
+
+ public abstract void displayWarning(String captionID, String messageID);
+
+ public abstract void displayError(String captionID, String messageID);
+
+ public abstract void init(ResourceBundle resourceBundel);
+
+ public abstract void setShutdownHook(BKUControllerInterface shutDown);
+
+} \ No newline at end of file
diff --git a/utils/src/main/java/at/gv/egiz/bku/utils/HexDump.java b/utils/src/main/java/at/gv/egiz/bku/utils/HexDump.java
new file mode 100644
index 00000000..88d49bad
--- /dev/null
+++ b/utils/src/main/java/at/gv/egiz/bku/utils/HexDump.java
@@ -0,0 +1,75 @@
+/*
+* Copyright 2008 Federal Chancellery Austria and
+* Graz University of Technology
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package at.gv.egiz.bku.utils;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringWriter;
+import java.io.Writer;
+
+public class HexDump {
+
+ public static String hexDump(InputStream is) throws IOException {
+ StringWriter writer = new StringWriter();
+ hexDump(is, writer);
+ return writer.toString();
+ }
+
+ public static void hexDump(InputStream is, Writer writer) throws IOException {
+ hexDump(is, writer, 16);
+ }
+
+ public static void hexDump(InputStream is, Writer writer, int chunkSize) throws IOException {
+
+ byte[] chunk = new byte[chunkSize];
+ long adr = 0;
+ for (int l; (l = is.read(chunk)) != -1;) {
+
+ writer.append(String.format("[%06x]", adr));
+ for (int i = 0; i < l; i++) {
+ if (i % 8 == 0) {
+ writer.append(" ");
+ } else {
+ writer.append(":");
+ }
+ writer.append(Integer.toHexString((chunk[i] & 240) >> 4));
+ writer.append(Integer.toHexString(chunk[i] & 15));
+ }
+
+ for (int i = 0; i < (chunkSize - l); i++) {
+ writer.append(" ");
+ }
+
+ for (int i = 0; i < l; i++) {
+ if (i % 8 == 0) {
+ writer.append(" ");
+ }
+ if (chunk[i] > 31 && chunk[i] < 127) {
+ writer.append((char) chunk[i]);
+ } else {
+ writer.append(".");
+ }
+ }
+
+ writer.append("\n");
+ adr += l;
+
+ }
+
+ }
+
+}
diff --git a/utils/src/main/java/at/gv/egiz/bku/utils/StreamUtil.java b/utils/src/main/java/at/gv/egiz/bku/utils/StreamUtil.java
new file mode 100644
index 00000000..a774df2b
--- /dev/null
+++ b/utils/src/main/java/at/gv/egiz/bku/utils/StreamUtil.java
@@ -0,0 +1,101 @@
+/*
+* Copyright 2008 Federal Chancellery Austria and
+* Graz University of Technology
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package at.gv.egiz.bku.utils;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.Writer;
+
+public class StreamUtil {
+
+ /**
+ * Copies data. None of the streams will be closed.
+ *
+ * @param is
+ * @param os
+ * @throws IOException
+ */
+ public static void copyStream(InputStream is, OutputStream os)
+ throws IOException {
+ copyStream(is, os, 1024);
+ }
+
+ /**
+ * Copies data. None of the streams will be closed.
+ *
+ * @param is
+ * @param os
+ * @throws IOException
+ */
+ public static void copyStream(InputStream is, OutputStream os, int bufferSize)
+ throws IOException {
+ byte[] buffer = new byte[bufferSize];
+ copyStream(is, os, buffer);
+ }
+
+ /**
+ * Copies data. None of the streams will be closed.
+ *
+ * @param is
+ * @param os
+ * @throws IOException
+ */
+ public static void copyStream(InputStream is, OutputStream os, byte[] buffer)
+ throws IOException {
+ for (int i = is.read(buffer); i > -1; i = is.read(buffer)) {
+ os.write(buffer, 0, i);
+ }
+ }
+
+ /**
+ * Copies data. None of the streams will be closed.
+ *
+ * @param is
+ * @param os
+ * @throws IOException
+ */
+ public static void copyStream(Reader is, Writer os)
+ throws IOException {
+ copyStream(is, os, 1024);
+ }
+
+ /**
+ * Copies data. None of the streams will be closed.
+ *
+ * @param is
+ * @param os
+ * @throws IOException
+ */
+ public static void copyStream(Reader is, Writer os, int bufferSize)
+ throws IOException {
+ char[] chars = new char[bufferSize];
+ for (int i = is.read(chars); i > -1; i = is.read(chars)) {
+ os.write(chars, 0, i);
+ }
+ }
+
+
+ public static String asString(InputStream is, String charset)
+ throws IOException {
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ copyStream(is, os);
+ return new String(os.toByteArray(), charset);
+ }
+}
diff --git a/utils/src/main/java/at/gv/egiz/bku/utils/binding/Protocol.java b/utils/src/main/java/at/gv/egiz/bku/utils/binding/Protocol.java
new file mode 100644
index 00000000..f0504697
--- /dev/null
+++ b/utils/src/main/java/at/gv/egiz/bku/utils/binding/Protocol.java
@@ -0,0 +1,41 @@
+/*
+* Copyright 2008 Federal Chancellery Austria and
+* Graz University of Technology
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package at.gv.egiz.bku.utils.binding;
+
+public enum Protocol {
+ HTTP("http"), HTTPS("https");
+
+ private String name;
+
+ Protocol(String s) {
+ name = s;
+ }
+
+ public String toString() {
+ return name;
+ }
+
+ public static Protocol fromString(String protocol) {
+ if (HTTP.toString().equalsIgnoreCase(protocol)) {
+ return HTTP;
+ }
+ if (HTTPS.toString().equalsIgnoreCase(protocol)) {
+ return HTTPS;
+ }
+ return null;
+ }
+}
diff --git a/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/FormDataURLSupplier.java b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/FormDataURLSupplier.java
new file mode 100644
index 00000000..7272f1bb
--- /dev/null
+++ b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/FormDataURLSupplier.java
@@ -0,0 +1,26 @@
+/*
+* Copyright 2008 Federal Chancellery Austria and
+* Graz University of Technology
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package at.gv.egiz.bku.utils.urldereferencer;
+
+import java.io.InputStream;
+
+public interface FormDataURLSupplier {
+ public static final String PROPERTY_KEY_NAME = "at.gv.egiz.bku.util.urldereferencer.FormDataURLSupplier";
+
+ public InputStream getFormData(String aParameterName);
+ public String getFormDataContentType(String aParameterName);
+} \ No newline at end of file
diff --git a/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/HTTPURLProtocolHandlerImpl.java b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/HTTPURLProtocolHandlerImpl.java
new file mode 100644
index 00000000..5cba2e1d
--- /dev/null
+++ b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/HTTPURLProtocolHandlerImpl.java
@@ -0,0 +1,76 @@
+/*
+* Copyright 2008 Federal Chancellery Austria and
+* Graz University of Technology
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package at.gv.egiz.bku.utils.urldereferencer;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.security.InvalidParameterException;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class HTTPURLProtocolHandlerImpl implements URLProtocolHandler {
+
+ private static Log log = LogFactory.getLog(HTTPURLProtocolHandlerImpl.class);
+
+ public final static String HTTP = "http";
+ public final static String HTTPS = "https";
+ public final static String FORMDATA = "formdata";
+ public final static String[] PROTOCOLS = { HTTP, HTTPS, FORMDATA };
+
+ public StreamData dereference(String aUrl, URLDereferencerContext aContext)
+ throws IOException {
+ String urlString = aUrl.toLowerCase().trim();
+ if (urlString.startsWith(FORMDATA)) {
+ log.debug("Requested to dereference a formdata url");
+ return dereferenceFormData(aUrl, aContext);
+ }
+
+ URL url = new URL(aUrl);
+ if ((!HTTP.equalsIgnoreCase(url.getProtocol()) && (!HTTPS
+ .equalsIgnoreCase(url.getProtocol())))) {
+ throw new InvalidParameterException("Url " + aUrl + " not supported");
+ }
+ return dereferenceHTTP(url);
+ }
+
+ protected StreamData dereferenceHTTP(URL url) throws IOException {
+ HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
+ return new StreamData(url.toString(), httpConn.getContentType(), httpConn
+ .getInputStream());
+ }
+
+ protected StreamData dereferenceFormData(String aUrl,
+ URLDereferencerContext aContext) throws IOException {
+ log.debug("Dereferencing formdata url: " + aUrl);
+ String[] parts = aUrl.split(":", 2);
+ FormDataURLSupplier supplier = (FormDataURLSupplier) aContext
+ .getProperty(FormDataURLSupplier.PROPERTY_KEY_NAME);
+ if (supplier == null) {
+ throw new NullPointerException(
+ "No FormdataUrlSupplier found in provided context");
+ }
+ String contentType = supplier.getFormDataContentType(parts[1]);
+ InputStream is = supplier.getFormData(parts[1]);
+ if (is != null) {
+ return new StreamData(aUrl, contentType, is);
+ }
+ return null;
+ }
+} \ No newline at end of file
diff --git a/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/SimpleFormDataContextImpl.java b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/SimpleFormDataContextImpl.java
new file mode 100644
index 00000000..e9da9c81
--- /dev/null
+++ b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/SimpleFormDataContextImpl.java
@@ -0,0 +1,41 @@
+/*
+* Copyright 2008 Federal Chancellery Austria and
+* Graz University of Technology
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package at.gv.egiz.bku.utils.urldereferencer;
+
+public class SimpleFormDataContextImpl implements URLDereferencerContext {
+ protected FormDataURLSupplier formdata;
+
+ /**
+ *
+ * @param formdata must not be null
+ */
+ public SimpleFormDataContextImpl(FormDataURLSupplier formdata) {
+ if (formdata == null) {
+ throw new NullPointerException("FormdataURLSupplier must not be null");
+ }
+ this.formdata = formdata;
+ }
+
+ @Override
+ public Object getProperty(Object key) {
+ if (key.equals(FormDataURLSupplier.PROPERTY_KEY_NAME)) {
+ return formdata;
+ }
+ return null;
+ }
+
+}
diff --git a/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/StreamData.java b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/StreamData.java
new file mode 100644
index 00000000..541c6878
--- /dev/null
+++ b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/StreamData.java
@@ -0,0 +1,61 @@
+/*
+* Copyright 2008 Federal Chancellery Austria and
+* Graz University of Technology
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package at.gv.egiz.bku.utils.urldereferencer;
+
+import java.io.InputStream;
+
+/**
+ * This class models the result when dereferencing an URL.
+ *
+ */
+public class StreamData {
+
+ protected InputStream inputStream;
+ protected String url;
+ protected String contentType;
+
+ /**
+ *
+ * @param url
+ * @param contentType
+ * @param stream must not be null
+ */
+ public StreamData(String url, String contentType, InputStream stream) {
+ if (stream == null) {
+ throw new NullPointerException("Parameter inputstream must not be null");
+ }
+ inputStream = stream;
+ this.contentType = contentType;
+ this.url = url;
+ }
+
+ public String getUrl() {
+ return url;
+ }
+
+ /**
+ *
+ * @return the returned stream must be closed
+ */
+ public InputStream getStream() {
+ return inputStream;
+ }
+
+ public String getContentType() {
+ return contentType;
+ }
+} \ No newline at end of file
diff --git a/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URIResolverAdapter.java b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URIResolverAdapter.java
new file mode 100644
index 00000000..2d11010e
--- /dev/null
+++ b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URIResolverAdapter.java
@@ -0,0 +1,96 @@
+/*
+* Copyright 2008 Federal Chancellery Austria and
+* Graz University of Technology
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package at.gv.egiz.bku.utils.urldereferencer;
+
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import javax.xml.transform.Source;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.URIResolver;
+import javax.xml.transform.stream.StreamSource;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * Adapter to make the Urldereferencer work as URIResolver for
+ * Stylesheettransforms.
+ *
+ * @author wbauer
+ *
+ */
+public class URIResolverAdapter implements URIResolver {
+
+ private static Log log = LogFactory.getLog(URIResolverAdapter.class);
+
+ private URLDereferencer urlDereferencer;
+ private URLDereferencerContext ctx;
+
+ /**
+ *
+ * @param deferecencer
+ * must not be null
+ * @param ctx may be null
+ */
+ public URIResolverAdapter(URLDereferencer deferecencer,
+ URLDereferencerContext ctx) {
+ if (deferecencer == null) {
+ throw new NullPointerException("Urlderefencer must not be set to null");
+ }
+ this.urlDereferencer = deferecencer;
+ this.ctx = ctx;
+ }
+
+ @Override
+ public Source resolve(String href, String base) throws TransformerException {
+ log.debug("Resolving href: "+href+" base: "+base);
+ try {
+ URI baseUri = null;
+ URI hrefUri = new URI(href);
+ if (base != null) {
+ baseUri = new URI(base);
+ }
+ URI abs;
+ if (baseUri != null) {
+ abs = baseUri.resolve(hrefUri);
+ } else {
+ abs = hrefUri;
+ }
+ if (!abs.isAbsolute()) {
+ throw new TransformerException("Only absolute URLs are supported");
+ }
+ return new StreamSource(urlDereferencer.dereference(abs.toString(), ctx)
+ .getStream());
+ } catch (URISyntaxException e) {
+ throw new TransformerException("Cannot resolve URI: base:" + base
+ + " href:" + href, e);
+ } catch (IOException iox) {
+ throw new TransformerException("Cannot resolve URI: base:" + base
+ + " href:" + href, iox);
+ }
+ }
+
+ public URLDereferencerContext getCtx() {
+ return ctx;
+ }
+
+ public void setCtx(URLDereferencerContext ctx) {
+ this.ctx = ctx;
+ }
+}
diff --git a/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLDereferencer.java b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLDereferencer.java
new file mode 100644
index 00000000..d747753f
--- /dev/null
+++ b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLDereferencer.java
@@ -0,0 +1,90 @@
+/*
+* Copyright 2008 Federal Chancellery Austria and
+* Graz University of Technology
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package at.gv.egiz.bku.utils.urldereferencer;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * Class used to dereference (external URLs).
+ *
+ * @author wbauer
+ *
+ */
+public class URLDereferencer {
+
+ private static Log log = LogFactory.getLog(URLDereferencer.class);
+
+ private static URLDereferencer instance = new URLDereferencer();
+
+ private Map<String, URLProtocolHandler> handlerMap = new HashMap<String, URLProtocolHandler>();
+
+ private URLDereferencer() {
+ registerHandlers();
+ }
+
+ /**
+ *
+ * @param aUrl
+ * must not be null
+ * @param aContext
+ * @throws MalformedURLException
+ * if the protocol is not supported
+ */
+ public StreamData dereference(String aUrl, URLDereferencerContext aContext)
+ throws IOException {
+ log.debug("Looking for handler for URL: " + aUrl);
+ int i = aUrl.indexOf(":");
+ if (i == -1) {
+ throw new MalformedURLException("Invalid url: " + aUrl);
+ }
+ String protocol = aUrl.substring(0, i).toLowerCase().trim();
+ URLProtocolHandler handler = handlerMap.get(protocol);
+ if (handler == null) {
+ throw new MalformedURLException("No handler for protocol: " + protocol
+ + " found");
+ }
+ return handler.dereference(aUrl, aContext);
+ }
+
+ /**
+ * Registers a handler for a protocol.
+ *
+ * @param aProtocol
+ * @param aHandler
+ * may be set to null to disable this protocol
+ */
+ public void registerHandler(String aProtocol, URLProtocolHandler aHandler) {
+ handlerMap.put(aProtocol.toLowerCase(), aHandler);
+ }
+
+ public static URLDereferencer getInstance() {
+ return instance;
+ }
+
+ protected void registerHandlers() {
+ URLProtocolHandler handler = new HTTPURLProtocolHandlerImpl();
+ for (String proto : HTTPURLProtocolHandlerImpl.PROTOCOLS) {
+ handlerMap.put(proto, handler);
+ }
+ }
+} \ No newline at end of file
diff --git a/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLDereferencerContext.java b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLDereferencerContext.java
new file mode 100644
index 00000000..6befd5b3
--- /dev/null
+++ b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLDereferencerContext.java
@@ -0,0 +1,27 @@
+/*
+* Copyright 2008 Federal Chancellery Austria and
+* Graz University of Technology
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package at.gv.egiz.bku.utils.urldereferencer;
+
+public interface URLDereferencerContext {
+
+ /**
+ *
+ * @param key
+ * @return may return null
+ */
+ public Object getProperty(Object key);
+} \ No newline at end of file
diff --git a/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLProtocolHandler.java b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLProtocolHandler.java
new file mode 100644
index 00000000..f584f450
--- /dev/null
+++ b/utils/src/main/java/at/gv/egiz/bku/utils/urldereferencer/URLProtocolHandler.java
@@ -0,0 +1,32 @@
+/*
+* Copyright 2008 Federal Chancellery Austria and
+* Graz University of Technology
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package at.gv.egiz.bku.utils.urldereferencer;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+
+
+public interface URLProtocolHandler {
+ /**
+ *
+ * @param aUrl
+ * @param aContext
+ * @return the streamdata of this url or null if the url cannot be resolved.
+ * @throws IOException
+ */
+ public StreamData dereference(String aUrl, URLDereferencerContext aContext) throws IOException;
+} \ No newline at end of file