當前位置: 首頁>>代碼示例>>Java>>正文


Java HttpResponse.setContent方法代碼示例

本文整理匯總了Java中org.jboss.netty.handler.codec.http.HttpResponse.setContent方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpResponse.setContent方法的具體用法?Java HttpResponse.setContent怎麽用?Java HttpResponse.setContent使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.jboss.netty.handler.codec.http.HttpResponse的用法示例。


在下文中一共展示了HttpResponse.setContent方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: handle

import org.jboss.netty.handler.codec.http.HttpResponse; //導入方法依賴的package包/類
@Override
public void handle(Channel channel, Token<DelegationTokenIdentifier> token,
    String serviceUrl) throws IOException {
  Assert.assertEquals(testToken, token);

  Credentials creds = new Credentials();
  creds.addToken(new Text(serviceUrl), token);
  DataOutputBuffer out = new DataOutputBuffer();
  creds.write(out);
  int fileLength = out.getData().length;
  ChannelBuffer cbuffer = ChannelBuffers.buffer(fileLength);
  cbuffer.writeBytes(out.getData());
  HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
  response.setHeader(HttpHeaders.Names.CONTENT_LENGTH,
      String.valueOf(fileLength));
  response.setContent(cbuffer);
  channel.write(response).addListener(ChannelFutureListener.CLOSE);
}
 
開發者ID:naver,項目名稱:hadoop,代碼行數:19,代碼來源:TestDelegationTokenRemoteFetcher.java

示例2: messageReceived

import org.jboss.netty.handler.codec.http.HttpResponse; //導入方法依賴的package包/類
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
  try {
    final HttpRequest req = (HttpRequest) e.getMessage();
    if (req.getMethod().equals(HttpMethod.POST)) {
      doPost(ctx, e, req);
    } else if (req.getMethod().equals(HttpMethod.GET)) {
      doGet(ctx, e, req);
    } else {
      writeResponseAndClose(e, new DefaultHttpResponse(
              HttpVersion.HTTP_1_1,
              HttpResponseStatus.BAD_REQUEST));
    }
  } catch (Exception ex) {
    if (logger.isDebugEnabled())
      logger.debug("Failed to process message", ex);
    HttpResponse response = new DefaultHttpResponse(
            HttpVersion.HTTP_1_1,
            HttpResponseStatus.INTERNAL_SERVER_ERROR);
    response.setContent(
            ChannelBuffers.copiedBuffer(ex.getMessage().getBytes()));
    writeResponseAndClose(e, response);
  }
}
 
開發者ID:yandex,項目名稱:opentsdb-flume,代碼行數:25,代碼來源:LegacyHttpSource.java

示例3: doPost

import org.jboss.netty.handler.codec.http.HttpResponse; //導入方法依賴的package包/類
private void doPost(ChannelHandlerContext ctx, MessageEvent e, HttpRequest req)
        throws IOException {

  final QueryStringDecoder decoded = new QueryStringDecoder(req.getUri());
  if (!decoded.getPath().equalsIgnoreCase("/write")) {
    writeResponseAndClose(e,
            new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND));
    return;
  }

  try {
    metricParser.parse(req);
  } catch (IllegalArgumentException iae) {
    logger.warn("Metric parser failed: " + iae.getMessage());
  }

  HttpResponse response = new DefaultHttpResponse(
          HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
  response.setContent(ChannelBuffers.copiedBuffer(
          ("Seen events").getBytes()
  ));
  writeResponseAndClose(e, response);
}
 
開發者ID:yandex,項目名稱:opentsdb-flume,代碼行數:24,代碼來源:LegacyHttpSource.java

示例4: messageReceived

import org.jboss.netty.handler.codec.http.HttpResponse; //導入方法依賴的package包/類
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent messageEvent) throws Exception {
    // store request, as this channel handler is created per pipeline
    HttpRequest request = (HttpRequest) messageEvent.getMessage();

    LOG.debug("Message received: {}", request);

    HttpServerChannelHandler handler = getHandler(request);
    if (handler != null) {
        // store handler as attachment
        ctx.setAttachment(handler);
        handler.messageReceived(ctx, messageEvent);
    } else {
        // this resource is not found, so send empty response back
        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, NOT_FOUND);
        response.headers().set(Exchange.CONTENT_TYPE, "text/plain");
        response.headers().set(Exchange.CONTENT_LENGTH, 0);
        response.setContent(ChannelBuffers.copiedBuffer(new byte[]{}));
        messageEvent.getChannel().write(response).syncUninterruptibly();
        // close the channel after send error message
        messageEvent.getChannel().close();
    }
}
 
開發者ID:HydAu,項目名稱:Camel,代碼行數:24,代碼來源:HttpServerMultiplexChannelHandler.java

