Changeset 3012 for OAIHarvester


Ignore:
Timestamp:
06/10/13 22:02:39 (11 years ago)
Author:
oschonef
Message:
  • fix storing raw responses in ZIP file with very large results
File:
1 edited

Legend:

Unmodified
Added
Removed
  • OAIHarvester/trunk/OAIHarvester/src/test/java/eu/clarin/cmdi/oai/harvester/HarvesterTest.java

    r3011 r3012  
    11package eu.clarin.cmdi.oai.harvester;
    22
    3 import java.io.BufferedInputStream;
    43import java.io.File;
     4import java.io.FileInputStream;
    55import java.io.FileOutputStream;
     6import java.io.FilterInputStream;
    67import java.io.IOException;
    78import java.io.InputStream;
     9import java.io.OutputStream;
    810import java.util.Date;
    911import java.util.List;
    1012import java.util.Properties;
     13import java.util.zip.GZIPInputStream;
     14import java.util.zip.GZIPOutputStream;
    1115import java.util.zip.ZipEntry;
    1216import java.util.zip.ZipException;
     
    3842        private int reqNum;
    3943        private String prefix;
    40         private byte[] buffer = new byte[8192];
     44        private byte[] buffer = new byte[16 * 1024];
    4145
    4246        public MyHarvestHandler(File file, boolean saveHeader) {
     
    5559                if (output != null) {
    5660                    output.flush();
     61                    output.finish();
    5762                    output.close();
    5863                    output = null;
     
    194199        @Override
    195200        public InputStream wrap(InputStream stream) throws IOException {
    196             final int size = 16*1024*1024;
    197             BufferedInputStream in =
    198                 new BufferedInputStream(stream, size);
    199             in.mark(size);
    200             String x = Integer.toHexString(reqNum++);
    201             while (x.length() < 4) {
    202                 x = "0" + x;
    203             }
    204             final String filename = "request/0x" + x + ".xml";
    205             System.err.println("FN: " + filename);
    206             ZipEntry entry = new ZipEntry(filename);
    207             output.putNextEntry(entry);
    208             int r = -1;
    209             do {
    210                 r = in.read(buffer, 0, buffer.length - 1);
    211                 if (r > 0) {
    212                     output.write(buffer, 0, r);
    213                 }
    214             } while (r > 0);
    215             output.closeEntry();
    216             output.flush();
    217             in.reset();
    218             return in;
     201            /*
     202             * !!! WARNING WARNING WARNING WARNING WARNING WARNING WARNING !!!
     203             * Do not use this in production. This is just for demo purposes
     204             * to also store the raw requests in the ZIP file. This code is
     205             * highly inefficient!
     206             * For production, just "return stream" in this method. If you're
     207             * using the HarvestHandlerAdapter class as super class, just do
     208             * not override this method.
     209             * !!! WARNING WARNING WARNING WARNING WARNING WARNING WARNING !!!
     210             *
     211             */
     212            return new FilterInputStream(stream) {
     213                private final File tmpFile =
     214                        File.createTempFile("oai-harvester", null);
     215                private final OutputStream tmpStream =
     216                        new GZIPOutputStream(new FileOutputStream(tmpFile));
     217
     218                @Override
     219                public int read(byte[] b, int off, int len) throws IOException {
     220                    final int read = super.read(b, off, len);
     221                    if (read > 0) {
     222                        tmpStream.write(b, off, read);
     223                    }
     224                    return read;
     225                }
     226
     227                @Override
     228                public void close() throws IOException {
     229                    super.close();
     230
     231                    try {
     232                        tmpStream.close();
     233
     234                        final String filename =
     235                                String.format("request/0x%04X.xml", reqNum++);
     236                        System.err.println("FN: " + filename);
     237
     238                        InputStream in = null;
     239                        try {
     240                            ZipEntry entry = new ZipEntry(filename);
     241                            output.putNextEntry(entry);
     242
     243                            in = new GZIPInputStream(
     244                                    new FileInputStream(tmpFile));
     245                            for (;;) {
     246                                int read = in.read(buffer);
     247                                if (read <= 0) {
     248                                    break;
     249                                }
     250                                output.write(buffer, 0, read);
     251                            }
     252                            in.close();
     253                        } catch (IOException e) {
     254                            e.printStackTrace();
     255                        } finally {
     256                            if (in != null) {
     257                                try {
     258                                    in.close();
     259                                } catch (IOException e) {
     260                                }
     261                            }
     262                            output.closeEntry();
     263                        }
     264                    } finally {
     265                        tmpFile.delete();
     266                    }
     267                }
     268            };
    219269        }
    220270
Note: See TracChangeset for help on using the changeset viewer.