001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2013 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * SonarQube is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.api.server.ws;
021    
022    import org.apache.commons.lang.StringUtils;
023    
024    import javax.annotation.CheckForNull;
025    
026    /**
027     * @since 4.2
028     */
029    public abstract class Request {
030    
031      public abstract WebService.Action action();
032    
033      /**
034       * Returns the name of the HTTP method with which this request was made. Possible
035       * values are GET and POST. Others are not supported.
036       */
037      public abstract String method();
038    
039      @CheckForNull
040      public abstract String param(String key);
041    
042      /**
043       * Returns value of a required parameter
044       *
045       * @throws java.lang.IllegalArgumentException is value is null or blank
046       */
047      public String requiredParam(String key) {
048        String value = param(key);
049        if (StringUtils.isBlank(value)) {
050          throw new IllegalArgumentException(String.format("Parameter '%s' is missing", key));
051        }
052        return value;
053      }
054    
055      @CheckForNull
056      public String param(String key, @CheckForNull String defaultValue) {
057        return StringUtils.defaultString(param(key), defaultValue);
058      }
059    
060      @CheckForNull
061      public Integer intParam(String key) {
062        String s = param(key);
063        return s == null ? null : Integer.parseInt(s);
064      }
065    
066      public int intParam(String key, int defaultValue) {
067        String s = param(key);
068        return s == null ? defaultValue : Integer.parseInt(s);
069      }
070    
071      @CheckForNull
072      public Boolean booleanParam(String key) {
073        String s = param(key);
074        return s == null ? null : Boolean.parseBoolean(s);
075      }
076    
077      public boolean booleanParam(String key, boolean defaultValue) {
078        String s = param(key);
079        return s == null ? defaultValue : Boolean.parseBoolean(s);
080      }
081    }