text:
package io.gatling.http.client.impl;

import static java.util.Collections.singletonList;

import io.gatling.http.client.HttpClient;
import io.gatling.http.client.HttpClientConfig;
import io.gatling.http.client.HttpListener;
import io.gatling.http.client.Request;
import io.gatling.http.client.body.is.InputStreamRequestBody;
import io.gatling.http.client.impl.compression.CustomDelegatingDecompressorFrameListener;
import io.gatling.http.client.impl.compression.CustomHttpContentDecompressor;
import io.gatling.http.client.pool.ChannelPool;
import io.gatling.http.client.pool.ChannelPoolKey;
import io.gatling.http.client.pool.RemoteKey;
import io.gatling.http.client.proxy.ProxyServer;
import io.gatling.http.client.proxy.SockProxyServer;
import io.gatling.http.client.ssl.Tls;
import io.gatling.http.client.uri.Uri;
import io.gatling.http.client.util.Pair;
import io.gatling.netty.util.Transports;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.websocketx.WebSocketFrameAggregator;
import io.netty.handler.codec.http2.*;
import io.netty.handler.ssl.ApplicationProtocolNames;
import io.netty.handler.ssl.ApplicationProtocolNegotiationHandler;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.resolver.NoopAddressResolverGroup;
import io.netty.util.NetUtil;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.concurrent.*;
import io.netty.util.internal.logging.InternalLoggerFactory;
import io.netty.util.internal.logging.Slf4JLoggerFactory;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DefaultHttpClient implements HttpClient {

  private static final Http2Settings DEFAULT_HTTP2_SETTINGS = Http2Settings.defaultSettings();

  static {
    InternalLoggerFactory.setDefaultFactory(Slf4JLoggerFactory.INSTANCE);
  }

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

  private static final String PINNED_HANDLER = "pinned";
  private static final String PROXY_HANDLER = "proxy";
  private static final String SSL_HANDLER = "ssl";
  public static final String HTTP_CLIENT_CODEC = "http";
  private static final String HTTP2_HANDLER = "http2";
  private static final String INFLATER_HANDLER = "inflater";
  private static final String CHUNKED_WRITER_HANDLER = "chunked-writer";
  private static final String WS_OBJECT_AGGREGATOR = "ws-object-aggregator";
  private static final String WS_COMPRESSION = "ws-compression";
  private static final String WS_FRAME_AGGREGATOR = "ws-frame-aggregator";
  private static final String APP_WS_HANDLER = "app-ws";
  private static final String ALPN_HANDLER = "alpn";
  static final String APP_HTTP2_HANDLER = "app-http2";

  public static final String APP_HTTP_HANDLER = "app-http";

  private ChannelHandler newHttpClientCodec() {
    return new HttpClientCodec(4096, Integer.MAX_VALUE, 8192, false, false, 128);
  }

  private class EventLoopResources {

    private final Bootstrap http1Bootstrap;
    private final Bootstrap http2Bootstrap;
    private final Bootstrap wsBootstrap;
    private final ChannelPool channelPool;

    private void addHttpHandlers(Channel channel) {
      channel
          .pipeline()
          .addLast(HTTP_CLIENT_CODEC, newHttpClientCodec())
          .addLast(INFLATER_HANDLER, new CustomHttpContentDecompressor())
          .addLast(CHUNKED_WRITER_HANDLER, new ChunkedWriteHandler())
          .addLast(
              APP_HTTP_HANDLER, new HttpAppHandler(DefaultHttpClient.this, channelPool, config));
    }

    private void addWsHandlers(Channel channel) {
      channel
          .pipeline()
          .addLast(HTTP_CLIENT_CODEC, newHttpClientCodec())
          .addLast(WS_OBJECT_AGGREGATOR, new HttpObjectAggregator(Integer.MAX_VALUE))
          .addLast(WS_COMPRESSION, AllowClientNoContextWebSocketClientCompressionHandler.INSTANCE)
          .addLast(WS_FRAME_AGGREGATOR, new WebSocketFrameAggregator(Integer.MAX_VALUE))
          .addLast(APP_WS_HANDLER, new WebSocketHandler(config));
    }

    private EventLoopResources(EventLoop eventLoop) {
      channelPool = new ChannelPool();
      long channelPoolIdleCleanerPeriod = config.getChannelPoolIdleCleanerPeriod();
      long idleTimeoutNanos = config.getChannelPoolIdleTimeout() * 1_000_000;
      eventLoop.scheduleWithFixedDelay(
          () -> channelPool.closeIdleChannels(idleTimeoutNanos),
          channelPoolIdleCleanerPeriod,
          channelPoolIdleCleanerPeriod,
          TimeUnit.MILLISECONDS);

      http1Bootstrap =
          new Bootstrap()
              .channelFactory(
                  Transports.newSocketChannelFactory(
                      config.isUseNativeTransport(), config.isUseIoUring()))
              .group(eventLoop)
              .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, (int) config.getConnectTimeout())
              .option(ChannelOption.SO_REUSEADDR, config.isSoReuseAddress())
              .option(ChannelOption.TCP_NODELAY, config.isTcpNoDelay())
              .option(ChannelOption.SO_KEEPALIVE, config.isSoKeepAlive())
              .resolver(NoopAddressResolverGroup.INSTANCE)
              .handler(
                  new ChannelInitializer<Channel>() {
                    @Override
                    protected void initChannel(Channel channel) {
                      channel.pipeline().addLast(PINNED_HANDLER, NoopHandler.INSTANCE);
                      addHttpHandlers(channel);
                    }
                  });

      http2Bootstrap =
          http1Bootstrap
              .clone()
              .handler(
                  new ChannelInitializer<Channel>() {
                    @Override
                    protected void initChannel(Channel channel) {
                      channel.pipeline().addLast(PINNED_HANDLER, NoopHandler.INSTANCE);
                    }
                  });

      wsBootstrap =
          http1Bootstrap
              .clone()
              .handler(
                  new ChannelInitializer<Channel>() {
                    @Override
                    protected void initChannel(Channel channel) {
                      channel.pipeline().addLast(PINNED_HANDLER, NoopHandler.INSTANCE);
                      addWsHandlers(channel);
                    }
                  });
    }

    private Bootstrap getHttp1BootstrapWithProxy(ProxyServer proxy) {
      return http1Bootstrap
          .clone()
          .handler(
              new ChannelInitializer<Channel>() {
                @Override
                protected void initChannel(Channel ch) {
                  ch.pipeline()
                      .addLast(PINNED_HANDLER, NoopHandler.INSTANCE)
                      .addLast(PROXY_HANDLER, proxy.newHandler());
                  addHttpHandlers(ch);
                }
              });
    }

    private Bootstrap getWsBootstrapWithProxy(ProxyServer proxy) {
      return wsBootstrap
          .clone()
          .handler(
              new ChannelInitializer<Channel>() {
                @Override
                protected void initChannel(Channel ch) {
                  ch.pipeline()
                      .addLast(PINNED_HANDLER, NoopHandler.INSTANCE)
                      .addLast(PROXY_HANDLER, proxy.newHandler());
                  addWsHandlers(ch);
                }
              });
    }
  }

  private final AtomicBoolean closed = new AtomicBoolean();
  private final HttpClientConfig config;
  private final EventExecutor channelGroupEventExecutor;
  private final ChannelGroup channelGroup;
  private final FastThreadLocal<EventLoopResources> eventLoopResources = new FastThreadLocal<>();

  public DefaultHttpClient(HttpClientConfig config) {
    this.config = config;
    channelGroupEventExecutor = new DefaultEventExecutor();
    channelGroup = new DefaultChannelGroup(channelGroupEventExecutor);
  }

  @Override
  public void close() {
    if (closed.compareAndSet(false, true)) {
      channelGroup.close().awaitUninterruptibly();
      channelGroupEventExecutor.shutdownGracefully(0, 1, TimeUnit.SECONDS);
      ReferenceCountUtil.release(config.getDefaultSslContext());
      ReferenceCountUtil.release(config.getDefaultAlpnSslContext());
    }
  }

  @Override
  public void sendRequest(
      Request request,
      long clientId,
      EventLoop eventLoop,
      HttpListener listener,
      SslContext sslContext,
      SslContext alpnSslContext) {
    if (isClosed()) {
      return;
    }

    if (sslContext == null) {
      sslContext = config.getDefaultSslContext();
      alpnSslContext = config.getDefaultAlpnSslContext();
    }

    HttpTx tx = buildTx(request, clientId, listener, sslContext, alpnSslContext);

    if (eventLoop.inEventLoop()) {
      sendTx(tx, eventLoop);
    } else if (!eventLoop.isShutdown()) {
      eventLoop.execute(() -> sendTx(tx, eventLoop));
    }
  }

  @Override
  public void sendHttp2Requests(
      Pair<Request, HttpListener>[] requestsAndListeners,
      long clientId,
      EventLoop eventLoop,
      SslContext sslContext,
      SslContext alpnSslContext) {
    if (isClosed()) {
      return;
    }
    for (Pair<Request, HttpListener> pair : requestsAndListeners) {
      pair.getRight().onSend();
    }

    Request headRequest = requestsAndListeners[0].getLeft();

    if (headRequest.getUri().isSecured() && headRequest.isHttp2Enabled() && !config.isEnableSni()) {
      for (Pair<Request, HttpListener> requestAndListener : requestsAndListeners) {
        HttpListener listener = requestAndListener.getRight();
        listener.onThrowable(
            new UnsupportedOperationException("HTTP/2 can't work if SNI is disabled."));
      }
      return;
    }

    if (sslContext == null) {
      sslContext = config.getDefaultSslContext();
      alpnSslContext = config.getDefaultAlpnSslContext();
    }

    List<HttpTx> txs = new ArrayList<>(requestsAndListeners.length);
    for (Pair<Request, HttpListener> requestAndListener : requestsAndListeners) {
      Request request = requestAndListener.getLeft();
      HttpListener listener = requestAndListener.getRight();
      txs.add(buildTx(request, clientId, listener, sslContext, alpnSslContext));
    }

    if (eventLoop.inEventLoop()) {
      sendHttp2Txs(txs, eventLoop);
    } else if (!eventLoop.isShutdown()) {
      eventLoop.execute(() -> sendHttp2Txs(txs, eventLoop));
    }
  }

  //////////////////// EVERYTHING BELOW ONLY HAPPENS IN SAME EVENTLOOP //////////////////

  private EventLoopResources eventLoopResources(EventLoop eventLoop) {
    EventLoopResources resources = eventLoopResources.get();
    if (resources == null) {
      resources = new EventLoopResources(eventLoop);
      eventLoopResources.set(resources);
    }
    return resources;
  }

  private HttpTx buildTx(
      Request request,
      long clientId,
      HttpListener listener,
      SslContext sslContext,
      SslContext alpnSslContext) {
    RequestTimeout requestTimeout =
        RequestTimeout.requestTimeout(request.getRequestTimeout(), listener);
    ChannelPoolKey key =
        new ChannelPoolKey(
            clientId,
            RemoteKey.newKey(request.getUri(), request.getVirtualHost(), request.getProxyServer()));
    return new HttpTx(request, listener, requestTimeout, key, sslContext, alpnSslContext);
  }

  // only retry pooled keep-alive connections = when keep-alive timeout triggered server side while
  // we were writing and request can be replayed
  boolean canRetry(HttpTx tx) {
    return tx.channelState == HttpTx.ChannelState.POOLED
        && !(tx.request.getBody() instanceof InputStreamRequestBody
            && ((InputStreamRequestBody) tx.request.getBody())
                .isConsumed()); // InputStreamRequestBody can't be replayed
  }

  void retry(HttpTx tx, EventLoop eventLoop) {
    if (isClosed()) {
      return;
    }

    tx.channelState = HttpTx.ChannelState.RETRY;
    LOGGER.debug("Retrying with new HTTP/1.1 connection");
    sendTx(tx, eventLoop);
  }

  void retryHttp2(List<HttpTx> txs, EventLoop eventLoop) {
    if (isClosed()) {
      return;
    }

    for (HttpTx tx : txs) {
      tx.channelState = HttpTx.ChannelState.RETRY;
    }
    LOGGER.debug("Retrying with new HTTP/2 connection");
    sendHttp2Txs(txs, eventLoop);
  }

  private void sendTx(HttpTx tx, EventLoop eventLoop) {

    EventLoopResources resources = eventLoopResources(eventLoop);
    Request request = tx.request;
    HttpListener listener = tx.listener;
    RequestTimeout requestTimeout = tx.requestTimeout;
    Uri requestUri = request.getUri();
    boolean tryHttp2 =
        request.isHttp2Enabled() && requestUri.isSecured() && !requestUri.isWebSocket();

    // use a fresh channel for WebSocket
    Channel pooledChannel = requestUri.isWebSocket() ? null : resources.channelPool.poll(tx.key);

    listener.onSend();
    if (tryHttp2 && !config.isEnableSni()) {
      listener.onThrowable(
          new UnsupportedOperationException("HTTP/2 can't work if SNI is disabled."));
      return;
    }

    // start timeout
    tx.requestTimeout.start(eventLoop);

    if (pooledChannel != null && tx.channelState != HttpTx.ChannelState.RETRY) {
      sendTxWithChannel(tx, pooledChannel);

    } else {
      InetSocketAddress unresolvedRemoteAddressThroughTunnelling =
          unresolvedRemoteAddressThroughTunnelling(request.getProxyServer(), requestUri);
      boolean logProxyAddress = unresolvedRemoteAddressThroughTunnelling != null;

      resolveRemoteAddresses(
              request,
              eventLoop,
              unresolvedRemoteAddressThroughTunnelling,
              listener,
              requestTimeout)
          .addListener(
              (Future<List<InetSocketAddress>> whenRemoteAddresses) -> {
                if (requestTimeout.isDone()) {
                  return;
                }

                if (whenRemoteAddresses.isSuccess()) {
                  List<InetSocketAddress> addresses = whenRemoteAddresses.getNow();

                  if (tryHttp2 && tx.channelState != HttpTx.ChannelState.RETRY) {
                    String domain = requestUri.getHost();
                    Channel coalescedChannel =
                        resources.channelPool.pollCoalescedChannel(
                            tx.key.clientId, domain, addresses);
                    if (coalescedChannel != null) {
                      tx.listener.onProtocolAwareness(true);
                      sendTxWithChannel(tx, coalescedChannel);
                    } else {
                      sendTxWithNewChannel(tx, resources, eventLoop, addresses, logProxyAddress);
                    }
                  } else {
                    sendTxWithNewChannel(tx, resources, eventLoop, addresses, logProxyAddress);
                  }
                }
              });
    }
  }

  private void sendHttp2Txs(List<HttpTx> txs, EventLoop eventLoop) {

    HttpTx tx = txs.get(0);
    EventLoopResources resources = eventLoopResources(eventLoop);
    Request request = tx.request;
    HttpListener listener = tx.listener;
    RequestTimeout requestTimeout = tx.requestTimeout;
    Uri requestUri = request.getUri();

    // start timeouts
    for (HttpTx t : txs) {
      t.requestTimeout.start(eventLoop);
    }

    ProxyServer proxyServer = request.getProxyServer();
    InetSocketAddress unresolvedRemoteAddressThroughTunnelling =
        unresolvedRemoteAddressThroughTunnelling(proxyServer, requestUri);
    boolean logProxyAddress = unresolvedRemoteAddressThroughTunnelling != null;

    resolveRemoteAddresses(
            request, eventLoop, unresolvedRemoteAddressThroughTunnelling, listener, requestTimeout)
        .addListener(
            (Future<List<InetSocketAddress>> whenRemoteAddresses) -> {
              if (requestTimeout.isDone()) {
                return;
              }

              if (whenRemoteAddresses.isSuccess()) {
                List<InetSocketAddress> addresses = whenRemoteAddresses.getNow();

                String domain = requestUri.getHost();
                Channel pooledChannel = resources.channelPool.poll(tx.key);
                if (pooledChannel == null) {
                  pooledChannel =
                      resources.channelPool.pollCoalescedChannel(
                          tx.key.clientId, domain, addresses);
                }

                if (pooledChannel != null) {
                  sendHttp2TxsWithChannel(txs, pooledChannel);
                } else {
                  sendHttp2TxsWithNewChannel(txs, resources, eventLoop, addresses, logProxyAddress);
                }
              }
            });
  }

  private void sendTxWithChannel(HttpTx tx, Channel channel) {

    if (isClosed()) {
      return;
    }

    if (ChannelPool.isHttp2(channel)) {
      tx.listener.onProtocolAwareness(true);
    }

    tx.requestTimeout.setChannel(channel);

    channel.write(tx);
  }

  private void sendHttp2TxsWithChannel(List<HttpTx> txs, Channel channel) {

    if (isClosed()) {
      return;
    }

    for (HttpTx tx : txs) {
      tx.requestTimeout.setChannel(channel);
      tx.listener.onProtocolAwareness(true);
      channel.write(tx);
    }
  }

  private InetSocketAddress unresolvedRemoteAddressThroughTunnelling(
      ProxyServer proxyServer, Uri requestUri) {
    return proxyServer != null
            && (proxyServer instanceof SockProxyServer
                || requestUri.isSecured()
                || requestUri.isWebSocket())
        ? InetSocketAddress.createUnresolved(requestUri.getHost(), requestUri.getExplicitPort())
        : null;
  }

  private Future<List<InetSocketAddress>> resolveRemoteAddresses(
      Request request,
      EventLoop eventLoop,
      InetSocketAddress unresolvedRemoteAddressThroughTunnelling,
      HttpListener listener,
      RequestTimeout requestTimeout) {
    ProxyServer proxyServer = request.getProxyServer();
    if (proxyServer != null) {
      InetSocketAddress remoteAddress =
          unresolvedRemoteAddressThroughTunnelling != null
              ?
              // ProxyHandler will take care of the connect logic
              unresolvedRemoteAddressThroughTunnelling
              :
              // directly connect to proxy over clear HTTP
              proxyServer.getAddress();

      return ImmediateEventExecutor.INSTANCE.newSucceededFuture(singletonList(remoteAddress));

    } else {
      Promise<List<InetSocketAddress>> p = eventLoop.newPromise();

      request
          .getNameResolver()
          .resolveAll(request.getUri().getHost(), eventLoop.newPromise(), listener)
          .addListener(
              (Future<List<InetAddress>> whenAddresses) -> {
                if (whenAddresses.isSuccess()) {
                  List<InetSocketAddress> remoteInetSocketAddresses =
                      whenAddresses.getNow().stream()
                          .map(
                              address ->
                                  new InetSocketAddress(
                                      address, request.getUri().getExplicitPort()))
                          .collect(Collectors.toList());

                  p.setSuccess(remoteInetSocketAddresses);
                } else {
                  if (!requestTimeout.isDone()) {
                    // only report if we haven't timed out
                    listener.onThrowable(whenAddresses.cause());
                  }
                  p.setFailure(whenAddresses.cause());
                  requestTimeout.cancel();
                }
              });
      return p;
    }
  }

  private void sendTxWithNewChannel(
      HttpTx tx,
      EventLoopResources resources,
      EventLoop eventLoop,
      List<InetSocketAddress> addresses,
      boolean logProxyAddress) {
    tx.channelState = HttpTx.ChannelState.NEW;
    openNewChannel(
            tx.request,
            logProxyAddress,
            eventLoop,
            resources,
            addresses,
            tx.listener,
            tx.requestTimeout)
        .addListener(
            (Future<Channel> whenNewChannel) -> {
              if (whenNewChannel.isSuccess()) {
                Channel channel = whenNewChannel.getNow();
                if (tx.requestTimeout.isDone()) {
                  channel.close();
                  return;
                }

                channelGroup.add(channel);
                ChannelPool.registerPoolKey(channel, tx.key);

                if (tx.request.getUri().isSecured()) {
                  LOGGER.debug("Installing SslHandler for {}", tx.request.getUri());
                  installSslHandler(tx, channel)
                      .addListener(
                          f -> {
                            if (tx.requestTimeout.isDone() || !f.isSuccess()) {
                              channel.close();
                              return;
                            }

                            if (tx.request.isAlpnRequired()) {
                              LOGGER.debug("Installing Http2Handler for {}", tx.request.getUri());
                              installHttp2Handler(tx, channel, resources.channelPool)
                                  .addListener(
                                      f2 -> {
                                        if (tx.requestTimeout.isDone() || !f2.isSuccess()) {
                                          channel.close();
                                          return;
                                        }
                                        sendTxWithChannel(tx, channel);
                                      });

                            } else {
                              sendTxWithChannel(tx, channel);
                            }
                          });
                } else {
                  sendTxWithChannel(tx, channel);
                }
              }
            });
  }

  private void sendHttp2TxsWithNewChannel(
      List<HttpTx> txs,
      EventLoopResources resources,
      EventLoop eventLoop,
      List<InetSocketAddress> addresses,
      boolean logProxyAddress) {
    HttpTx tx = txs.get(0);
    openNewChannel(
            tx.request,
            logProxyAddress,
            eventLoop,
            resources,
            addresses,
            tx.listener,
            tx.requestTimeout)
        .addListener(
            (Future<Channel> whenNewChannel) -> {
              if (whenNewChannel.isSuccess()) {
                Channel channel = whenNewChannel.getNow();
                if (tx.requestTimeout.isDone()) {
                  channel.close();
                  return;
                }

                channelGroup.add(channel);
                ChannelPool.registerPoolKey(channel, tx.key);

                LOGGER.debug("Installing SslHandler for {}", tx.request.getUri());
                installSslHandler(tx, channel)
                    .addListener(
                        f -> {
                          if (tx.requestTimeout.isDone() || !f.isSuccess()) {
                            channel.close();
                            return;
                          }
                          LOGGER.debug("Installing Http2Handler for {}", tx.request.getUri());
                          installHttp2Handler(tx, channel, resources.channelPool)
                              .addListener(
                                  f2 -> {
                                    if (tx.requestTimeout.isDone() || !f2.isSuccess()) {
                                      channel.close();
                                      return;
                                    }
                                    sendHttp2TxsWithChannel(txs, channel);
                                  });
                        });
              }
            });
  }

  private Bootstrap bootstrap(Request request, EventLoopResources resources) {
    Uri uri = request.getUri();
    ProxyServer proxyServer = request.getProxyServer();

    if (proxyServer != null) {
      if (uri.isWebSocket()) {
        return resources.getWsBootstrapWithProxy(proxyServer);
      } else if (proxyServer instanceof SockProxyServer || uri.isSecured()) {
        // FIXME HTTP/2 with proxy
        return resources.getHttp1BootstrapWithProxy(proxyServer);
      }
    }

    if (uri.isWebSocket()) {
      return resources.wsBootstrap;
    } else if (request.isAlpnRequired() && request.getUri().isSecured()) {
      return resources.http2Bootstrap;
    } else {
      return resources.http1Bootstrap;
    }
  }

  private static InetSocketAddress localAddressWithRandomPort(InetAddress localAddress) {
    return localAddress != null ? new InetSocketAddress(localAddress, 0) : null;
  }

  private Future<Channel> openNewChannel(
      Request request,
      boolean logProxyAddress,
      EventLoop eventLoop,
      EventLoopResources resources,
      List<InetSocketAddress> remoteAddresses,
      HttpListener listener,
      RequestTimeout requestTimeout) {
    LOGGER.debug("Opening new channel");
    Bootstrap bootstrap = bootstrap(request, resources);
    Promise<Channel> channelPromise = eventLoop.newPromise();
    InetSocketAddress loggedProxyAddress =
        logProxyAddress ? request.getProxyServer().getAddress() : null;
    openNewChannelRec(
        remoteAddresses,
        loggedProxyAddress,
        localAddressWithRandomPort(request.getLocalIpV4Address()),
        localAddressWithRandomPort(request.getLocalIpV6Address()),
        0,
        channelPromise,
        bootstrap,
        listener,
        requestTimeout);
    return channelPromise;
  }

  private static final Exception IGNORE_REQUEST_TIMEOUT_REACHED_WHILE_TRYING_TO_CONNECT =
      new TimeoutException("Request timeout reached while trying to connect, should be ignored") {
        @Override
        public synchronized Throwable fillInStackTrace() {
          return this;
        }
      };

  private void openNewChannelRec(
      List<InetSocketAddress> remoteAddresses,
      InetSocketAddress loggedProxyAddress,
      InetSocketAddress localIpV4Address,
      InetSocketAddress localIpV6Address,
      int i,
      Promise<Channel> channelPromise,
      Bootstrap bootstrap,
      HttpListener listener,
      RequestTimeout requestTimeout) {

    if (isClosed()) {
      return;
    }

    InetSocketAddress remoteAddress = remoteAddresses.get(i);
    InetSocketAddress localAddress;
    boolean forceMoveToNextRemoteAddress = false;

    if (localIpV4Address == null && localIpV6Address == null) {
      // non explicit local addresses, skip
      localAddress = null;
    } else if (remoteAddress.getAddress() instanceof Inet6Address) {
      if (localIpV6Address == null) {
        // forcing local IPv4 while remote is IPv6 is bound to fail => move to next address
        localAddress = null;
        forceMoveToNextRemoteAddress = true;
      } else {
        localAddress = localIpV6Address;
      }
    } else {
      // IPv4
      localAddress =
          NetUtil.isIpV6AddressesPreferred() && localIpV6Address != null
              ? localIpV6Address
              : localIpV4Address;
    }

    if (forceMoveToNextRemoteAddress) {
      int nextI = i + 1;
      if (nextI < remoteAddresses.size()) {
        openNewChannelRec(
            remoteAddresses,
            loggedProxyAddress,
            localIpV4Address,
            null,
            nextI,
            channelPromise,
            bootstrap,
            listener,
            requestTimeout);

      } else {
        requestTimeout.cancel();
        Exception cause =
            new UnsupportedOperationException(
                "Can't connect to IPv6 remote "
                    + remoteAddress
                    + " + from IPv4 local one "
                    + localIpV4Address);
        listener.onThrowable(cause);
        channelPromise.setFailure(cause);
      }
    } else {
      // [e]
      //
      // [e]
      ChannelFuture whenChannel = bootstrap.connect(remoteAddress, localAddress);

      whenChannel.addListener(
          f -> {
            if (f.isSuccess()) {
              // [e]
              //
              //
              //
              //
              //
              // [e]
              channelPromise.setSuccess(whenChannel.channel());

            } else {
              if (LOGGER.isDebugEnabled()) {
                LOGGER.debug(
                    "Failed to connect to remoteAddress="
                        + remoteAddress
                        + " from localAddress="
                        + localAddress,
                    f.cause());
              }
              // [e]
              //
              // [e]

              if (requestTimeout.isDone()) {
                channelPromise.setFailure(IGNORE_REQUEST_TIMEOUT_REACHED_WHILE_TRYING_TO_CONNECT);
                return;
              }

              int nextI = i + 1;
              if (nextI < remoteAddresses.size()) {
                openNewChannelRec(
                    remoteAddresses,
                    loggedProxyAddress,
                    localIpV4Address,
                    localIpV6Address,
                    nextI,
                    channelPromise,
                    bootstrap,
                    listener,
                    requestTimeout);

              } else {
                requestTimeout.cancel();
                listener.onThrowable(f.cause());
                channelPromise.setFailure(f.cause());
              }
            }
          });
    }
  }

  private Future<Channel> installSslHandler(HttpTx tx, Channel channel) {
    // [e]
    //
    // [e]

    try {
      SslHandler sslHandler =
          SslHandlers.newSslHandler(
              tx.sslContext(),
              channel.alloc(),
              tx.request.getUri(),
              tx.request.getVirtualHost(),
              config);

      ChannelPipeline pipeline = channel.pipeline();
      String after = pipeline.get(PROXY_HANDLER) != null ? PROXY_HANDLER : PINNED_HANDLER;
      pipeline.addAfter(after, SSL_HANDLER, sslHandler);

      return sslHandler
          .handshakeFuture()
          .addListener(
              f -> {
                if (tx.requestTimeout.isDone()) {
                  return;
                }

                if (f.isSuccess()) {
                  if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug(
                        "TLS handshake successful: protocol={} cipher suite={}",
                        sslHandler.engine().getSession().getProtocol(),
                        sslHandler.engine().getSession().getCipherSuite());
                  }

                  // [e]
                  //
                  // [e]
                } else {
                  tx.requestTimeout.cancel();
                  // [e]
                  //
                  // [e]
                  tx.listener.onThrowable(f.cause());
                }
              });
    } catch (RuntimeException e) {
      tx.requestTimeout.cancel();
      // [e]
      //
      // [e]
      tx.listener.onThrowable(e);
      return new DefaultPromise<Channel>(ImmediateEventExecutor.INSTANCE).setFailure(e);
    }
  }

  private Future<Void> installHttp2Handler(HttpTx tx, Channel channel, ChannelPool channelPool) {

    Promise<Void> whenAlpn = channel.eventLoop().newPromise();

    channel
        .pipeline()
        .addAfter(
            SSL_HANDLER,
            ALPN_HANDLER,
            new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) {
              @Override
              protected void configurePipeline(ChannelHandlerContext ctx, String protocol)
                  throws Exception {
                switch (protocol) {
                  case ApplicationProtocolNames.HTTP_2:
                    LOGGER.debug(
                        "ALPN led to HTTP/2 with remote {}", tx.request.getUri().getHost());
                    tx.listener.onProtocolAwareness(true);
                    Http2Connection connection = new DefaultHttp2Connection(false);

                    ChannelPool.registerHttp2Connection(channel, connection);

                    HttpToHttp2ConnectionHandler http2Handler =
                        new HttpToHttp2ConnectionHandlerBuilder()
                            .initialSettings(DEFAULT_HTTP2_SETTINGS)
                            .connection(connection)
                            .frameListener(
                                new CustomDelegatingDecompressorFrameListener(
                                    connection,
                                    new ChunkedInboundHttp2ToHttpAdapter(
                                        connection, false, whenAlpn)))
                            .build();

                    ctx.pipeline()
                        .addLast(HTTP2_HANDLER, http2Handler)
                        .addLast(CHUNKED_WRITER_HANDLER, new ChunkedWriteHandler())
                        .addLast(
                            APP_HTTP2_HANDLER,
                            new Http2AppHandler(
                                DefaultHttpClient.this, http2Handler, channelPool, config));

                    channelPool.offer(channel);

                    SslHandler sslHandler = (SslHandler) ctx.pipeline().get(SSL_HANDLER);
                    Set<String> subjectAlternativeNames =
                        Tls.extractSubjectAlternativeNames(sslHandler.engine());
                    if (LOGGER.isDebugEnabled()) {
                      LOGGER.debug(
                          "TLS handshake successful: protocol={} cipher suite={}",
                          sslHandler.engine().getSession().getProtocol(),
                          sslHandler.engine().getSession().getCipherSuite());
                    }
                    if (subjectAlternativeNames.size() > 1) {
                      channelPool.offerCoalescedChannel(
                          subjectAlternativeNames,
                          (InetSocketAddress) channel.remoteAddress(),
                          channel,
                          tx.key);
                    }
                    break;

                  case ApplicationProtocolNames.HTTP_1_1:
                    LOGGER.debug(
                        "ALPN led to HTTP/1 with remote {}", tx.request.getUri().getHost());
                    if (tx.request.isHttp2PriorKnowledge()) {
                      IllegalStateException e =
                          new IllegalStateException(
                              "HTTP/2 Prior knowledge was set on host "
                                  + tx.request.getUri().getHost()
                                  + " but it only supports HTTP/1");
                      whenAlpn.setFailure(e);
                      throw e;
                    }
                    tx.listener.onProtocolAwareness(false);
                    ctx.pipeline()
                        .addLast(HTTP_CLIENT_CODEC, newHttpClientCodec())
                        .addLast(INFLATER_HANDLER, new CustomHttpContentDecompressor())
                        .addLast(CHUNKED_WRITER_HANDLER, new ChunkedWriteHandler())
                        .addLast(
                            APP_HTTP_HANDLER,
                            new HttpAppHandler(DefaultHttpClient.this, channelPool, config));
                    whenAlpn.setSuccess(null);
                    break;

                  default:
                    IllegalStateException e =
                        new IllegalStateException("Unknown protocol: " + protocol);
                    whenAlpn.setFailure(e);
                    ctx.close();
                    // FIXME do we really need to throw?
                    throw e;
                }
              }
            });

    whenAlpn.addListener(
        f -> {
          if (!f.isSuccess()) {
            tx.listener.onThrowable(f.cause());
          }
        });

    return whenAlpn;
  }

  @Override
  public boolean isClosed() {
    return closed.get();
  }

  @Override
  public void flushClientIdChannels(long clientId, EventLoop eventLoop) {
    if (eventLoop.inEventLoop()) {
      eventLoopResources(eventLoop).channelPool.flushClientIdChannelPoolPartitions(clientId);
    } else if (!eventLoop.isShutdown()) {
      eventLoop.execute(
          () ->
              eventLoopResources(eventLoop)
                  .channelPool
                  .flushClientIdChannelPoolPartitions(clientId));
    }
  }
}


