@Override
  public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
    getLogger(ctx.getChannel()).debug("Error while processing imap request", e.getCause());

    if (e.getCause() instanceof TooLongFrameException) {

      // Max line length exceeded
      // See RFC 2683 section 3.2.1
      //
      // "For its part, a server should allow for a command line of at
      // least
      // 8000 octets. This provides plenty of leeway for accepting
      // reasonable
      // length commands from clients. The server should send a BAD
      // response
      // to a command that does not end within the server's maximum
      // accepted
      // command length."
      //
      // See also JAMES-1190
      ImapResponseComposer composer = (ImapResponseComposer) ctx.getAttachment();
      composer.untaggedResponse(
          ImapConstants.BAD + " failed. Maximum command line length exceeded");
    } else {
      // logout on error not sure if that is the best way to handle it
      final ImapSession imapSession = (ImapSession) attributes.get(ctx.getChannel());
      if (imapSession != null) imapSession.logout();

      // Make sure we close the channel after all the buffers were flushed out
      Channel channel = ctx.getChannel();
      if (channel.isConnected()) {
        channel.write(ChannelBuffers.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
      }
    }
  }
  private boolean handleRevision1Response(ChannelHandlerContext ctx, int payloadLength)
      throws Exception {
    int code = buffered.readInt();

    int descriptionLength = buffered.readInt();
    byte[] descBytes = new byte[descriptionLength];
    buffered.readBytes(descBytes, 0, descBytes.length);

    String description = new String(descBytes, StandardCharsets.UTF_8);

    logger.debug(
        "Decoded payload with length:[{}], code:[{}], descriptionLength:[{}], description:[{}] on connection [{}]",
        payloadLength,
        code,
        descriptionLength,
        description,
        ctx.getChannel().getLocalAddress());

    if (200 <= code && code <= 299) {
      logger.info(
          "Connected to Found Elasticsearch: [{}]: [{}] on connection [{}]",
          code,
          description,
          ctx.getChannel().getLocalAddress());
      return true;
    } else {
      logger.error(
          "Unable to connect to Found Elasticsearch: [{}]: [{}] on connection [{}]",
          code,
          description,
          ctx.getChannel().getLocalAddress());
      return false;
    }
  }
  private void cleanup(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
    try {
      ChannelBuffer cumulation = this.cumulation;
      if (cumulation == null) {
        return;
      } else {
        this.cumulation = null;
      }

      if (cumulation.readable()) {
        // Make sure all frames are read before notifying a closed
        // channel.
        callDecode(ctx, ctx.getChannel(), cumulation, null);
      }

      // Call decodeLast() finally. Please note that decodeLast() is
      // called even if there's nothing more to read from the buffer to
      // notify a user that the connection was closed explicitly.
      Object partialFrame = decodeLast(ctx, ctx.getChannel(), cumulation);
      if (partialFrame != null) {
        Channels.fireMessageReceived(ctx, partialFrame, null);
      }
    } finally {
      ctx.sendUpstream(e);
    }
  }
  @Override
  public void messageReceived(ChannelHandlerContext ctx, MessageEvent event) throws Exception {
    if (event.getMessage() instanceof MappingHttpRequest) {
      MappingHttpRequest httpRequest = (MappingHttpRequest) event.getMessage();

      if (httpRequest.getMessage() instanceof ObjectStorageDataRequestType) {
        if (httpRequest.isChunked()) {
          // Chunked request, and beginning, setup map etc.
          initializeNewPut(ctx, (ObjectStorageDataRequestType) httpRequest.getMessage());
        }
      }
    } else if (event.getMessage() instanceof HttpChunk) {
      // Add the chunk to the current streams channel buffer.
      HttpChunk chunk = (HttpChunk) event.getMessage();
      appendChunk(chunk.getContent(), ctx.getChannel());

      if (chunk.isLast()) {
        // Remove from the map
        Logs.extreme()
            .debug(
                "Removing data map due to last chunk processed event for channel: "
                    + ctx.getChannel().getId());
        dataMap.remove(ctx.getChannel());
      }
    }
    // Always pass it on
    ctx.sendUpstream(event);
  }
 @Override
 public void incomingMessage(ChannelHandlerContext ctx, MessageEvent event) throws Exception {
   if (event.getMessage() instanceof MappingHttpRequest) {
     MappingHttpRequest httpRequest = (MappingHttpRequest) event.getMessage();
     if (httpRequest.getMessage() instanceof WalrusRequestType) {
       WalrusRequestType request = (WalrusRequestType) httpRequest.getMessage();
       BucketLogData logData = request.getLogData();
       if (logData != null) {
         long currentTime = System.currentTimeMillis();
         logData.setTotalTime(currentTime);
         logData.setTurnAroundTime(currentTime);
         logData.setUri(httpRequest.getUri());
         String referrer = httpRequest.getHeader(HttpHeaders.Names.REFERER);
         if (referrer != null) logData.setReferrer(referrer);
         String userAgent = httpRequest.getHeader(HttpHeaders.Names.USER_AGENT);
         if (userAgent != null) logData.setUserAgent(userAgent);
         logData.setTimestamp(
             String.format("[%1$td/%1$tb/%1$tY:%1$tH:%1$tM:%1$tS %1$tz]", Calendar.getInstance()));
         User user = Contexts.lookup(httpRequest.getCorrelationId()).getUser();
         if (user != null) logData.setAccessorId(user.getUserId());
         if (request.getBucket() != null) logData.setBucketName(request.getBucket());
         if (request.getKey() != null) logData.setKey(request.getKey());
         if (ctx.getChannel().getRemoteAddress() instanceof InetSocketAddress) {
           InetSocketAddress sockAddress = (InetSocketAddress) ctx.getChannel().getRemoteAddress();
           logData.setSourceAddress(sockAddress.getAddress().getHostAddress());
         }
       }
     }
   }
 }
  @Override
  public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
    if (LOG.isTraceEnabled()) {
      LOG.trace("Channel closed: {}", ctx.getChannel());
    }

    Exchange exchange = getExchange(ctx);
    AsyncCallback callback = getAsyncCallback(ctx);

    // remove state
    producer.removeState(ctx.getChannel());

    // to keep track of open sockets
    producer.getAllChannels().remove(ctx.getChannel());

    if (producer.getConfiguration().isSync() && !messageReceived && !exceptionHandled) {
      // To avoid call the callback.done twice
      exceptionHandled = true;
      // session was closed but no message received. This could be because the remote server had an
      // internal error
      // and could not return a response. We should count down to stop waiting for a response
      if (LOG.isDebugEnabled()) {
        LOG.debug(
            "Channel closed but no message received from address: {}",
            producer.getConfiguration().getAddress());
      }
      exchange.setException(
          new CamelExchangeException(
              "No response received from remote server: "
                  + producer.getConfiguration().getAddress(),
              exchange));
      // signal callback
      callback.done(false);
    }
  }
