Project

General

Profile

« Previous | Next » 

Revision 1780

Added by Matt Jones over 21 years ago

Added a new subpackage (edu.ucsb.nceas.metacat.client) which is a client
access library for the metacat. There is an interface (Metacat.java) that
provides simple access methods for the main metacat functions. This is to
support clients like Morpho and others that want to programatically access
a metacat without worrying about network communication issues. The implementation is not complete. This checkin is principally for review of the
Metacat.java interface.

View differences:

src/edu/ucsb/nceas/metacat/client/MetacatAuthException.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 a Metacat server denies a login authentication
29
 * request.  Either the username or password are incorrect.
30
 */
31
public class MetacatAuthException extends Exception {
32

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

  
src/edu/ucsb/nceas/metacat/client/InsufficientKarmaException.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 because a Metacat server operation
29
 * requires higher access rights than the currently logged in user has.
30
 */
31
public class InsufficientKarmaException extends Exception {
32

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

  
src/edu/ucsb/nceas/metacat/client/MetacatInaccessibleException.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 a Metacat server is not accessible for the
29
 * operation requested.  This can be a network outage, or a configuration error.
30
 */
31
public class MetacatInaccessibleException extends Exception {
32

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

  
src/edu/ucsb/nceas/metacat/client/MetacatFactory.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
import java.io.Reader;
28

  
29
/**
30
 *  This class is a factory which allows the caller to create an instance of
31
 *  a Metacat object for accessing a metacat server.
32
 */
33
public class MetacatFactory
34
{
35
    private static final String metacatClientClass = 
36
         "edu.ucsb.nceas.metacat.client.MetacatClient";
37

  
38
    /**
39
     *  Create a new instance of a Metacat object of raccessing a server.
40
     *
41
     *  @param metacatUrl the url location of the metacat server
42
     *  @throws MetacatInaccessibleException when the metacat server can not
43
     *                    be reached
44
     */
45
    public static Metacat createMetacatConnection(String metacatUrl) 
46
           throws MetacatInaccessibleException
47
    {
48
        Metacat m = null;
49
        try {
50
            Class c = Class.forName(metacatClientClass);
51
            m = (Metacat)c.newInstance();
52
        } catch (InstantiationException e) {
53
            throw new MetacatInaccessibleException(e.getMessage());
54
        } catch (IllegalAccessException e) {
55
            throw new MetacatInaccessibleException(e.getMessage());
56
        } catch (ClassNotFoundException e) {
57
            throw new MetacatInaccessibleException(e.getMessage());
58
        }
59

  
60
        m.setMetacatUrl(metacatUrl);
61

  
62
        return m;
63
    }
64
}
0 65

  
src/edu/ucsb/nceas/metacat/client/MetacatClient.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
import java.io.InputStream;
28
import java.io.Reader;
29
import java.net.URL;
30
import java.util.Properties;
31

  
32
import edu.ucsb.nceas.utilities.HttpMessage;
33

  
34
/**
35
 *  This interface provides methods for initializing and logging in to a 
36
 *  Metacat server, and then querying, reading, transforming, inserting, 
37
 *  updating and deleting documents from that server.
38
 */
39
public class MetacatClient implements Metacat
40
{
41
    /** The URL string for the metacat server */
42
    private String metacatUrl;
43

  
44
    /**
45
     * Constructor to create a new instance. Protected because instances
46
     * should only be created by the factory MetacatFactory.
47
     */
48
    protected MetacatClient()
49
    {
50
    }
51

  
52
    /**
53
     *  Method used to log in to a metacat server. Implementations will need
54
     *  to cache a cookie value to make the session persistent.  Each time a
55
     *  call is made to one of the other methods (e.g., read), the cookie will
56
     *  need to be passed back to the metacat server along with the request.
57
     *
58
     *  @param username   the username of the user, like an LDAP DN
59
     *  @param password   the password for that user for authentication
60
     *  @throws MetacatAuthException when the username/password could
61
     *                    not be authenticated
62
     */
63
    public void login(String username, String password) 
64
           throws MetacatAuthException, MetacatInaccessibleException
65
    {
66
    }
67

  
68
    /**
69
     * Read an XML document from the metacat server session, accessed by docid,
70
     * and returned as a Reader.
71
     *
72
     * @param docid the identifier of the document to be read
73
     * @return a Reader for accessing the document
74
     * @throws InsufficientKarmaException when the user has insufficent rights 
75
     *                                    for the operation
76
     */
77
    public Reader read(String docid) throws InsufficientKarmaException
78
    {
79
        Reader r = null;
80
        return r;
81
    }
82

  
83
    /**
84
     * Query the metacat document store with the given metacat-compatible 
85
     * query document, and return the result set as a Reader.
86
     *
87
     * @param xmlQuery a Reader for accessing the XML version of the query
88
     * @return a Reader for accessing the result set
89
     */
90
    public Reader query(Reader xmlQuery)
91
    {
92
        Reader r = null;
93
        return r;
94
    }
95

  
96
    /**
97
     * Insert an XML document into the repository.
98
     *
99
     * @param docid the docid to insert the document
100
     * @param xmlDocument a Reader for accessing the XML document to be inserted
101
     * @param schema a Reader for accessing the DTD or XML Schema for 
102
     *               the document
103
     * @throws InsufficientKarmaException when the user has insufficent rights 
104
     *                                    for the operation
105
     */
106
    public void insert(String docid, Reader xmlDocument, Reader schema)
107
        throws InsufficientKarmaException
108
    {
109
    }
110

  
111
    /**
112
     * Update an XML document in the repository.
113
     *
114
     * @param docid the docid to update
115
     * @param xmlDocument a Reader for accessing the XML text to be updated
116
     * @param schema a Reader for accessing the DTD or XML Schema for 
117
     *               the document
118
     * @throws InsufficientKarmaException when the user has insufficent rights 
119
     *                                    for the operation
120
     */
121
    public void update(String docid, Reader xmlDocument, Reader schema)
122
        throws InsufficientKarmaException
123
    {
124
    }
125

  
126
    /**
127
     * Delete an XML document in the repository.
128
     *
129
     * @param docid the docid to delete
130
     * @throws InsufficientKarmaException when the user has insufficent rights 
131
     *                                    for the operation
132
     */
133
    public void delete(String docid)
134
        throws InsufficientKarmaException
135
    {
136
    }
137

  
138
    /**
139
     * When the MetacatFactory creates an instance it needs to set the
140
     * MetacatUrl to which connections should be made.
141
     *
142
     * @param metacatUrl the URL for the metacat server
143
     */
144
    public void setMetacatUrl(String metacatUrl)
145
    {
146
    }
147

  
148
    /************************************************************************
149
     * PRIVATE METHODS
150
     ************************************************************************/
151

  
152
    synchronized private InputStream sendDataOnce(Properties prop) 
153
        throws Exception
154
    {
155
        InputStream returnStream = null;
156
        URL url = new URL(metacatUrl);
157
        HttpMessage msg = new HttpMessage(url);
158
        returnStream = msg.sendPostData(prop);
159
        return returnStream;
160
    }
161

  
162
    /**
163
     * Send a request to Metacat
164
     *
165
     * @param prop  the properties to be sent to Metacat
166
     * @return      InputStream as returned by Metacat
167
     */
168
    synchronized private InputStream sendData(Properties prop) throws Exception
169
    {   
170
        InputStream returnStream = null;
171

  
172
        /*
173
            Note:  The reason that there are three try statements all executing
174
            the same code is that there is a problem with the initial connection
175
            using the HTTPClient protocol handler.  These try statements make 
176
            sure that a connection is made because it gives each connection a 
177
            2nd and 3rd chance to work before throwing an error.
178
            THIS IS A TOTAL HACK.  THIS NEEDS TO BE LOOKED INTO AFTER THE BETA1
179
            RELEASE OF MORPHO!!!  cwb (7/24/01)
180
          */
181
        try {
182
           return sendDataOnce(prop);
183
        } catch (Exception e) {
184
            try {
185
                return sendDataOnce(prop);
186
            } catch (Exception e2) {
187
                try {
188
                    return sendDataOnce(prop);
189
                } catch (Exception e3) {
190
                    System.err.println(
191
                            "Failed to send data to metacat 3 times.");
192
                    throw e3;
193
                }
194
            }
195
        }
196
    }
197
}
0 198

  
src/edu/ucsb/nceas/metacat/client/Metacat.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
import java.io.Reader;
28

  
29
/**
30
 *  This interface provides methods for initializing and logging in to a 
31
 *  Metacat server, and then querying, reading, transforming, inserting, 
32
 *  updating and deleting documents from that server.
33
 */
34
public interface Metacat
35
{
36
    /**
37
     *  Method used to log in to a metacat server. Implementations will need
38
     *  to cache a cookie value to make the session persistent.  Each time a
39
     *  call is made to one of the other methods (e.g., read), the cookie will
40
     *  need to be passed back to the metacat server along with the request.
41
     *
42
     *  @param username   the username of the user, like an LDAP DN
43
     *  @param password   the password for that user for authentication
44
     *  @throws MetacatAuthException when the username/password could
45
     *                    not be authenticated
46
     */
47
    public void login(String username, String password) 
48
           throws MetacatAuthException, MetacatInaccessibleException;
49

  
50
    /**
51
     * Read an XML document from the metacat server session, accessed by docid,
52
     * and returned as a Reader.
53
     *
54
     * @param docid the identifier of the document to be read
55
     * @return a Reader for accessing the document
56
     * @throws InsufficientKarmaException when the user has insufficent rights 
57
     *                                    for the operation
58
     */
59
    public Reader read(String docid) throws InsufficientKarmaException;
60

  
61
    /**
62
     * Query the metacat document store with the given metacat-compatible 
63
     * query document, and return the result set as a Reader.
64
     *
65
     * @param xmlQuery a Reader for accessing the XML version of the query
66
     * @return a Reader for accessing the result set
67
     */
68
    public Reader query(Reader xmlQuery);
69

  
70
    /**
71
     * Insert an XML document into the repository.
72
     *
73
     * @param docid the docid to insert the document
74
     * @param xmlDocument a Reader for accessing the XML document to be inserted
75
     * @param schema a Reader for accessing the DTD or XML Schema for 
76
     *               the document
77
     * @throws InsufficientKarmaException when the user has insufficent rights 
78
     *                                    for the operation
79
     */
80
    public void insert(String docid, Reader xmlDocument, Reader schema)
81
        throws InsufficientKarmaException;
82

  
83
    /**
84
     * Update an XML document in the repository.
85
     *
86
     * @param docid the docid to update
87
     * @param xmlDocument a Reader for accessing the XML text to be updated
88
     * @param schema a Reader for accessing the DTD or XML Schema for 
89
     *               the document
90
     * @throws InsufficientKarmaException when the user has insufficent rights 
91
     *                                    for the operation
92
     */
93
    public void update(String docid, Reader xmlDocument, Reader schema)
94
        throws InsufficientKarmaException;
95

  
96
    /**
97
     * Delete an XML document in the repository.
98
     *
99
     * @param docid the docid to delete
100
     * @throws InsufficientKarmaException when the user has insufficent rights 
101
     *                                    for the operation
102
     */
103
    public void delete(String docid)
104
        throws InsufficientKarmaException;
105

  
106
    /**
107
     * When the MetacatFactory creates an instance it needs to set the
108
     * MetacatUrl to which connections should be made.
109
     *
110
     * @param metacatUrl the URL for the metacat server
111
     */
112
    public void setMetacatUrl(String metacatUrl);
113
}
0 114

  
build.xml
96 96
      <property name="default-style" value="knb" />
97 97
      <property name="morphosourcedir" value="../morpho" />
98 98
      <property name="defaultcontenttype" value="application/octet-stream" />
99
      <property name="utilitiesdir" value="../utilities" />
99 100

  
100 101
      <!-- Make sure these paths match the location of the jar files
101 102
           on your system, the defaults should usually work -->
......
114 115
      <property name="apis"
115 116
                value="lib/xalan/xml-apis.jar" />
116 117
      <property name="httpclient"
117
                value="${morphosourcedir}/lib/httpclient.jar" />
118
                value="lib/httpclient.jar" />
118 119
      <property name="itis"
119 120
                value="${morphosourcedir}/lib/itislib.jar" />
120 121
      <property name="emlparser"
......
125 126
                value="lib/itislib.jar" />
126 127
      <property name="emlparser"
127 128
                value="lib/eml.jar" />
129
      <property name="util"
130
                value="lib/utilities.jar" />
128 131
  </target>
129 132

  
130 133
   <target name="init" depends="config">
......
152 155
      <property name="stmmlnamespace" value="http://www.xml-cml.org/schema/stmml" />
153 156

  
154 157
      <property name="cpath"
155
                value="${xmlp}:${xalan}:${apis}:${jdbc}:${jsdk}:${srb}:${cos}:${morpho}:${emlparser}:${jdbc-add1}:${jdbc-add2}"/>
158
                value="${xmlp}:${xalan}:${apis}:${jdbc}:${jsdk}:${srb}:${cos}:${morpho}:${emlparser}:${jdbc-add1}:${jdbc-add2}:${util}:${httpclient}"/>
156 159

  
157 160
      <filter token="docrooturl" value="./" />
158 161
      <filter token="jdbc-connect" value="${jdbc-connect}"/>
......
254 257
            includes="**/protocols/" />
255 258
   </target>
256 259

  
260
   <target name="utilities" depends="config">
261
     <ant dir="${utilitiesdir}" target="clean"/>
262
     <ant dir="${utilitiesdir}" target="jar"/>
263
     <copy file="${utilitiesdir}/lib/httpclient.jar" todir="lib" />
264
     <copy file="${utilitiesdir}/build/utilities.jar" todir="lib" />
265
   </target>
266

  
267
   <target name="client" depends="utilities, prepare">
268
       <javac srcdir="${build.src}"
269
              destdir="${build.dest}"
270
              classpath="${cpath}"
271
              includes="edu/ucsb/nceas/metacat/client/*" />
272
   </target>
273

  
274
   <target name="clientjar" depends="client">
275
       <delete file="${build.dir}/${name}-client.jar" />
276
       <jar jarfile="${build.dir}/${name}-client.jar" basedir="${build.dest}"
277
            includes="**/metacat/client/" />
278
   </target>
279

  
257 280
   <target name="getdtdschema" depends="prepare">
258 281
     <echo>Enter CVS password: </echo>
259 282
     <cvs cvsRoot="${cvsroot}"

Also available in: Unified diff