text:
<?php

/**
 * @file
 * Contains \Drupal\common_test\Controller\CommonTestController.
 */

namespace Drupal\common_test\Controller;

use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\Response;

/**
 * Controller routines for common_test routes.
 */
class CommonTestController {

  /**
   * Returns links to the current page, with and without query strings.
   *
   * Using #type 'link' causes these links to be rendered with _l().
   */
  public function typeLinkActiveClass() {
    return array(
      'no_query' => array(
        '#type' => 'link',
        '#title' => t('Link with no query string'),
        '#url' => Url::fromRoute('<current>'),
        '#options' => array(
          'set_active_class' => TRUE,
        ),
      ),
      'with_query' => array(
        '#type' => 'link',
        '#title' => t('Link with a query string'),
        '#url' => Url::fromRoute('<current>'),
        '#options' => array(
          'query' => array(
            'foo' => 'bar',
            'one' => 'two',
          ),
          'set_active_class' => TRUE,
        ),
      ),
      'with_query_reversed' => array(
        '#type' => 'link',
        '#title' => t('Link with the same query string in reverse order'),
        '#url' => Url::fromRoute('<current>'),
        '#options' => array(
          'query' => array(
            'one' => 'two',
            'foo' => 'bar',
          ),
          'set_active_class' => TRUE,
        ),
      ),
    );
  }

