Java Code Examples for org.jboss.netty.handler.codec.http.HttpResponseStatus#OK

The following examples show how to use org.jboss.netty.handler.codec.http.HttpResponseStatus#OK . 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 File: HlsStreamsHandler.java    From feeyo-hlsserver with 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 File: WelcomeHandler.java    From feeyo-hlsserver with 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 File: FileServerHandler.java    From netty-file-parent with 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 File: HttpServerHandler.java    From glowroot with 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 5
Source File: HlsVodHandler.java    From feeyo-hlsserver with 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 6
Source File: NettyHttpChannel.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private HttpResponseStatus getStatus(RestStatus status) {
    switch (status) {
        case CONTINUE:
            return HttpResponseStatus.CONTINUE;
        case SWITCHING_PROTOCOLS:
            return HttpResponseStatus.SWITCHING_PROTOCOLS;
        case OK:
            return HttpResponseStatus.OK;
        case CREATED:
            return HttpResponseStatus.CREATED;
        case ACCEPTED:
            return HttpResponseStatus.ACCEPTED;
        case NON_AUTHORITATIVE_INFORMATION:
            return HttpResponseStatus.NON_AUTHORITATIVE_INFORMATION;
        case NO_CONTENT:
            return HttpResponseStatus.NO_CONTENT;
        case RESET_CONTENT:
            return HttpResponseStatus.RESET_CONTENT;
        case PARTIAL_CONTENT:
            return HttpResponseStatus.PARTIAL_CONTENT;
        case MULTI_STATUS:
            // no status for this??
            return HttpResponseStatus.INTERNAL_SERVER_ERROR;
        case MULTIPLE_CHOICES:
            return HttpResponseStatus.MULTIPLE_CHOICES;
        case MOVED_PERMANENTLY:
            return HttpResponseStatus.MOVED_PERMANENTLY;
        case FOUND:
            return HttpResponseStatus.FOUND;
        case SEE_OTHER:
            return HttpResponseStatus.SEE_OTHER;
        case NOT_MODIFIED:
            return HttpResponseStatus.NOT_MODIFIED;
        case USE_PROXY:
            return HttpResponseStatus.USE_PROXY;
        case TEMPORARY_REDIRECT:
            return HttpResponseStatus.TEMPORARY_REDIRECT;
        case BAD_REQUEST:
            return HttpResponseStatus.BAD_REQUEST;
        case UNAUTHORIZED:
            return HttpResponseStatus.UNAUTHORIZED;
        case PAYMENT_REQUIRED:
            return HttpResponseStatus.PAYMENT_REQUIRED;
        case FORBIDDEN:
            return HttpResponseStatus.FORBIDDEN;
        case NOT_FOUND:
            return HttpResponseStatus.NOT_FOUND;
        case METHOD_NOT_ALLOWED:
            return HttpResponseStatus.METHOD_NOT_ALLOWED;
        case NOT_ACCEPTABLE:
            return HttpResponseStatus.NOT_ACCEPTABLE;
        case PROXY_AUTHENTICATION:
            return HttpResponseStatus.PROXY_AUTHENTICATION_REQUIRED;
        case REQUEST_TIMEOUT:
            return HttpResponseStatus.REQUEST_TIMEOUT;
        case CONFLICT:
            return HttpResponseStatus.CONFLICT;
        case GONE:
            return HttpResponseStatus.GONE;
        case LENGTH_REQUIRED:
            return HttpResponseStatus.LENGTH_REQUIRED;
        case PRECONDITION_FAILED:
            return HttpResponseStatus.PRECONDITION_FAILED;
        case REQUEST_ENTITY_TOO_LARGE:
            return HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE;
        case REQUEST_URI_TOO_LONG:
            return HttpResponseStatus.REQUEST_URI_TOO_LONG;
        case UNSUPPORTED_MEDIA_TYPE:
            return HttpResponseStatus.UNSUPPORTED_MEDIA_TYPE;
        case REQUESTED_RANGE_NOT_SATISFIED:
            return HttpResponseStatus.REQUESTED_RANGE_NOT_SATISFIABLE;
        case EXPECTATION_FAILED:
            return HttpResponseStatus.EXPECTATION_FAILED;
        case UNPROCESSABLE_ENTITY:
            return HttpResponseStatus.BAD_REQUEST;
        case LOCKED:
            return HttpResponseStatus.BAD_REQUEST;
        case FAILED_DEPENDENCY:
            return HttpResponseStatus.BAD_REQUEST;
        case TOO_MANY_REQUESTS:
            return TOO_MANY_REQUESTS;
        case INTERNAL_SERVER_ERROR:
            return HttpResponseStatus.INTERNAL_SERVER_ERROR;
        case NOT_IMPLEMENTED:
            return HttpResponseStatus.NOT_IMPLEMENTED;
        case BAD_GATEWAY:
            return HttpResponseStatus.BAD_GATEWAY;
        case SERVICE_UNAVAILABLE:
            return HttpResponseStatus.SERVICE_UNAVAILABLE;
        case GATEWAY_TIMEOUT:
            return HttpResponseStatus.GATEWAY_TIMEOUT;
        case HTTP_VERSION_NOT_SUPPORTED:
            return HttpResponseStatus.HTTP_VERSION_NOT_SUPPORTED;
        default:
            return HttpResponseStatus.INTERNAL_SERVER_ERROR;
    }
}