Java Code Examples for org.jboss.netty.handler.codec.http.HttpVersion

The following examples show how to use org.jboss.netty.handler.codec.http.HttpVersion. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: feeyo-hlsserver   Source File: HlsStreamsHandler.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void execute(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
	
	VelocityBuilder velocityBuilder = new VelocityBuilder();
	Map<String,Object> model = new HashMap<String, Object>();
	
	model.put("streams", HlsLiveStreamMagr.INSTANCE().getAllLiveStream()); 
	
	String htmlText = velocityBuilder.generate("streams.vm", "UTF8", model);
	
	HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
	response.headers().add(HttpHeaders.Names.CONTENT_LENGTH, htmlText.length());
	response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/html; charset=UTF-8");

	ChannelBuffer buffer = ChannelBuffers.copiedBuffer(htmlText, Charset.defaultCharset());// CharsetUtil.UTF_8);
	response.setContent(buffer);

	ChannelFuture channelFuture = ctx.getChannel().write(response);
	if (channelFuture.isSuccess()) {
		channelFuture.getChannel().close();
	}
	
}
 
Example 2
Source Project: feeyo-hlsserver   Source File: WelcomeHandler.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void execute(ChannelHandlerContext ctx, MessageEvent e) {

	VelocityBuilder velocityBuilder = new VelocityBuilder();
	String htmlText = velocityBuilder.generate("index.vm", "UTF8", null);	
	
	HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
	response.headers().add(HttpHeaders.Names.CONTENT_LENGTH, htmlText.length());
	response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/html; charset=UTF-8");
	
	ChannelBuffer buffer = ChannelBuffers.copiedBuffer(htmlText, CharsetUtil.UTF_8);
	response.setContent(buffer);

	ChannelFuture channelFuture = ctx.getChannel().write(response);
	if (channelFuture.isSuccess()) {
		channelFuture.getChannel().close();
	}
}
 
Example 3
Source Project: netty-file-parent   Source File: FileServerHandler.java    License: Apache License 2.0 6 votes vote down vote up
private void writeResponse(Channel channel) {
	ChannelBuffer buf = ChannelBuffers.copiedBuffer(
			this.responseContent.toString(), CharsetUtil.UTF_8);
	this.responseContent.setLength(0);

	boolean close = ("close".equalsIgnoreCase(this.request
			.getHeader("Connection")))
			|| ((this.request.getProtocolVersion()
					.equals(HttpVersion.HTTP_1_0)) && (!"keep-alive"
					.equalsIgnoreCase(this.request.getHeader("Connection"))));

	HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
			HttpResponseStatus.OK);
	response.setContent(buf);
	response.setHeader("Content-Type", "text/plain; charset=UTF-8");
	if (!close) {
		response.setHeader("Content-Length",
				String.valueOf(buf.readableBytes()));
	}
	ChannelFuture future = channel.write(response);
	if (close)
		future.addListener(ChannelFutureListener.CLOSE);
}
 
Example 4
Source Project: netty-file-parent   Source File: WrapFileClientHandler.java    License: Apache License 2.0 5 votes vote down vote up
public WrapFileClientHandler(String host, URI uri, String userName,
		String pwd) {
	this.host = host;
	this.uri = uri;
	this.userName = userName;
	this.pwd = pwd;
	this.request = new DefaultHttpRequest(HttpVersion.HTTP_1_1,
			HttpMethod.POST, uri.toASCIIString());
	setHeaderDatas();
}
 
