001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 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 /**
040 * Returns value of a mandatory parameter
041 *
042 * @throws java.lang.IllegalArgumentException is value is null or blank
043 */
044 public String mandatoryParam(String key) {
045 String value = param(key);
046 if (StringUtils.isBlank(value)) {
047 throw new IllegalArgumentException(String.format("Parameter '%s' is missing", key));
048 }
049 return value;
050 }
051
052 /**
053 * Returns value of a mandatory parameter
054 *
055 * @throws java.lang.IllegalArgumentException is value is null or blank
056 */
057 public boolean mandatoryParamAsBoolean(String key) {
058 String s = mandatoryParam(key);
059 return Boolean.parseBoolean(s);
060 }
061
062 /**
063 * Returns value of a mandatory parameter
064 *
065 * @throws java.lang.IllegalArgumentException is value is null or blank
066 */
067 public int mandatoryParamAsInt(String key) {
068 String s = mandatoryParam(key);
069 return Integer.parseInt(s);
070 }
071
072 /**
073 * Returns value of a mandatory parameter
074 *
075 * @throws java.lang.IllegalArgumentException is value is null or blank
076 */
077 public long mandatoryParamAsLong(String key) {
078 String s = mandatoryParam(key);
079 return Long.parseLong(s);
080 }
081
082 @CheckForNull
083 public abstract String param(String key);
084
085 @CheckForNull
086 public String param(String key, @CheckForNull String defaultValue) {
087 return StringUtils.defaultString(param(key), defaultValue);
088 }
089
090 @CheckForNull
091 public Integer paramAsInt(String key) {
092 String s = param(key);
093 return s == null ? null : Integer.parseInt(s);
094 }
095
096 public int paramAsInt(String key, int defaultValue) {
097 String s = param(key);
098 return s == null ? defaultValue : Integer.parseInt(s);
099 }
100
101 @CheckForNull
102 public Long paramAsLong(String key) {
103 String s = param(key);
104 return s == null ? null : Long.parseLong(s);
105 }
106
107 public long paramAsLong(String key, long defaultValue) {
108 String s = param(key);
109 return s == null ? defaultValue : Long.parseLong(s);
110 }
111
112 @CheckForNull
113 public Boolean paramAsBoolean(String key) {
114 String s = param(key);
115 return s == null ? null : Boolean.parseBoolean(s);
116 }
117
118 public boolean paramAsBoolean(String key, boolean defaultValue) {
119 String s = param(key);
120 return s == null ? defaultValue : Boolean.parseBoolean(s);
121 }
122 }