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.internal; 021 022 import com.google.common.base.Splitter; 023 import com.google.common.collect.Lists; 024 import org.apache.commons.lang.StringUtils; 025 import org.slf4j.LoggerFactory; 026 import org.sonar.api.server.ws.Request; 027 import org.sonar.api.server.ws.WebService; 028 029 import javax.annotation.CheckForNull; 030 import javax.annotation.Nullable; 031 import java.util.List; 032 import java.util.Set; 033 034 /** 035 * @since 4.2 036 */ 037 public abstract class ValidatingRequest extends Request { 038 039 private WebService.Action action; 040 041 public void setAction(WebService.Action action) { 042 this.action = action; 043 } 044 045 public WebService.Action action() { 046 return action; 047 } 048 049 @CheckForNull 050 public String param(String key) { 051 return param(key, true); 052 } 053 054 @CheckForNull 055 private String param(String key, boolean validateValue) { 056 WebService.Param definition = action.param(key); 057 String value = readParamOrDefaultValue(key, definition); 058 if (value != null && validateValue) { 059 validate(value, definition); 060 } 061 return value; 062 } 063 064 @CheckForNull 065 @Override 066 public List<String> paramAsStrings(String key) { 067 WebService.Param definition = action.param(key); 068 String value = readParamOrDefaultValue(key, definition); 069 if (value == null) { 070 return null; 071 } 072 List<String> values = Lists.newArrayList(Splitter.on(',').omitEmptyStrings().trimResults().split(value)); 073 for (String s : values) { 074 validate(s, definition); 075 } 076 return values; 077 } 078 079 @CheckForNull 080 @Override 081 public <E extends Enum<E>> List<E> paramAsEnums(String key, Class<E> enumClass) { 082 WebService.Param definition = action.param(key); 083 String value = readParamOrDefaultValue(key, definition); 084 if (value == null) { 085 return null; 086 } 087 Iterable<String> values = Splitter.on(',').omitEmptyStrings().trimResults().split(value); 088 List<E> result = Lists.newArrayList(); 089 for (String s : values) { 090 validate(s, definition); 091 result.add(Enum.valueOf(enumClass, s)); 092 } 093 return result; 094 } 095 096 @CheckForNull 097 private String readParamOrDefaultValue(String key, @Nullable WebService.Param definition) { 098 if (definition == null) { 099 String message = String.format("BUG - parameter '%s' is undefined for action '%s'", key, action.key()); 100 LoggerFactory.getLogger(getClass()).error(message); 101 throw new IllegalArgumentException(message); 102 } 103 String value = StringUtils.defaultString(readParam(key), definition.defaultValue()); 104 if (value == null) { 105 return null; 106 } 107 return value; 108 } 109 110 @CheckForNull 111 protected abstract String readParam(String key); 112 113 private void validate(String value, WebService.Param definition) { 114 Set<String> possibleValues = definition.possibleValues(); 115 if (possibleValues != null && !possibleValues.contains(value)) { 116 throw new IllegalArgumentException(String.format( 117 "Value of parameter '%s' (%s) must be one of: %s", definition.key(), value, possibleValues)); 118 } 119 } 120 }