  /**
   * Adds a JavaScript file and a CSS file with a query string appended.
   *
   * @return string
   *   An empty string.
   */
  public function jsAndCssQuerystring() {
    $attached = array(
      '#attached' => array(
        'library' => array(
          'node/drupal.node',
        ),
        'css' => array(
          drupal_get_path('module', 'node') . '/css/node.admin.css' => array(),
          // A relative URI may have a query string.
          '/' . drupal_get_path('module', 'node') . '/node-fake.css?arg1=value1&arg2=value2' => array(),
        ),
      ),
    );
    return \Drupal::service('renderer')->renderRoot($attached);
  }

  /**
   * Prints a destination query parameter.
   *
   * @return \Symfony\Component\HttpFoundation\Response
   *   A new Response object containing a string with the destination query
   *   parameter.
   */
  public function destination() {
    $destination = \Drupal::destination()->getAsArray();
    $output = "The destination: " . SafeMarkup::checkPlain($destination['destination']);
    return new Response($output);
  }

}

redpajama_set_name: RedPajamaGithub


text:
import { moduleFor, test } from 'ember-qunit';

moduleFor('controller:ember-flexberry-dummy-application-user-list', 'Unit | Controller | ember flexberry dummy application user list', {
  // Specify the other units that are required for this test.
  // needs: ['controller:foo']
});

