View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  package org.opencastproject.oaipmh.server;
22  
23  import static org.apache.commons.lang3.StringUtils.trimToNull;
24  
25  import org.opencastproject.oaipmh.OaiPmhConstants;
26  
27  import java.util.Optional;
28  
29  /**
30   * Helper to encapsulate HTTP parameter handling.
31   */
32  public abstract class Params {
33    /**
34     * Implement this method to supply the needed parameters.
35     *
36     * @return the parameter or null or a blank string to indicate a missing parameter
37     */
38    abstract String getParameter(String key);
39  
40    /**
41     * Return the complete repository URL. This is everything before the query parameters.
42     * Examples: http://localhost:8080/oaipmh or http://localhost:8080/oaipmh/cq5
43     */
44    abstract String getRepositoryUrl();
45  
46    boolean isVerbGetRecord() {
47      return getVerb().map(s -> OaiPmhConstants.VERB_GET_RECORD.equals(s)).orElse(false);
48    }
49  
50    boolean isVerbIdentify() {
51      return getVerb().map(s -> OaiPmhConstants.VERB_IDENTIFY.equals(s)).orElse(false);
52    }
53  
54    boolean isVerbListMetadataFormats() {
55      return getVerb().map(s -> OaiPmhConstants.VERB_LIST_METADATA_FORMATS.equals(s)).orElse(false);
56    }
57  
58    boolean isVerbListSets() {
59      return getVerb().map(s -> OaiPmhConstants.VERB_LIST_SETS.equals(s)).orElse(false);
60    }
61  
62    boolean isVerbListIdentifiers() {
63      return getVerb().map(s -> OaiPmhConstants.VERB_LIST_IDENTIFIERS.equals(s)).orElse(false);
64    }
65  
66    boolean isVerbListRecords() {
67      return getVerb().map(s -> OaiPmhConstants.VERB_LIST_RECORDS.equals(s)).orElse(false);
68    }
69  
70    Optional<String> getVerb() {
71      return Optional.ofNullable(trimToNull(getParameter("verb")));
72    }
73  
74    Optional<String> getIdentifier() {
75      return Optional.ofNullable(trimToNull(getParameter("identifier")));
76    }
77  
78    Optional<String> getMetadataPrefix() {
79      return Optional.ofNullable(trimToNull(getParameter("metadataPrefix")));
80    }
81  
82    Optional<String> getFrom() {
83      return Optional.ofNullable(trimToNull(getParameter("from")));
84    }
85  
86    Optional<String> getUntil() {
87      return Optional.ofNullable(trimToNull(getParameter("until")));
88    }
89  
90    Optional<String> getSet() {
91      return Optional.ofNullable(trimToNull(getParameter("set")));
92    }
93  
94    Optional<String> getResumptionToken() {
95      return Optional.ofNullable(trimToNull(getParameter("resumptionToken")));
96    }
97  
98    @Override
99    public String toString() {
100     final StringBuilder sb = new StringBuilder();
101     sb.append("Params");
102     sb.append("{verb=");
103     sb.append(getVerb());
104     sb.append("}");
105     return sb.toString();
106   }
107 }