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 */
020package org.sonar.api.server.ws.internal;
021
022import com.google.common.base.Splitter;
023import com.google.common.collect.Lists;
024import org.apache.commons.lang.StringUtils;
025import org.sonar.api.server.ws.Request;
026import org.sonar.api.server.ws.WebService;
027import org.sonar.api.utils.log.Loggers;
028
029import javax.annotation.CheckForNull;
030import javax.annotation.Nullable;
031
032import java.io.InputStream;
033import java.util.List;
034import java.util.Set;
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    if (value != null && validateValue) {
068      validate(value, definition);
069    }
070    return value;
071  }
072
073  @CheckForNull
074  @Override
075  public List<String> paramAsStrings(String key) {
076    WebService.Param definition = action.param(key);
077    String value = readParamOrDefaultValue(key, definition);
078    if (value == null) {
079      return null;
080    }
081    List<String> values = Lists.newArrayList(Splitter.on(',').omitEmptyStrings().trimResults().split(value));
082    for (String s : values) {
083      validate(s, definition);
084    }
085    return values;
086  }
087
088  @CheckForNull
089  @Override
090  public <E extends Enum<E>> List<E> paramAsEnums(String key, Class<E> enumClass) {
091    WebService.Param definition = action.param(key);
092    String value = readParamOrDefaultValue(key, definition);
093    if (value == null) {
094      return null;
095    }
096    Iterable<String> values = Splitter.on(',').omitEmptyStrings().trimResults().split(value);
097    List<E> result = Lists.newArrayList();
098    for (String s : values) {
099      validate(s, definition);
100      result.add(Enum.valueOf(enumClass, s));
101    }
102    return result;
103  }
104
105  @CheckForNull
106  private String readParamOrDefaultValue(String key, @Nullable WebService.Param definition) {
107    if (definition == null) {
108      String message = String.format("BUG - parameter '%s' is undefined for action '%s'", key, action.key());
109      Loggers.get(getClass()).error(message);
110      throw new IllegalArgumentException(message);
111    }
112    String deprecatedKey = definition.deprecatedKey();
113    String value = deprecatedKey != null ? StringUtils.defaultString(readParam(deprecatedKey), readParam(key)) : readParam(key);
114    value = StringUtils.defaultString(value, definition.defaultValue());
115    if (value == null) {
116      return null;
117    }
118    return value;
119  }
120
121  @CheckForNull
122  protected abstract String readParam(String key);
123
124  @CheckForNull
125  protected abstract InputStream readInputStreamParam(String key);
126
127  private void validate(String value, WebService.Param definition) {
128    Set<String> possibleValues = definition.possibleValues();
129    if (possibleValues != null && !possibleValues.contains(value)) {
130      throw new IllegalArgumentException(String.format(
131        "Value of parameter '%s' (%s) must be one of: %s", definition.key(), value, possibleValues));
132    }
133  }
134}