// Replace this with your real tests.
test('it exists', function(assert) {
  let controller = this.subject();
  assert.ok(controller);
});


text:
from celery import Celery
import rethinkdb as r
from ..model.datadir import DataDir
from ..model.datafile import DataFile
from mcapi import dmutil
from mcapi import mcexceptions
from os.path import basename, dirname, splitext, getsize, relpath
import os
from ..utils import mkdirp, runtika
import hashlib
from PIL import Image
import json
from distutils import dir_util
import shutil
import traceback

celery = Celery('db', broker='amqp://guest@localhost//')

class StateCreateSaver(object):
    def __init__(self):
        self.objects = {}

    def insert(self, table, entry):
        rv = r.table('saver').insert(entry).run()
        id = rv['generated_keys'][0]
        self.objects[id] = table
        return id

    def insert_newval(self, table, entry):
        rv = r.table('saver').insert(entry, return_vals=True).run()
        id = rv['generated_keys'][0]
        self.objects[id] = table
        return rv

    def move_to_tables(self):
        for key in self.objects:
            table_name = self.objects[key]
            o = r.table('saver').get(key).run(time_format='raw')
            r.table(table_name).insert(o).run()

    def delete_tables(self):
        for key in self.objects:
            r.table('saver').get(key).delete().run()
        self.objects.clear()