示例5: sendHttpResponse

import org.jboss.netty.handler.codec.http.HttpResponse; //導入方法依賴的package包/類
private static void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res)
{
	// Generate an error page if response status code is not OK (200).
	if (res.getStatus().getCode() != 200)
	{
		res.setContent(ChannelBuffers.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8));
		setContentLength(res, res.getContent().readableBytes());
	}
	
	// Send the response and close the connection if necessary.
	ChannelFuture f = ctx.getChannel().write(res);
	if (!isKeepAlive(req) || (res.getStatus().getCode() != 200))
	{
		f.addListener(ChannelFutureListener.CLOSE);
	}
}
 
開發者ID:EricssonResearch,項目名稱:trap,代碼行數:17,代碼來源:WebServerSocketHandler.java

示例6: encode

import org.jboss.netty.handler.codec.http.HttpResponse; //導入方法依賴的package包/類
@Override
protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg)
        throws Exception
{
  RestResponse response = (RestResponse) msg;

  HttpResponse nettyResponse =
      new DefaultHttpResponse(HttpVersion.HTTP_1_1,
                              HttpResponseStatus.valueOf(response.getStatus()));

  for (Map.Entry<String, String> e : response.getHeaders().entrySet())
  {
    nettyResponse.setHeader(e.getKey(), e.getValue());
  }
  final ByteString entity = response.getEntity();
  ChannelBuffer buf = ChannelBuffers.wrappedBuffer(entity.asByteBuffer());
  nettyResponse.setContent(buf);
  nettyResponse.setHeader(HttpHeaders.Names.CONTENT_LENGTH, entity.length());

  return nettyResponse;
}
 
開發者ID:ppdai,項目名稱:rest4j,代碼行數:22,代碼來源:RAPServerCodec.java

示例7: writeResponse

import org.jboss.netty.handler.codec.http.HttpResponse; //導入方法依賴的package包/類
private void writeResponse(MessageEvent e) {
  HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
  response.setHeader(HttpHeaders.Names.CONTENT_TYPE, "application/json; charset=UTF-8");

  ChannelBuffer content = ChannelBuffers.dynamicBuffer();
  Writer writer = new OutputStreamWriter(new ChannelBufferOutputStream(content), CharsetUtil.UTF_8);
  reportAdapter.toJson(report.get(), writer);
  try {
    writer.close();
  } catch (IOException e1) {
    LOG.error("error writing resource report", e1);
  }
  response.setContent(content);
  ChannelFuture future = e.getChannel().write(response);
  future.addListener(ChannelFutureListener.CLOSE);
}
 
開發者ID:chtyim,項目名稱:incubator-twill,代碼行數:17,代碼來源:TrackerService.java

示例8: messageReceived

import org.jboss.netty.handler.codec.http.HttpResponse; //導入方法依賴的package包/類
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
        throws Exception {
    HttpRequest request = (HttpRequest) e.getMessage();
    String uri = request.getUri();
    
    System.out.println("uri:" + uri);
    HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    ChannelBuffer buffer = new DynamicChannelBuffer(2048);
    buffer.writeBytes("hello!! 你好".getBytes("UTF-8"));
    response.setContent(buffer);
    response.setHeader("Content-Type", "text/html; charset=UTF-8");
    response.setHeader("Content-Length", response.getContent().writerIndex());
    Channel ch = e.getChannel();
    // Write the initial line and the header.
    ch.write(response);
    ch.disconnect();
    ch.close();

}
 
開發者ID:laizhihuan,項目名稱:java-test-demo,代碼行數:21,代碼來源:HttpServerHandler.java

示例9: sendResponse

