[Osaas-commits] r22 - in trunk: . client client/java client/java/src client/java/src/de client/java/src/de/intevation client/java/src/de/intevation/osaas
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Wed Aug 29 12:41:35 CEST 2007
Author: bh
Date: 2007-08-29 12:41:35 +0200 (Wed, 29 Aug 2007)
New Revision: 22
Added:
trunk/client/java/
trunk/client/java/README.txt
trunk/client/java/build.xml
trunk/client/java/src/
trunk/client/java/src/de/
trunk/client/java/src/de/intevation/
trunk/client/java/src/de/intevation/osaas/
trunk/client/java/src/de/intevation/osaas/Base64Encoder.java
trunk/client/java/src/de/intevation/osaas/OSAASClient.java
Modified:
trunk/ChangeLog
Log:
* client/java/src/de/intevation/osaas/OSAASClient.java,
client/java/src/de/intevation/osaas/Base64Encoder.java,
client/java/build.xml, client/java/README.txt: New. Java client
implementation
Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog 2007-08-28 18:43:34 UTC (rev 21)
+++ trunk/ChangeLog 2007-08-29 10:41:35 UTC (rev 22)
@@ -1,5 +1,12 @@
2007-08-28 Bernhard Herzog <bh at intevation.de>
+ * client/java/src/de/intevation/osaas/OSAASClient.java,
+ client/java/src/de/intevation/osaas/Base64Encoder.java,
+ client/java/build.xml, client/java/README.txt: New. Java client
+ implementation
+
+2007-08-28 Bernhard Herzog <bh at intevation.de>
+
* server/test/test_dbbackend.py (TestDBBackend.test_create_tables):
Adapt test case
Added: trunk/client/java/README.txt
===================================================================
--- trunk/client/java/README.txt 2007-08-28 18:43:34 UTC (rev 21)
+++ trunk/client/java/README.txt 2007-08-29 10:41:35 UTC (rev 22)
@@ -0,0 +1,15 @@
+README for the OSAAS Java client
+================================
+
+This a small library for Java programs that want to send data to the
+OSAAS server.
+
+
+Building the library
+--------------------
+
+Simply invoke ant without parameters:
+
+ $ ant
+
+The default target builds a jar file in the dist/ subdirectory.
Property changes on: trunk/client/java/README.txt
___________________________________________________________________
Name: svn:keywords
+ Author Date Id Revision
Name: svn:eol-style
+ native
Added: trunk/client/java/build.xml
===================================================================
--- trunk/client/java/build.xml 2007-08-28 18:43:34 UTC (rev 21)
+++ trunk/client/java/build.xml 2007-08-29 10:41:35 UTC (rev 22)
@@ -0,0 +1,50 @@
+<project name="osaas-java-client" default="dist-bin" basedir=".">
+ <description>
+ Build file for OSAAS java client package
+ </description>
+
+ <property name="src" location="src"/>
+ <property name="build" location="build"/>
+ <property name="dist" location="dist"/>
+ <property name="doc" location="doc"/>
+
+ <target name="init">
+ <mkdir dir="${build}"/>
+ <mkdir dir="${dist}"/>
+ </target>
+
+ <target name="compile" depends="init"
+ description="compile the source">
+ <javac srcdir="${src}" destdir="${build}" debug="true"/>
+ </target>
+
+ <target name="dist-bin" depends="compile"
+ description="generate the jar-file">
+ <jar jarfile="${dist}/osaas-client.jar" basedir="${build}"/>
+ </target>
+
+ <target name="dist-src" depends="init"
+ description="generate the source tarball">
+ <tar destfile="${dist}/osaas-client.tar.bz2" compression="bzip2">
+ <tarfileset dir="." prefix="osaas-client"
+ includes="build.xml,LGPL.txt"/>
+ <tarfileset dir="${src}" prefix="osaas-client/src" />
+ </tar>
+ </target>
+
+ <target name="dist" depends="dist-src,dist-bin"/>
+
+ <target name="javadoc" depends="init"
+ description="build the javadoc API docs">
+ <mkdir dir="${doc}" />
+ <javadoc packagenames="de.*" sourcepath="${src}" destdir="${doc}" />
+ </target>
+
+ <target name="clean"
+ description="clean up" >
+ <delete dir="${build}"/>
+ <delete dir="${dist}"/>
+ <delete dir="${doc}"/>
+ </target>
+
+</project>
Added: trunk/client/java/src/de/intevation/osaas/Base64Encoder.java
===================================================================
--- trunk/client/java/src/de/intevation/osaas/Base64Encoder.java 2007-08-28 18:43:34 UTC (rev 21)
+++ trunk/client/java/src/de/intevation/osaas/Base64Encoder.java 2007-08-29 10:41:35 UTC (rev 22)
@@ -0,0 +1,220 @@
+/*
+ * Copyright (c) 2006, 2007 by Intevation GmbH
+ *
+ * Author: Sascha L. Teichmann <teichmann at intevation.de>
+ *
+ * This program is free software under the LGPL (>=v2.1)
+ * Read the file LGPL coming with the software for details.
+ */
+
+package de.intevation.osaas;
+
+import java.io.OutputStream;
+import java.io.FilterOutputStream;
+import java.io.FilterInputStream;
+import java.io.IOException;
+
+import java.util.LinkedList;
+
+public class Base64Encoder {
+ public static final int LINE_LENGTH = 76;
+
+ protected static final int [] trans = new int[64];
+
+ private static final int MASK = 255 >> 2;
+
+ static {
+ for (int i = 0; i < 26; ++i)
+ trans[i] = 'A' + i;
+
+ for (int i = 26; i < 52; ++i)
+ trans[i] = 'a' + (i-26);
+
+ for (int i = 52; i < 62; ++i)
+ trans[i] = '0' + (i-52);
+
+ trans[62] = '+';
+ trans[63] = '/';
+ }
+
+ private Base64Encoder() {
+ }
+
+ public static class InputStream extends FilterInputStream {
+ protected int cpl;
+ protected int cc;
+ protected int state;
+ protected int back;
+
+ // XXX: Inefficient!
+ protected LinkedList queue;
+
+ public InputStream(java.io.InputStream in) {
+ this(in, LINE_LENGTH);
+ }
+
+ public InputStream(java.io.InputStream in, int cpl) {
+ super(in);
+ this.cpl = cpl;
+ queue = new LinkedList();
+ }
+
+ public int read() throws IOException {
+ if (!queueEmpty())
+ return out(dequeue());
+
+ int b, b0;
+
+ switch (state) {
+ case 0:
+ if ((b = in.read()) < 0) {
+ state = 3;
+ return out(b);
+ }
+ state = 1;
+ back = (b & 3) << 4;
+ return out(trans[(b >> 2) & MASK]);
+
+ case 1:
+ if ((b = in.read()) < 0) {
+ enqueue('=');
+ enqueue('=');
+ state = 3;
+ return out(trans[back]);
+ }
+ state = 2;
+ b0 = back;
+ back = (b & 0xf) << 2;
+ return out(trans[b0 | ((b >> 4) & 0xf)]);
+
+ case 2:
+ if ((b = in.read()) < 0) {
+ state = 3;
+ enqueue('=');
+ return(trans[back]);
+ }
+ state = 0;
+
+ enqueue(trans[b & MASK]);
+ return out(trans[back | ((b >> 6) & 3)]);
+
+ case 3:
+ state = -1;
+ return cc > 0 ? out('\n') : -1;
+ }
+
+ return -1;
+ }
+
+ public int read(byte [] buf, int ofs, int len) throws IOException {
+ if (state < 0) return -1;
+ if (len < 0) return 0;
+ int b;
+ int start = ofs;
+ int end = ofs+len;
+ while (ofs < end && (b = read()) != -1)
+ buf[ofs++] = (byte)b;
+ return ofs-start;
+ }
+
+ protected boolean queueEmpty() {
+ return queue.isEmpty();
+ }
+
+ protected int dequeue() {
+ return ((Integer)queue.removeFirst()).intValue();
+ }
+
+ protected void enqueue(int b) {
+ queue.addLast(new Integer(b));
+ }
+
+ protected int out(int b) {
+ if (cc++ >= cpl) {
+ cc = 0;
+ enqueue(b);
+ return '\n';
+ }
+ return b;
+ }
+
+ } // class InputStream
+
+ public static class OutputStream extends FilterOutputStream {
+ protected int state;
+ protected int back;
+ protected int cpl;
+ protected int cc;
+
+ public OutputStream(java.io.OutputStream parent) {
+ this(parent, LINE_LENGTH);
+ }
+
+ public OutputStream(java.io.OutputStream parent, int cpl) {
+ super(parent);
+ this.cpl = cpl;
+ }
+
+ protected final void out(int b) throws IOException {
+ out.write(b);
+ if (++cc >= cpl) {
+ cc = 0;
+ out.write('\n');
+ }
+ }
+
+ public void write(int b) throws IOException {
+ switch (state) {
+ case 0:
+ back = (b & 3) << 4;
+ out(trans[(b >> 2) & MASK]);
+ state = 1;
+ break;
+
+ case 1:
+ int nback = (b & 0xf) << 2;
+ out(trans[back | ((b >> 4) & 0xf)]);
+ back = nback;
+ state = 2;
+ break;
+
+ case 2:
+ out(trans[back | ((b >> 6) & 3)]);
+ out(trans[b & MASK]);
+ state = 0;
+ break;
+ }
+ }
+
+ public void flush() throws IOException {
+ out.flush();
+ }
+
+ public void close() throws IOException {
+ switch (state) {
+ case -2:
+ case -1:
+ return;
+
+ case 1:
+ out(trans[back]);
+ out('=');
+ out('=');
+ break;
+
+ case 2:
+ out(trans[back]);
+ out('=');
+ break;
+ }
+ if (cc > 0) {
+ cc = 0;
+ out.write('\n');
+ }
+ state = -2;
+ flush();
+ out.close();
+ }
+ } // class OutputStream
+}
+// end of file
Added: trunk/client/java/src/de/intevation/osaas/OSAASClient.java
===================================================================
--- trunk/client/java/src/de/intevation/osaas/OSAASClient.java 2007-08-28 18:43:34 UTC (rev 21)
+++ trunk/client/java/src/de/intevation/osaas/OSAASClient.java 2007-08-29 10:41:35 UTC (rev 22)
@@ -0,0 +1,131 @@
+/*
+ * Copyright (c) 2007 by Intevation GmbH
+ *
+ * Author: Bernhard Herzog <bh at intevation.de>
+ *
+ * This program is free software under the LGPL (>=v2.1)
+ * Read the file LGPL coming with the software for details.
+ */
+
+package de.intevation.osaas;
+
+import java.io.BufferedWriter;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.UnsupportedEncodingException;
+import java.io.Writer;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.util.GregorianCalendar;
+
+
+public class OSAASClient {
+
+ private URL osaasURL;
+ private String basicauth;
+ private Writer failLog;
+
+ public OSAASClient(URL url, String username, String password,
+ File failLogFile) throws Exception {
+ osaasURL = url;
+ basicauth = "Basic " + base64encode(username + ":" + password);
+
+ OutputStream out = new FileOutputStream(failLogFile, true);
+ failLog = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
+ }
+
+ private static String base64encode(String s) {
+ try {
+ ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+ OutputStream encoder = new Base64Encoder.OutputStream(bytes);
+ encoder.write(s.getBytes("UTF-8"));
+ encoder.close();
+ String encoded = bytes.toString("US-ASCII");
+ if (encoded.endsWith("\n")) {
+ encoded = encoded.substring(0, encoded.length() - 1);
+ }
+ return encoded;
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ private String getTimestamp() {
+ return String.format("%1$tFT%1$tTZ", new GregorianCalendar());
+ }
+
+ private void logFailedRequest(String request) {
+ try {
+ failLog.write(request);
+ failLog.write("\n");
+ failLog.flush();
+ }
+ catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private String buildRequest(String user, String queryString,
+ String wmsidextern, String wmsidintern) {
+ String[][] fields = {
+ {"user", user},
+ {"responsetime", getTimestamp()},
+ {"wmsidextern", wmsidextern},
+ {"wmsidintern", wmsidintern},
+ {"requeststring", queryString},
+ };
+ StringBuilder buf = new StringBuilder();
+
+ for (int i = 0; i < fields.length; i++) {
+ if (i > 0)
+ buf.append("&");
+ buf.append(fields[i][0] + "=");
+ try {
+ buf.append(URLEncoder.encode(fields[i][1], "UTF-8"));
+ }
+ catch (UnsupportedEncodingException e) {
+ // We shouldn't get here because UTF-8 is one of the
+ // standard encodings
+ e.printStackTrace();
+ }
+ }
+
+ return buf.toString();
+ }
+
+ public void sendRequest(String user, String queryString, String wmsidextern,
+ String wmsidintern) {
+ String request = buildRequest(user, queryString, wmsidextern,
+ wmsidintern);
+ int responseCode = 0;
+ try {
+ HttpURLConnection conn =
+ (HttpURLConnection)osaasURL.openConnection();
+ conn.setDoOutput(true);
+ conn.setRequestProperty("Authorization", basicauth);
+
+ OutputStreamWriter wr =
+ new OutputStreamWriter(conn.getOutputStream());
+
+
+ wr.write(request);
+ wr.close();
+
+ responseCode = conn.getResponseCode();
+ } catch (Exception e) {
+ System.out.println("Could not send OSAAS request: " + e);
+ }
+ finally {
+ if (responseCode < 200 || responseCode >= 300) {
+ logFailedRequest(request);
+ }
+ }
+ }
+}
More information about the Osaas-commits
mailing list