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: jones $'
|
7
|
* '$Date: 2003-08-11 12:19:04 -0700 (Mon, 11 Aug 2003) $'
|
8
|
* '$Revision: 1783 $'
|
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
|
import java.io.InputStream;
|
28
|
import java.io.InputStreamReader;
|
29
|
import java.io.StringWriter;
|
30
|
import java.io.Reader;
|
31
|
import java.net.URL;
|
32
|
import java.util.Properties;
|
33
|
|
34
|
import edu.ucsb.nceas.utilities.HttpMessage;
|
35
|
|
36
|
/**
|
37
|
* This interface provides methods for initializing and logging in to a
|
38
|
* Metacat server, and then querying, reading, transforming, inserting,
|
39
|
* updating and deleting documents from that server.
|
40
|
*/
|
41
|
public class MetacatClient implements Metacat
|
42
|
{
|
43
|
/** The URL string for the metacat server */
|
44
|
private String metacatUrl;
|
45
|
|
46
|
/**
|
47
|
* Constructor to create a new instance. Protected because instances
|
48
|
* should only be created by the factory MetacatFactory.
|
49
|
*/
|
50
|
protected MetacatClient()
|
51
|
{
|
52
|
}
|
53
|
|
54
|
/**
|
55
|
* Method used to log in to a metacat server. Implementations will need
|
56
|
* to cache a cookie value to make the session persistent. Each time a
|
57
|
* call is made to one of the other methods (e.g., read), the cookie will
|
58
|
* need to be passed back to the metacat server along with the request.
|
59
|
*
|
60
|
* @param username the username of the user, like an LDAP DN
|
61
|
* @param password the password for that user for authentication
|
62
|
* @throws MetacatAuthException when the username/password could
|
63
|
* not be authenticated
|
64
|
*/
|
65
|
public void login(String username, String password)
|
66
|
throws MetacatAuthException, MetacatInaccessibleException
|
67
|
{
|
68
|
Properties prop = new Properties();
|
69
|
prop.put("action", "login");
|
70
|
prop.put("qformat", "xml");
|
71
|
prop.put("username", username);
|
72
|
prop.put("password", password);
|
73
|
|
74
|
String response = null;
|
75
|
try {
|
76
|
response = sendDataForString(prop);
|
77
|
} catch (Exception e) {
|
78
|
throw new MetacatInaccessibleException(e.getMessage());
|
79
|
}
|
80
|
|
81
|
if (response.indexOf("<login>") == -1) {
|
82
|
HttpMessage.setCookie(null);
|
83
|
throw new MetacatAuthException(response);
|
84
|
}
|
85
|
}
|
86
|
|
87
|
/**
|
88
|
* Read an XML document from the metacat server session, accessed by docid,
|
89
|
* and returned as a Reader.
|
90
|
*
|
91
|
* @param docid the identifier of the document to be read
|
92
|
* @return a Reader for accessing the document
|
93
|
* @throws InsufficientKarmaException when the user has insufficent rights
|
94
|
* for the operation
|
95
|
*/
|
96
|
public Reader read(String docid) throws InsufficientKarmaException
|
97
|
{
|
98
|
Reader r = null;
|
99
|
return r;
|
100
|
}
|
101
|
|
102
|
/**
|
103
|
* Query the metacat document store with the given metacat-compatible
|
104
|
* query document, and return the result set as a Reader.
|
105
|
*
|
106
|
* @param xmlQuery a Reader for accessing the XML version of the query
|
107
|
* @return a Reader for accessing the result set
|
108
|
*/
|
109
|
public Reader query(Reader xmlQuery)
|
110
|
{
|
111
|
Reader r = null;
|
112
|
return r;
|
113
|
}
|
114
|
|
115
|
/**
|
116
|
* Insert an XML document into the repository.
|
117
|
*
|
118
|
* @param docid the docid to insert the document
|
119
|
* @param xmlDocument a Reader for accessing the XML document to be inserted
|
120
|
* @param schema a Reader for accessing the DTD or XML Schema for
|
121
|
* the document
|
122
|
* @throws InsufficientKarmaException when the user has insufficent rights
|
123
|
* for the operation
|
124
|
*/
|
125
|
public void insert(String docid, Reader xmlDocument, Reader schema)
|
126
|
throws InsufficientKarmaException
|
127
|
{
|
128
|
}
|
129
|
|
130
|
/**
|
131
|
* Update an XML document in the repository.
|
132
|
*
|
133
|
* @param docid the docid to update
|
134
|
* @param xmlDocument a Reader for accessing the XML text to be updated
|
135
|
* @param schema a Reader for accessing the DTD or XML Schema for
|
136
|
* the document
|
137
|
* @throws InsufficientKarmaException when the user has insufficent rights
|
138
|
* for the operation
|
139
|
*/
|
140
|
public void update(String docid, Reader xmlDocument, Reader schema)
|
141
|
throws InsufficientKarmaException
|
142
|
{
|
143
|
}
|
144
|
|
145
|
/**
|
146
|
* Delete an XML document in the repository.
|
147
|
*
|
148
|
* @param docid the docid to delete
|
149
|
* @throws InsufficientKarmaException when the user has insufficent rights
|
150
|
* for the operation
|
151
|
*/
|
152
|
public void delete(String docid)
|
153
|
throws InsufficientKarmaException
|
154
|
{
|
155
|
}
|
156
|
|
157
|
/**
|
158
|
* When the MetacatFactory creates an instance it needs to set the
|
159
|
* MetacatUrl to which connections should be made.
|
160
|
*
|
161
|
* @param metacatUrl the URL for the metacat server
|
162
|
*/
|
163
|
public void setMetacatUrl(String metacatUrl)
|
164
|
{
|
165
|
this.metacatUrl = metacatUrl;
|
166
|
}
|
167
|
|
168
|
/************************************************************************
|
169
|
* PRIVATE METHODS
|
170
|
************************************************************************/
|
171
|
|
172
|
/**
|
173
|
* Send a request to metacat.
|
174
|
*
|
175
|
* @param prop the properties to be URL encoded and sent
|
176
|
*/
|
177
|
synchronized private InputStream sendDataOnce(Properties prop)
|
178
|
throws Exception
|
179
|
{
|
180
|
InputStream returnStream = null;
|
181
|
URL url = new URL(metacatUrl);
|
182
|
HttpMessage msg = new HttpMessage(url);
|
183
|
returnStream = msg.sendPostData(prop);
|
184
|
return returnStream;
|
185
|
}
|
186
|
|
187
|
/**
|
188
|
* Send a request to Metacat
|
189
|
*
|
190
|
* @param prop the properties to be sent to Metacat
|
191
|
* @return InputStream as returned by Metacat
|
192
|
*/
|
193
|
synchronized private InputStream sendData(Properties prop) throws Exception
|
194
|
{
|
195
|
InputStream returnStream = null;
|
196
|
|
197
|
/*
|
198
|
Note: The reason that there are three try statements all executing
|
199
|
the same code is that there is a problem with the initial connection
|
200
|
using the HTTPClient protocol handler. These try statements make
|
201
|
sure that a connection is made because it gives each connection a
|
202
|
2nd and 3rd chance to work before throwing an error.
|
203
|
THIS IS A TOTAL HACK. THIS NEEDS TO BE LOOKED INTO AFTER THE BETA1
|
204
|
RELEASE OF MORPHO!!! cwb (7/24/01)
|
205
|
*/
|
206
|
try {
|
207
|
return sendDataOnce(prop);
|
208
|
} catch (Exception e) {
|
209
|
try {
|
210
|
return sendDataOnce(prop);
|
211
|
} catch (Exception e2) {
|
212
|
try {
|
213
|
return sendDataOnce(prop);
|
214
|
} catch (Exception e3) {
|
215
|
System.err.println(
|
216
|
"Failed to send data to metacat 3 times.");
|
217
|
throw e3;
|
218
|
}
|
219
|
}
|
220
|
}
|
221
|
}
|
222
|
|
223
|
/**
|
224
|
* Send a request to Metacat
|
225
|
*
|
226
|
* @param prop the properties to be sent to Metacat
|
227
|
* @return a string as returned by Metacat
|
228
|
*/
|
229
|
synchronized private String sendDataForString(Properties prop)
|
230
|
throws Exception
|
231
|
{
|
232
|
String response = null;
|
233
|
|
234
|
try {
|
235
|
InputStreamReader returnStream =
|
236
|
new InputStreamReader(sendData(prop));
|
237
|
StringWriter sw = new StringWriter();
|
238
|
int len;
|
239
|
char[] characters = new char[512];
|
240
|
while ((len = returnStream.read(characters, 0, 512)) != -1) {
|
241
|
sw.write(characters, 0, len);
|
242
|
}
|
243
|
returnStream.close();
|
244
|
response = sw.toString();
|
245
|
sw.close();
|
246
|
} catch (Exception e) {
|
247
|
throw e;
|
248
|
}
|
249
|
return response;
|
250
|
}
|
251
|
}
|