Пример #7
0
  @Override
  public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
    super.channelDisconnected(ctx, e);

    if (!ctx.getChannel().isConnected()) {
      tryToFinishOffChannel(ctx.getChannel());
    }
  }
Пример #8
0
  public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
    logger.error(
        "something goes wrong with channel:{}, exception={}",
        ctx.getChannel(),
        ExceptionUtils.getStackTrace(e.getCause()));

    ctx.getChannel().close();
  }
Пример #9
0
 @Override
 public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
   context.getWorkers().put(ctx.getChannel(), new MasterWorkerHolder(ctx.getChannel()));
   Channel channel = ctx.getChannel();
   SocketAddress addr = channel.getRemoteAddress();
   SocketLog.info("worker connected , :" + addr.toString());
   super.channelConnected(ctx, e);
 }
  @Override
  public void channelBound(final ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {

    ImapSession imapsession =
        new NettyImapSession(
            ctx.getChannel(), logger, context, enabledCipherSuites, compress, plainAuthDisallowed);
    attributes.set(ctx.getChannel(), imapsession);
    super.channelBound(ctx, e);
  }
  @Override
  public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    if (handshakeComplete) {
      super.messageReceived(ctx, e);
    } else {
      if (e.getMessage() instanceof ChannelBuffer) {
        ChannelBuffer newBuffer = (ChannelBuffer) e.getMessage();
        buffered = ChannelBuffers.copiedBuffer(buffered, newBuffer);

        if (buffered.readableBytes() < 8) {
          return;
        }
        int payloadLength = buffered.getInt(0);
        int revision = buffered.getInt(4);

        boolean handshakeSuccessful = false;

        if (revision == 1 || revision == -1) {
          if (buffered.readableBytes() < payloadLength + 4) {
            return;
          }
          buffered.skipBytes(8);

          if (revision == 1) {
            handshakeSuccessful = handleRevision1Response(ctx, payloadLength);
          } else {
            handshakeSuccessful = handleGenericResponse(ctx, payloadLength);
          }
        } else {
          handshakeSuccessful = handleUnknownRevisionResponse(ctx);
        }

        if (!handshakeSuccessful) {
          ctx.getChannel().close();
        }

        if (keepAliveInterval.millis() > 0) {
          ctx.getPipeline()
              .addBefore(
                  ctx.getName(),
                  "found-connection-keep-alive",
                  new ConnectionKeepAliveHandler(scheduler, keepAliveInterval));
        }

        handshakeComplete = true;

        ChannelBuffer remaining = buffered.slice();
        if (remaining.readableBytes() > 0) {
          ctx.sendUpstream(
              new UpstreamMessageEvent(
                  ctx.getChannel(), remaining, ctx.getChannel().getRemoteAddress()));
        }

        ctx.getPipeline().remove(this);
      }
    }
  }
