changeset 1025:2996395e44c9

Improved the logging of incoming mapviewer call data. gnv/trunk@1279 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 29 Nov 2010 09:20:01 +0000
parents b45faa9315ce
children 12c3863b2d8a
files gnv/ChangeLog gnv/src/main/java/de/intevation/gnv/action/mapviewer/MapViewerCallAction.java gnv/src/main/java/de/intevation/gnv/util/CaptureInputStream.java
diffstat 3 files changed, 92 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/gnv/ChangeLog	Fri Nov 26 10:48:30 2010 +0000
+++ b/gnv/ChangeLog	Mon Nov 29 09:20:01 2010 +0000
@@ -1,3 +1,13 @@
+2010-11-29  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/gnv/util/CaptureInputStream.java: New. This
+	  class makes it possible to capture incoming data (InputStream) in a proper
+	  way.
+
+	* src/main/java/de/intevation/gnv/action/mapviewer/MapViewerCallAction.java:
+	  Improved the mechanism to print the incoming data to log by using the
+	  CaptureInputStream class.
+
 2010-11-26  Ingo Weinzierl <ingo@intevation.de>
 
 	  ISSUE #323
--- a/gnv/src/main/java/de/intevation/gnv/action/mapviewer/MapViewerCallAction.java	Fri Nov 26 10:48:30 2010 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/action/mapviewer/MapViewerCallAction.java	Mon Nov 29 09:20:01 2010 +0000
@@ -31,6 +31,7 @@
 import de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClientFactory;
 import de.intevation.gnv.artifactdatabase.objects.ArtifactObject;
 import de.intevation.gnv.artifactdatabase.objects.map.MapService;
+import de.intevation.gnv.util.CaptureInputStream;
 
 /**
  * This class provides the businesslogic for handling an 
@@ -73,21 +74,19 @@
         }
 
         if (inputStream != null){
-
-            ByteArrayOutputStream out = new ByteArrayOutputStream();
-            byte[] b = new byte[4096];
-            int i = -1;
-            while ((i = inputStream.read(b)) > 0) {
-                out.write(b, 0, i);
-            }
-            out.close();
-            inputStream.reset();
-
-            log.debug("REQUEST DOCUMENT: " + out.toString());
+            CaptureInputStream capture = log.isDebugEnabled()
+                ? new CaptureInputStream(inputStream)
+                : null;
 
             try {
-                ExternalCallParser ecp = new XMLExternalCallParser(inputStream);
+                ExternalCallParser ecp = new XMLExternalCallParser(capture);
                 ecp.parse();
+
+                // print incoming data to log
+                if (capture != null) {
+                    log.debug("REQUEST DOCUMENT: " + capture.copyToString());
+                }
+
                 String geometry = ecp.getGeometry();
                 String srs = ecp.getSRS();
                 Collection<MapService> mapServices = ecp.getMapServices();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/util/CaptureInputStream.java	Mon Nov 29 09:20:01 2010 +0000
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2010 by Intevation GmbH
+ *
+ * This program is free software under the LGPL (>=v2.1)
+ * Read the file LGPL.txt coming with the software for details
+ * or visit http://www.gnu.org/licenses/ if it does not exist.
+ */
+
+package de.intevation.gnv.util;
+
+import java.io.FilterInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+
+/**
+ * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha Teichmann</a>
+ */
+public class CaptureInputStream
+extends      FilterInputStream
+{
+    protected ByteArrayOutputStream copy;
+
+    public CaptureInputStream(InputStream in) {
+        super(in);
+        copy = new ByteArrayOutputStream(10*1024);
+    }
+
+    public int read() throws IOException {
+        int x = in.read();
+        if (x != -1) {
+            copy.write(x);
+        }
+        return x;
+    }
+
+    public int read(byte [] b, int off, int len) throws IOException {
+        int r = in.read(b, off, len);
+        if (r > 0) {
+            copy.write(b, off, r);
+        }
+        return r;
+    }
+
+    public long skip(long n) throws IOException {
+        long m = in.skip(n);
+        for (long i = m; i > 0L; --i) { // simulate gab
+            copy.write(0);
+        }
+        return m;
+    }
+
+    public String copyToString() {
+        return copy.toString();
+    }
+
+    public String copyToString(String charsetName)
+    throws UnsupportedEncodingException
+    {
+        return copy.toString(charsetName);
+    }
+
+    public byte [] toByteArray() {
+        return copy.toByteArray();
+    }
+
+    public void clear() {
+        copy.reset();
+    }
+}

http://dive4elements.wald.intevation.org