Project

General

Profile

« Previous | Next » 

Revision 1784

Added by Matt Jones over 21 years ago

Implemented the 'read' API call. Some more to go...

View differences:

test/edu/ucsb/nceas/metacattest/client/MetacatClientTest.java
26 26
package edu.ucsb.nceas.metacattest.client;
27 27

  
28 28
import edu.ucsb.nceas.metacat.client.*;
29
import edu.ucsb.nceas.utilities.IOUtil;
29 30

  
31
import java.io.FileReader;
32
import java.io.InputStreamReader;
33
import java.io.IOException;
34
import java.io.Reader;
35
import java.io.StringWriter;
36

  
30 37
import junit.framework.Test;
31 38
import junit.framework.TestCase;
32 39
import junit.framework.TestResult;
......
42 49
    private String username = "@mcuser@";
43 50
    private String password = "@mcpassword@";
44 51
    private String failpass = "uidfnkj43987yfdn";
52
    private String docid = "jones.204.22";
53
    private String testfile = "test/jones.204.22.xml";
54
    //private String docid = "Gramling.61.26";
55
    private String testdocument = "";
45 56
    
46 57
    private Metacat m;
47 58

  
......
61 72
    public void setUp()
62 73
    {
63 74
        try {
75
            FileReader fr = new FileReader(testfile);
76
            StringBuffer buf = IOUtil.getAsStringBuffer(fr, true);
77
            testdocument = buf.toString();
78
        } catch (IOException ioe) {
79
            fail("Can't read test data to run the test: " + testfile);
80
        }
81

  
82
        try {
64 83
            m = MetacatFactory.createMetacatConnection(metacatUrl);
65 84
        } catch (MetacatInaccessibleException mie) {
66 85
            fail("Metacat connection failed." + mie.getMessage());
......
83 102
        suite.addTest(new MetacatClientTest("initialize"));
84 103
        suite.addTest(new MetacatClientTest("invalidLogin"));
85 104
        suite.addTest(new MetacatClientTest("login"));
105
        suite.addTest(new MetacatClientTest("read"));
86 106
        return suite;
87 107
    }
88 108
  
......
117 137
    {
118 138
        // Try an invalid login
119 139
        try {
120
            System.err.println("Username: " + username);
121 140
            m.login(username, failpass);
122 141
            fail("Authorization should have failed.");
123 142
        } catch (MetacatAuthException mae) {
......
126 145
            fail("Metacat Inaccessible:\n" + mie.getMessage());
127 146
        }
128 147
    }
148

  
149
    /**
150
     * Test the read() function with a known document
151
     */
152
    public void read()
153
    {
154
        try {
155
            m.login(username, password);
156
            Reader r = m.read(docid);
157
            String doc = readerToString(r);
158
            System.err.println(doc);
159
            assertTrue(doc.equals(testdocument));
160

  
161
        } catch (MetacatAuthException mae) {
162
            fail("Authorization failed:\n" + mae.getMessage());
163
        } catch (MetacatInaccessibleException mie) {
164
            fail("Metacat Inaccessible:\n" + mie.getMessage());
165
        } catch (Exception e) {
166
            fail("General exception:\n" + e.getMessage());
167
        }
168
    }
169

  
170
/*
171
 * PRIVATE METHODS
172
 */
173

  
174
    /**
175
     * Utility method to convert a reader to a String.
176
     *
177
     * @param reader the Reader to be converted
178
     * @return a String representation fo the data read
179
     */
180
    private String readerToString(Reader reader) throws Exception
181
    {
182
        String response = null;
183

  
184
        try {
185
            StringWriter sw = new StringWriter();
186
            int len;
187
            char[] characters = new char[512];
188
            while ((len = reader.read(characters, 0, 512)) != -1) {
189
                sw.write(characters, 0, len);
190
            }
191
            response = sw.toString();
192
            sw.close();
193
            reader.close();
194
        } catch (Exception e) {
195
            throw e;
196
        }
197
        return response;
198
    }
129 199
}
src/edu/ucsb/nceas/metacat/client/MetacatClient.java
26 26

  
27 27
import java.io.InputStream;
28 28
import java.io.InputStreamReader;
29
import java.io.PushbackReader;
30
import java.io.IOException;
29 31
import java.io.StringWriter;
30 32
import java.io.Reader;
31 33
import java.net.URL;
......
92 94
     * @return a Reader for accessing the document
93 95
     * @throws InsufficientKarmaException when the user has insufficent rights 
94 96
     *                                    for the operation
97
     * @throws MetacatInaccessibleException when the metacat server can not be
98
     *                                    reached or does not respond
99
     * @throws MetacatException when the metacat server generates another error
95 100
     */
96
    public Reader read(String docid) throws InsufficientKarmaException
101
    public Reader read(String docid) throws InsufficientKarmaException,
102
        MetacatInaccessibleException, MetacatException
97 103
    {
98
        Reader r = null;
99
        return r;
104
        PushbackReader pbr = null;
105

  
106
        Properties prop = new Properties();
107
        prop.put("action", "read");
108
        prop.put("qformat", "xml");
109
        prop.put("docid", docid);
110

  
111
        InputStream response = null;
112
        try {
113
            response = sendData(prop);
114
        } catch (Exception e) {
115
            throw new MetacatInaccessibleException(e.getMessage());
116
        }
117
  
118
        pbr = new PushbackReader(new InputStreamReader(response), 512);
119
        try {
120
            char[] characters = new char[512];
121
            int len = pbr.read(characters, 0, 512);
122
            StringWriter sw = new StringWriter();
123
            sw.write(characters, 0, len);
124
            String message = sw.toString();
125
            sw.close();
126
            pbr.unread(characters, 0, len);
127

  
128
            if (message.indexOf("<error>") != -1) {
129
                if (message.indexOf("does not have permission") != -1) {
130
                    throw new InsufficientKarmaException(message);
131
                } else {
132
                    throw new MetacatException(message);
133
                }
134
            }
135
        } catch (IOException ioe) {
136
            throw new MetacatException(
137
                    "MetacatClient: Error converting Reader to String." 
138
                    + ioe.getMessage());
139
        }
140

  
141
        return pbr;
100 142
    }
101 143

  
102 144
    /**
src/edu/ucsb/nceas/metacat/client/MetacatException.java
1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2000 Regents of the University of California and the
4
 *             National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author$'
7
 *     '$Date$'
8
 * '$Revision$'
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
 */
24

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

  
27
/**
28
 * Exception thrown when an error occurs during a metacat operation.
29
 */
30
public class MetacatException extends Exception {
31

  
32
    /**
33
     * Create a new MetacatException.
34
     *
35
     * @param message The error or warning message.
36
     */
37
    public MetacatException(String message) {
38
        super(message);
39
    }
40
}
0 41

  
src/edu/ucsb/nceas/metacat/client/Metacat.java
55 55
     * @return a Reader for accessing the document
56 56
     * @throws InsufficientKarmaException when the user has insufficent rights 
57 57
     *                                    for the operation
58
     * @throws MetacatInaccessibleException when the metacat server can not be
59
     *                                    reached or does not respond
60
     * @throws MetacatException when the metacat server generates another error
58 61
     */
59
    public Reader read(String docid) throws InsufficientKarmaException;
62
    public Reader read(String docid) throws InsufficientKarmaException,
63
        MetacatInaccessibleException, MetacatException;
60 64

  
61 65
    /**
62 66
     * Query the metacat document store with the given metacat-compatible 

Also available in: Unified diff