Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Guava net module with JDK equivalents #225

Merged
merged 7 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/main/java/com/treasuredata/client/TDClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
import java.util.Properties;
import java.util.regex.Pattern;

import static com.google.common.net.UrlEscapers.urlPathSegmentEscaper;
import static java.util.Objects.requireNonNull;

/**
Expand Down Expand Up @@ -185,7 +184,7 @@ protected static String buildUrl(String urlPrefix, String... args)
s.append(urlPrefix);
for (String a : args) {
s.append("/");
s.append(urlPathSegmentEscaper().escape(a));
s.append(UrlPathSegmentEscaper.escape(a));
}
return s.toString();
}
Expand Down Expand Up @@ -1034,7 +1033,7 @@ public TDBulkLoadSessionStartResult startBulkLoadSession(String name, TDBulkLoad
@Override
public long lookupConnection(String name)
{
return doGet(buildUrl("/v3/connections/lookup?name=" + urlPathSegmentEscaper().escape(name)), TDConnectionLookupResult.class).getId();
return doGet(buildUrl("/v3/connections/lookup?name=" + UrlPathSegmentEscaper.escape(name)), TDConnectionLookupResult.class).getId();
}

@Override
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/treasuredata/client/TDHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@
import java.util.regex.Pattern;
import java.util.stream.Stream;

import static com.google.common.net.HttpHeaders.AUTHORIZATION;
import static com.google.common.net.HttpHeaders.CONTENT_LENGTH;
import static com.google.common.net.HttpHeaders.DATE;
import static com.google.common.net.HttpHeaders.LOCATION;
import static com.google.common.net.HttpHeaders.USER_AGENT;
import static com.treasuredata.client.TDApiRequest.urlEncode;
import static com.treasuredata.client.TDClientException.ErrorType.INVALID_JSON_RESPONSE;
import static com.treasuredata.client.TDHttpRequestHandler.ResponseContext;
Expand All @@ -79,6 +74,12 @@ public class TDHttpClient
{
private static final Logger logger = LoggerFactory.getLogger(TDHttpClient.class);

private static final String AUTHORIZATION = "Authorization";
private static final String CONTENT_LENGTH = "Content-Length";
private static final String DATE = "Date";
private static final String LOCATION = "Location";
private static final String USER_AGENT = "User-Agent";

// Used for reading JSON response
static ObjectMapper defaultObjectMapper = new ObjectMapper()
.registerModule(new JsonOrgModule()) // for mapping query json strings into JSONObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static com.google.common.net.HttpHeaders.RETRY_AFTER;
import static com.treasuredata.client.TDClientException.ErrorType.CLIENT_ERROR;
import static com.treasuredata.client.TDClientException.ErrorType.INVALID_INPUT;
import static com.treasuredata.client.TDClientException.ErrorType.INVALID_JSON_RESPONSE;
Expand All @@ -43,6 +42,8 @@
*/
public class TDRequestErrorHandler
{
private static final String RETRY_AFTER = "Retry-After";

private TDRequestErrorHandler()
{
}
Expand Down
85 changes: 85 additions & 0 deletions src/main/java/com/treasuredata/client/UrlPathSegmentEscaper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.treasuredata.client;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Escape URL path segment in a compatible way with com.google.common.net.UrlEscapers.urlPathSegmentEscaper;
*/
final class UrlPathSegmentEscaper
{
private UrlPathSegmentEscaper()
{
}

private static String replaceAllMatched(String text, Pattern pattern)
{
Matcher matcher = pattern.matcher(text);
if (!matcher.find()) {
return text;
}
StringBuilder sb = new StringBuilder();
int previousEnd = 0;
do {
sb.append(text.subSequence(previousEnd, matcher.start()));
sb.append(replace(matcher.group()));
previousEnd = matcher.end();
} while (matcher.find());
sb.append(text.subSequence(previousEnd, text.length()));
return sb.toString();
}

private static final Pattern GUAVA_INCOMPATIBLE = Pattern.compile("\\+|%(?:2[146789BC]|3[ABD]|7E|40)");

private static String replace(String token)
{
switch (token) {
case "+": return "%20";
case "%21": return "!";
case "%24": return "$";
case "%26": return "&";
case "%27": return "'";
case "%28": return "(";
case "%29": return ")";
case "%2B": return "+";
case "%2C": return ",";
case "%3A": return ":";
case "%3B": return ";";
case "%3D": return "=";
case "%7E": return "~";
case "%40": return "@";
default: throw new IllegalStateException("Unknown token: " + token);
}
}

static String escape(String s)
{
try {
String encoded = URLEncoder.encode(s, "UTF-8");
return replaceAllMatched(encoded, GUAVA_INCOMPATIBLE);
}
catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@
import java.io.IOException;
import java.util.Optional;

import static com.google.common.net.HttpHeaders.PROXY_AUTHORIZATION;

/**
*
*/
public class ProxyAuthenticator
implements Authenticator
{
private static final String PROXY_AUTHORIZATION = "Proxy-Authorization";
private final Logger logger = LoggerFactory.getLogger(ProxyAuthenticator.class);
private final ProxyConfig proxyConfig;
private Optional<String> proxyAuthCache = Optional.empty();
Expand Down
7 changes: 4 additions & 3 deletions src/test/java/com/treasuredata/client/TestServerFailures.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package com.treasuredata.client;

import com.google.common.net.HttpHeaders;
import okhttp3.mockwebserver.Dispatcher;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
Expand Down Expand Up @@ -52,6 +51,8 @@
*/
public class TestServerFailures
{
private static final String CONTENT_TYPE = "Content-Type";

private static Logger logger = LoggerFactory.getLogger(TestServerFailures.class);

private MockWebServer server;
Expand Down Expand Up @@ -275,8 +276,8 @@ public MockResponse dispatch(RecordedRequest request)
public void corruptedJsonResponse()
throws Exception
{
server.enqueue(new MockResponse().setBody("{broken json}").setHeader(HttpHeaders.CONTENT_TYPE, "plain/text"));
server.enqueue(new MockResponse().setBody("{\"database\":1}").setHeader(HttpHeaders.CONTENT_TYPE, "plain/text"));
server.enqueue(new MockResponse().setBody("{broken json}").setHeader(CONTENT_TYPE, "plain/text"));
server.enqueue(new MockResponse().setBody("{\"database\":1}").setHeader(CONTENT_TYPE, "plain/text"));
server.start(port);

TDClient client = TDClient
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/treasuredata/client/TestTDHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import static com.google.common.net.HttpHeaders.USER_AGENT;
import static com.treasuredata.client.TDHttpRequestHandlers.stringContentHandler;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
Expand All @@ -60,6 +59,8 @@
*/
public class TestTDHttpClient
{
private static final String USER_AGENT = "User-Agent";

private static Logger logger = LoggerFactory.getLogger(TestTDHttpClient.class);
private TDHttpClient client;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.treasuredata.client;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* Ensure compatibility with com.google.common.net.UrlEscapers.urlPathSegmentEscaper;
*/
public class TestUrlPathSegmentEscaper
{
@Test
public void nonAscii()
{
String v = UrlPathSegmentEscaper.escape("あ漢ü🇸🇨");
assertEquals("%E3%81%82%E6%BC%A2%C3%BC%F0%9F%87%B8%F0%9F%87%A8", v);
}

@Test
public void space()
{
String v = UrlPathSegmentEscaper.escape(" ");
assertEquals("%20", v);
}

@Test
public void specialCharsUnescaped()
{
String v = UrlPathSegmentEscaper.escape("-_.~!*'();:@&=+$,");
assertEquals("-_.~!*'();:@&=+$,", v);
}

@Test
public void specialCharsEscaped()
{
String v = UrlPathSegmentEscaper.escape("/?%#[]^¥|{}\\<>");
assertEquals("%2F%3F%25%23%5B%5D%5E%C2%A5%7C%7B%7D%5C%3C%3E", v);
}

@Test
public void alphaNum()
{
String v = UrlPathSegmentEscaper.escape("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
assertEquals("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", v);
}
}