Project

General

Profile

Revision 5397

Added by berkley over 9 years ago

MetacatPopulator is now working. It allows you to do a query from one metacat instance, then insert any returned docs into another metacat instance

View differences:

test/edu/ucsb/nceas/metacat/util/MetacatPopulatorTest.java
100 100
            MetacatPopulator mp = new MetacatPopulator(
101 101
                    "http://knb.ecoinformatics.org/knb", 
102 102
                    "http://knb-mn.ecoinformatics.org/knb", 
103
                    "knb-lter-and.2722", "uid=kepler,o=unaffiliated,dc=ecoinformatics,dc=org", "kepler");
103
                    "grass", "uid=kepler,o=unaffiliated,dc=ecoinformatics,dc=org", "kepler");
104 104
            mp.populate();
105 105
        }
106 106
        catch(Exception e)
src/edu/ucsb/nceas/metacat/util/MetacatPopulator.java
101 101
    public void populate()
102 102
      throws Exception
103 103
    {
104
        printHeader("Source login");
104 105
        String sourceSessionid = loginSource();
105
        String destSessionid = loginDest();
106
        
106 107
        //do a query
107 108
        String params = "returndoctype=eml://ecoinformatics.org/eml-2.0.1&" +
108 109
                        "returndoctype=eml://ecoinformatics.org/eml-2.0.0&" +
......
112 113
        params += "qformat=xml&";
113 114
        params += "anyfield=" + query;
114 115
        
116
        printHeader("Searching source");
115 117
        System.out.println("searching '" + sourceUrl + "' for '" + query + "' with sessionid '" + sourceSessionid + "'");
116 118
        InputStream is = getResponse(sourceUrl, "/metacat",
117 119
                params, "POST");
118 120
        String response = streamToString(is);
119 121
        //System.out.println("response: " + response);
120 122
        Vector<Document> docs = parseResponse(response);
121

  
123
        printHeader("Parsing source results");
124
        D1Client d1 = new D1Client(destinationUrl + "/");
125
        printHeader("logging in to the destination " + destinationUrl);
126
        AuthToken authtoken = d1.login(username, password);
122 127
        for(int i=0; i<docs.size(); i++)
123 128
        {
124 129
            //for each document in the query
125 130
            Document doc = docs.get(i);
126 131
            String docid = doc.docid;
127 132
            //get the doc from source
128
            System.out.println("retrieving doc " + docid);
133
            printHeader("Getting document " + doc.docid + " from source " + sourceUrl);
129 134
            params = "action=read&qformat=xml&docid=" + docid;
130 135
            is = getResponse(sourceUrl, "/metacat", params, "POST");
131 136
            String doctext = streamToString(is);
132 137
            //System.out.println("Done retrieving document: " + doctext);
133 138
            is = stringToStream(doctext);
134 139
            doc.doctext = doctext;
135
            //params += "&doctext=" + doc;
136
            //is = getResponse(sourceUrl, "/metacat",
137
            //        params, "POST");
138
            //System.out.println("done with upload: " + streamToString(is));
139
            
140
            D1Client d1 = new D1Client(destinationUrl + "/");
140

  
141
            printHeader("creating document on destination " + destinationUrl);            
141 142
            SystemMetadata sysmeta = generateSystemMetadata(doc);
142
            AuthToken authtoken = d1.login(username, password);
143
            Identifier fakeId = new Identifier();
144
            fakeId.setValue("XXX");
145
            Identifier id = d1.create(authtoken, /*sysmeta.getIdentifier()*/fakeId, 
146
                  IOUtils.toInputStream(doc.doctext), sysmeta);
147
            System.out.println("Document created with id " + id.getValue());
148
            //insert it into destination
149
            /*System.out.println("Creating multipart message");
150
            final MimeMultipart mmp = createMimeMultipart(is);
151
            final InputStreamFromOutputStream<String> multipartStream = 
152
                new InputStreamFromOutputStream<String>() {
153
                @Override
154
                public String produce(final OutputStream dataSink) throws Exception {
155
                    mmp.writeTo(dataSink);
156
                    IOUtils.closeQuietly(dataSink);
157
                    return "Completed";
158
                }
159
            };
160
            System.out.println("uploading document to " + destinationUrl + 
161
                    "with docid " + docid + " with sessionid " + destSessionid);
162
            InputStream uploadResponse = sendRequest(destinationUrl, "/object", 
163
                    destSessionid, "PUT", "action=inser&docid=" + docid, 
164
                    "multipart/form-data", multipartStream);
165
            String res = streamToString(uploadResponse);*/
166
            /*CrudService cs = new CrudService();
167
            cs.setContextUrl(destinationUrl);
168
            Identifier guid = new Identifier();
169
            guid.setValue(docid);
170
            cs.create(new AuthToken(destSessionid), guid, is, null);*/
171
            
172
            //System.out.println("uploading complete: " + res);
173
            //System.out.println("Done with document " + docid);
143
            try
144
            {
145
              Identifier id = d1.create(authtoken, sysmeta.getIdentifier(), 
146
                    IOUtils.toInputStream(doc.doctext), sysmeta);
147
              System.out.println("Success inserting document " + id.getValue());
148
            }
149
            catch(Exception e)
150
            {
151
                System.out.println("Could not create document with id " + 
152
                        sysmeta.getIdentifier().getValue() + " : " + e.getMessage());
153
            }
154
            finally
155
            {
156
                printHeader("Done inserting document " + sysmeta.getIdentifier().getValue());
157
            }
174 158
        }
175 159
        
176 160
        logout();
177 161
    }
178 162
    
163
    private void printHeader(String s)
164
    {
165
        System.out.println("****** " + s + " *******");
166
    }
167
    
179 168
    /**
180 169
     * produce an md5 checksum for item
181 170
     */
......
447 436
        "POST");
448 437
        String response = streamToString(is);
449 438
        //System.out.println("response: " + response);
439
        if(response.indexOf("sessionId") == -1)
440
        {
441
            throw new Exception("Error logging into " + sourceUrl);
442
        }
443
        
450 444
        String sessionid = response.substring(
451 445
                response.indexOf("<sessionId>") + "<sessionId>".length(), 
452 446
                response.indexOf("</sessionId>"));

Also available in: Unified diff