me.redaalaoui.org.sonarqube.ws.client.BaseRequest Maven / Gradle / Ivy
The newest version!
/*
* SonarQube
* Copyright (C) 2009-2019 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package me.redaalaoui.org.sonarqube.ws.client;
import me.redaalaoui.org.sonarqube.ws.thirdparty.com.me.redaalaoui.org.sonarqube.ws.thirdparty.google.common.collect.LinkedListMultimap;
import me.redaalaoui.org.sonarqube.ws.thirdparty.com.me.redaalaoui.org.sonarqube.ws.thirdparty.google.common.collect.ListMultimap;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import me.redaalaoui.org.sonarqube.ws.MediaTypes;
import static me.redaalaoui.org.sonarqube.ws.thirdparty.com.me.redaalaoui.org.sonarqube.ws.thirdparty.google.common.base.Preconditions.checkArgument;
import static me.redaalaoui.org.sonarqube.ws.thirdparty.com.me.redaalaoui.org.sonarqube.ws.thirdparty.google.common.base.Strings.isNullOrEmpty;
import static java.util.Collections.singletonList;
import static java.util.Collections.unmodifiableSet;
import static java.util.Objects.requireNonNull;
abstract class BaseRequest implements WsRequest {
private final String path;
private String mediaType = MediaTypes.JSON;
private final DefaultParameters parameters = new DefaultParameters();
private final DefaultHeaders headers = new DefaultHeaders();
private OptionalInt timeOutInMs = OptionalInt.empty();
BaseRequest(String path) {
this.path = path;
}
@Override
public String getPath() {
return path;
}
@Override
public String getMediaType() {
return mediaType;
}
@Override
public OptionalInt getTimeOutInMs() {
return timeOutInMs;
}
public SELF setTimeOutInMs(int timeOutInMs) {
this.timeOutInMs = OptionalInt.of(timeOutInMs);
return (SELF) this;
}
/**
* Expected media type of response. Default is {@link MediaTypes#JSON}.
*/
@SuppressWarnings("unchecked")
public SELF setMediaType(String s) {
requireNonNull(s, "media type of response cannot be null");
this.mediaType = s;
return (SELF) this;
}
public SELF setParam(String key, @Nullable String value) {
return setSingleValueParam(key, value);
}
public SELF setParam(String key, @Nullable Integer value) {
return setSingleValueParam(key, value);
}
public SELF setParam(String key, @Nullable Long value) {
return setSingleValueParam(key, value);
}
public SELF setParam(String key, @Nullable Float value) {
return setSingleValueParam(key, value);
}
public SELF setParam(String key, @Nullable Boolean value) {
return setSingleValueParam(key, value);
}
@SuppressWarnings("unchecked")
private SELF setSingleValueParam(String key, @Nullable Object value) {
checkArgument(!isNullOrEmpty(key), "a WS parameter key cannot be null");
if (value == null) {
return (SELF) this;
}
parameters.setValue(key, value.toString());
return (SELF) this;
}
@SuppressWarnings("unchecked")
public SELF setParam(String key, @Nullable Collection extends Object> values) {
checkArgument(!isNullOrEmpty(key), "a WS parameter key cannot be null");
if (values == null || values.isEmpty()) {
return (SELF) this;
}
parameters.setValues(key, values.stream()
.filter(Objects::nonNull)
.map(Object::toString)
.collect(Collectors.toList()));
return (SELF) this;
}
@Override
public Map getParams() {
return parameters.keyValues.keySet().stream()
.collect(Collectors.toMap(
Function.identity(),
key -> parameters.keyValues.get(key).get(0),
(v1, v2) -> {
throw new IllegalStateException(String.format("Duplicate key '%s' in request", v1));
},
LinkedHashMap::new));
}
@Override
public Parameters getParameters() {
return parameters;
}
@Override
public Headers getHeaders() {
return headers;
}
@SuppressWarnings("unchecked")
public SELF setHeader(String name, @Nullable String value) {
requireNonNull(name, "Header name can't be null");
headers.setValue(name, value);
return (SELF) this;
}
private static class DefaultParameters implements Parameters {
// preserve insertion order
private final ListMultimap keyValues = LinkedListMultimap.create();
@Override
@CheckForNull
public String getValue(String key) {
return keyValues.containsKey(key) ? keyValues.get(key).get(0) : null;
}
@Override
public List getValues(String key) {
return keyValues.get(key);
}
@Override
public Set getKeys() {
return keyValues.keySet();
}
private DefaultParameters setValue(String key, String value) {
checkArgument(!isNullOrEmpty(key));
checkArgument(value != null);
keyValues.putAll(key, singletonList(value));
return this;
}
private DefaultParameters setValues(String key, Collection values) {
checkArgument(!isNullOrEmpty(key));
checkArgument(values != null && !values.isEmpty());
this.keyValues.putAll(key, values.stream().map(Object::toString).filter(Objects::nonNull).collect(Collectors.toList()));
return this;
}
}
private static class DefaultHeaders implements Headers {
private final Map keyValues = new HashMap<>();
@Override
public Optional getValue(String name) {
return Optional.ofNullable(keyValues.get(name));
}
private DefaultHeaders setValue(String name, @Nullable String value) {
checkArgument(!isNullOrEmpty(name));
if (value == null) {
keyValues.remove(name);
} else {
keyValues.put(name, value);
}
return this;
}
@Override
public Set getNames() {
return unmodifiableSet(keyValues.keySet());
}
}
}