@celery.task
def load_data_dirs(user, dirs, state_id):
    try:
        r.connect('localhost', 30815, db='materialscommons').repl()
        for directory in dirs:
            load_directory(user, directory, state_id)
    except Exception as exc:
        raise load_data_dirs.retry(exc=exc)

@celery.task
def load_data_dir(user, directory, state_id):
    state_saver = StateCreateSaver()
    try:
        r.connect('localhost', 30815, db='materialscommons').repl()
        load_data(user, directory, state_id, state_saver)
    except mcexceptions.RequiredAttributeException as rae:
        traceback.print_exc()
        state_saver.delete_tables()
        print "Missing attribute: %s" % (rae.attr)
    except Exception as exc:
        traceback.print_exc()
        state_saver.delete_tables()
        raise load_data_dir.retry(exc=exc)
    finally:
        state_saver.delete_tables()

@celery.task
def load_data_file(datafile, project, datadir):
    try:
        r.connect('localhost', 30815, db='materialscommons').repl()
    except Exception as exc:
        traceback.print_exc()
        raise load_data_file.retry(exc=exc)

@celery.task
def import_data_dir_to_repo(dirpath):
    try:
        print "Copying over: %s" %(dirpath)
        copy_data_over(dirpath)
    except Exception as exc:
        raise import_data_dir_to_repo.retry(exc)

@celery.task
def import_data_dirs_to_repo(dirpaths):
    try:
        for directory in dirpaths:
            copy_data_over(directory)
    except Exception as exc:
        raise import_data_dirs_to_repo.retry(exc)

def load_data(user, directory, state_id, state_saver):
    load_provenance_from_state(state_id, state_saver)
    load_directory(user, directory, state_saver.process_id)
    r.table('state').get(state_id).delete().run()
    state_saver.move_to_tables()
    state_saver.delete_tables()

def copy_data_over(dirpath):
    print "Copying over: %s" % (dirpath)
    # For now hard code where we copy data to
    dir_util.copy_tree(dirpath, '/var/www/html/assets/materialscommons')
    print "Removing dir: %s" % (dirpath)
    shutil.rmtree(dirpath)

def load_provenance_from_state(state_id, saver):
    state = r.table('state').get(state_id).run()
    attributes = state['attributes']
    user = state['owner']
    project_id = attributes['project']['id']
    saver.project_id = project_id
    create_process_from_template(attributes['process'], saver)
    process_id = saver.process_id
    if 'input_files' in attributes:
        add_input_files_to_process(process_id, attributes['input_files'])
    input_conditions = dmutil.get_optional('input_conditions', attributes, [])
    output_conditions = dmutil.get_optional('output_conditions', attributes, [])
    create_conditions_from_templates(process_id, user, input_conditions, output_conditions, saver)
    return process_id

def create_process_from_template(j, saver):
    project_id = saver.project_id
    p = dict()
    p['template'] = dmutil.get_required('id', j)
    p['project'] = project_id
    m = j['model']
    p['name'] = dmutil.get_required_prop('name', m)
    p['birthtime'] = r.now()
    p['mtime'] = p['birthtime']
    p['machine'] = dmutil.get_optional_prop('machine', m)
    p['process_type'] = dmutil.get_required_prop('process_type', m)
    p['description'] = dmutil.get_required_prop('description', m)
    p['version'] = dmutil.get_optional_prop('version', m)
    p['notes'] = dmutil.get_optional_prop('notes', m, [])
    p['input_conditions'] = dmutil.get_optional_prop('input_conditions', m, [])
    p['input_files'] = dmutil.get_optional_prop('input_files', m, [])
    p['output_conditions'] = dmutil.get_optional_prop('output_conditions', m, [])
    p['output_files'] = dmutil.get_optional_prop('output_files', m, [])
    p['runs'] = dmutil.get_optional_prop('runs', m, [])
    p['citations'] = dmutil.get_optional_prop('citations', m, [])
    p['status'] = dmutil.get_optional_prop('status', m)
    process_id = saver.insert('processes', p)
    saver.process_id = process_id
    saver.insert('project2processes', {'project_id': project_id, 'process_id': process_id})

def add_input_files_to_process(process_id, input_files):
    process = r.table('saver').get(process_id).run()
    for id in input_files:
        process['input_files'].append(id)
    ifiles = process['input_files']
    r.table('saver').get(process_id).update({'input_files':ifiles}).run()

def add_output_files_to_process(process_id, output_files):
    process = r.table('saver').get(process_id).run()
    for id in output_files:
        process['output_files'].append(id)
    ifiles = process['output_files']
    r.table('saver').get(process_id).update({'output_files':ifiles}).run()

def load_directory(user, directory, process_id):
    parents = {}
    base = basename(directory)
    df_ids = []
    for root, dirs, files in os.walk(directory):
        if basename(root) == ".conversion" :
            continue
        dir_name = construct_datadir_name(base, root, directory)
        location = dir_name
        parents[dir_name] = ""
        parentname = dirname(dir_name)
        parentid = parents[parentname] if parentname in parents else ""
        fixed_name = fix_name(dir_name)
        if fixed_name is None:
            continue
        ddir = DataDir(fixed_name, "private", user, parentid)
        for datafile in files:
            id = load_data_file(datafile, location, ddir.id, user, root)
            df_ids.append(id)
            ddir.datafiles.append(id)
        rv = r.table('datadirs').insert(ddir.__dict__, return_vals=True).run()
        dbobj = rv['new_val']
        if rv['errors']:
            for data_item in ddir.datafiles:
                dbobj['datafiles'].append(data_item)
            r.table('datadirs').update(dbobj).run()
        parents[dir_name] = dbobj['id']
    add_dfids_to_process(process_id, df_ids)

def add_dfids_to_process(process_id, df_ids):
    process = r.table('saver').get(process_id).run()
    for id in df_ids:
        process['output_files'].append(id)
    output_files = process['output_files']
    r.table('saver').get(process_id).update({'output_files':output_files}).run()

def fix_name(dir_name):
    """The dir path at this point has the project name
included in it. We want to strip out the pieces we
don't care about.
"""
    if '/' not in dir_name:
        return None
    slash = dir_name.index('/')
    return dir_name[slash+1:]

def construct_datadir_name(base, root, directory):
    rpath = relpath(root, directory)
    return base + ("" if rpath == "." else "/" + rpath)

def load_data_file(datafile, location, ddirid, user, root):
    data_item = DataFile(datafile, "private", user)
    path = getpath(root, datafile)
    mediatype = get_media_type(path)
    data_item.mediatype = mediatype
    data_item.location = fix_name(location)
    data_item.checksum = getchecksum(path)
    data_item.size = getsize(path)
    metatags = get_meta_tags(path)
    convert_file_if_needed(path, mediatype)
    data_item.datadirs.append(ddirid)
    data_item.metatags = metatags
    dbobj = r.table('datafiles').insert(data_item.__dict__, return_vals=True).run()['new_val']
    data_item.id = dbobj['id']
    return dbobj['id']

def getpath(root, datafile):
    path = root + '/' + datafile
    print "File: " + path
    return path

def convert_file_if_needed(path, mediatype):
    if mediatype == "image/tiff":
        dir = dirname(path)
        conversiondir = os.path.join(dir, ".conversion")
        mkdirp(conversiondir)
        filename = basename(path)
        file, ext = splitext(filename)
        im = Image.open(path)
        saveto = os.path.join(conversiondir, file + ".jpg")
        if im.mode != 'RGB':
            im = im.convert('RGB')
        im.save(saveto)

def getchecksum(path):
    with open(path) as fd:
        data = fd.read()
        return hashlib.md5(data).hexdigest()

def get_meta_tags(path):
    data = runtika("-j", path)
    d = json.loads(data)
    metatags = []
    for key in d.keys():
        if usetag(key, d[key]):
            entry = {'name': key, 'value': str(d[key])}
            metatags.append(entry)
    return metatags

def usetag(tagname, tagdata):
    if "Unknown tag" in tagname:
        if isinstance(tagdata, unicode):
            value = ''.join(tagdata.split())
            return not value.isdigit()
        else:
            return True
    else:
        return True

def get_media_type(path):
    return runtika("-d", path).rstrip()


text:
using System;
using System.Diagnostics;

