<commit_before>package viper.app.photo;


import com.amazon.s3.QueryStringAuthGenerator;
import java.io.File;
import java.net.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.*;
import org.jboss.netty.channel.socket.ClientSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import viper.net.server.chunkproxy.HttpChunkProxyHandler;
import viper.net.server.chunkproxy.HttpChunkRelayProxy;
import viper.net.server.chunkproxy.StaticFileServerHandler;
import viper.net.server.chunkproxy.s3.S3StandardChunkProxy;
import viper.net.server.chunkproxy.s3.S3StaticFileServerHandler;
import viper.net.server.router.HostRouterHandler;
import viper.net.server.router.RouteMatcher;
import viper.net.server.router.RouterHandler;
import viper.net.server.router.UriRouteMatcher;


public class PhotoServer
{
  private ServerBootstrap _bootstrap;

  public static PhotoServer create(int port,
                                   String awsId,
                                   String awsSecret,
                                   String bucketName)
    throws URISyntaxException
  {
    Set<ChannelHandlerContext> listeners = new CopyOnWriteArraySet<ChannelHandlerContext>();

    PhotoServer photoServer = new PhotoServer();

    photoServer._bootstrap =
      new ServerBootstrap(
        new NioServerSocketChannelFactory(
          Executors.newCachedThreadPool(),
          Executors.newCachedThreadPool()));

    Executor executor = Executors.newCachedThreadPool();
    ClientSocketChannelFactory cf = new NioClientSocketChannelFactory(executor, executor);

    QueryStringAuthGenerator authGenerator = new QueryStringAuthGenerator(awsId, awsSecret, false);

    String remoteHost = String.format("%s.s3.amazonaws.com", bucketName);

    String staticFileRoot = "src/main/resources/public";

    new File(staticFileRoot).mkdir();

    PhotoServerChannelPipelineFactory photoServerChannelPipelineFactory =
      new PhotoServerChannelPipelineFactory(
        authGenerator,
        bucketName,
        cf,
        URI.create(String.format("http://%s:%s", remoteHost, 80)),
        (1024*1024)*1024,
        staticFileRoot);

    HostRouterHandler hostRouterHandler =
      createHostRouterHandler(
        URI.create(String.format("http://localhost:%s", port)),
        photoServerChannelPipelineFactory);

    ServerPipelineFactory factory = new ServerPipelineFactory(hostRouterHandler);

    photoServer._bootstrap.setPipelineFactory(factory);
    photoServer._bootstrap.bind(new InetSocketAddress(port));

    return photoServer;
  }

  static HostRouterHandler createHostRouterHandler(URI localHost, ChannelPipelineFactory lhPipelineFactory)
  {
    ConcurrentHashMap<String, ChannelPipelineFactory> routes = new ConcurrentHashMap<String, ChannelPipelineFactory>();

    List<String> localhostNames = new ArrayList<String>();
    localhostNames.add(localHost.getHost());

    try {
      String osHostName = InetAddress.getLocalHost().getHostName();
      localhostNames.add(osHostName);
      localhostNames.add(InetAddress.getLocalHost().getCanonicalHostName());

      if (osHostName.contains("."))
      {
        localhostNames.add(osHostName.substring(0, osHostName.indexOf(".")));
      }
    } catch (UnknownHostException e) {
      e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }

    for (String hostname : localhostNames)
    {
      if (localHost.getPort() == 80)
      {
        routes.put(hostname, lhPipelineFactory);
      }
      routes.put(String.format("%s:%s", hostname, localHost.getPort()), lhPipelineFactory);
    }

    return new HostRouterHandler(routes);
  }

  private static class PhotoServerChannelPipelineFactory implements ChannelPipelineFactory
  {
    private final QueryStringAuthGenerator _authGenerator;
    private final ClientSocketChannelFactory _cf;
    private final URI _amazonHost;
    private final int _maxContentLength;
    private final String _bucketName;
    private final String _staticFileRoot;

    public PhotoServerChannelPipelineFactory(
      QueryStringAuthGenerator s3AuthGenerator,
      String s3BucketName,
      ClientSocketChannelFactory cf,
      URI amazonHost,
      int maxContentLength,
      String staticFileRoot)
    {
      _authGenerator = s3AuthGenerator;
      _bucketName = s3BucketName;
      _cf = cf;
      _amazonHost = amazonHost;
      _maxContentLength = maxContentLength;
      _staticFileRoot = staticFileRoot;
    }

