Refine search
@Path("/display/*") @GET public void display(RakamHttpRequest request) { if (!database.isPresent()) { throw new RakamException("Custom page feature is not implemented", NOT_IMPLEMENTED); } String path = request.path().substring(21); String[] projectCustomPage = path.split("/", 3); byte[] bytes; try { InputStream file = database.get().getFile(Integer.parseInt(projectCustomPage[0]), projectCustomPage[1], projectCustomPage[2]); if (file == null) { request.response(NOT_FOUND.reasonPhrase(), NOT_FOUND).end(); } bytes = ByteStreams.toByteArray(file); } catch (IOException e) { throw Throwables.propagate(e); } HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); response.headers().set(CONTENT_TYPE, "text/html"); HttpHeaders.setContentLength(response, bytes.length); request.context().write(response); request.context().write(Unpooled.wrappedBuffer(bytes)); ChannelFuture lastContentFuture = request.context().writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); if (!HttpHeaders.isKeepAlive(request)) { lastContentFuture.addListener(ChannelFutureListener.CLOSE); } }
private void writeResponse(Channel channel, boolean keepAlive, HttpVersion httpRequestVersion, HttpResponseStatus status, String origin) { final HttpResponse response = new DefaultHttpResponse(httpRequestVersion, status); response.headers().set(HttpHeaderNames.CONTENT_LENGTH, 0); response.headers().set(HttpHeaderNames.CONNECTION, keepAlive ? HttpHeaderValues.KEEP_ALIVE : HttpHeaderValues.CLOSE); if (enableCors && origin != null && !origin.isEmpty()) { response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, origin); response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, true); response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, "Authorization, Content-Type"); } final ChannelFuture channelFuture = channel.writeAndFlush(response); if (!keepAlive) { channelFuture.addListener(ChannelFutureListener.CLOSE); } } }
/** * Sets the content type header for the HTTP Response * * @param response HTTP response * @param file file to extract content type */ private static void setContentTypeHeader(HttpResponse response, File file) { String contentType = StringKit.mimeType(file.getName()); if (null == contentType) { contentType = URLConnection.guessContentTypeFromName(file.getName()); } response.headers().set(HttpConst.CONTENT_TYPE, contentType); }
/** * Sets the content type header for the HTTP Response * * @param response HTTP response * @param file file to extract content type */ private static void setContentTypeHeader(HttpResponse response, File file) { String contentType = StringKit.mimeType(file.getName()); if (null == contentType) { contentType = URLConnection.guessContentTypeFromName(file.getName()); } response.headers().set(HttpConst.CONTENT_TYPE, contentType); }
/** * Sets the Date and Cache headers for the HTTP Response * * @param response HTTP response * @param fileToCache file to extract content type */ private void setDateAndCacheHeaders(HttpResponse response, File fileToCache) { response.headers().set(HttpConst.DATE, DateKit.gmtDate()); // Add cache headers if (httpCacheSeconds > 0) { response.headers().set(HttpConst.EXPIRES, DateKit.gmtDate(LocalDateTime.now().plusSeconds(httpCacheSeconds))); response.headers().set(HttpConst.CACHE_CONTROL, "private, max-age=" + httpCacheSeconds); if (null != fileToCache) { response.headers().set(HttpConst.LAST_MODIFIED, DateKit.gmtDate(new Date(fileToCache.lastModified()))); } else { response.headers().set(HttpConst.LAST_MODIFIED, DateKit.gmtDate(LocalDateTime.now().plusDays(-1))); } } }
/** * Sets the Date and Cache headers for the HTTP Response * * @param response HTTP response * @param fileToCache file to extract content type */ private void setDateAndCacheHeaders(HttpResponse response, File fileToCache) { response.headers().set(HttpConst.DATE, DateKit.gmtDate()); // Add cache headers if (httpCacheSeconds > 0) { response.headers().set(HttpConst.EXPIRES, DateKit.gmtDate(LocalDateTime.now().plusSeconds(httpCacheSeconds))); response.headers().set(HttpConst.CACHE_CONTROL, "private, max-age=" + httpCacheSeconds); if (null != fileToCache) { response.headers().set(HttpConst.LAST_MODIFIED, DateKit.gmtDate(new Date(fileToCache.lastModified()))); } else { response.headers().set(HttpConst.LAST_MODIFIED, DateKit.gmtDate(LocalDateTime.now().plusDays(-1))); } } }
private static void forbidden(final ChannelHandlerContext ctx, final HttpRequest request) { HttpResponse response = new DefaultFullHttpResponse(request.protocolVersion(), FORBIDDEN); response.headers().set(HttpHeaderNames.CONTENT_LENGTH, HttpHeaderValues.ZERO); release(request); respond(ctx, request, response); }
/** * Return that we need cannot not support the web socket version */ public static ChannelFuture sendUnsupportedVersionResponse(Channel channel, ChannelPromise promise) { HttpResponse res = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.UPGRADE_REQUIRED); res.headers().set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, WebSocketVersion.V13.toHttpHeaderValue()); HttpUtil.setContentLength(res, 0); return channel.writeAndFlush(res, promise); } }
/** * Sets the Date and Cache headers for the HTTP Response * * @param response * HTTP response * @param fileToCache * file to extract content type */ private static void setDateAndCacheHeaders(io.netty.handler.codec.http.HttpResponse response, File fileToCache) { SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US); dateFormatter.setTimeZone(TimeZone.getTimeZone(HTTP_DATE_GMT_TIMEZONE)); // Date header Calendar time = new GregorianCalendar(); response.headers().set(DATE, dateFormatter.format(time.getTime())); // Add cache headers time.add(Calendar.SECOND, HTTP_CACHE_SECONDS); response.headers() .set(EXPIRES, dateFormatter.format(time.getTime())) .set(CACHE_CONTROL, "private, max-age=" + HTTP_CACHE_SECONDS) .set(LAST_MODIFIED, dateFormatter.format(new Date(fileToCache.lastModified()))); }
@Override public void close() throws IOException { try { this.flush(); FileChannel file = new FileInputStream(this.file).getChannel(); long fileLength = file.size(); HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); httpResponse.headers().set(HttpConst.CONTENT_LENGTH, fileLength); httpResponse.headers().set(HttpConst.DATE, DateKit.gmtDate()); httpResponse.headers().set(HttpConst.SERVER, "blade/" + Const.VERSION); boolean keepAlive = WebContext.request().keepAlive(); if (keepAlive) { httpResponse.headers().set(HttpConst.CONNECTION, HttpConst.KEEP_ALIVE); } // Write the initial line and the header. ctx.write(httpResponse); ctx.write(new DefaultFileRegion(file, 0, fileLength), ctx.newProgressivePromise()); // Write the end marker. ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); } finally { if(null != outputStream){ outputStream.close(); } } }
@Override public void close() throws IOException { try { this.flush(); FileChannel file = new FileInputStream(this.file).getChannel(); long fileLength = file.size(); HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); httpResponse.headers().set(HttpConst.CONTENT_LENGTH, fileLength); httpResponse.headers().set(HttpConst.DATE, DateKit.gmtDate()); httpResponse.headers().set(HttpConst.SERVER, "blade/" + Const.VERSION); boolean keepAlive = WebContext.request().keepAlive(); if (keepAlive) { httpResponse.headers().set(HttpConst.CONNECTION, HttpConst.KEEP_ALIVE); } // Write the initial line and the header. ctx.write(httpResponse); ctx.write(new DefaultFileRegion(file, 0, fileLength), ctx.newProgressivePromise()); // Write the end marker. ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); } finally { if(null != outputStream){ outputStream.close(); } } }