namespace Atomiv.Test.AspNetCore
{
    public class WebProjectDotNetRunner : BaseProcessRunner
    {
        private const string DotNet = "dotnet";
        private const string Environment = "--environment";

        public WebProjectDotNetRunner(WebProjectPaths paths, string environmentName)
            : base(false)
        {
            Paths = paths;
            EnvironmentName = environmentName;
        }

        public WebProjectPaths Paths { get; }

        public string EnvironmentName { get; }

        protected override Process CreateProcess()
        {
            var startInfo = new ProcessStartInfo
            {
                // WorkingDirectory = Paths.ProjectDirectoryPath,
                FileName = DotNet,
                Arguments = $"run --project {Paths.ProjectFilePath} {Environment} {EnvironmentName}",
                UseShellExecute = false,
                CreateNoWindow = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
            };

            var process = new Process
            {
                StartInfo = startInfo
            };

            // TODO: VC: Move to base?

            process.OutputDataReceived += Process_OutputDataReceived;
            process.ErrorDataReceived += Process_ErrorDataReceived;

            return process;
        }

        private void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
        {
            throw new Exception($"Detected process error: {e.Data}");
        }

        private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
        }
    }
}


text:
<?xml version="1.0" encoding="UTF-8"?>
<citations>
	<bibl><title level="j">JOURNAL OF VIROLOGY</title>,.<biblScope type="vol">23</biblScope>.<biblScope type="pp">11534–11543</biblScope>.<date>2001 Dec. 2001</date>, p. <biblScope type="pp">11534–11543</biblScope> Vol.<biblScope type="vol">75</biblScope> ,No .<biblScope type="issue">23</biblScope></bibl>
</citations>


text:
Creating Menus as Services
==========================

This bundle gives you a really convenient way to create menus by following
a convention and - if needed - injecting the entire container.

However, if you want to, you can instead choose to create a service for your
menu object. The advantage of this method is that you can inject the exact
dependencies that your menu needs, instead of injecting the entire service
container. This can lead to code that is more testable and also potentially
more reusable. The disadvantage is that it needs just a little more setup.

Start by creating a builder for your menu. You can stick as many menus into
a builder as you want, so you may only have one (or just a few) of these
builder classes in your application:

```php
<?php
// src/Acme/MainBundle/Menu/MenuBuilder.php

namespace Acme\MainBundle\Menu;

use Knp\Menu\FactoryInterface;
use Symfony\Component\HttpFoundation\Request;

class MenuBuilder
{
    private $factory;

    /**
     * @param FactoryInterface $factory
     */
    public function __construct(FactoryInterface $factory)
    {
        $this->factory = $factory;
    }

    public function createMainMenu(Request $request)
    {
        $menu = $this->factory->createItem('root');
        $menu->setCurrentUri($request->getRequestUri());

        $menu->addChild('Home', array('route' => 'homepage'));
        // ... add more children

        return $menu;
    }
}
```

Next, register two services: one for your menu builder, and one for the menu
object created by the `createMainMenu` method:

```yaml
# src/Acme/MainBundle/Resources/config/services.yml
services:
    acme_main.menu_builder:
        class: Acme\MainBundle\Menu\MenuBuilder
        arguments: ["@knp_menu.factory"]

    acme_main.menu.main:
        class: Knp\Menu\MenuItem # the service definition requires setting the class
        factory_service: acme_hello.menu_builder
        factory_method: createMainMenu
        arguments: ["@request"]
        scope: request # needed as we have the request as a dependency here
        tags:
            - { name: knp_menu.menu, alias: main } # The alias is what is used to retrieve the menu
```

>**NOTE**
>The menu service must be public as it will be retrieved at runtime to keep
>it lazy-loaded.

You can now render the menu directly in a template via the name given in the
`alias` key above:

```jinja
{{ knp_menu_render('main') }}
```

Suppose now we need to create a second menu for the sidebar. The process
is simple! Start by adding a new method to your builder:

```php
<?php
// src/Acme/MainBundle/Menu/MenuBuilder.php

// ...

class MenuBuilder
{
    // ...

    public function createSidebarMenu(Request $request)
    {
        $menu = $this->factory->createItem('sidebar');
        $menu->setCurrentUri($request->getRequestUri());

        $menu->addChild('Home', array('route' => 'homepage'));
        // ... add more children

        return $menu;
    }
}
```

Now, create a service for *just* your new menu, giving it a new name, like
`sidebar`:

```yaml
# src/Acme/MainBundle/Resources/config/services.yml
services:

    acme_main.menu.sidebar:
        class: Knp\Menu\MenuItem
        factory_service: acme_hello.menu_builder
        factory_method: createSidebarMenu
        arguments: ["@request"]
        scope: request
        tags:
            - { name: knp_menu.menu, alias: sidebar } # Named "sidebar" this time
```

It can now be rendered, just like the other menu:

```jinja
{{ knp_menu_render('sidebar') }}
```


text:
package appeng.api.storage;

import appeng.api.config.AccessRestriction;
import appeng.api.storage.data.IAEStack;

/**
 * Thin logic layer that can be swapped with different IMEInventory
 * implementations, used to handle features related to storage, that are
 * Separate from the storage medium itself.
 *
 * @param <StackType>
 */
public interface IMEInventoryHandler<StackType extends IAEStack> extends IMEInventory<StackType>
{

	/**
	 * determine if items can be injected/extracted.
	 *
	 * @return the access
	 */
	public AccessRestriction getAccess();

	/**
	 * determine if a particular item is prioritized for this inventory handler,
	 * if it is, then it will be added to this inventory prior to any
	 * non-prioritiezed inventories.
	 *
	 * @param input
	 *            - item that might be added
	 * @return if its prioritized
	 */
	public boolean isPrioritized(StackType input);

	/**
	 * determine if an item can be accepted and stored.
	 *
	 * @param input
	 *            - item that might be added
	 * @return if the item can be added
	 */
	public boolean canAccept(StackType input);

	/**
	 * determine what the priority of the inventory is.
	 *
	 * @return the priority, zero is default, positive and negative are
	 *         supported.
	 */
	public int getPriority();

}


text:
package org.jasig.portal.rest;

import java.util.ArrayList;
import java.util.List;

import javax.portlet.WindowState;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jasig.portal.IUserPreferencesManager;
import org.jasig.portal.IUserProfile;
import org.jasig.portal.layout.IUserLayoutStore;
import org.jasig.portal.layout.dlm.DistributedUserLayout;
import org.jasig.portal.portlet.dao.IPortletDefinitionDao;
import org.jasig.portal.portlet.om.IPortletDefinition;
import org.jasig.portal.portlet.om.IPortletDefinitionParameter;
import org.jasig.portal.portlet.om.IPortletWindowId;
import org.jasig.portal.rest.layout.LayoutPortlet;
import org.jasig.portal.rest.layout.TabListOfNodes;
import org.jasig.portal.security.IPerson;
import org.jasig.portal.security.IPersonManager;
import org.jasig.portal.url.IPortalUrlBuilder;
import org.jasig.portal.url.IPortalUrlProvider;
import org.jasig.portal.url.IPortletUrlBuilder;
import org.jasig.portal.url.UrlType;
import org.jasig.portal.user.IUserInstance;
import org.jasig.portal.user.IUserInstanceManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

@Controller
public class LayoutRESTController {

    protected final Log log = LogFactory.getLog(getClass());

    IUserLayoutStore userLayoutStore;

    @Autowired(required = true)
    public void setUserLayoutStore(IUserLayoutStore userLayoutStore) {
        this.userLayoutStore = userLayoutStore;
    }

    IPersonManager personManager;

    @Autowired(required = true)
    public void setPersonManager(IPersonManager personManager) {
        this.personManager = personManager;
    }

    private IPortalUrlProvider urlProvider;

    @Autowired(required = true)
    public void setPortalUrlProvider(IPortalUrlProvider urlProvider) {
        this.urlProvider = urlProvider;
    }

    private IUserInstanceManager userInstanceManager;

    @Autowired(required = true)
    public void setUserInstanceManager(IUserInstanceManager userInstanceManager) {
        this.userInstanceManager = userInstanceManager;
    }

    private IPortletDefinitionDao portletDao;

    @Autowired(required = true)
    public void setPortletDao(IPortletDefinitionDao portletDao) {
        this.portletDao = portletDao;
    }