    @Override
    public ChannelPipeline getPipeline() throws Exception
    {
      HttpChunkRelayProxy proxy =
        new S3StandardChunkProxy(
          _authGenerator,
          _bucketName,
          _cf,
          _amazonHost);

      FileUploadChunkRelayEventListener relayListener = new FileUploadChunkRelayEventListener();

      LinkedHashMap<RouteMatcher, ChannelHandler> localhostRoutes = new LinkedHashMap<RouteMatcher, ChannelHandler>();
      localhostRoutes.put(new UriRouteMatcher(UriRouteMatcher.MatchMode.startsWith, "/u/"), new HttpChunkProxyHandler(proxy, relayListener, _maxContentLength));
      localhostRoutes.put(new UriRouteMatcher(UriRouteMatcher.MatchMode.startsWith, "/d/"), new S3StaticFileServerHandler(_authGenerator, _bucketName, _cf, _amazonHost));
      localhostRoutes.put(new UriRouteMatcher(UriRouteMatcher.MatchMode.startsWith, "/"), new StaticFileServerHandler(_staticFileRoot, 60*60));<commit_msg>code cleanup<commit_after>package viper.app.photo;


import com.amazon.s3.QueryStringAuthGenerator;
import java.io.File;
import java.net.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.*;
import org.jboss.netty.channel.socket.ClientSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import viper.net.server.chunkproxy.HttpChunkProxyHandler;
import viper.net.server.chunkproxy.HttpChunkRelayProxy;
import viper.net.server.chunkproxy.StaticFileServerHandler;
import viper.net.server.chunkproxy.s3.S3StandardChunkProxy;
import viper.net.server.chunkproxy.s3.S3StaticFileServerHandler;
import viper.net.server.router.HostRouterHandler;
import viper.net.server.router.RouteMatcher;
import viper.net.server.router.RouterHandler;
import viper.net.server.router.UriRouteMatcher;


public class PhotoServer
{
  private ServerBootstrap _bootstrap;

  public static PhotoServer create(int port,
                                   String awsId,
                                   String awsSecret,
                                   String bucketName)
    throws URISyntaxException
  {
    Set<ChannelHandlerContext> listeners = new CopyOnWriteArraySet<ChannelHandlerContext>();

    PhotoServer photoServer = new PhotoServer();

    photoServer._bootstrap =
      new ServerBootstrap(
        new NioServerSocketChannelFactory(
          Executors.newCachedThreadPool(),
          Executors.newCachedThreadPool()));

    Executor executor = Executors.newCachedThreadPool();
    ClientSocketChannelFactory cf = new NioClientSocketChannelFactory(executor, executor);

    QueryStringAuthGenerator authGenerator = new QueryStringAuthGenerator(awsId, awsSecret, false);

    String remoteHost = String.format("%s.s3.amazonaws.com", bucketName);

    String staticFileRoot = "src/main/resources/public";

    new File(staticFileRoot).mkdir();

    PhotoServerChannelPipelineFactory photoServerChannelPipelineFactory =
      new PhotoServerChannelPipelineFactory(
        authGenerator,
        bucketName,
        cf,
        URI.create(String.format("http://%s:%s", remoteHost, 80)),
        (1024*1024)*1024,
        staticFileRoot);

    HostRouterHandler hostRouterHandler =
      createHostRouterHandler(
        URI.create(String.format("http://localhost:%s", port)),
        photoServerChannelPipelineFactory);

    ServerPipelineFactory factory = new ServerPipelineFactory(hostRouterHandler);

    photoServer._bootstrap.setPipelineFactory(factory);
    photoServer._bootstrap.bind(new InetSocketAddress(port));

    return photoServer;
  }

  static HostRouterHandler createHostRouterHandler(URI localHost, ChannelPipelineFactory lhPipelineFactory)
  {
    ConcurrentHashMap<String, ChannelPipelineFactory> routes = new ConcurrentHashMap<String, ChannelPipelineFactory>();

    List<String> localhostNames = new ArrayList<String>();
    localhostNames.add(localHost.getHost());

    try {
      String osHostName = InetAddress.getLocalHost().getHostName();
      localhostNames.add(osHostName);
      localhostNames.add(InetAddress.getLocalHost().getCanonicalHostName());

      if (osHostName.contains("."))
      {
        localhostNames.add(osHostName.substring(0, osHostName.indexOf(".")));
      }
    } catch (UnknownHostException e) {
      e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }

    for (String hostname : localhostNames)
    {
      if (localHost.getPort() == 80)
      {
        routes.put(hostname, lhPipelineFactory);
      }
      routes.put(String.format("%s:%s", hostname, localHost.getPort()), lhPipelineFactory);
    }

    return new HostRouterHandler(routes);
  }

  private static class PhotoServerChannelPipelineFactory implements ChannelPipelineFactory
  {
    private final QueryStringAuthGenerator _authGenerator;
    private final ClientSocketChannelFactory _cf;
    private final URI _amazonHost;
    private final int _maxContentLength;
    private final String _bucketName;
    private final String _staticFileRoot;

    public PhotoServerChannelPipelineFactory(
      QueryStringAuthGenerator s3AuthGenerator,
      String s3BucketName,
      ClientSocketChannelFactory cf,
      URI amazonHost,
      int maxContentLength,
      String staticFileRoot)
    {
      _authGenerator = s3AuthGenerator;
      _bucketName = s3BucketName;
      _cf = cf;
      _amazonHost = amazonHost;
      _maxContentLength = maxContentLength;
      _staticFileRoot = staticFileRoot;
    }

    @Override
    public ChannelPipeline getPipeline() throws Exception
    {
      HttpChunkRelayProxy proxy =
        new S3StandardChunkProxy(
          _authGenerator,
          _bucketName,
          _cf,
          _amazonHost);

      FileUploadChunkRelayEventListener relayListener = new FileUploadChunkRelayEventListener();

      LinkedHashMap<RouteMatcher, ChannelHandler> localhostRoutes = new LinkedHashMap<RouteMatcher, ChannelHandler>();
      localhostRoutes.put(new UriRouteMatcher(UriRouteMatcher.MatchMode.startsWith, "/u/"), new HttpChunkProxyHandler(proxy, relayListener, _maxContentLength));
      localhostRoutes.put(new UriRouteMatcher(UriRouteMatcher.MatchMode.startsWith, "/d/"), new S3StaticFileServerHandler(_authGenerator, _bucketName, _cf, _amazonHost));
      localhostRoutes.put(new UriRouteMatcher(UriRouteMatcher.MatchMode.startsWith, "/"), new StaticFileServerHandler(_staticFileRoot));
{'fraction_non_alphanumeric': 0.08618613452606227, 'fraction_numerical': 0.005332874591432995, 'mean_word_length': 4.075076385857704, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3825070', 'n_tokens_mistral': 3177, 'n_tokens_neox': 3079, 'n_words': 587}