Skip to content

Commit

Permalink
#405 relay is clean
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Nov 8, 2023
1 parent fe9afb0 commit fd6c8a2
Show file tree
Hide file tree
Showing 24 changed files with 254 additions and 215 deletions.
1 change: 1 addition & 0 deletions s3auth-hosts/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public int status() {
@Override
public long writeTo(final OutputStream stream) throws IOException {
stream.write(this.content);
return (long) this.content.length;
return this.content.length;
}

@Override
Expand Down
16 changes: 11 additions & 5 deletions s3auth-relay/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,6 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
<!-- version from parent -->
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId>
<version>3.0.1u2</version>
</dependency>
<dependency>
<groupId>com.rexsl</groupId>
<artifactId>rexsl-test</artifactId>
Expand All @@ -162,6 +157,16 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
<artifactId>commons-net</artifactId>
<version>3.10.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
Expand Down Expand Up @@ -244,6 +249,7 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
<excludes>
<exclude>xml:/src/it/settings.xml</exclude>
<exclude>xml:/src/main/production/pom.xml</exclude>
<exclude>duplicatefinder:.*</exclude>
</excludes>
</configuration>
</plugin>
Expand Down
33 changes: 18 additions & 15 deletions s3auth-relay/src/main/java/com/s3auth/relay/FtpFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,17 @@ final class FtpFacade implements Closeable {
/**
* Executor service, with socket openers.
*/
private final transient ScheduledExecutorService frontend =
Executors.newScheduledThreadPool(2, new VerboseThreads("FTP-front"));
private final transient ScheduledExecutorService frontend;

/**
* Executor service, with consuming threads.
*/
private final transient ScheduledExecutorService backend =
Executors.newScheduledThreadPool(
FtpFacade.THREADS,
new VerboseThreads("FTP-back")
);
private final transient ScheduledExecutorService backend;

/**
* Blocking queue of ready-to-be-processed sockets.
*/
private final transient BlockingQueue<Socket> sockets =
new SynchronousQueue<>();
private final transient BlockingQueue<Socket> sockets;

/**
* Server socket.
Expand All @@ -105,12 +99,21 @@ final class FtpFacade implements Closeable {
* @param port Port number
* @throws IOException If can't initialize
*/
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
FtpFacade(@NotNull final Hosts hosts, final int port)
throws IOException {
this.frontend = Executors.newScheduledThreadPool(
2, new VerboseThreads("FTP-front")
);
this.backend = Executors.newScheduledThreadPool(
FtpFacade.THREADS,
new VerboseThreads("FTP-back")
);
this.sockets = new SynchronousQueue<>();
this.server = new ServerSocket(port);
final FtpThread thread = new FtpThread(this.sockets, hosts);
final Runnable runnable = new VerboseRunnable(
new FtpFacade.FTPThreadRunnable(thread), true, true
new FtpThreadRunnable(thread), true, true
);
for (int idx = 0; idx < FtpFacade.THREADS; ++idx) {
this.backend.scheduleWithFixedDelay(
Expand All @@ -125,9 +128,7 @@ final class FtpFacade implements Closeable {
*/
public void listen() {
this.frontend.scheduleWithFixedDelay(
new VerboseRunnable(
() -> FtpFacade.this.process(FtpFacade.this.server)
),
new VerboseRunnable(() -> this.process(this.server)),
0L, 1L, TimeUnit.NANOSECONDS
);
}
Expand Down Expand Up @@ -206,8 +207,10 @@ private void shutdown(final ExecutorService service)

/**
* Dispatcher of FTPThread.
*
* @since 0.0.1
*/
private static final class FTPThreadRunnable implements Runnable {
private static final class FtpThreadRunnable implements Runnable {
/**
* The thread to run.
*/
Expand All @@ -217,7 +220,7 @@ private static final class FTPThreadRunnable implements Runnable {
* Constructor.
* @param thrd The FTPThread
*/
FTPThreadRunnable(final FtpThread thrd) {
FtpThreadRunnable(final FtpThread thrd) {
this.thread = thrd;
}

Expand Down
3 changes: 2 additions & 1 deletion s3auth-relay/src/main/java/com/s3auth/relay/FtpResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public FtpResponse withText(@NotNull final String text) {
* @return The response's canonical String representation ($code $text)
*/
public String asString() {
return "";
return this.toString();
}

@Override
Expand All @@ -92,6 +92,7 @@ public String toString() {
* Send it to the socket.
* @param socket The socket to write to
* @return How many bytes were actually sent
* @checkstyle NonStaticMethodCheck (10 lines)
*/
@Loggable(
value = Loggable.DEBUG, limit = Integer.MAX_VALUE,
Expand Down
27 changes: 16 additions & 11 deletions s3auth-relay/src/main/java/com/s3auth/relay/HttpFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,17 @@ final class HttpFacade implements Closeable {
/**
* Executor service, with socket openers.
*/
private final transient ScheduledExecutorService frontend =
Executors.newScheduledThreadPool(2, new VerboseThreads("front"));
private final transient ScheduledExecutorService frontend;

/**
* Executor service, with consuming threads.
*/
private final transient ScheduledExecutorService backend =
Executors.newScheduledThreadPool(
HttpFacade.THREADS,
new VerboseThreads("back")
);
private final transient ScheduledExecutorService backend;

/**
* Blocking queue of ready-to-be-processed sockets.
*/
private final transient BlockingQueue<Socket> sockets =
new SynchronousQueue<>();
private final transient BlockingQueue<Socket> sockets;

/**
* Server socket.
Expand All @@ -114,9 +108,16 @@ final class HttpFacade implements Closeable {
* @param sslport SSL port number.
* @throws IOException If can't initialize
*/
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
HttpFacade(@NotNull final Hosts hosts, final int port, final int sslport)
throws IOException {
this.frontend = Executors.newScheduledThreadPool(2, new VerboseThreads("front"));
this.server = new ServerSocket(port);
this.backend = Executors.newScheduledThreadPool(
HttpFacade.THREADS,
new VerboseThreads("back")
);
this.sockets = new SynchronousQueue<>();
this.secured = SSLServerSocketFactory.getDefault()
.createServerSocket(sslport);
final Runnable runnable = new VerboseRunnable(
Expand All @@ -139,13 +140,13 @@ final class HttpFacade implements Closeable {
public void listen() {
this.frontend.scheduleWithFixedDelay(
new VerboseRunnable(
() -> HttpFacade.this.process(HttpFacade.this.server)
() -> this.process(this.server)
),
0L, 1L, TimeUnit.NANOSECONDS
);
this.frontend.scheduleWithFixedDelay(
new VerboseRunnable(
() -> HttpFacade.this.process(HttpFacade.this.secured)
() -> this.process(this.secured)
),
0L, 1L, TimeUnit.NANOSECONDS
);
Expand Down Expand Up @@ -232,19 +233,23 @@ private void shutdown(final ExecutorService service)

/**
* Dispatcher of HttpThread.
*
* @since 0.0.1
*/
private static final class HttpThreadRunnable implements Runnable {
/**
* The thread to run.
*/
private final transient HttpThread thread;

/**
* Constructor.
* @param thrd The HttpThread
*/
HttpThreadRunnable(final HttpThread thrd) {
this.thread = thrd;
}

@Override
public void run() {
try {
Expand Down
13 changes: 7 additions & 6 deletions s3auth-relay/src/main/java/com/s3auth/relay/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.net.Socket;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -52,7 +53,6 @@
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.apache.commons.collections4.map.CaseInsensitiveMap;
import org.apache.commons.io.Charsets;
import org.apache.commons.lang3.StringUtils;

/**
Expand Down Expand Up @@ -141,9 +141,10 @@ final class HttpRequest {
* @see <a href="http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol">HTTP</a>
* @checkstyle ExecutableStatementCountCheck (100 lines)
*/
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
HttpRequest(@NotNull final Socket socket) throws IOException {
final BufferedReader reader = new BufferedReader(
new InputStreamReader(socket.getInputStream(), Charsets.UTF_8)
new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8)
);
final String top = reader.readLine();
if (top == null) {
Expand All @@ -159,7 +160,7 @@ final class HttpRequest {
String.format("invalid first line: '%s'", top)
);
}
this.parms = Collections.unmodifiableMap(this.parseParameters(top));
this.parms = Collections.unmodifiableMap(HttpRequest.parseParameters(top));
final String method = matcher.group(1);
if (!"GET".equals(method) && !"HEAD".equals(method)) {
throw new HttpException(
Expand Down Expand Up @@ -187,7 +188,7 @@ final class HttpRequest {
}
headers.add(line);
}
this.hdrs = Collections.unmodifiableMap(this.parseHeaders(headers));
this.hdrs = Collections.unmodifiableMap(HttpRequest.parseHeaders(headers));
}

/**
Expand Down Expand Up @@ -260,7 +261,7 @@ public Range range() throws HttpException {
* @return Map of headers
* @throws HttpException If some socket problem
*/
private Map<String, Collection<String>> parseHeaders(
private static Map<String, Collection<String>> parseHeaders(
final Iterable<String> lines) throws HttpException {
final Map<String, Collection<String>> map =
new CaseInsensitiveMap<>();
Expand All @@ -287,7 +288,7 @@ private Map<String, Collection<String>> parseHeaders(
* @param request Request string
* @return Map of headers
*/
private Map<String, Collection<String>> parseParameters(
private static Map<String, Collection<String>> parseParameters(
final CharSequence request) {
final Map<String, Collection<String>> map =
new HashMap<>(0);
Expand Down
4 changes: 2 additions & 2 deletions s3auth-relay/src/main/java/com/s3auth/relay/HttpResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.LinkedList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.validation.constraints.NotNull;
import lombok.EqualsAndHashCode;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.io.Charsets;

/**
* HTTP response, writable to IO socket.
Expand Down Expand Up @@ -157,7 +157,7 @@ public HttpResponse withBody(@NotNull final String text) {
)
public long send(@NotNull final Socket socket) throws IOException {
final OutputStream stream = socket.getOutputStream();
final Writer writer = new OutputStreamWriter(stream, Charsets.UTF_8);
final Writer writer = new OutputStreamWriter(stream, StandardCharsets.UTF_8);
try {
writer.write(
String.format(
Expand Down
5 changes: 3 additions & 2 deletions s3auth-relay/src/main/java/com/s3auth/relay/LocalHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import com.s3auth.hosts.Resource;
import com.s3auth.hosts.Stats;
import com.s3auth.hosts.Version;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
Expand Down Expand Up @@ -75,6 +74,7 @@ final class LocalHost implements Host {
* @param name The name of host, provided in "Host" HTTP header
* @return TRUE if this is a localhost
*/
@SuppressWarnings("PMD.ProhibitPublicStaticMethods")
public static boolean isIt(@NotNull final String name) {
return "relay.s3auth.com".equals(name);
}
Expand Down Expand Up @@ -136,7 +136,6 @@ public Stats stats() {
* @return The exception to throw
*/
@SuppressWarnings("PMD.DoNotCallSystemExit")
@SuppressFBWarnings("DM_EXIT")
private IOException halt(final String uri) {
if (uri.equals(LocalHost.SHUTDOWN)) {
Logger.warn(this, "fetch(%s): shutting down..", uri);
Expand All @@ -155,6 +154,8 @@ private IOException halt(final String uri) {

/**
* Dummy host stats.
*
* @since 0.0.1
*/
private static final class DummyStats implements Stats {
@Override
Expand Down
6 changes: 1 addition & 5 deletions s3auth-relay/src/main/java/com/s3auth/relay/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
*/
package com.s3auth.relay;

import com.amazonaws.SDKGlobalConfiguration;
import com.jcabi.aspects.Immutable;
import com.jcabi.aspects.Loggable;
import com.jcabi.log.Logger;
Expand Down Expand Up @@ -65,6 +64,7 @@ private Main() {
* @todo #213:30min Create a FtpFacade in order to provide a FTP gateway.
* Also unignore test 'connectDisconnect' in FtpFacadeTest.
*/
@SuppressWarnings("PMD.ProhibitPublicStaticMethods")
public static void main(final String[] args) throws Exception {
final OptionParser parser = new OptionParser("p:s:d");
final OptionSet options = parser.parse(args);
Expand All @@ -83,10 +83,6 @@ public static void main(final String[] args) throws Exception {
final HttpFacade facade =
new HttpFacade(new DynamoHosts(), port, secured);
facade.listen();
System.setProperty(
SDKGlobalConfiguration.ENABLE_S3_SIGV4_SYSTEM_PROPERTY,
"true"
);
Logger.warn(Main.class, "started at http://localhost:%d...", port);
if (options.has("d")) {
while (true) {
Expand Down
Loading

0 comments on commit fd6c8a2

Please sign in to comment.