Пример #12
0
 private void handleInitialize(ChannelHandlerContext ctx, MappingHttpRequest request)
     throws IOException, SocketException {
   InetSocketAddress addr = (InetSocketAddress) ctx.getChannel().getRemoteAddress();
   LOG.info(LogUtil.subheader("Using " + addr.getHostName() + " as the database address."));
   Component.db.setHostAddress(addr.getHostName());
   Component.db.markEnabled();
   Component.dns.setHostAddress(addr.getHostName());
   Component.eucalyptus.setHostAddress(addr.getHostName());
   Component.cluster.setHostAddress(addr.getHostName());
   Component.jetty.setHostAddress(addr.getHostName());
   HeartbeatType msg = (HeartbeatType) request.getMessage();
   LOG.info(LogUtil.header("Got heartbeat event: " + LogUtil.dumpObject(msg)));
   for (HeartbeatComponentType component : msg.getComponents()) {
     LOG.info(LogUtil.subheader("Registering local component: " + LogUtil.dumpObject(component)));
     System.setProperty("euca." + component.getComponent() + ".name", component.getName());
     Component.valueOf(component.getComponent()).markLocal();
     // FIXME: this is needed because we can't dynamically change the mule config, so we need to
     // disable at init time and hup when a new component is loaded.
     initializedComponents.add(component.getComponent());
   }
   // FIXME: this is needed because we can't dynamically change the mule config, so we need to
   // disable at init time and hup when a new component is loaded.
   if (!initializedComponents.contains(Component.storage.name())) {
     Component.storage.markDisabled();
   }
   // FIXME: this is needed because we can't dynamically change the mule config, so we need to
   // disable at init time and hup when a new component is loaded.
   if (!initializedComponents.contains(Component.walrus.name())) {
     Component.walrus.markDisabled();
   }
   System.setProperty("euca.db.password", Hashes.getHexSignature());
   System.setProperty("euca.db.url", Component.db.getUri().toASCIIString());
   boolean foundDb = false;
   try {
     foundDb = NetworkUtil.testReachability(addr.getHostName());
     LOG.debug("Initializing SSL just in case: " + SslSetup.class);
     foundDb = true;
   } catch (Throwable e) {
     foundDb = false;
   }
   if (foundDb) {
     HttpResponse response =
         new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK);
     ChannelFuture writeFuture = ctx.getChannel().write(response);
     writeFuture.addListener(ChannelFutureListener.CLOSE);
     initialized = true;
     if (this.channel != null) {
       this.channel.close();
     }
   } else {
     HttpResponse response =
         new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.NOT_ACCEPTABLE);
     ChannelFuture writeFuture = ctx.getChannel().write(response);
     writeFuture.addListener(ChannelFutureListener.CLOSE);
   }
 }
