Skip to content

Commit

Permalink
Replace Guava classes (#2215)
Browse files Browse the repository at this point in the history
Replace Guava classes
  • Loading branch information
pks-1981 committed Jun 14, 2023
1 parent f659ce0 commit a02ea76
Show file tree
Hide file tree
Showing 21 changed files with 82 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.github.tomakehurst.wiremock.admin;

import static com.github.tomakehurst.wiremock.http.RequestMethod.ANY;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Objects.requireNonNull;

import com.github.tomakehurst.wiremock.common.url.PathParams;
import com.github.tomakehurst.wiremock.common.url.PathTemplate;
Expand All @@ -28,8 +28,8 @@ public class RequestSpec {
private final PathTemplate uriTemplate;

public RequestSpec(RequestMethod method, String uriTemplate) {
checkNotNull(method);
checkNotNull(uriTemplate);
requireNonNull(method);
requireNonNull(uriTemplate);
this.method = method;
this.uriTemplate = new PathTemplate(uriTemplate);
}
Expand Down Expand Up @@ -66,9 +66,7 @@ public boolean equals(Object o) {
RequestSpec that = (RequestSpec) o;

if (!method.equals(that.method)) return false;
if (!uriTemplate.equals(that.uriTemplate)) return false;

return true;
return uriTemplate.equals(that.uriTemplate);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import static com.github.tomakehurst.wiremock.common.Exceptions.throwUnchecked;
import static com.github.tomakehurst.wiremock.common.HttpClientUtils.getEntityAsStringAndCloseStream;
import static com.github.tomakehurst.wiremock.security.NoClientAuthenticator.noClientAuthenticator;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Objects.requireNonNull;
import static org.apache.hc.core5.http.HttpHeaders.HOST;

import com.github.tomakehurst.wiremock.admin.*;
Expand Down Expand Up @@ -177,7 +177,6 @@ public ListStubMappingsResult listAllStubMappings() {
}

@Override
@SuppressWarnings("unchecked")
public SingleStubMappingResult getStubMapping(UUID id) {
return executeRequest(
adminRoutes.requestSpecForTask(GetStubMappingTask.class),
Expand Down Expand Up @@ -538,7 +537,7 @@ private String safelyExecuteRequest(String url, ClassicHttpRequest request) {

private String urlFor(Class<? extends AdminTask> taskClass) {
RequestSpec requestSpec = adminRoutes.requestSpecForTask(taskClass);
checkNotNull(requestSpec, "No admin task URL is registered for " + taskClass.getSimpleName());
requireNonNull(requestSpec, "No admin task URL is registered for " + taskClass.getSimpleName());
return String.format(ADMIN_URL_PREFIX + requestSpec.path(), scheme, host, port, urlPathPrefix);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.github.tomakehurst.wiremock.common;

import static com.google.common.base.Charsets.UTF_8;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.github.tomakehurst.wiremock.security.NotAuthorisedException;
import com.google.common.io.Files;
Expand All @@ -26,14 +26,15 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public abstract class AbstractFileSource implements FileSource {

protected final File rootDirectory;

public AbstractFileSource(File rootDirectory) {
protected AbstractFileSource(File rootDirectory) {
this.rootDirectory = rootDirectory;
}

Expand Down Expand Up @@ -80,7 +81,7 @@ public List<TextFile> listFilesRecursively() {
}

private void recursivelyAddFilesToList(File root, List<File> fileList) {
File[] files = root.listFiles();
File[] files = Optional.ofNullable(root.listFiles()).orElse(new File[0]);
for (File file : files) {
if (file.isDirectory()) {
recursivelyAddFilesToList(file, fileList);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2016-2022 Thomas Akehurst
* Copyright (C) 2016-2023 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,13 +15,14 @@
*/
package com.github.tomakehurst.wiremock.common;

import static com.github.tomakehurst.wiremock.common.Exceptions.throwUnchecked;
import static java.nio.charset.StandardCharsets.UTF_8;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class Encoding {

private Encoding() {}

private static Base64Encoder encoder = null;

private static Base64Encoder getInstance() {
Expand All @@ -48,11 +49,7 @@ public static String encodeBase64(byte[] content, boolean padding) {
return content != null ? getInstance().encode(content, padding) : null;
}

public static String urlEncode(String unencodedUrl) {
try {
return URLEncoder.encode(unencodedUrl, "utf-8");
} catch (UnsupportedEncodingException e) {
return throwUnchecked(e, String.class);
}
public static String urlEncode(String encodedUrl) {
return URLEncoder.encode(encodedUrl, UTF_8);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011-2021 Thomas Akehurst
* Copyright (C) 2011-2023 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,7 +15,7 @@
*/
package com.github.tomakehurst.wiremock.common;

import static com.google.common.base.Charsets.UTF_8;
import static java.nio.charset.StandardCharsets.UTF_8;

import java.io.IOException;
import org.apache.hc.core5.http.ClassicHttpResponse;
Expand All @@ -25,11 +25,13 @@

public class HttpClientUtils {

private HttpClientUtils() {}

public static String getEntityAsStringAndCloseStream(ClassicHttpResponse httpResponse) {
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
try {
String content = EntityUtils.toString(entity, UTF_8.name());
String content = EntityUtils.toString(entity, UTF_8);
entity.getContent().close();
return content;
} catch (IOException | ParseException ioe) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
*/
package com.github.tomakehurst.wiremock.common;

import static com.google.common.base.Charsets.UTF_8;
import static java.lang.System.lineSeparator;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.commons.lang3.StringUtils.getLevenshteinDistance;

import java.nio.charset.Charset;
import org.apache.commons.lang3.text.WordUtils;

public class Strings {

private Strings() {}

public static final Charset DEFAULT_CHARSET = UTF_8;

public static String stringFromBytes(byte[] bytes) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011-2021 Thomas Akehurst
* Copyright (C) 2011-2023 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,7 +15,7 @@
*/
package com.github.tomakehurst.wiremock.common;

import static com.google.common.base.Charsets.UTF_8;
import static java.nio.charset.StandardCharsets.UTF_8;

import java.io.File;
import java.net.URI;
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/com/github/tomakehurst/wiremock/common/Urls.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@
package com.github.tomakehurst.wiremock.common;

import static com.github.tomakehurst.wiremock.common.Exceptions.throwUnchecked;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.github.tomakehurst.wiremock.http.QueryParameter;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableListMultimap.Builder;
import com.google.common.collect.Maps;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
Expand All @@ -36,6 +35,8 @@

public class Urls {

private Urls() {}

public static Map<String, QueryParameter> splitQueryFromUrl(String url) {
String queryPart =
url.contains("?") && !url.endsWith("?") ? url.substring(url.indexOf('?') + 1) : null;
Expand All @@ -56,7 +57,7 @@ public static Map<String, QueryParameter> splitQuery(String query) {
return Collections.emptyMap();
}

Iterable<String> pairs = Splitter.on('&').split(query);
List<String> pairs = Arrays.stream(query.split("&")).collect(Collectors.toList());
Builder<String, String> builder = ImmutableListMultimap.builder();
for (String queryElement : pairs) {
int firstEqualsIndex = queryElement.indexOf('=');
Expand Down Expand Up @@ -93,11 +94,7 @@ public static String urlToPathParts(URI uri) {
}

public static String decode(String encoded) {
try {
return URLDecoder.decode(encoded, "utf-8");
} catch (UnsupportedEncodingException e) {
return throwUnchecked(e, String.class);
}
return URLDecoder.decode(encoded, UTF_8);
}

public static URL safelyCreateURL(String url) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

import static java.lang.String.format;

import com.google.common.base.Objects;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -102,7 +102,7 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PathTemplate that = (PathTemplate) o;
return Objects.equal(templateString, that.templateString);
return Objects.equals(templateString, that.templateString);
}

@Override
Expand Down Expand Up @@ -156,7 +156,7 @@ void addVariable(String variable) {

void addWildcard() {
templatePattern.append("(.*?)");
templateVariables.add("" + wildcardCount++);
templateVariables.add(String.valueOf(wildcardCount++));
}

Parser build() {
Expand Down Expand Up @@ -213,13 +213,13 @@ public String apply(PathParams input) {
}

void addWildcard() {
final String wildcardIndex = "" + wildcardCount++;
final String wildcardIndex = String.valueOf(wildcardCount++);
class Wildcard implements Function<PathParams, String> {
@Override
public String apply(PathParams input) {
String value = input.get(wildcardIndex);
if (value == null) {
throw new IllegalArgumentException(format("Wildcard was not bound"));
throw new IllegalArgumentException("Wildcard was not bound");
}
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.github.tomakehurst.wiremock.global.GlobalSettings;
import com.github.tomakehurst.wiremock.store.SettingsStore;
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
import com.google.common.collect.ImmutableList;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.InetAddress;
Expand All @@ -51,10 +50,10 @@ public class ProxyResponseRenderer implements ResponseRenderer {
private static final String CONTENT_ENCODING = "content-encoding";
private static final String CONTENT_LENGTH = "content-length";
private static final String HOST_HEADER = "host";
public static final ImmutableList<String> FORBIDDEN_RESPONSE_HEADERS =
ImmutableList.of(TRANSFER_ENCODING, "connection");
public static final ImmutableList<String> FORBIDDEN_REQUEST_HEADERS =
ImmutableList.of(CONTENT_LENGTH, TRANSFER_ENCODING, "connection");
public static final List<String> FORBIDDEN_RESPONSE_HEADERS =
List.of(TRANSFER_ENCODING, "connection");
public static final List<String> FORBIDDEN_REQUEST_HEADERS =
List.of(CONTENT_LENGTH, TRANSFER_ENCODING, "connection");

private final CloseableHttpClient reverseProxyClient;
private final CloseableHttpClient forwardProxyClient;
Expand Down Expand Up @@ -84,7 +83,7 @@ public ProxyResponseRenderer(
proxySettings,
trustStoreSettings,
true,
Collections.<String>emptyList(),
Collections.emptyList(),
true);
forwardProxyClient =
HttpClientFactory.createClient(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2016-2022 Thomas Akehurst
* Copyright (C) 2016-2023 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@
package com.github.tomakehurst.wiremock.http.trafficlistener;

import static com.github.tomakehurst.wiremock.common.LocalNotifier.notifier;
import static java.nio.charset.StandardCharsets.UTF_8;

import java.net.Socket;
import java.nio.ByteBuffer;
Expand All @@ -24,10 +25,11 @@
import java.nio.charset.CharsetDecoder;

public class CollectingNetworkTrafficListener implements WiremockNetworkTrafficListener {

private final StringBuilder requestBuilder = new StringBuilder();
private final StringBuilder responseBuilder = new StringBuilder();

private final Charset charset = Charset.forName("UTF-8");
private final Charset charset = UTF_8;
private final CharsetDecoder decoder = charset.newDecoder();

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2014-2022 Thomas Akehurst
* Copyright (C) 2014-2023 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,9 +17,9 @@

import static com.github.tomakehurst.wiremock.common.Exceptions.throwUnchecked;
import static com.github.tomakehurst.wiremock.jetty.JettyUtils.unwrapResponse;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.github.tomakehurst.wiremock.core.FaultInjector;
import com.google.common.base.Charsets;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.Socket;
Expand All @@ -31,8 +31,7 @@

public class JettyFaultInjector implements FaultInjector {

private static final byte[] GARBAGE =
"lskdu018973t09sylgasjkfg1][]'./.sdlv".getBytes(Charsets.UTF_8);
private static final byte[] GARBAGE = "lskdu018973t09sylgasjkfg1][]'./.sdlv".getBytes(UTF_8);

private final Response response;
private final Socket socket;
Expand Down
Loading

0 comments on commit a02ea76

Please sign in to comment.