Project

General

Profile

« Previous | Next » 

Revision 5066

return InputStream instead of Reader in the MetacatClient read() and readInlineData() methods. Can now handle binary data
http://bugzilla.ecoinformatics.org/show_bug.cgi?id=4432

View differences:

src/edu/ucsb/nceas/metacat/client/MetacatClient.java
24 24

  
25 25
package edu.ucsb.nceas.metacat.client;
26 26

  
27
import java.io.BufferedInputStream;
28
import java.io.BufferedOutputStream;
29
import java.io.FileOutputStream;
27 30
import java.io.InputStream;
28 31
import java.io.InputStreamReader;
29
import java.io.PushbackReader;
30 32
import java.io.StringReader;
31 33
import java.io.IOException;
32 34
import java.io.StringWriter;
......
44 46
import edu.ucsb.nceas.utilities.XMLUtilities;
45 47
import java.io.File;
46 48

  
49

  
47 50
/**
48 51
 *  This interface provides methods for initializing and logging in to a
49 52
 *  Metacat server, and then querying, reading, transforming, inserting,
......
56 59
    /** The session identifier for the session */
57 60
    private String sessionId;
58 61
    
62
    public static void main(String[] args) {
63
    	try {
64
    		MetacatClient mc = (MetacatClient) MetacatFactory.createMetacatConnection("http://fred.msi.ucsb.edu:8080/knb/metacat");
65
    		//InputStream r = mc.read("zip.1.1");
66
    		InputStream r = mc.read("jlee.2808.2");
67
    		
68
    		//FileOutputStream fos = new FileOutputStream("test.zip");
69
    		FileOutputStream fos = new FileOutputStream("jlee.xml");
70
            BufferedOutputStream bfos = new BufferedOutputStream(fos);
71

  
72
            int c = r.read();
73
            while(c != -1)
74
            {
75
              bfos.write(c);
76
              c = r.read();
77
            }
78
            bfos.flush();
79
            bfos.close();
80
            fos.flush();
81
            fos.close();
82
    	} catch (Exception e) {
83
    		e.printStackTrace();
84
    	}
85
    }
86
    
59 87
    /**
60 88
     * Constructor to create a new instance. Protected because instances
61 89
     * should only be created by the factory MetacatFactory.
......
193 221
        return response;
194 222
    }
195 223
    
196
    /**
224
   
225

  
226
	/**
197 227
     * Read an XML document from the metacat server session, accessed by docid,
198 228
     * and returned as a Reader.
199 229
     *
......
205 235
     *                                    reached or does not respond
206 236
     * @throws MetacatException when the metacat server generates another error
207 237
     */
208
    public Reader read(String docid) throws InsufficientKarmaException,
238
    public InputStream read(String docid) throws InsufficientKarmaException,
209 239
            MetacatInaccessibleException, MetacatException, DocumentNotFoundException {
210
        PushbackReader pbr = null;
240
    	Reader r = null;
211 241
        
212 242
        Properties prop = new Properties();
213 243
        prop.put("action", "read");
......
219 249
        } catch (Exception e) {
220 250
            throw new MetacatInaccessibleException(e.getMessage());
221 251
        }
222
        pbr = new PushbackReader(new InputStreamReader(response), 512);
252
        BufferedInputStream bis = new BufferedInputStream(response);
253
        r = new InputStreamReader(bis);
223 254
        try {
255
        	bis.mark(512);
224 256
            char[] characters = new char[512];
225
            int len = pbr.read(characters, 0, 512);
257
            int len = r.read(characters, 0, 512);
226 258
            StringWriter sw = new StringWriter();
227 259
            sw.write(characters, 0, len);
228 260
            String message = sw.toString();
229 261
            sw.close();
230
            pbr.unread(characters, 0, len);
262
            bis.reset();
231 263
            if (message.indexOf("<error>") != -1) {
232 264
                if (message.indexOf("does not have permission") != -1) {
233 265
                    throw new InsufficientKarmaException(message);
......
242 274
                    "MetacatClient: Error converting Reader to String."
243 275
                    + ioe.getMessage());
244 276
        }
245
        return pbr;
277
        return bis;
246 278
    }
247 279
    
248 280
    
......
258 290
     *                                    reached or does not respond
259 291
     * @throws MetacatException when the metacat server generates another error
260 292
     */
261
    public Reader readInlineData(String inlinedataid)
293
    public InputStream readInlineData(String inlinedataid)
262 294
    throws InsufficientKarmaException,
263 295
            MetacatInaccessibleException, MetacatException {
264
        PushbackReader pbr = null;
296
        Reader r = null;
265 297
        
266 298
        Properties prop = new Properties();
267 299
        prop.put("action", "readinlinedata");
......
273 305
        } catch (Exception e) {
274 306
            throw new MetacatInaccessibleException(e.getMessage());
275 307
        }
276
        
277
        pbr = new PushbackReader(new InputStreamReader(response), 512);
308
        BufferedInputStream bis = new BufferedInputStream(response);
309
        r = new InputStreamReader(bis);
278 310
        try {
311
        	bis.mark(512);
279 312
            char[] characters = new char[512];
280
            int len = pbr.read(characters, 0, 512);
313
            int len = r.read(characters, 0, 512);
281 314
            StringWriter sw = new StringWriter();
282 315
            sw.write(characters, 0, len);
283 316
            String message = sw.toString();
284 317
            sw.close();
285
            pbr.unread(characters, 0, len);
286
            
318
            bis.reset();
287 319
            if (message.indexOf("<error>") != -1) {
288 320
                if (message.indexOf("does not have permission") != -1) {
289 321
                    throw new InsufficientKarmaException(message);
......
297 329
                    + ioe.getMessage());
298 330
        }
299 331
        
300
        return pbr;
332
        return bis;
301 333
    }
302 334
    
303 335
    /**
src/edu/ucsb/nceas/metacat/client/Metacat.java
75 75
     *                                    reached or does not respond
76 76
     * @throws MetacatException when the metacat server generates another error
77 77
     */
78
    public Reader read(String docid) throws InsufficientKarmaException,
78
    public InputStream read(String docid) throws InsufficientKarmaException,
79 79
        MetacatInaccessibleException, DocumentNotFoundException, MetacatException;
80 80

  
81 81
    /**
......
90 90
     *                                    reached or does not respond
91 91
     * @throws MetacatException when the metacat server generates another error
92 92
     */
93
    public Reader readInlineData(String inlinedataid)
93
    public InputStream readInlineData(String inlinedataid)
94 94
        throws InsufficientKarmaException,
95 95
        MetacatInaccessibleException, MetacatException;
96 96

  
src/edu/ucsb/nceas/metacat/oaipmh/provider/server/catalog/MetacatCatalog.java
12 12

  
13 13
import java.io.File;
14 14
import java.io.IOException;
15
import java.io.InputStreamReader;
15 16
import java.io.Reader;
16 17
import java.sql.Connection;
17 18
import java.sql.DriverManager;
......
387 388
      try {
388 389
        /* Perform a Metacat read operation on this docid */
389 390
        Metacat metacat = MetacatFactory.createMetacatConnection(metacatURL);
390
        Reader reader = metacat.read(docid);
391
        Reader reader = new InputStreamReader(metacat.read(docid));
391 392
        StringBuffer stringBuffer = IOUtil.getAsStringBuffer(reader, true);
392 393
        String emlString = stringBuffer.toString();
393 394
        recordMap.put("recordBytes", emlString);

Also available in: Unified diff