Пример #13
0
  @Override
  public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
    super.channelClosed(ctx, e);

    if (!ctx.getChannel().isOpen()) {
      tryToFinishOffChannel(ctx.getChannel());
    }

    channelGroup.remove(e.getChannel());
  }
 /* (non-Javadoc)
  * @see org.jboss.netty.channel.ChannelFutureListener#operationComplete(org.jboss.netty.channel.ChannelFuture)
  */
 @Override
 public void operationComplete(ChannelFuture future) throws Exception {
   if (upstreamContext != null && bgpEvent != null) {
     upstreamContext.sendUpstream(
         new UpstreamMessageEvent(
             upstreamContext.getChannel(),
             bgpEvent,
             upstreamContext.getChannel().getRemoteAddress()));
   }
 }
Пример #15
0
 @Override
 public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
   ChannelBuffer buffer = (ChannelBuffer) e.getMessage();
   Route route = app.getRouter().parseRoute(getUtf(buffer));
   byte[] data = new byte[buffer.readableBytes()];
   buffer.readBytes(data);
   LocalSession localSession = sessions.getSession(ctx.getChannel().getId()).getLocalSession();
   byte[] response = server.handle(route, localSession, data);
   if (response != null) {
     ctx.getChannel().write(response);
   }
 }
  @Override
  public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
    InetSocketAddress address = (InetSocketAddress) ctx.getChannel().getRemoteAddress();
    getLogger(ctx.getChannel())
        .info("Connection closed for " + address.getAddress().getHostAddress());

    // remove the stored attribute for the channel to free up resources
    // See JAMES-1195
    ImapSession imapSession = (ImapSession) attributes.remove(ctx.getChannel());
    if (imapSession != null) imapSession.logout();

    super.channelClosed(ctx, e);
  }
Пример #17
0
 private void sendByteArray(ChannelHandlerContext ctx, byte[] buffer) {
   try {
     Channel channel = ctx.getChannel();
     ChannelFuture future = Channels.future(ctx.getChannel());
     ChannelBuffer b = ChannelBuffers.dynamicBuffer();
     // first send encoded key bytes size
     b.writeInt(buffer.length);
     // then the public key
     b.writeBytes(buffer);
     Channels.write(ctx, future, b);
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
  @Override
  public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
    InetSocketAddress address = (InetSocketAddress) ctx.getChannel().getRemoteAddress();
    getLogger(ctx.getChannel())
        .info("Connection established from " + address.getAddress().getHostAddress());

    ImapResponseComposer response =
        new ImapResponseComposerImpl(new ChannelImapResponseWriter(ctx.getChannel()));
    ctx.setAttachment(response);

    // write hello to client
    response.untagged().message("OK").message(hello).end();
    super.channelConnected(ctx, e);
  }
 @Override
 public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent evt) throws Exception {
   // Clear the map for this context
   try {
     Logs.extreme()
         .debug(
             "Removing data map on channel closed event for channel: " + ctx.getChannel().getId());
     dataMap.remove(ctx.getChannel());
   } catch (final Throwable f) {
     // Nothing to lookup.
   } finally {
     super.channelClosed(ctx, evt);
   }
 }
  private void sendHeader(ChannelHandlerContext ctx) throws IOException {
    headerSent = true;
    logger.info(
        "Authenticating with Found Elasticsearch at [{}] on connection [{}]",
        ctx.getChannel().getRemoteAddress(),
        ctx.getChannel().getLocalAddress());
    ChannelBuffer message = new FoundTransportHeader(clusterName.value(), apiKey).getHeaderBuffer();

    ctx.sendDownstream(
        new DownstreamMessageEvent(
            ctx.getChannel(),
            Channels.succeededFuture(ctx.getChannel()),
            message,
            ctx.getChannel().getRemoteAddress()));
  }
  private void sendModules(List<NetData.ModuleRequest> moduleRequestList) {
    for (NetData.ModuleRequest request : moduleRequestList) {
      NetData.ModuleDataHeader.Builder result = NetData.ModuleDataHeader.newBuilder();
      result.setId(request.getModuleId());
      Module module = moduleManager.getEnvironment().get(new Name(request.getModuleId()));
      if (module.isOnClasspath()
          || module.getLocations().size() != 1
          || !Files.isReadable(module.getLocations().get(0))) {
        result.setError("Module not available for download");
      } else {
        Path location = module.getLocations().get(0);
        try {
          result.setVersion(module.getVersion().toString());
          result.setSize(Files.size(location));
          channelHandlerContext
              .getChannel()
              .write(NetData.NetMessage.newBuilder().setModuleDataHeader(result).build());
        } catch (IOException e) {
          logger.error("Error sending module data header", e);
          channelHandlerContext.getChannel().close();
          break;
        }

        try (InputStream stream = new BufferedInputStream(Files.newInputStream(location))) {

          long remainingData = Files.size(location);
          byte[] data = new byte[1024];
          while (remainingData > 0) {
            int nextBlock = (int) Math.min(remainingData, 1024);
            ByteStreams.read(stream, data, 0, nextBlock);
            channelHandlerContext
                .getChannel()
                .write(
                    NetData.NetMessage.newBuilder()
                        .setModuleData(
                            NetData.ModuleData.newBuilder()
                                .setModule(ByteString.copyFrom(data, 0, nextBlock)))
                        .build());
            remainingData -= nextBlock;
          }
        } catch (IOException e) {
          logger.error("Error sending module", e);
          channelHandlerContext.getChannel().close();
          break;
        }
      }
    }
  }
  @Override
  public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent exceptionEvent)
      throws Exception {
    if (LOG.isTraceEnabled()) {
      LOG.trace("Exception caught at Channel: " + ctx.getChannel(), exceptionEvent.getCause());
    }

    if (exceptionHandled) {
      // ignore subsequent exceptions being thrown
      return;
    }

    exceptionHandled = true;
    Throwable cause = exceptionEvent.getCause();

    if (LOG.isDebugEnabled()) {
      LOG.debug("Closing channel as an exception was thrown from Netty", cause);
    }

    Exchange exchange = getExchange(ctx);
    AsyncCallback callback = getAsyncCallback(ctx);

    // the state may not be set
    if (exchange != null && callback != null) {
      // set the cause on the exchange
      exchange.setException(cause);

      // close channel in case an exception was thrown
      NettyHelper.close(exceptionEvent.getChannel());

      // signal callback
      callback.done(false);
    }
  }
