source: main/trunk/greenstone2/runtime-src/src/oaiservr/listidsaction.cpp@ 27220

Last change on this file since 27220 was 22739, checked in by mdewsnip, 14 years ago

Added copyright header to runtime-src/src/oaiserver/*.cpp and runtime-src/src/oaiserver/*.h.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.9 KB
Line 
1/**********************************************************************
2 *
3 * listidsaction.cpp --
4 *
5 * Copyright (C) 2004-2010 The New Zealand Digital Library Project
6 *
7 * A component of the Greenstone digital library software
8 * from the New Zealand Digital Library Project at the
9 * University of Waikato, New Zealand.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 *********************************************************************/
26
27#include "listidsaction.h"
28#include "recptprototools.h"
29#include "oaitools.h"
30
31//--------------------------------------------------------------------------------------------------
32
33bool listidsaction::validateAction(recptproto *protocol, oaiargs &params)
34{
35 // ----------------------------------------------------------------------------
36 // 1. Check for invalid arguments
37 // ----------------------------------------------------------------------------
38 bool invalid_argument_supplied = false;
39 text_tmap::const_iterator param_iterator = params.begin();
40 while (param_iterator != params.end())
41 {
42 // Check for arguments that aren't valid for this action
43 if (param_iterator->first != "verb" &&
44 param_iterator->first != "from" &&
45 param_iterator->first != "until" &&
46 param_iterator->first != "set" &&
47 param_iterator->first != "resumptionToken" &&
48 param_iterator->first != "metadataPrefix")
49 {
50 // We've found an invalid argument
51 invalid_argument_supplied = true;
52
53 // Delete the invalid argument from the list so it doesn't end up in the <request> tag that is returned
54 params.erase(param_iterator->first);
55 }
56
57 // The metadataPrefix argument is not allowed in OAI v1.1
58 else if (param_iterator->first == "metadataPrefix" && this->configuration->getOAIVersion() <= 110)
59 {
60 // We've found an invalid argument
61 invalid_argument_supplied = true;
62
63 // Delete the invalid argument from the list so it doesn't end up in the <request> tag that is returned
64 params.erase(param_iterator->first);
65 }
66
67 param_iterator++;
68 }
69
70 // If we found an invalid argument it's an error, so don't go any further
71 if (invalid_argument_supplied)
72 {
73 this->errorType = "badArgument";
74 return false;
75 }
76
77 // ----------------------------------------------------------------------------
78 // 2. Handle any exclusive arguments
79 // ----------------------------------------------------------------------------
80
81 // The resumptionToken argument is exclusive
82 if (params["resumptionToken"] != "")
83 {
84 // This argument is exclusive, so no other arguments are allowed (except "verb" of course)
85 if (params.getSize() != 2)
86 {
87 this->errorType = "badArgument";
88 return false;
89 }
90
91 // Check the resumption token is valid
92 ResumptionToken token(params["resumptionToken"]);
93 if (token.isValid())
94 {
95 // Everything is fine, and we don't continue further because this is an exclusive argument
96 this->errorType = "";
97 return true;
98 }
99 else
100 {
101 // There was an error with the resumption token
102 this->errorType = "badResumptionToken";
103 return false;
104 }
105 }
106
107 // ----------------------------------------------------------------------------
108 // 3. Handle any required arguments
109 // ----------------------------------------------------------------------------
110
111 // OAI v2.0 requires metadataPrefix
112 if (this->configuration->getOAIVersion() > 110)
113 {
114 text_t metadataPrefix = params["metadataPrefix"];
115
116 // Check that the metadataPrefix argument exists
117 if (metadataPrefix == "")
118 {
119 this->errorType = "badArgument";
120 return false;
121 }
122
123 // Check that the metadataPrefix is a format we support
124 if (this->formatNotSupported(metadataPrefix))
125 {
126 this->errorType = "cannotDisseminateFormat";
127 return false;
128 }
129 }
130
131 // ----------------------------------------------------------------------------
132 // 4. Check any remaining arguments
133 // ----------------------------------------------------------------------------
134
135 // Check "from" and "until" arguments
136 if (params["from"] != "" || params["until"] != "")
137 {
138 text_t from = params["from"];
139 text_t until = params["until"];
140
141 // Check the from date is in the correct format: YYYY-MM-DD
142 if (from != "")
143 {
144 // Must be in the form YYYY-MM-DD
145 if (from.size() != 10 || from[4] != '-' || from[7] != '-')
146 {
147 this->errorType = "badArgument";
148 params.erase("from");
149 }
150 }
151 // Check the until date is in the correct format: YYYY-MM-DD
152 if (until != "")
153 {
154 // Must be in the form YYYY-MM-DD
155 if (until.size() != 10 || until[4] != '-' || until[7] != '-')
156 {
157 this->errorType = "badArgument";
158 params.erase("until");
159 }
160 }
161
162 if (this->errorType == "badArgument")
163 {
164 return false;
165 }
166
167 // If both arguments are supplied the from date must be less than or equal to the until date
168 if (from != "" && until != "" && !(from <= until))
169 {
170 this->errorType = "badArgument";
171 return false;
172 }
173 }
174
175 // Check "set" argument
176 if (params["set"] != "")
177 {
178 // Example set specification: "demo:CL2"
179 text_t set = params["set"];
180
181 // Extract the collection name from the set specification
182 text_t collection = "";
183 oaiclassifier::toGSDL(collection, set);
184
185 // Check that the collection is accessible
186 ColInfoResponse_t cinfo;
187 comerror_t err;
188 protocol->get_collectinfo(collection, cinfo, err, cerr);
189 if (err != noError)
190 {
191 this->errorType = "badArgument";
192 return false;
193 }
194
195 // Check the collection is one that is in the list in the oai.cfg file
196 text_tarray &collections = this->configuration->getCollectionsList();
197 bool collection_found = false;
198 for (int c = 0; c < collections.size(); c++)
199 {
200 if (collections[c] == collection)
201 {
202 collection_found = true;
203 break;
204 }
205 }
206
207 // The collection was not found
208 if (!collection_found)
209 {
210 this->errorType = "badArgument";
211 return false;
212 }
213
214 // Check the child set if it was given
215 if (set != "" && !this->check_classifier(protocol, collection, set))
216 {
217 this->errorType = "badArgument";
218 return false;
219 }
220 }
221
222 // If we've reached here everything must be fine
223 this->errorType = "";
224 return true;
225}
226
227//--------------------------------------------------------------------------------------------------
228
229bool listidsaction::output_document(ostream& output, recptproto *protocol, const text_t &collection,
230 const text_t &OID, const text_t &metadataPrefix /* ignored */)
231{
232 FilterResponse_t response;
233 ResultDocInfo_t doc_info;
234 text_tset metadata;
235 ofstream logout("oai.log", ios::app);
236 text_t lastModified;
237 int oaiVersion = this->configuration->getOAIVersion();
238 text_t repos_id = this->configuration->getRepositoryId();
239 get_info(OID, collection, "", metadata, false, protocol, response, logout);
240 doc_info = response.docInfo[0];
241 this->getLastModifiedDate(doc_info, lastModified);
242
243 // output the record for this document
244 text_t oaiLabel = OID;
245 oaiclassifier::toOAI(repos_id, collection, oaiLabel);
246
247 if(oaiVersion <= 110)
248 output << " <identifier>" << oaiLabel << "</identifier>\n";
249 else
250 this->output_record_header(output, oaiLabel, lastModified,
251 doc_info.metadata["memberof"].values, oaiVersion);
252
253 return true;
254}
Note: See TracBrowser for help on using the repository browser.