Project

General

Profile

« Previous | Next » 

Revision 1786

Added by Jing Tao over 21 years ago

Implement query method.

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.BufferedReader;
27 28
import java.io.InputStream;
28 29
import java.io.InputStreamReader;
29 30
import java.io.PushbackReader;
......
148 149
     * @param xmlQuery a Reader for accessing the XML version of the query
149 150
     * @return a Reader for accessing the result set
150 151
     */
151
    public Reader query(Reader xmlQuery)
152
    public Reader query(Reader xmlQuery) throws MetacatInaccessibleException,
153
                                                IOException
152 154
    {
153
        Reader r = null;
154
        return r;
155
        Reader reader = null;
156
        String query = null;
157
        try
158
        {
159
          query = readerToString(xmlQuery);
160
        }
161
        catch (IOException ioE)
162
        {
163
          throw ioE;
164
        }
165

  
166
        //set up properties
167
        Properties prop = new Properties();
168
        prop.put("action", "squery");
169
        prop.put("qformat", "xml");
170
        prop.put("query", query);
171
        
172
        InputStream response = null;
173
        try {
174
            response = sendData(prop);
175
        } catch (Exception e) {
176
            throw new MetacatInaccessibleException(e.getMessage());
177
        }
178
        reader = new InputStreamReader(response);
179
        return reader;
155 180
    }
156 181

  
157 182
    /**
......
290 315
        }
291 316
        return response;
292 317
    }
318
    /**
319
     * Utility method to convert a reader to a String.
320
     *
321
     * @param reader the Reader to be converted
322
     * @return a String representation fo the data read
323
     */
324
    private String readerToString(Reader reader) throws IOException
325
    {
326
        String response = null;
327

  
328
        try {
329
            StringWriter sw = new StringWriter();
330
            int len;
331
            char[] characters = new char[512];
332
            while ((len = reader.read(characters, 0, 512)) != -1) {
333
                sw.write(characters, 0, len);
334
            }
335
            response = sw.toString();
336
            sw.close();
337
            reader.close();
338
        } catch (IOException e) {
339
            throw e;
340
        }
341
        return response;
342
    }
343
    
293 344
}

Also available in: Unified diff