Пример #23
0
  /**
   * Starts an SSL / TLS handshake for the specified channel.
   *
   * @return a {@link ChannelFuture} which is notified when the handshake succeeds or fails.
   */
  public ChannelFuture handshake() {
    if (handshaken && !isEnableRenegotiation()) {
      throw new IllegalStateException("renegotiation disabled");
    }

    ChannelHandlerContext ctx = this.ctx;
    Channel channel = ctx.getChannel();
    ChannelFuture handshakeFuture;
    synchronized (handshakeLock) {
      if (handshaking) {
        return this.handshakeFuture;
      } else {
        handshaking = true;
        try {
          engine.beginHandshake();
          runDelegatedTasks();
          handshakeFuture = this.handshakeFuture = future(channel);
        } catch (SSLException e) {
          handshakeFuture = this.handshakeFuture = failedFuture(channel, e);
        }
      }
    }

    try {
      wrapNonAppData(ctx, channel);
    } catch (SSLException e) {
      handshakeFuture.setFailure(e);
    }
    return handshakeFuture;
  }
Пример #24
0
 @Override
 public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
   // На канале произошло исключение. Выводим ошибку, закрываем канал.
   // Server.logger.log(Level.WARNING, "Exception from downstream", e.getCause());
   ctx.getChannel().close();
   log.info("exceptionCaught", e.getCause());
 }
Пример #25
0
 @Override
 public NettyHttpResponse chunked() {
   response.setHeader(Names.TRANSFER_ENCODING, Values.CHUNKED);
   response.setChunked(true);
   ctx.getChannel().write(response);
   return this;
 }
