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  import static org.opencastproject.util.data.Option.eq;
25  import static org.opencastproject.util.data.Option.option;
26  
27  import org.opencastproject.oaipmh.OaiPmhConstants;
28  import org.opencastproject.util.data.Option;
29  
30  /**
31   * Helper to encapsulate HTTP parameter handling.
32   */
33  public abstract class Params {
34    /**
35     * Implement this method to supply the needed parameters.
36     *
37     * @return the parameter or null or a blank string to indicate a missing parameter
38     */
39    abstract String getParameter(String key);
40  
41    /**
42     * Return the complete repository URL. This is everything before the query parameters.
43     * Examples: http://localhost:8080/oaipmh or http://localhost:8080/oaipmh/cq5
44     */
45    abstract String getRepositoryUrl();
46  
47    boolean isVerbGetRecord() {
48      return getVerb().map(eq(OaiPmhConstants.VERB_GET_RECORD)).getOrElse(false);
49    }
50  
51    boolean isVerbIdentify() {
52      return getVerb().map(eq(OaiPmhConstants.VERB_IDENTIFY)).getOrElse(false);
53    }
54  
55    boolean isVerbListMetadataFormats() {
56      return getVerb().map(eq(OaiPmhConstants.VERB_LIST_METADATA_FORMATS)).getOrElse(false);
57    }
58  
59    boolean isVerbListSets() {
60      return getVerb().map(eq(OaiPmhConstants.VERB_LIST_SETS)).getOrElse(false);
61    }
62  
63    boolean isVerbListIdentifiers() {
64      return getVerb().map(eq(OaiPmhConstants.VERB_LIST_IDENTIFIERS)).getOrElse(false);
65    }
66  
67    boolean isVerbListRecords() {
68      return getVerb().map(eq(OaiPmhConstants.VERB_LIST_RECORDS)).getOrElse(false);
69    }
70  
71    Option<String> getVerb() {
72      return option(trimToNull(getParameter("verb")));
73    }
74  
75    Option<String> getIdentifier() {
76      return option(trimToNull(getParameter("identifier")));
77    }
78  
79    Option<String> getMetadataPrefix() {
80      return option(trimToNull(getParameter("metadataPrefix")));
81    }
82  
83    Option<String> getFrom() {
84      return option(trimToNull(getParameter("from")));
85    }
86  
87    Option<String> getUntil() {
88      return option(trimToNull(getParameter("until")));
89    }
90  
91    Option<String> getSet() {
92      return option(trimToNull(getParameter("set")));
93    }
94  
95    Option<String> getResumptionToken() {
96      return option(trimToNull(getParameter("resumptionToken")));
97    }
98  
99    @Override
100   public String toString() {
101     final StringBuilder sb = new StringBuilder();
102     sb.append("Params");
103     sb.append("{verb=");
104     sb.append(getVerb());
105     sb.append("}");
106     return sb.toString();
107   }
108 }