[22739] | 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 |
|
---|
[8182] | 27 | #include "listidsaction.h"
|
---|
[15428] | 28 | #include "recptprototools.h"
|
---|
[8182] | 29 | #include "oaitools.h"
|
---|
| 30 |
|
---|
| 31 | //--------------------------------------------------------------------------------------------------
|
---|
| 32 |
|
---|
| 33 | bool listidsaction::validateAction(recptproto *protocol, oaiargs ¶ms)
|
---|
[16710] | 34 | {
|
---|
[16712] | 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 | }
|
---|
[16718] | 122 |
|
---|
[16712] | 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 |
|
---|
[27528] | 181 | // Is the set a super collection??
|
---|
| 182 | text_tarray &super_collections = this->configuration->getSuperCollectionsList();
|
---|
| 183 | bool super_collection_found = false;
|
---|
| 184 | for (int c = 0; c < super_collections.size(); c++)
|
---|
| 185 | {
|
---|
| 186 | if (super_collections[c] == set)
|
---|
| 187 | {
|
---|
| 188 | super_collection_found = true;
|
---|
| 189 | break;
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 | if (super_collection_found) {
|
---|
| 193 | this->errorType = "";
|
---|
| 194 | return true;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[16714] | 197 | // Extract the collection name from the set specification
|
---|
[16712] | 198 | text_t collection = "";
|
---|
| 199 | oaiclassifier::toGSDL(collection, set);
|
---|
| 200 |
|
---|
| 201 | // Check that the collection is accessible
|
---|
| 202 | ColInfoResponse_t cinfo;
|
---|
| 203 | comerror_t err;
|
---|
| 204 | protocol->get_collectinfo(collection, cinfo, err, cerr);
|
---|
| 205 | if (err != noError)
|
---|
| 206 | {
|
---|
| 207 | this->errorType = "badArgument";
|
---|
| 208 | return false;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | // Check the collection is one that is in the list in the oai.cfg file
|
---|
[27534] | 212 | if (!this->configuration->isValidCollection(collection)) {
|
---|
[16712] | 213 | this->errorType = "badArgument";
|
---|
| 214 | return false;
|
---|
[27534] | 215 | }
|
---|
[16712] | 216 |
|
---|
| 217 | // Check the child set if it was given
|
---|
| 218 | if (set != "" && !this->check_classifier(protocol, collection, set))
|
---|
| 219 | {
|
---|
| 220 | this->errorType = "badArgument";
|
---|
| 221 | return false;
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | // If we've reached here everything must be fine
|
---|
| 226 | this->errorType = "";
|
---|
| 227 | return true;
|
---|
[8182] | 228 | }
|
---|
| 229 |
|
---|
| 230 | //--------------------------------------------------------------------------------------------------
|
---|
| 231 |
|
---|
| 232 | bool listidsaction::output_document(ostream& output, recptproto *protocol, const text_t &collection,
|
---|
| 233 | const text_t &OID, const text_t &metadataPrefix /* ignored */)
|
---|
| 234 | {
|
---|
| 235 | FilterResponse_t response;
|
---|
| 236 | ResultDocInfo_t doc_info;
|
---|
| 237 | text_tset metadata;
|
---|
[8310] | 238 | ofstream logout("oai.log", ios::app);
|
---|
[31903] | 239 | // logout closed automatically by object destructor at this method's end:
|
---|
| 240 | // https://stackoverflow.com/questions/748014/do-i-need-to-manually-close-an-ifstream
|
---|
[31387] | 241 | text_t lastModified = "";
|
---|
| 242 | text_t deleted_status = "";
|
---|
[8182] | 243 | int oaiVersion = this->configuration->getOAIVersion();
|
---|
[22289] | 244 | text_t repos_id = this->configuration->getRepositoryId();
|
---|
[31387] | 245 | get_oai_info(OID, collection, "", metadata, false, protocol, response, logout);
|
---|
[8182] | 246 | doc_info = response.docInfo[0];
|
---|
| 247 | this->getLastModifiedDate(doc_info, lastModified);
|
---|
[31387] | 248 | this->getMeta(doc_info, "oaiinf.status", deleted_status);
|
---|
[8182] | 249 |
|
---|
| 250 | // output the record for this document
|
---|
| 251 | text_t oaiLabel = OID;
|
---|
[22289] | 252 | oaiclassifier::toOAI(repos_id, collection, oaiLabel);
|
---|
[8182] | 253 |
|
---|
| 254 | if(oaiVersion <= 110)
|
---|
| 255 | output << " <identifier>" << oaiLabel << "</identifier>\n";
|
---|
| 256 | else
|
---|
[31387] | 257 | this->output_record_header(output, oaiLabel, lastModified, deleted_status,
|
---|
[8182] | 258 | doc_info.metadata["memberof"].values, oaiVersion);
|
---|
| 259 |
|
---|
| 260 | return true;
|
---|
| 261 | }
|
---|