1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * metaformatsaction.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 "metaformatsaction.h"
|
---|
28 | #include "metaformat.h"
|
---|
29 | #include "recptprototools.h"
|
---|
30 | #include "oaitools.h"
|
---|
31 |
|
---|
32 | bool metaformatsaction::validateAction(recptproto *protocol, oaiargs ¶ms)
|
---|
33 | {
|
---|
34 | // ----------------------------------------------------------------------------
|
---|
35 | // 1. Check for invalid arguments
|
---|
36 | // ----------------------------------------------------------------------------
|
---|
37 | bool invalid_argument_supplied = false;
|
---|
38 | text_tmap::const_iterator param_iterator = params.begin();
|
---|
39 | while (param_iterator != params.end())
|
---|
40 | {
|
---|
41 | // Check for arguments that aren't valid for this action
|
---|
42 | if (param_iterator->first != "verb" &&
|
---|
43 | param_iterator->first != "identifier")
|
---|
44 | {
|
---|
45 | // We've found an invalid argument
|
---|
46 | invalid_argument_supplied = true;
|
---|
47 |
|
---|
48 | // Delete the invalid argument from the list so it doesn't end up in the <request> tag that is returned
|
---|
49 | params.erase(param_iterator->first);
|
---|
50 | }
|
---|
51 |
|
---|
52 | param_iterator++;
|
---|
53 | }
|
---|
54 |
|
---|
55 | // If we found an invalid argument it's an error, so don't go any further
|
---|
56 | if (invalid_argument_supplied)
|
---|
57 | {
|
---|
58 | this->errorType = "badArgument";
|
---|
59 | return false;
|
---|
60 | }
|
---|
61 |
|
---|
62 | // ----------------------------------------------------------------------------
|
---|
63 | // 2. Handle any exclusive arguments
|
---|
64 | // ----------------------------------------------------------------------------
|
---|
65 |
|
---|
66 | // None!
|
---|
67 |
|
---|
68 | // ----------------------------------------------------------------------------
|
---|
69 | // 3. Handle any required arguments
|
---|
70 | // ----------------------------------------------------------------------------
|
---|
71 |
|
---|
72 | // None!
|
---|
73 |
|
---|
74 | // ----------------------------------------------------------------------------
|
---|
75 | // 4. Check any remaining arguments
|
---|
76 | // ----------------------------------------------------------------------------
|
---|
77 |
|
---|
78 | // Check "identifier" argument
|
---|
79 | if (params["identifier"] != "")
|
---|
80 | {
|
---|
81 | text_t identifier = params["identifier"];
|
---|
82 |
|
---|
83 | text_t oai_OID_prefix = "oai:"+this->configuration->getRepositoryId()+":";
|
---|
84 | if(identifier.replace(oai_OID_prefix, "") <= 0) {
|
---|
85 | this->errorType = "idDoesNotExist";
|
---|
86 | // Only throw an error if we're using v2.0.
|
---|
87 | if (this->configuration->getOAIVersion() >= 200) {
|
---|
88 | return false;
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | // Extract the collection name from the identifier specification
|
---|
93 | text_t collection = "";
|
---|
94 | oaiclassifier::toGSDL(collection, identifier);
|
---|
95 |
|
---|
96 | // Check a document with the specified identifier exists
|
---|
97 | text_tset metadata;
|
---|
98 |
|
---|
99 | if (!get_info(identifier, collection, "", metadata, false, protocol, this->gsdlResponse, *logout))
|
---|
100 | {
|
---|
101 | this->errorType = "idDoesNotExist";
|
---|
102 |
|
---|
103 | // Only throw an error if we're using v2.0.
|
---|
104 | // v1.1 should simply return a response without a metadataFormat container. 1.1
|
---|
105 | // should still set the errorType so we know that the error was encountered
|
---|
106 | // the information retrieved here is retained for the output_content function below
|
---|
107 | if (this->configuration->getOAIVersion() >= 200)
|
---|
108 | {
|
---|
109 | return false;
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | // If we've reached here everything must be fine
|
---|
115 | this->errorType = "";
|
---|
116 | return true;
|
---|
117 | }
|
---|
118 |
|
---|
119 | bool metaformatsaction::output_content(ostream &output, recptproto *protocol, oaiargs ¶ms)
|
---|
120 | {
|
---|
121 | // if an identifier parameter is being used, then respond to it - currently we reject anything other
|
---|
122 | // than dublin core (unqualified)
|
---|
123 | //
|
---|
124 | // TODO: only return the selected identifier (if given) and don't validate the entry; just
|
---|
125 | // return nothing if an invalid or unknown metadata format is given
|
---|
126 |
|
---|
127 | // give the required xml response
|
---|
128 |
|
---|
129 | text_t gsdlId = params["identifier"];
|
---|
130 | text_t gsdlCollect;
|
---|
131 |
|
---|
132 | // convert record identifier into GSDL format from OAI
|
---|
133 | if (gsdlId != "") {
|
---|
134 | oaiclassifier::toGSDL(gsdlCollect, gsdlId);
|
---|
135 | }
|
---|
136 |
|
---|
137 | metaformat_map::iterator here = this->formats->begin();
|
---|
138 | metaformat_map::iterator end = this->formats->end();
|
---|
139 |
|
---|
140 | // v1.1 should check that the id - if supplied - didn't cause an error. If it did,
|
---|
141 | // don't output the metadataFormat tags. OAI v2.0 should already have bailed.
|
---|
142 | if(this->errorType != "idDoesNotExist"){
|
---|
143 | while (here != end) { // Loop through the repository's supported metadata formats
|
---|
144 | bool doOutput = true;
|
---|
145 |
|
---|
146 | // if we were given an identifier, then check if it supports the metadata format in question
|
---|
147 | if (gsdlId != "") {
|
---|
148 | doOutput = here->second.get_class()->is_available(gsdlCollect, this->gsdlResponse.docInfo[0]);
|
---|
149 | }
|
---|
150 |
|
---|
151 | if (doOutput) {
|
---|
152 | output << " <metadataFormat>" << endl;
|
---|
153 | here->second.get_class()->output_formatdata(output);
|
---|
154 | output << " </metadataFormat>\n";
|
---|
155 | }
|
---|
156 | ++here;
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | return true;
|
---|
161 | }
|
---|
162 |
|
---|
163 | void metaformatsaction::setConfiguration(oaiconfig *configuration)
|
---|
164 | {
|
---|
165 | this->configuration = configuration;
|
---|
166 |
|
---|
167 | metaformat_map::iterator here = this->formats->begin();
|
---|
168 | metaformat_map::iterator end = this->formats->end();
|
---|
169 | while (here != end) {
|
---|
170 | here->second.set_configuration((oaiconfig *) configuration);
|
---|
171 | ++here;
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|