    /**
     * A REST call to get a json feed of the current users layout
     * @param request The servlet request. Utilized to get the users instance and eventually there layout
     * @param tab The tab name of which you would like to filter; optional; if not provided, will return entire layout.
     * @return json feed of the layout
     */
    @RequestMapping(value="/layoutDoc", method = RequestMethod.GET)
    public ModelAndView getRESTController(HttpServletRequest request, @RequestParam(value = "tab", required = false) String tab) {
        final IPerson person = personManager.getPerson(request);
        List<LayoutPortlet> portlets = new ArrayList<LayoutPortlet>();

        try {

            final IUserInstance ui = userInstanceManager.getUserInstance(request);

            final IUserPreferencesManager upm = ui.getPreferencesManager();

            final IUserProfile profile = upm.getUserProfile();
            final DistributedUserLayout userLayout = userLayoutStore.getUserLayout(person, profile);
            Document document = userLayout.getLayout();

            NodeList portletNodes = null;
            if(tab != null) {
               NodeList folders = document.getElementsByTagName("folder");
               for (int i = 0; i < folders.getLength(); i++) {
                   Node node = folders.item(i);
                   if(tab.equalsIgnoreCase(node.getAttributes().getNamedItem("name").getNodeValue())) {
                       TabListOfNodes tabNodes = new TabListOfNodes();
                       tabNodes.addAllChannels(node.getChildNodes());
                       portletNodes = tabNodes;
                       break;
                   }
               }
            } else {
               portletNodes = document.getElementsByTagName("channel");
            }
            for (int i = 0; i < portletNodes.getLength(); i++) {
                try {

                    LayoutPortlet portlet = new LayoutPortlet();
                    NamedNodeMap attributes = portletNodes.item(i).getAttributes();
                    portlet.setTitle(attributes.getNamedItem("title").getNodeValue());
                    portlet.setDescription(attributes.getNamedItem("description").getNodeValue());
                    portlet.setNodeId(attributes.getNamedItem("ID").getNodeValue());
                    portlet.setFname(attributes.getNamedItem("fname").getNodeValue());

                    IPortletDefinition def = portletDao.getPortletDefinitionByFname(attributes.getNamedItem("fname").getNodeValue());

                    //get icons
                    IPortletDefinitionParameter iconParam = def.getParameter("iconUrl");
                    if (iconParam != null) {
                        portlet.setIconUrl(iconParam.getValue());
                    }

                    IPortletDefinitionParameter faIconParam = def.getParameter("faIcon");
                    if(faIconParam != null) {
                        portlet.setFaIcon(faIconParam.getValue());
                    }

                    //get alt max URL
                    IPortletDefinitionParameter alternativeMaximizedLink = def.getParameter("alternativeMaximizedLink");
                    if( alternativeMaximizedLink != null) {
                    	portlet.setUrl(alternativeMaximizedLink.getValue());
                    } else {
	                    // get the maximized URL for this portlet
	                    final IPortalUrlBuilder portalUrlBuilder = urlProvider.getPortalUrlBuilderByLayoutNode(request, attributes.getNamedItem("ID").getNodeValue(), UrlType.RENDER);
	                    final IPortletWindowId targetPortletWindowId = portalUrlBuilder.getTargetPortletWindowId();
	                    if (targetPortletWindowId != null) {
	                        final IPortletUrlBuilder portletUrlBuilder = portalUrlBuilder.getPortletUrlBuilder(targetPortletWindowId);
	                        portletUrlBuilder.setWindowState(WindowState.MAXIMIZED);
	                    }
	                    portlet.setUrl(portalUrlBuilder.getUrlString());
                    }
                    portlets.add(portlet);

                } catch (Exception e) {
                    log.warn("Exception construction JSON representation of mobile portlet", e);
                }
            }

            ModelAndView mv = new ModelAndView();
            mv.addObject("layout", portlets);
            mv.setViewName("json");
            return mv;
        } catch (Exception e) {
            log.error("Error retrieving user layout document", e);
        }

        return null;
    }

}


text:
#region LGPL License

#endregion LGPL License

#region SVN Version Information

// <file>
//     <license see="http://axiom3d.net/wiki/index.php/license.txt"/>
//     <id value="$Id$"/>
// </file>

#endregion SVN Version Information

#region Namespace Declarations

#endregion Namespace Declarations

namespace Axiom.Core
{
	/// <summary>
	/// This class allows you to plug in new ways to define the camera setup when
	///	rendering and projecting shadow textures.
	/// </summary>
	/// <remarks>
	/// The default projection used when rendering shadow textures is a uniform
	/// frustum. This is pretty straight forward but doesn't make the best use of
	/// the space in the shadow map since texels closer to the camera will be larger,
	/// resulting in 'jaggies'. There are several ways to distribute the texels
	/// in the shadow texture differently, and this class allows you to override
	/// that.
	/// <para />
	/// Axiom is provided with several alternative shadow camera setups, including
	/// LiSPSM (<see cref="LiSPSMShadowCameraSetup"/>) and Plane Optimal
	/// (<see cref="PlaneOptimalShadowCameraSetup"/>).
	/// Others can of course be written to incorporate other algorithms. All you
	/// have to do is instantiate one of these classes and enable it using
	/// <see cref="SceneManager.SetShadowCameraSetup"/> (global) or
	/// <see cref="Light.SetCustomShadowCameraSetup"/>
	/// (per light). In both cases the instance is be deleted automatically when
	/// no more references to it exist.
	/// <para />
	/// Shadow map matrices, being projective matrices, have 15 degrees of freedom.
	/// 3 of these degrees of freedom are fixed by the light's position.  4 are used to
	/// affinely affect z values.  6 affinely affect u,v sampling.  2 are projective
	/// degrees of freedom.  This class is meant to allow custom methods for
	/// handling optimization.
	/// </remarks>
	public interface IShadowCameraSetup
	{
		/// <summary>
		/// Gets a specific implementation of a ShadowCamera setup.
		/// </summary>
		/// <param name="sceneManager"></param>
		/// <param name="camera"></param>
		/// <param name="viewport"></param>
		/// <param name="light"></param>
		/// <param name="textureCamera"></param>
		/// <param name="iteration"></param>
		void GetShadowCamera( SceneManager sceneManager, Camera camera, Viewport viewport, Light light, Camera textureCamera, int iteration );
	}
}


text:
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">

  <title>CORS Planner - Jasmine Spec Runner</title>

  <link rel="shortcut icon" type="image/png" href="lib/jasmine-1.3.1/jasmine_favicon.png">
  <link rel="stylesheet" type="text/css" href="lib/jasmine-1.3.1/jasmine.css">
  <script type="text/javascript" src="lib/jasmine-1.3.1/jasmine.js"></script>
  <script type="text/javascript" src="lib/jasmine-1.3.1/jasmine-html.js"></script>

  <!-- include libs and requirejs files here... -->
  <script src="../src/js/libs/jquery-1.8.3.js"></script>
  <!-- all other application scripts are loaded through require.  -->
  <script src="../src/js/libs/require.js"></script>
  <script src="lib/main.test.js"></script>

  <script type="text/javascript">
      // include all the test specs here
      require([
          "util/pubsub"
        , "spec/moduleTest"
        , "spec/modulesTest"
      ], function() {
          $(function() {
              var jasmineEnv = jasmine.getEnv()
              , htmlReporter = new jasmine.HtmlReporter();

              jasmineEnv.updateInterval = 1000;
              jasmineEnv.addReporter(htmlReporter);
              jasmineEnv.specFilter = function(spec) {
                return htmlReporter.specFilter(spec);
              };

            jasmineEnv.execute();
          });
      });
  </script>

</head>

<body>
</body>
</html>


text:
import base64
import cv2
import json
import os

from flask import abort
from flask import Flask
from flask import jsonify
from flask import render_template
from flask import request
from flask import send_file

LOCKS_DATA_FILE = os.path.join(os.path.dirname(__file__),
    'data/locks.json')
STATE_DATA_FILE = os.path.join(os.path.dirname(__file__),
    'data/state.json')

app = Flask(__name__, static_url_path='')
app.secret_key = 'drew_heilman'

GENERIC_ERROR_MESSAGE = "Error: Somthing Dun Goofed. Coding is hard. :("

@app.route('/')
def Home():
  return 'Hello'

@app.route('/lock/id/<lock_id>', methods=['GET', 'POST', 'DELETE'])
def LockId(lock_id=None):
  locks = _LoadLocks()
  if request.method == 'POST':
    request_image = request.form.get('image') if request.form.get('image') else "NULL"
    locks[lock_id] = {
          "waiting": True,
          "approved": False,
          "image": request_image,
          "time": request.form.get('time') if request.form.get('time') else "NULL",
        }
    # Face recognition
    image = cv2.imread(_SaveImage(locks[lock_id]))
    faceCascade = cv2.CascadeClassifier("/var/www/kirmani.io/api/codered/public_html/haarcascade_frontalface_default.xml")
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        flags = cv2.cv.CV_HAAR_SCALE_IMAGE
        )
    print("Found {0} faces!".format(len(faces)))
    locks[lock_id]["faces"] = len(faces)
    print([{"x": int(face[0]), "y": int(face[1]), "w": int(face[2]), "h": int(face[3])} for face in faces])
    image_faces = []
    for (x, y, w, h) in faces:
      cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
    filename = "/var/www/kirmani.io/api/codered/public_html/data/images/" + str(locks[lock_id]["time"]) + ".png"
    cv2.imwrite(filename, image)
    locks[lock_id]["image"] = base64.b64encode(open(filename, 'rb').read())
    _WriteLocks(locks)
    print("Added Lock with ID: %s" % lock_id)
    return jsonify(result=locks[lock_id])
  if request.method == 'GET':
    if lock_id not in locks:
      return ("Error: Lock %s doesn't exist" % lock_id)
    return jsonify(result=locks[lock_id])
  if request.method == 'DELETE':
    if lock_id not in locks:
      return ("Error: Lock %s doesn't exist" % lock_id)
    del locks[lock_id]
    _WriteLocks(locks)
    return ("Successfully removed lock with ID: %s" % lock_id)
  return GENERIC_ERROR_MESSAGE

