Skip to content

Commit

Permalink
Reformat with prettier (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhaber authored Jul 14, 2023
1 parent bd42bdd commit 60adfab
Show file tree
Hide file tree
Showing 127 changed files with 2,066 additions and 1,200 deletions.
Empty file added .prettier-java-enabled
Empty file.
5 changes: 4 additions & 1 deletion src/main/java/com/hubspot/imap/ImapChannelAttrs.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import io.netty.util.AttributeKey;

public class ImapChannelAttrs {
public static final AttributeKey<ImapClientConfiguration> CONFIGURATION = AttributeKey.valueOf("IMAP_CONFIG");

public static final AttributeKey<ImapClientConfiguration> CONFIGURATION = AttributeKey.valueOf(
"IMAP_CONFIG"
);

private ImapChannelAttrs() {}
}
40 changes: 28 additions & 12 deletions src/main/java/com/hubspot/imap/ImapChannelInitializer.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
package com.hubspot.imap;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import com.google.common.net.HostAndPort;
import com.hubspot.imap.utils.LoggingSocks4ProxyHandler;

import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.ssl.SslContext;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;

@Sharable
public class ImapChannelInitializer extends ChannelInitializer<SocketChannel> {
private static final StringEncoder STRING_ENCODER = new StringEncoder(Charset.forName("UTF-8"));

private static final StringEncoder STRING_ENCODER = new StringEncoder(
Charset.forName("UTF-8")
);

private final SslContext sslContext;
private final String clientName;
private final ImapClientConfiguration configuration;

public ImapChannelInitializer(SslContext sslContext, String clientName, ImapClientConfiguration configuration) {
public ImapChannelInitializer(
SslContext sslContext,
String clientName,
ImapClientConfiguration configuration
) {
this.sslContext = sslContext;
this.clientName = clientName;
this.configuration = configuration;
}

public ImapChannelInitializer(String clientName, ImapClientConfiguration configuration) {
public ImapChannelInitializer(
String clientName,
ImapClientConfiguration configuration
) {
this(null, clientName, configuration);
}

Expand All @@ -37,14 +45,22 @@ protected void initChannel(SocketChannel socketChannel) throws Exception {

if (configuration.socksProxyConfig().isPresent()) {
HostAndPort proxyHost = configuration.socksProxyConfig().get().proxyHost();
channelPipeline.addLast(new LoggingSocks4ProxyHandler(clientName,
new InetSocketAddress(proxyHost.getHost(), proxyHost.getPort())));
channelPipeline.addLast(
new LoggingSocks4ProxyHandler(
clientName,
new InetSocketAddress(proxyHost.getHost(), proxyHost.getPort())
)
);
}

if (sslContext != null) {
channelPipeline.addLast(sslContext.newHandler(socketChannel.alloc(),
configuration.hostAndPort().getHost(),
configuration.hostAndPort().getPortOrDefault(993)));
channelPipeline.addLast(
sslContext.newHandler(
socketChannel.alloc(),
configuration.hostAndPort().getHost(),
configuration.hostAndPort().getPortOrDefault(993)
)
);
}

channelPipeline.addLast(STRING_ENCODER);
Expand Down
27 changes: 12 additions & 15 deletions src/main/java/com/hubspot/imap/ImapClientConfigurationIF.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
package com.hubspot.imap;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.net.HostAndPort;
import com.hubspot.imap.protocol.capabilities.AuthMechanism;
import com.hubspot.imap.utils.ConfigDefaults;
import java.util.List;
import java.util.Optional;

import javax.net.ssl.TrustManagerFactory;

import org.immutables.value.Value.Default;
import org.immutables.value.Value.Derived;
import org.immutables.value.Value.Immutable;
import org.immutables.value.Value.Style;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.net.HostAndPort;
import com.hubspot.imap.protocol.capabilities.AuthMechanism;
import com.hubspot.imap.utils.ConfigDefaults;

@Immutable
@Style(
typeAbstract = {"*IF"},
typeImmutable = "*"
)
@Style(typeAbstract = { "*IF" }, typeImmutable = "*")
@JsonDeserialize(as = ImapClientConfiguration.class)
@JsonSerialize(as = ImapClientConfiguration.class)
public interface ImapClientConfigurationIF {

HostAndPort hostAndPort();

/**
Expand All @@ -33,7 +26,9 @@ public interface ImapClientConfigurationIF {
@Derived
@Deprecated
default AuthMechanism authType() {
return allowedAuthMechanisms().size() > 0 ? allowedAuthMechanisms().get(0) : AuthMechanism.UNKNOWN;
return allowedAuthMechanisms().size() > 0
? allowedAuthMechanisms().get(0)
: AuthMechanism.UNKNOWN;
}

@Default
Expand Down Expand Up @@ -102,7 +97,9 @@ default Optional<ProxyConfig> proxyConfig() {
}

@Default
default Optional<SocksProxyConfig> socksProxyConfig() { return Optional.empty(); }
default Optional<SocksProxyConfig> socksProxyConfig() {
return Optional.empty();
}

@Default
default List<AuthMechanism> allowedAuthMechanisms() {
Expand Down
154 changes: 94 additions & 60 deletions src/main/java/com/hubspot/imap/ImapClientFactory.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
package com.hubspot.imap;

import java.io.Closeable;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.CompletableFuture;

import javax.net.ssl.SSLException;
import javax.net.ssl.TrustManagerFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.net.HostAndPort;
import com.hubspot.imap.client.ImapClient;
import com.hubspot.imap.protocol.command.ProxyCommand;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import java.io.Closeable;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.CompletableFuture;
import javax.net.ssl.SSLException;
import javax.net.ssl.TrustManagerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ImapClientFactory implements Closeable {

private static final Logger LOGGER = LoggerFactory.getLogger(ImapClientFactory.class);

private final ImapClientFactoryConfiguration configuration;
Expand All @@ -37,59 +34,92 @@ public ImapClientFactory() {
}

public ImapClientFactory(ImapClientFactoryConfiguration configuration) {
this(configuration, (KeyStore)null);
this(configuration, (KeyStore) null);
}

public ImapClientFactory(ImapClientFactoryConfiguration configuration, KeyStore keyStore) {
public ImapClientFactory(
ImapClientFactoryConfiguration configuration,
KeyStore keyStore
) {
this.configuration = configuration;

try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm()
);
trustManagerFactory.init(keyStore);

sslContext = SslContextBuilder.forClient()
.trustManager(trustManagerFactory)
.build();
sslContext =
SslContextBuilder.forClient().trustManager(trustManagerFactory).build();
} catch (NoSuchAlgorithmException | SSLException | KeyStoreException e) {
throw new RuntimeException(e);
}
}

public CompletableFuture<ImapClient> connect(ImapClientConfiguration clientConfiguration) {
public CompletableFuture<ImapClient> connect(
ImapClientConfiguration clientConfiguration
) {
return connect("unknown-client", clientConfiguration);
}

public CompletableFuture<ImapClient> connect(String clientName, ImapClientConfiguration clientConfiguration) {
Supplier<SslContext> sslContextSupplier = Suppliers.memoize(() -> getSslContext(clientConfiguration));
boolean useSsl = clientConfiguration.useSsl() && !clientConfiguration.proxyConfig().isPresent();

Bootstrap bootstrap = new Bootstrap().group(configuration.eventLoopGroup())
.option(ChannelOption.SO_LINGER, clientConfiguration.soLinger())
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, clientConfiguration.connectTimeoutMillis())
.option(ChannelOption.SO_KEEPALIVE, false)
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
.channel(configuration.channelClass())
.handler(useSsl
? new ImapChannelInitializer(sslContextSupplier.get(), clientName, clientConfiguration)
: new ImapChannelInitializer(clientName, clientConfiguration));
public CompletableFuture<ImapClient> connect(
String clientName,
ImapClientConfiguration clientConfiguration
) {
Supplier<SslContext> sslContextSupplier = Suppliers.memoize(() ->
getSslContext(clientConfiguration)
);
boolean useSsl =
clientConfiguration.useSsl() && !clientConfiguration.proxyConfig().isPresent();

Bootstrap bootstrap = new Bootstrap()
.group(configuration.eventLoopGroup())
.option(ChannelOption.SO_LINGER, clientConfiguration.soLinger())
.option(
ChannelOption.CONNECT_TIMEOUT_MILLIS,
clientConfiguration.connectTimeoutMillis()
)
.option(ChannelOption.SO_KEEPALIVE, false)
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
.channel(configuration.channelClass())
.handler(
useSsl
? new ImapChannelInitializer(
sslContextSupplier.get(),
clientName,
clientConfiguration
)
: new ImapChannelInitializer(clientName, clientConfiguration)
);

HostAndPort connectHost = getConnectHost(clientConfiguration);

CompletableFuture<ImapClient> connectFuture = new CompletableFuture<>();


bootstrap.connect(connectHost.getHost(), connectHost.getPort()).addListener(f -> {
if (f.isSuccess()) {
Channel channel = ((ChannelFuture) f).channel();

ImapClient client = new ImapClient(clientConfiguration, channel, sslContextSupplier, configuration.executor(), clientName);
configuration.executor().execute(() -> {
connectFuture.complete(client);
});
} else {
configuration.executor().execute(() -> connectFuture.completeExceptionally(f.cause()));
}
});
bootstrap
.connect(connectHost.getHost(), connectHost.getPort())
.addListener(f -> {
if (f.isSuccess()) {
Channel channel = ((ChannelFuture) f).channel();

ImapClient client = new ImapClient(
clientConfiguration,
channel,
sslContextSupplier,
configuration.executor(),
clientName
);
configuration
.executor()
.execute(() -> {
connectFuture.complete(client);
});
} else {
configuration
.executor()
.execute(() -> connectFuture.completeExceptionally(f.cause()));
}
});

return handleProxyConnect(clientConfiguration, connectFuture);
}
Expand All @@ -99,31 +129,35 @@ private SslContext getSslContext(ImapClientConfiguration clientConfiguration) {
return sslContext;
}
try {
return SslContextBuilder.forClient()
.trustManager(clientConfiguration.trustManagerFactory().get())
.build();
return SslContextBuilder
.forClient()
.trustManager(clientConfiguration.trustManagerFactory().get())
.build();
} catch (SSLException e) {
throw new RuntimeException(e);
}
}

private CompletableFuture<ImapClient> handleProxyConnect(ImapClientConfiguration clientConfiguration,
CompletableFuture<ImapClient> connectFuture) {
private CompletableFuture<ImapClient> handleProxyConnect(
ImapClientConfiguration clientConfiguration,
CompletableFuture<ImapClient> connectFuture
) {
if (!clientConfiguration.proxyConfig().isPresent()) {
return connectFuture;
}
ProxyCommand proxyCommand = new ProxyCommand(
clientConfiguration.hostAndPort(),
clientConfiguration.proxyConfig().get().proxyLocalIpAddress()
clientConfiguration.hostAndPort(),
clientConfiguration.proxyConfig().get().proxyLocalIpAddress()
);
return connectFuture.thenCompose(imapClient ->
imapClient.send(proxyCommand)
.thenApply(ignored -> {
if (clientConfiguration.useSsl()) {
imapClient.addTlsToChannel();
}
return imapClient;
})
imapClient
.send(proxyCommand)
.thenApply(ignored -> {
if (clientConfiguration.useSsl()) {
imapClient.addTlsToChannel();
}
return imapClient;
})
);
}

Expand Down
Loading

0 comments on commit 60adfab

Please sign in to comment.