001/* 002 * SonarQube 003 * Copyright (C) 2009-2016 SonarSource SA 004 * mailto:contact AT sonarsource DOT com 005 * 006 * This program 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 * This program 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 */ 020package org.sonar.api.server.ws.internal; 021 022import static com.google.common.base.Preconditions.checkNotNull; 023 024import com.google.common.base.CharMatcher; 025import com.google.common.base.Splitter; 026import com.google.common.collect.Lists; 027import java.io.InputStream; 028import java.util.ArrayList; 029import java.util.List; 030import java.util.Set; 031import javax.annotation.CheckForNull; 032import javax.annotation.Nullable; 033import org.apache.commons.lang.StringUtils; 034import org.sonar.api.server.ws.LocalConnector; 035import org.sonar.api.server.ws.Request; 036import org.sonar.api.server.ws.WebService; 037import org.sonar.api.utils.log.Loggers; 038 039/** 040 * @since 4.2 041 */ 042public abstract class ValidatingRequest extends Request { 043 044 private WebService.Action action; 045 private LocalConnector localConnector; 046 047 public void setAction(WebService.Action action) { 048 this.action = action; 049 } 050 051 public WebService.Action action() { 052 return action; 053 } 054 055 @Override 056 public LocalConnector localConnector() { 057 checkNotNull(localConnector, "Local connector has not been set"); 058 return localConnector; 059 } 060 061 public void setLocalConnector(LocalConnector lc) { 062 this.localConnector = lc; 063 } 064 065 @Override 066 @CheckForNull 067 public String param(String key) { 068 return param(key, true); 069 } 070 071 @Override 072 @CheckForNull 073 public InputStream paramAsInputStream(String key) { 074 return readInputStreamParam(key); 075 } 076 077 @Override 078 @CheckForNull 079 public Part paramAsPart(String key) { 080 return readPart(key); 081 } 082 083 @CheckForNull 084 private String param(String key, boolean validateValue) { 085 WebService.Param definition = action.param(key); 086 String value = readParamOrDefaultValue(key, definition); 087 String trimmedValue = value == null ? value : CharMatcher.WHITESPACE.trimFrom(value); 088 if (trimmedValue != null && validateValue) { 089 validate(trimmedValue, definition); 090 } 091 return trimmedValue; 092 } 093 094 @CheckForNull 095 @Override 096 public List<String> paramAsStrings(String key) { 097 WebService.Param definition = action.param(key); 098 String value = readParamOrDefaultValue(key, definition); 099 if (value == null) { 100 return null; 101 } 102 List<String> values = Lists.newArrayList(Splitter.on(',').omitEmptyStrings().trimResults().split(value)); 103 for (String s : values) { 104 validate(s, definition); 105 } 106 return values; 107 } 108 109 @CheckForNull 110 @Override 111 public <E extends Enum<E>> List<E> paramAsEnums(String key, Class<E> enumClass) { 112 WebService.Param definition = action.param(key); 113 String value = readParamOrDefaultValue(key, definition); 114 if (value == null) { 115 return null; 116 } 117 Iterable<String> values = Splitter.on(',').omitEmptyStrings().trimResults().split(value); 118 List<E> result = new ArrayList<>(); 119 for (String s : values) { 120 validate(s, definition); 121 result.add(Enum.valueOf(enumClass, s)); 122 } 123 return result; 124 } 125 126 @CheckForNull 127 private String readParamOrDefaultValue(String key, @Nullable WebService.Param definition) { 128 if (definition == null) { 129 String message = String.format("BUG - parameter '%s' is undefined for action '%s'", key, action.key()); 130 Loggers.get(getClass()).error(message); 131 throw new IllegalArgumentException(message); 132 } 133 String deprecatedKey = definition.deprecatedKey(); 134 String value = deprecatedKey != null ? StringUtils.defaultString(readParam(deprecatedKey), readParam(key)) : readParam(key); 135 value = StringUtils.defaultString(value, definition.defaultValue()); 136 if (value == null) { 137 return null; 138 } 139 return value; 140 } 141 142 @CheckForNull 143 protected abstract String readParam(String key); 144 145 @CheckForNull 146 protected abstract InputStream readInputStreamParam(String key); 147 148 @CheckForNull 149 protected abstract Part readPart(String key); 150 151 private static void validate(String value, WebService.Param definition) { 152 Set<String> possibleValues = definition.possibleValues(); 153 if (possibleValues != null && !possibleValues.contains(value)) { 154 throw new IllegalArgumentException(String.format( 155 "Value of parameter '%s' (%s) must be one of: %s", definition.key(), value, possibleValues)); 156 } 157 } 158}