Пример #26
0
  @Override
  public synchronized void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
      throws Exception {
    channel = ctx.getChannel();

    List<Command<K, V, ?>> tmp = new ArrayList<Command<K, V, ?>>(queue.size() + 2);

    if (password != null) {
      CommandArgs<K, V> args = new CommandArgs<K, V>(codec).add(password);
      tmp.add(new Command<K, V, String>(AUTH, new StatusOutput<K, V>(codec), args, false));
    }

    if (db != 0) {
      CommandArgs<K, V> args = new CommandArgs<K, V>(codec).add(db);
      tmp.add(new Command<K, V, String>(SELECT, new StatusOutput<K, V>(codec), args, false));
    }

    tmp.addAll(queue);
    queue.clear();

    for (Command<K, V, ?> cmd : tmp) {
      if (!cmd.isCancelled()) {
        queue.add(cmd);
        channel.write(cmd);
      }
    }

    tmp.clear();
  }
Пример #27
0
 @Override
 public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
   Channel channel = ctx.getChannel();
   if (!sessions.removeSession(channel.getId())) {
     Log.error("删除Session失败! sockId: " + channel.getId());
   }
 }
Пример #28
0
    @Override
    public void run(Timeout timeout) throws Exception {
      if (timeout.isCancelled()) {
        return;
      }

      if (!ctx.getChannel().isOpen()) {
        return;
      }

      State state = (State) ctx.getAttachment();
      long currentTime = System.currentTimeMillis();
      long nextDelay = timeoutMillis - (currentTime - state.lastReadTime);
      if (nextDelay <= 0) {
        // Read timed out - set a new timeout and notify the callback.
        state.timeout = timer.newTimeout(this, timeoutMillis, TimeUnit.MILLISECONDS);
        try {
          // FIXME This should be called from an I/O thread.
          //       To be fixed in Netty 4.
          readTimedOut(ctx);
        } catch (Throwable t) {
          fireExceptionCaught(ctx, t);
        }
      } else {
        // Read occurred before the timeout - set a new timeout with shorter delay.
        state.timeout = timer.newTimeout(this, nextDelay, TimeUnit.MILLISECONDS);
      }
    }
Пример #29
0
  public static void serve404(
      NotFound e, ChannelHandlerContext ctx, Request request, HttpRequest nettyRequest) {
    Logger.trace("serve404: begin");
    HttpResponse nettyResponse =
        new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND);
    nettyResponse.setHeader(SERVER, signature);

    nettyResponse.setHeader(CONTENT_TYPE, "text/html");
    Map<String, Object> binding = getBindingForErrors(e, false);

    String format = Request.current().format;
    if (format == null) {
      format = "txt";
    }
    nettyResponse.setHeader(
        CONTENT_TYPE, (MimeTypes.getContentType("404." + format, "text/plain")));

    String errorHtml = TemplateLoader.load("errors/404." + format).render(binding);
    try {
      ChannelBuffer buf = ChannelBuffers.copiedBuffer(errorHtml.getBytes("utf-8"));
      nettyResponse.setContent(buf);
      ChannelFuture writeFuture = ctx.getChannel().write(nettyResponse);
      writeFuture.addListener(ChannelFutureListener.CLOSE);
    } catch (UnsupportedEncodingException fex) {
      Logger.error(fex, "(utf-8 ?)");
    }
    Logger.trace("serve404: end");
  }
Пример #30
0
  protected static void writeResponse(
      ChannelHandlerContext ctx,
      Response response,
      HttpResponse nettyResponse,
      HttpRequest nettyRequest) {
    Logger.trace("writeResponse: begin");
    byte[] content = null;

    final boolean keepAlive = isKeepAlive(nettyRequest);
    if (nettyRequest.getMethod().equals(HttpMethod.HEAD)) {
      content = new byte[0];
    } else {
      content = response.out.toByteArray();
    }

    ChannelBuffer buf = ChannelBuffers.copiedBuffer(content);
    nettyResponse.setContent(buf);

    if (keepAlive) {
      // Add 'Content-Length' header only for a keep-alive connection.
      Logger.trace("writeResponse: content length [" + response.out.size() + "]");
      setContentLength(nettyResponse, response.out.size());
    }

    ChannelFuture f = ctx.getChannel().write(nettyResponse);

    // Decide whether to close the connection or not.
    if (!keepAlive) {
      // Close the connection when the whole content is written out.
      f.addListener(ChannelFutureListener.CLOSE);
    }
    Logger.trace("writeResponse: end");
  }