/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.solr.request; import org.apache.solr.core.SolrException; import java.util.Iterator; import java.util.Formatter; /** * @author jjl * @version $Id$ */ public class RequiredSolrParams extends DefaultSolrParams { protected final SolrParams requires; /** Trivial helper class which could be broken out if useful elsewhere. * Returns the same value (String or String[]) for any key whatsoever **/ public static class FixedSolrParams extends SolrParams { String[] fixed; public FixedSolrParams( String[] arr ) { fixed = arr; } public FixedSolrParams( String str ) { this( new String[]{ str } ); } public String get(String param) { return fixed[0]; } public String[] getParams(String param) { return fixed; } public String toString() { return "fixed(" + (fixed == null ? "null" : ("'" + fixed[0] + "'" + (fixed.length > 1 ? "..." : ""))) + ")"; } public Iterator getParameterNamesIterator() { throw new SolrException(500, "getParameterNamesIterator() not currently implemented in FixedSolrParams"); } }; /** If the param is found in requires, but not found in params, throw a SolrException rather * than delegating to defaults, * using the requires param value as a Formatter string for the exception message **/ public RequiredSolrParams(SolrParams params, SolrParams requires, SolrParams defaults) { super( params, defaults ); this.requires = requires; } /** If the param is not found in params, throw a SolrException * using the requires param value as a Formatter string for the exception message **/ public RequiredSolrParams(SolrParams params, SolrParams requires ) { this( params, requires, null ); } /** If the param is not found in params, throw a SolrException * using the defaultFmt as a Formatter string for the exception message **/ public RequiredSolrParams(SolrParams params, String defaultFmt) { this( params, new FixedSolrParams(defaultFmt) ); } /** If the param is not found in params, throw a SolrException * using a default exception message **/ public RequiredSolrParams(SolrParams params) { this( params, "Missing required parameter: %s" ); } /** If the param is not found in params, throw a SolrException * using the requires param value as a Formatter string for the exception message * defaulting to the defaultFmt for params not found in requires * * commented out to prevent ambiguous method signature... call it this way yourself public RequiredSolrParams(SolrParams params, SolrParams requires, String defaultFmt) { this( params, new DefaultSolrParams( requires, new FixedSolrParams(defaultFmt) ) ); } **/ /** get the param from params, fail if not found and it is required, otherwise pass on to defaults **/ public String get(String param) { String val = params.get(param); if( val != null ) return val; String fmt = requires == null ? null : requires.get(param); if( fmt == null ) return defaults == null ? null : defaults.get(param); throw new SolrException( 400, new Formatter().format( fmt, param ).toString() ); } public String[] getParams(String param) { String[] vals = params.getParams(param); if( vals != null ) return vals; String fmt = requires == null ? null : requires.get(param); if( fmt == null ) return defaults == null ? null : defaults.getParams(param); throw new SolrException( 400, new Formatter().format( fmt, param ).toString() ); } /* do we need to define this to throw up on any requires which is not defined in params? * public Iterator getParameterNamesIterator() { ... } */ public String toString() { return "{params("+String.valueOf(params)+"),requires("+String.valueOf(requires)+",defaults("+String.valueOf(defaults)+")}"; } }