Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2009 University of New Mexico and the 
4
 *                  Regents of the University of California
5
 *
6
 *   '$Author: costa $'
7
 *     '$Date: 2009-07-27 17:47:44 -0400 (Mon, 27 Jul 2009) $'
8
 * '$Revision: 4999 $'
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
 * Additional Copyright 2006 OCLC, Online Computer Library Center
25
 * Licensed under the Apache License, Version 2.0 (the "License");
26
 * you may not use this file except in compliance with the License.
27
 * You may obtain a copy of the License at
28
 *
29
 * http://www.apache.org/licenses/LICENSE-2.0
30
 *
31
 * Unless required by applicable law or agreed to in writing, software
32
 * distributed under the License is distributed on an "AS IS" BASIS,
33
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34
 * See the License for the specific language governing permissions and
35
 * limitations under the License.
36
 */
37

    
38
package edu.ucsb.nceas.metacat.oaipmh.harvester;
39

    
40
import java.io.IOException;
41
import java.net.URLEncoder;
42

    
43
import javax.xml.parsers.ParserConfigurationException;
44
import javax.xml.transform.TransformerException;
45
import org.xml.sax.SAXException;
46

    
47

    
48
/**
49
 * This class represents an ListRecords response on either the server or on the
50
 * client
51
 * 
52
 * @author Jeffrey A. Young, OCLC Online Computer Library Center
53
 */
54
public class ListRecords extends HarvesterVerb {
55

    
56
  /**
57
   * Mock object constructor (for unit testing purposes)
58
   */
59
  public ListRecords() {
60
    super();
61
  }
62

    
63

    
64
  /**
65
   * Client-side ListRecords verb constructor
66
   * 
67
   * @param baseURL
68
   *          the baseURL of the server to be queried
69
   * @exception MalformedURLException
70
   *              the baseURL is bad
71
   * @exception SAXException
72
   *              the xml response is bad
73
   * @exception IOException
74
   *              an I/O error occurred
75
   */
76
  public ListRecords(String baseURL, String from, String until,
77
                     String metadataPrefix, String setSpec) 
78
          throws IOException, ParserConfigurationException,
79
      SAXException, TransformerException {
80
    super(getRequestURL(baseURL, from, until, metadataPrefix, setSpec));
81
  }
82

    
83

    
84
  /**
85
   * Client-side ListRecords verb constructor (resumptionToken version)
86
   * 
87
   * @param baseURL
88
   * @param resumptionToken
89
   * @throws IOException
90
   * @throws ParserConfigurationException
91
   * @throws SAXException
92
   * @throws TransformerException
93
   */
94
  public ListRecords(String baseURL, String resumptionToken)
95
      throws IOException, ParserConfigurationException, SAXException,
96
      TransformerException {
97
    super(getRequestURL(baseURL, resumptionToken));
98
  }
99

    
100

    
101
  /**
102
   * Get the oai:resumptionToken from the response
103
   * 
104
   * @return the oai:resumptionToken value
105
   * @throws TransformerException
106
   * @throws NoSuchFieldException
107
   */
108
  public String getResumptionToken() throws TransformerException,
109
      NoSuchFieldException {
110
    String schemaLocation = getSchemaLocation();
111
    if (schemaLocation.indexOf(SCHEMA_LOCATION_V2_0) != -1) {
112
      return getSingleString("/oai20:OAI-PMH/oai20:ListRecords/oai20:resumptionToken");
113
    } 
114
    else {
115
      throw new NoSuchFieldException(schemaLocation);
116
    }
117
  }
118

    
119

    
120
  /**
121
   * Construct the query portion of the http request
122
   * 
123
   * @return a String containing the query portion of the http request
124
   */
125
  private static String getRequestURL(String baseURL, String from,
126
                                      String until, String metadataPrefix, 
127
                                      String setSpec) 
128
  {
129
    StringBuffer requestURL = new StringBuffer(baseURL);
130
    requestURL.append("?verb=ListRecords");
131
    if (from != null)
132
      requestURL.append("&from=").append(from);
133
    if (until != null)
134
      requestURL.append("&until=").append(until);
135
    if (setSpec != null)
136
      requestURL.append("&set=").append(setSpec);
137
    requestURL.append("&metadataPrefix=").append(metadataPrefix);
138
    return requestURL.toString();
139
  }
140

    
141

    
142
  /**
143
   * Construct the query portion of the http request (resumptionToken version)
144
   * 
145
   * @param baseURL
146
   * @param resumptionToken
147
   * @return
148
   */
149
  @SuppressWarnings("deprecation")
150
  private static String getRequestURL(String baseURL, String resumptionToken) {
151
    StringBuffer requestURL = new StringBuffer(baseURL);
152
    requestURL.append("?verb=ListRecords");
153
    requestURL.append("&resumptionToken=").append(
154
        URLEncoder.encode(resumptionToken));
155
    return requestURL.toString();
156
  }
157
}
(6-6/8)