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.LocalConnector;
033import org.sonar.api.server.ws.Request;
034import org.sonar.api.server.ws.WebService;
035import org.sonar.api.utils.log.Loggers;
036
037import static com.google.common.base.Preconditions.checkNotNull;
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  @CheckForNull
078  private String param(String key, boolean validateValue) {
079    WebService.Param definition = action.param(key);
080    String value = readParamOrDefaultValue(key, definition);
081    String trimmedValue = value == null ? value : CharMatcher.WHITESPACE.trimFrom(value);
082    if (trimmedValue != null && validateValue) {
083      validate(trimmedValue, definition);
084    }
085    return trimmedValue;
086  }
087
088  @CheckForNull
089  @Override
090  public List<String> paramAsStrings(String key) {
091    WebService.Param definition = action.param(key);
092    String value = readParamOrDefaultValue(key, definition);
093    if (value == null) {
094      return null;
095    }
096    List<String> values = Lists.newArrayList(Splitter.on(',').omitEmptyStrings().trimResults().split(value));
097    for (String s : values) {
098      validate(s, definition);
099    }
100    return values;
101  }
102
103  @CheckForNull
104  @Override
105  public <E extends Enum<E>> List<E> paramAsEnums(String key, Class<E> enumClass) {
106    WebService.Param definition = action.param(key);
107    String value = readParamOrDefaultValue(key, definition);
108    if (value == null) {
109      return null;
110    }
111    Iterable<String> values = Splitter.on(',').omitEmptyStrings().trimResults().split(value);
112    List<E> result = new ArrayList<>();
113    for (String s : values) {
114      validate(s, definition);
115      result.add(Enum.valueOf(enumClass, s));
116    }
117    return result;
118  }
119
120  @CheckForNull
121  private String readParamOrDefaultValue(String key, @Nullable WebService.Param definition) {
122    if (definition == null) {
123      String message = String.format("BUG - parameter '%s' is undefined for action '%s'", key, action.key());
124      Loggers.get(getClass()).error(message);
125      throw new IllegalArgumentException(message);
126    }
127    String deprecatedKey = definition.deprecatedKey();
128    String value = deprecatedKey != null ? StringUtils.defaultString(readParam(deprecatedKey), readParam(key)) : readParam(key);
129    value = StringUtils.defaultString(value, definition.defaultValue());
130    if (value == null) {
131      return null;
132    }
133    return value;
134  }
135
136  @CheckForNull
137  protected abstract String readParam(String key);
138
139  @CheckForNull
140  protected abstract InputStream readInputStreamParam(String key);
141
142  private static void validate(String value, WebService.Param definition) {
143    Set<String> possibleValues = definition.possibleValues();
144    if (possibleValues != null && !possibleValues.contains(value)) {
145      throw new IllegalArgumentException(String.format(
146        "Value of parameter '%s' (%s) must be one of: %s", definition.key(), value, possibleValues));
147    }
148  }
149}