/** * Returns the value of the {@code "Host"} header. If there is no such * header, the {@code defaultValue} is returned. */ public static String getHost(HttpMessage message, String defaultValue) { return getHeader(message, Names.HOST, defaultValue); }
/** * Returns the integer header value with the specified header name. If * there are more than one header value for the specified header name, the * first value is returned. * * @return the header value or the {@code defaultValue} if there is no such * header or the header value is not a number */ public static int getIntHeader(HttpMessage message, String name, int defaultValue) { String value = getHeader(message, name); if (value == null) { return defaultValue; } try { return Integer.parseInt(value); } catch (NumberFormatException e) { // Not an integer header - use the default. return defaultValue; } }
/** * Returns the integer header value with the specified header name. If * there are more than one header value for the specified header name, the * first value is returned. * * @return the header value * @throws NumberFormatException * if there is no such header or the header value is not a number */ public static int getIntHeader(HttpMessage message, String name) { String value = getHeader(message, name); if (value == null) { throw new NumberFormatException("header not found: " + name); } return Integer.parseInt(value); }
/** * Returns the length of the content. Please note that this value is * not retrieved from {@link HttpMessage#getContent()} but from the * {@code "Content-Length"} header, and thus they are independent from each * other. * * @return the content length * * @throws NumberFormatException * if the message does not have the {@code "Content-Length"} header * or its value is not a number */ public static long getContentLength(HttpMessage message) { String value = getHeader(message, Names.CONTENT_LENGTH); if (value != null) { return Long.parseLong(value); } // We know the content length if it's a Web Socket message even if // Content-Length header is missing. long webSocketContentLength = getWebSocketContentLength(message); if (webSocketContentLength >= 0) { return webSocketContentLength; } // Otherwise we don't. throw new NumberFormatException("header not found: " + Names.CONTENT_LENGTH); }
/** * Returns the date header value with the specified header name. If * there are more than one header value for the specified header name, the * first value is returned. * * @return the header value * @throws ParseException * if there is no such header or the header value is not a formatted date */ public static Date getDateHeader(HttpMessage message, String name) throws ParseException { String value = getHeader(message, name); if (value == null) { throw new ParseException("header not found: " + name, 0); } return HttpHeaderDateFormat.get().parse(value); }
/** * Returns the date header value with the specified header name. If * there are more than one header value for the specified header name, the * first value is returned. * * @return the header value or the {@code defaultValue} if there is no such * header or the header value is not a formatted date */ public static Date getDateHeader(HttpMessage message, String name, Date defaultValue) { final String value = getHeader(message, name); if (value == null) { return defaultValue; } try { return HttpHeaderDateFormat.get().parse(value); } catch (ParseException e) { // Not a date header - use the default. return defaultValue; } }
@Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { final Channel channel = ctx.getChannel(); if (e instanceof ClosedChannelException || channel == null || !channel.isOpen()) { log.debug("Not writing any response, channel is already closed.", e.getCause()); return; } log.error("Uncaught exception during jersey resource handling", e.getCause()); final HttpRequest request = (HttpRequest) ctx.getAttachment(); final HttpVersion protocolVersion; if (request != null && request.getProtocolVersion() != null) { protocolVersion = request.getProtocolVersion(); } else { protocolVersion = HttpVersion.HTTP_1_0; } final DefaultHttpResponse response = new DefaultHttpResponse(protocolVersion, HttpResponseStatus.INTERNAL_SERVER_ERROR); final ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); new ChannelBufferOutputStream(buffer).writeBytes(e.toString()); response.setContent(buffer); final ChannelFuture channelFuture = channel.write(response); if ((protocolVersion == HttpVersion.HTTP_1_0) || request == null || HttpHeaders.getHeader(request, HttpHeaders.Names.CONNECTION).equalsIgnoreCase("close")) { channelFuture.addListener(ChannelFutureListener.CLOSE); } }
chunkedRequestAssembler.setup(e.getChannel(), httpRequest); String expectHeader = HttpHeaders.getHeader(httpRequest, HttpHeaders.Names.EXPECT); if (expectHeader != null && expectHeader.equals("100-continue")) { final DefaultHttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE); String[] schemeCreds = extractBasicAuthCredentials(HttpHeaders.getHeader(httpRequest, HttpHeaders.Names.AUTHORIZATION)); String scheme = null; final String connectionHeader = HttpHeaders.getHeader(httpRequest, HttpHeaders.Names.CONNECTION); if (connectionHeader != null && connectionHeader.equals("close")) { closeConnection = true;