import org.jboss.netty.handler.codec.http.HttpResponse; //導入方法依賴的package包/類
private void sendResponse(Channel channel, String message) {
    if (channel != null) {
        HttpResponse response = new DefaultHttpResponse(
                HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
        response.setContent(ChannelBuffers.copiedBuffer(
                ByteOrder.BIG_ENDIAN, message, StandardCharsets.US_ASCII));
        channel.write(response);
    }
}
 
開發者ID:bamartinezd,項目名稱:traccar-service,代碼行數:10,代碼來源:PiligrimProtocolDecoder.java

示例10: sendResponse

import org.jboss.netty.handler.codec.http.HttpResponse; //導入方法依賴的package包/類
private void sendResponse(Channel channel, short packetId, short packetCount) {
    HttpResponse response = new DefaultHttpResponse(
            HttpVersion.HTTP_1_1, HttpResponseStatus.OK);

    ChannelBuffer begin = ChannelBuffers.copiedBuffer("#ACK#", StandardCharsets.US_ASCII);
    ChannelBuffer end = ChannelBuffers.directBuffer(3);
    end.writeByte(packetId);
    end.writeByte(packetCount);
    end.writeByte(0);

    response.setContent(ChannelBuffers.wrappedBuffer(begin, end));
    channel.write(response);
}
 
開發者ID:bamartinezd,項目名稱:traccar-service,代碼行數:14,代碼來源:Mta6ProtocolDecoder.java

示例11: sendError

import org.jboss.netty.handler.codec.http.HttpResponse; //導入方法依賴的package包/類
protected void sendError(ChannelHandlerContext ctx, String message,
    HttpResponseStatus status) {
  HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status);
  response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8");
  // Put shuffle version into http header
  response.setHeader(ShuffleHeader.HTTP_HEADER_NAME,
      ShuffleHeader.DEFAULT_HTTP_HEADER_NAME);
  response.setHeader(ShuffleHeader.HTTP_HEADER_VERSION,
      ShuffleHeader.DEFAULT_HTTP_HEADER_VERSION);
  response.setContent(
    ChannelBuffers.copiedBuffer(message, CharsetUtil.UTF_8));

  // Close the connection as soon as the error message is sent.
  ctx.getChannel().write(response).addListener(ChannelFutureListener.CLOSE);
}
 
開發者ID:naver,項目名稱:hadoop,代碼行數:16,代碼來源:ShuffleHandler.java

示例12: writeResponse

import org.jboss.netty.handler.codec.http.HttpResponse; //導入方法依賴的package包/類
private void writeResponse(MessageEvent e) 
{
    // Build the response object.
    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
    
    response.setContent(ChannelBuffers.copiedBuffer(buf.toString(), CharsetUtil.UTF_8));
    response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8");

    // Write the response.
    ChannelFuture future = e.getChannel().write(response);

    // Close the non-keep-alive connection after the write operation is done.
    future.addListener(ChannelFutureListener.CLOSE);
}
 
開發者ID:ZalemSoftware,項目名稱:OpenMobster,代碼行數:15,代碼來源:HttpServerHandler.java

示例13: sendHttpResponse

import org.jboss.netty.handler.codec.http.HttpResponse; //導入方法依賴的package包/類
/**
 * Sends an HTTP response
 * @param ctx The channel handler context
 * @param req The HTTP request being responded to
 * @param res The HTTP response to send
 */
private void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res) {
    // Generate an error page if response status code is not OK (200).
    if (res.getStatus().getCode() != 200) {
        res.setContent(ChannelBuffers.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8));
        setContentLength(res, res.getContent().readableBytes());
    }

    // Send the response and close the connection if necessary.
    ChannelFuture f = ctx.getChannel().write(res);
    if (!isKeepAlive(req) || res.getStatus().getCode() != 200) {
        f.addListener(ChannelFutureListener.CLOSE);
    }
}
 
開發者ID:nickman,項目名稱:HeliosStreams,代碼行數:20,代碼來源:WebSocketServiceHandler.java

示例14: getParameterReceived

import org.jboss.netty.handler.codec.http.HttpResponse; //導入方法依賴的package包/類
/**
 * Handle GET_PARAMETER request. Currently only {@code volume} is supported
 */
public synchronized void getParameterReceived(final ChannelHandlerContext ctx, final HttpRequest req) throws ProtocolException {
	final StringBuilder body = new StringBuilder();

	if (audioOutputQueue != null) {
		/* Report output gain */
		body.append("volume: ");
		body.append(audioOutputQueue.getRequestedVolume());
		body.append("\r\n");
	}

	final HttpResponse response = new DefaultHttpResponse(RtspVersions.RTSP_1_0,  RtspResponseStatuses.OK);
	response.setContent(ChannelBuffers.wrappedBuffer(body.toString().getBytes(Charset.forName("ASCII"))));
	ctx.getChannel().write(response);
}
 
開發者ID:SergioChan,項目名稱:Android-Airplay-Server,代碼行數:18,代碼來源:RaopAudioHandler.java

示例15: process

import org.jboss.netty.handler.codec.http.HttpResponse; //導入方法依賴的package包/類
public boolean process(HttpRequest request, HttpResponse response)
    throws Exception {
    String uri = request.getUri();
    if (uri.equalsIgnoreCase(FAVICON)) {
        response.setStatus(HttpResponseStatus.NOT_FOUND);
        log.warn("favicon.ico isn't found!");
        response.setContent(ChannelBuffers.copiedBuffer(HttpResponseStatus.NOT_FOUND.toString(), CharsetUtil.UTF_8));
        return false;
    }

    return true;
}
 
開發者ID:sunguangran,項目名稱:navi,代碼行數:13,代碼來源:FaviousRequestLister.java


注:本文中的org.jboss.netty.handler.codec.http.HttpResponse.setContent方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。