@app.route('/lock/id/<lock_id>/approve', methods=['PUT'])
def LockIdApprove(lock_id=None):
  locks = _LoadLocks()
  if request.method == 'PUT':
    if lock_id not in locks:
      return ("Error: Lock %s doesn't exist" % lock_id)
    locks[lock_id]["waiting"] = False
    locks[lock_id]["approved"] = True
    _WriteLocks(locks)
    print("Approved Lock with ID: %s" % lock_id)
    return jsonify(result=locks[lock_id])
  return GENERIC_ERROR_MESSAGE

@app.route('/lock/id/<lock_id>/deny', methods=['PUT'])
def LockIdDeny(lock_id=None):
  locks = _LoadLocks()
  if request.method == 'PUT':
    if lock_id not in locks:
      return ("Error: Lock %s doesn't exist" % lock_id)
    locks[lock_id]["waiting"] = False
    locks[lock_id]["approved"] = False
    _WriteLocks(locks)
    print("Approved Lock with ID: %s" % lock_id)
    return jsonify(result=locks[lock_id])
  return GENERIC_ERROR_MESSAGE

@app.route('/lock/list', methods=['GET'])
def LockList():
  locks = _LoadLocks()
  return jsonify(result=[lock for lock in locks])

@app.route('/lock/open', methods=['PUT'])
def LockOpen():
  locks = _LoadLocks()
  locks["OVERRIDE"] = {
      "action": "open"
      }
  _WriteLocks(locks)
  return jsonify(result=locks["OVERRIDE"])

@app.route('/lock/state', methods=['PUT', 'GET'])
def LockState():
  state = _LoadState()
  if request.method == 'PUT':
    state["open"] = request.form.get('open')
    _WriteState(state)
    return jsonify(result=state)
  if request.method == 'GET':
    return jsonify(result=state)
  return GENERIC_ERROR_MESSAGE

@app.route('/lock/close', methods=['PUT'])
def LockClose():
  locks = _LoadLocks()
  locks["OVERRIDE"] = {
      "action": "close"
      }
  _WriteLocks(locks)
  return jsonify(result=locks["OVERRIDE"])

def _SaveImage(lock):
  if lock["image"] != "NULL":
    imgdata = base64.b64decode(lock["image"])
    filename = "/var/www/kirmani.io/api/codered/public_html/data/images/" + str(lock["time"]) + ".png"
    with open(filename, 'wb') as f:
      f.write(imgdata)
      return filename

@app.route('/static/images', defaults={'path': ''})
@app.route('/static/images/<path:path>')
def StaticImage(path):
  BASE_DIR = '/var/www/kirmani.io/api/codered/public_html/data/images'
  abs_path = os.path.join(BASE_DIR, path)

  if not os.path.exists(abs_path):
    return abort(404)
  if os.path.isfile(abs_path):
    return send_file(abs_path)
  files = os.listdir(abs_path)
  return render_template('files.html', files=files)

def _LoadLocks():
  try:
    with open(LOCKS_DATA_FILE, 'r+') as f:
      return json.load(fp=f)
  except IOError:
    return {}

def _WriteLocks(data):
  j = json.dumps(data, indent=4)
  with open(LOCKS_DATA_FILE, 'w') as f:
    f.write(j)

def _LoadState():
  try:
    with open(STATE_DATA_FILE, 'r+') as f:
      return json.load(fp=f)
  except IOError:
    return {}

def _WriteState(data):
  j = json.dumps(data, indent=4)
  with open(STATE_DATA_FILE, 'w') as f:
    f.write(j)

if __name__ == '__main__':
  port = int(os.environ.get('PORT', 33507))
  app.run(host='0.0.0.0', port=port, debug=True)


text:
```plpgsql
CREATE OR REPLACE FUNCTION purchase.get_supplier_id_by_supplier_code(text)
RETURNS bigint
```
* Schema : [purchase](../../schemas/purchase.md)
* Function Name : get_supplier_id_by_supplier_code
* Arguments : text
* Owner : frapid_db_user
* Result Type : bigint
* Description :

**Source:**
```sql
CREATE OR REPLACE FUNCTION purchase.get_supplier_id_by_supplier_code(text)
 RETURNS bigint
 LANGUAGE plpgsql
AS $function$
BEGIN
    RETURN
    (
        SELECT
            supplier_id
        FROM
            inventory.suppliers
        WHERE
            inventory.suppliers.supplier_code=$1
	AND NOT
	    inventory.suppliers.deleted
    );
END
$function$

```

### Related Contents
* [Schema List](../../schemas.md)
* [Function List](../../functions.md)
* [Trigger List](../../triggers.md)
* [Table of Contents](../../README.md)


text:
require 'aspose_pdf_cloud'

class Attachment

  include AsposePDFCloud
  include AsposeStorageCloud

  def initialize
    #Get App key and App SID from https://cloud.aspose.com
    AsposeApp.app_key_and_sid("APP_KEY", "APP_SID")
    @pdf_api = PdfApi.new
  end

  def upload_file(file_name)
    @storage_api = StorageApi.new
    response = @storage_api.put_create(file_name, File.open("../../../data/" << file_name,"r") { |io| io.read } )
  end

  def add_image_stamp_to_a_pdf_page
    file_name = "sample-input.pdf"
    image_file = "aspose-cloud.png"
    upload_file(file_name)
    upload_file(image_file)

    page_number = 1
    stamp = Stamp.new
    stamp.file_name = image_file
    stamp.value = "Aspose"
    stamp.background = true
    stamp.type = "Image"
    stamp.page_index = 0
    stamp.left_margin = 0.0
    stamp.opacity = 0.5
    stamp.right_margin = 0.0
    stamp.top_margin = 0.0
    stamp.y_indent = 100.0
    stamp.x_indent = 100.0
    stamp.zoom = 1.0
    stamp.text_state = null
    stamp.width = 300.0
    stamp.height = 300.0

    response = @pdf_api.put_page_add_stamp(file_name, page_number, stamp)
  end

end

attachment = Attachment.new()
puts attachment.add_image_stamp_to_a_pdf_page
#ExEnd:


text:
package org.oscm.app.iaas.data;

import java.util.HashSet;
import java.util.Set;

/**
 * Basic data object for unit testing.
 */
public class SmallVServerConfiguration implements VServerConfiguration {

    private String serverType;
    private String diskImageId;
    private String serverId;
    private String serverName;
    private String numCPU;
    private String hostName;
    private Set<VNIC> virtualNICS;
    private boolean isEFM;
    private String networkId;

    /**
     * @param diskImageId
     *            the diskImageId to set
     */
    public void setDiskImageId(String diskImageId) {
        this.diskImageId = diskImageId;
    }

    /**
     * @param serverId
     *            the serverId to set
     */
    public void setServerId(String serverId) {
        this.serverId = serverId;
    }

    /**
     * @param serverName
     *            the serverName to set
     */
    public void setServerName(String serverName) {
        this.serverName = serverName;
    }

    /**
     * @param serverType
     *            the serverType to set
     */
    public void setServerType(String serverType) {
        this.serverType = serverType;
    }

    /**
     * @param numCPU
     *            the numCPU to set
     */
    public void setNumCPU(String numCPU) {
        this.numCPU = numCPU;
    }

    /**
     * @param hostName
     *            the hostName to set
     */
    public void setHostName(String hostName) {
        this.hostName = hostName;
    }

    /**
     * @param virtualNICS
     *            the virtualNICS to set
     */
    public void setVirtualNICS(Set<VNIC> virtualNICS) {
        this.virtualNICS = virtualNICS;
    }

    @Override
    public String getDiskImageId() {
        return diskImageId;
    }

    @Override
    public String getServerId() {
        return serverId;
    }

    @Override
    public String getServerName() {
        return serverName;
    }

    @Override
    public String getNumOfCPU() {
        return numCPU;
    }

    @Override
    public String getHostName() {
        return hostName;
    }

    public Set<VNIC> getVirtualNICs() {
        return virtualNICS == null ? new HashSet<VNIC>() : virtualNICS;
    }

    /**
     * @param nic
     */
    public void addVirtualNic(VNIC nic) {
        if (virtualNICS == null) {
            virtualNICS = new HashSet<VNIC>();
        }
        virtualNICS.add(nic);
    }

    public boolean isEFM() {
        return isEFM;
    }

    /**
     * @param isEFM
     *            the isEFM to set
     */
    public void setEFM(boolean isEFM) {
        this.isEFM = isEFM;
    }

    @Override
    public String getServerType() {
        return serverType;
    }

    @Override
    public String getNetworkId() {
        return networkId;
    }

    @Override
    public String getMemorySize() {
        return null;
    }

    public void setNetworkId(String networkId) {
        this.networkId = networkId;
    }

}


text:
class WiggumActionContext(object):
    """ Object where actions will return and pass through the chain actions
        This object will say when the chain needs to break.
    """

    def __init__(self, request, response, extra_context=None,
                 break_chain=False):
        self.request = request
        self.response = response
        if not extra_context:
            extra_context = {}
        self.extra_context = extra_context
        self.break_chain = break_chain