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