001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.solr.request;
017:
018: import org.apache.solr.core.SolrException;
019:
020: import java.util.Iterator;
021:
022: /**
023: * This is a simple wrapper to SolrParams that will throw a 400
024: * exception if you ask for a parameter that does not exist. Fields
025: * specified with
026: *
027: * In short, any value you for from a <code>RequiredSolrParams</code>
028: * will return a valid non-null value or throw a 400 exception.
029: * (If you pass in <code>null</code> as the default value, you can
030: * get a null return value)
031: *
032: * @author jjl
033: * @version $Id: RequiredSolrParams.java 542679 2007-05-29 22:28:21Z ryan $
034: * @since solr 1.2
035: */
036: public class RequiredSolrParams extends SolrParams {
037: protected final SolrParams params;
038:
039: public RequiredSolrParams(SolrParams params) {
040: this .params = params;
041: }
042:
043: /** get the param from params, fail if not found **/
044: @Override
045: public String get(String param) {
046: String val = params.get(param);
047: if (val == null) {
048: throw new SolrException(
049: SolrException.ErrorCode.BAD_REQUEST,
050: "Missing required parameter: " + param);
051: }
052: return val;
053: }
054:
055: @Override
056: public String[] getParams(String param) {
057: String[] vals = params.getParams(param);
058: if (vals == null || vals.length == 0) {
059: throw new SolrException(
060: SolrException.ErrorCode.BAD_REQUEST,
061: "Missing required parameter: " + param);
062: }
063: return vals;
064: }
065:
066: /** returns an Iterator over the parameter names */
067: @Override
068: public Iterator<String> getParameterNamesIterator() {
069: return params.getParameterNamesIterator();
070: }
071:
072: @Override
073: public String toString() {
074: return "{required(" + params + ")}";
075: }
076:
077: //----------------------------------------------------------
078: // Functions with a default value - pass directly to the
079: // wrapped SolrParams (they won't return null - unless its the default)
080: //----------------------------------------------------------
081:
082: @Override
083: public String get(String param, String def) {
084: return params.get(param, def);
085: }
086:
087: @Override
088: public int getInt(String param, int def) {
089: return params.getInt(param, def);
090: }
091:
092: @Override
093: public float getFloat(String param, float def) {
094: return params.getFloat(param, def);
095: }
096:
097: @Override
098: public boolean getBool(String param, boolean def) {
099: return params.getBool(param, def);
100: }
101:
102: @Override
103: public int getFieldInt(String field, String param, int def) {
104: return params.getFieldInt(field, param, def);
105: }
106:
107: @Override
108: public boolean getFieldBool(String field, String param, boolean def) {
109: return params.getFieldBool(field, param, def);
110: }
111:
112: @Override
113: public float getFieldFloat(String field, String param, float def) {
114: return params.getFieldFloat(field, param, def);
115: }
116:
117: @Override
118: public String getFieldParam(String field, String param, String def) {
119: return params.getFieldParam(field, param, def);
120: }
121: }
|