Ignore:
Timestamp:
2008-08-12T11:57:10+12:00 (16 years ago)
Author:
mdewsnip
Message:

Completely tidied up abstractlistaction::validateAction() to handle the resumptionToken argument properly, and to structure the checking code more consistently.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gsdl/trunk/runtime-src/src/oaiservr/abstractlistaction.cpp

    r16708 r16710  
    44#include "oaitools.h"
    55
    6 bool abstractlistaction::validateAction(recptproto *protocol, oaiargs &params, int &numArgs)
    7 {
    8   // Remove any parameters that aren't valid for this action
     6bool abstractlistaction::validateAction(recptproto *protocol, oaiargs &params)
     7{
     8  // ----------------------------------------------------------------------------
     9  //  1. Check for invalid arguments
     10  // ----------------------------------------------------------------------------
     11  bool invalid_argument_supplied = false;
    912  text_tmap::const_iterator param_iterator = params.begin();
    1013  while (param_iterator != params.end())
    1114  {
     15    // Check for arguments that aren't valid for this action
    1216    if (param_iterator->first != "verb" &&
    1317    param_iterator->first != "from" &&
     
    1721    param_iterator->first != "metadataPrefix")
    1822    {
     23      // We've found an invalid argument
     24      invalid_argument_supplied = true;
     25
     26      // Delete the invalid argument from the list so it doesn't end up in the <request> tag that is returned
    1927      params.erase(param_iterator->first);
    2028    }
    2129
     30    // The metadataPrefix argument is not allowed in OAI v1.1
     31    else if (param_iterator->first == "metadataPrefix" && this->configuration->getOAIVersion() <= 110)
     32    {
     33      // We've found an invalid argument
     34      invalid_argument_supplied = true;
     35
     36      // Delete the invalid argument from the list so it doesn't end up in the <request> tag that is returned
     37      params.erase(param_iterator->first);
     38    }
     39
    2240    param_iterator++;
    2341  }
    2442
    25   text_t from  = params["from"];
    26   text_t until = params["until"];
    27 
    28   // from date must be less than, or equal to, until date
    29   if ((from != "") && (until != "") && !(from <= until)){
     43  // If we found an invalid argument it's an error, so don't go any further
     44  if (invalid_argument_supplied)
     45  {
    3046    this->errorType = "badArgument";
    3147    return false;
    3248  }
    3349
    34   if (from != ""){
    35     // Must be in the form YYYY-MM-DD
    36     if(from.size() != 10){
     50  // ----------------------------------------------------------------------------
     51  //  2. Handle any exclusive arguments
     52  // ----------------------------------------------------------------------------
     53
     54  //  The resumptionToken argument is exclusive
     55  if (params["resumptionToken"] != "")
     56  {
     57    // This argument is exclusive, so no other arguments are allowed (except "verb" of course)
     58    if (params.getSize() != 2)
     59    {
    3760      this->errorType = "badArgument";
    38       params.erase("from");
    39     }
    40     else{
    41       if(from[4] != '-' || from[7] != '-'){
     61      return false;
     62    }
     63
     64    // Check the resumption token is valid
     65    ResumptionToken token(params["resumptionToken"]);
     66    if (token.isValid())
     67    {
     68      // Everything is fine, and we don't continue further because this is an exclusive argument
     69      this->errorType = "";
     70      return true;
     71    }
     72    else
     73    {
     74      // There was an error with the resumption token
     75      this->errorType = "badResumptionToken";
     76      return false;
     77    }
     78  }
     79
     80  // ----------------------------------------------------------------------------
     81  //  3. Handle any required arguments
     82  // ----------------------------------------------------------------------------
     83
     84  // OAI v2.0 requires metadataPrefix
     85  if (this->configuration->getOAIVersion() > 110)
     86  {
     87    text_t metadataPrefix = params["metadataPrefix"];
     88
     89    // Check that the metadataPrefix argument exists
     90    if (metadataPrefix == "")
     91    {
     92      this->errorType = "badArgument";
     93      return false;
     94    }
     95    // Check that the metadataPrefix is a format we support
     96    if (this->formatNotSupported(metadataPrefix))
     97    {
     98      this->errorType = "cannotDisseminateFormat";
     99      return false;
     100    }
     101  }
     102
     103  // ----------------------------------------------------------------------------
     104  // 4. Check any remaining arguments
     105  // ----------------------------------------------------------------------------
     106
     107  // Check "from" and "until" arguments
     108  if (params["from"] != "" || params["until"] != "")
     109  {
     110    text_t from  = params["from"];
     111    text_t until = params["until"];
     112
     113    // Check the from date is in the correct format: YYYY-MM-DD
     114    if (from != "")
     115    {
     116      // Must be in the form YYYY-MM-DD
     117      if (from.size() != 10 || from[4] != '-' || from[7] != '-')
     118      {
    42119    this->errorType = "badArgument";
    43120    params.erase("from");
    44121      }
    45122    }
    46     ++numArgs; // Increase valid args count
    47   }
    48 
    49   if (until != ""){
    50     // Must be in the form YYYY-MM-DD
    51     if(until.size() != 10){
    52       this->errorType = "badArgument";
    53       params.erase("until");
    54     }
    55     else{
    56       if(until[4] != '-' || until[7] != '-'){
     123    // Check the until date is in the correct format: YYYY-MM-DD
     124    if (until != "")
     125    {
     126      // Must be in the form YYYY-MM-DD
     127      if (until.size() != 10 || until[4] != '-' || until[7] != '-')
     128      {
    57129    this->errorType = "badArgument";
    58130    params.erase("until");
    59131      }
    60132    }
    61     ++numArgs; // Increase valid args count
    62   }
    63 
    64   if (this->errorType == "badArgument")
     133
     134    if (this->errorType == "badArgument")
     135    {
     136      return false;
     137    }
     138
     139    // If both arguments are supplied the from date must be less than or equal to the until date
     140    if (from != "" && until != "" && !(from <= until))
     141    {
     142      this->errorType = "badArgument";
     143      return false;
     144    }
     145  }
     146
     147  // Check "set" argument
     148  if (params["set"] != "")
    65149  {
    66     return false;
    67   }
    68 
    69   if (params["set"] != "") {
    70     text_t gsdlSet = params["set"];
    71     text_t gsdlCollect = "";
    72 
    73     // given 'demo:CL2', toGSDL returns 'demo' in gsdlCollect and 'CL2' in gsdlSet. If there is no further
    74     // set specified after the name of the collection however, then gsdlSet is empty.
    75     oaiclassifier::toGSDL(gsdlCollect, gsdlSet);
    76 
     150    // Example set specification: "demo:CL2"
     151    text_t set = params["set"];
     152
     153    // given 'demo:CL2', toGSDL returns 'demo' in collection and 'CL2' in set. If there is no further
     154    // set specified after the name of the collection however, then set is empty.
     155    text_t collection = "";
     156    oaiclassifier::toGSDL(collection, set);
     157
     158    // Check that the collection is accessible
     159    ColInfoResponse_t cinfo;
    77160    comerror_t err;
    78     ColInfoResponse_t cinfo;
    79 
    80     // check that the collection is accessible
    81     protocol->get_collectinfo(gsdlCollect, cinfo, err, cerr);
    82     if (err != noError) {
     161    protocol->get_collectinfo(collection, cinfo, err, cerr);
     162    if (err != noError)
     163    {
    83164      this->errorType = "badArgument";
    84165      return false;
    85166    }
    86167
    87     // exclude collections that are not listed in the configured OAI list
     168    // Check the collection is one that is in the list in the oai.cfg file
    88169    text_tarray &collections = this->configuration->getCollectionsList();
    89     int          c;
    90     for (c = 0; c < collections.size(); c ++) {
    91       if (collections[c] == gsdlCollect)
     170    bool collection_found = false;
     171    for (int c = 0; c < collections.size(); c++)
     172    {
     173      if (collections[c] == collection)
     174      {
     175    collection_found = true;
    92176    break;
    93     }
    94     if (c == collections.size()) {
     177      }
     178    }
     179
     180    // The collection was not found
     181    if (!collection_found)
     182    {
    95183      this->errorType = "badArgument";
    96184      return false;
    97185    }
    98    
    99     if (gsdlSet != "") {
    100       // check the child set if it is given
    101       if (!this->check_classifier(protocol, gsdlCollect, gsdlSet)) {
    102     this->errorType = "badArgument";
    103     return false;
    104       }
    105     }
    106     ++numArgs;
    107   }
    108 
    109   if (params["resumptionToken"] != "") {
    110     ResumptionToken token(params["resumptionToken"]);
    111 
    112     if (!token.isValid()) {
    113       this->errorType = "badResumptionToken";
    114       return false;
    115     }
    116     ++numArgs;
    117   }
    118  
     186
     187    // Check the child set if it was given
     188    if (set != "" && !this->check_classifier(protocol, collection, set))
     189    {
     190      this->errorType = "badArgument";
     191      return false;
     192    }
     193  }
     194
     195  // If we've reached here everything must be fine
    119196  this->errorType = "";
    120197  return true;
Note: See TracChangeset for help on using the changeset viewer.