Example 5
Source Project: glowroot   Source File: HttpServerHandler.java    License: Apache License 2.0 5 votes vote down vote up
private void writeResponse(MessageEvent e) throws Exception {
    HttpResponse response =
            new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    response.setContent(ChannelBuffers.copiedBuffer("Hello world", CharsetUtil.UTF_8));
    addHeader(response, HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8");
    ChannelFuture future = e.getChannel().write(response);
    future.addListener(ChannelFutureListener.CLOSE);
}
 
Example 6
Source Project: elasticsearch-helper   Source File: HttpAction.java    License: Apache License 2.0 5 votes vote down vote up
protected HttpRequest newRequest(HttpMethod method, URL url, String path, ChannelBuffer buffer) {
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, method, path);
    request.headers().add(HttpHeaders.Names.HOST, url.getHost());
    request.headers().add(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
    request.headers().add(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
    if (buffer != null) {
        request.setContent(buffer);
        int length = request.getContent().readableBytes();
        request.headers().add(HttpHeaders.Names.CONTENT_TYPE, "application/json");
        request.headers().add(HttpHeaders.Names.CONTENT_LENGTH, length);
    }
    return request;
}
 
Example 7
Source Project: feeyo-hlsserver   Source File: HlsVodHandler.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void execute(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
	
    HttpRequest request = (DefaultHttpRequest) e.getMessage();
    if ( !request.getUri().matches(regex) ) {
        LOGGER.warn("bad request: " + request.getUri());
        HttpUtil.sendError(ctx, HttpResponseStatus.NOT_FOUND);
        return;
    }

    
    String[] path = request.getUri().split("/");
    String alias = path[3];
    final String reqFileName = path[4];
    
    Long streamId = HlsLiveStreamMagr.INSTANCE().getStreamIdByAlias(alias);
    if( streamId == null ) {
        LOGGER.warn(" aac vod, lookup alias failed: " + alias);
        HttpUtil.sendError(ctx, HttpResponseStatus.NOT_FOUND);
        return;
    }
    
    
    OssUtil ossOperation = new OssUtil();
    byte[] content = null;

    if (reqFileName.endsWith(".m3u8")) {
    	
        if ( !ossOperation.doesObjectExist(reqFileName, streamId) ) {
        	
            boolean needWaite = false;
            // the very first listener of a specific m3u8 will create the m3u8 file and the ts file
            // the reset will be in a waiter set
            synchronized (m3u8WaiteSet) {
                if (!m3u8WaiteSet.add(reqFileName)) {
                    needWaite = true;
                }
            }

            if (needWaite) {
                synchronized (m3u8WaiteSet) {
                    while (m3u8WaiteSet.contains(reqFileName)) {
                        m3u8WaiteSet.wait(1000);
                    }
                }
            } else {
                content = generateTsFiles(reqFileName, streamId);
            }
        }
    }

    if (content == null) {
        content = cachedVodTsFiles.get(reqFileName);
        if (content == null) {
            if (ossOperation.doesObjectExist(reqFileName, streamId)){
                InputStream inputStream = ossOperation.readObject(reqFileName, streamId);

                ObjectMetadata objectMetadata = ossOperation.getObjectMetadata(reqFileName,streamId);
                int len = (int)objectMetadata.getContentLength();
                content = new byte[len];

                int writePtr = 0;
                for (;;) {
                    int ret = inputStream.read(content, writePtr,len-writePtr);
                    if ((ret == -1) || (writePtr += ret) >= len)
                        break;
                }
            } else {
                LOGGER.warn("request file not on OSS: " + request.getUri());
                HttpUtil.sendError(ctx, HttpResponseStatus.NOT_FOUND);
                return;
            }
        }
    }

    ossOperation.closeOSSClient();

    long timeMillis = System.currentTimeMillis();

    DefaultHttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    response.headers().set(HttpHeaders.Names.DATE, HttpUtil.getDateString(timeMillis));
    response.headers().set(HttpHeaders.Names.CONTENT_TYPE, HttpUtil.getMimeType(reqFileName));
    response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, content.length);
    response.headers().set(HttpHeaders.Names.LAST_MODIFIED, HttpUtil.getDateString(timeMillis));
    response.headers().set(HttpHeaders.Names.EXPIRES, HttpUtil.getDateString(timeMillis + VOD_CACHE_TIME));
    response.headers().set(HttpHeaders.Names.CACHE_CONTROL, "max-age=" + (VOD_CACHE_TIME/1000));

    response.setContent(ChannelBuffers.copiedBuffer(content));

    e.getChannel().write(response);
}
 
Example 8
Source Project: feeyo-hlsserver   Source File: HttpServerRequestHandler.java    License: Apache License 2.0 4 votes vote down vote up
private HttpResponse buildDefaultResponse(String msg, HttpResponseStatus status){
	HttpResponse errorResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);
	errorResponse.setContent( ChannelBuffers.copiedBuffer(msg, Charset.defaultCharset())  );
    return errorResponse;
}
 
Example 9
Source Project: feeyo-hlsserver   Source File: HttpServerRequestHandler.java    License: Apache License 2.0 4 votes vote down vote up
private void sendRedirect(ChannelHandlerContext ctx, String newUri) {
	HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FOUND);
    response.headers().set(HttpHeaders.Names.LOCATION, newUri);
    ctx.getChannel().write(response).addListener(ChannelFutureListener.CLOSE);
}
 
Example 10
Source Project: Elasticsearch   Source File: NettyHttpChannel.java    License: Apache License 2.0 4 votes vote down vote up
private boolean isHttp10() {
    return nettyRequest.getProtocolVersion().equals(HttpVersion.HTTP_1_0);
}