List of usage examples for io.netty.handler.codec.http HttpMethod GET
HttpMethod GET
To view the source code for io.netty.handler.codec.http HttpMethod GET.
Click Source Link
From source file:org.apache.tajo.storage.http.ExampleHttpServerHandler.java
License:Apache License
@Override protected void channelRead0(ChannelHandlerContext context, FullHttpRequest request) throws Exception { if (request.getMethod().equals(HttpMethod.HEAD)) { processHead(context, request);//from ww w . j a v a 2 s .c o m } else if (request.getMethod().equals(HttpMethod.GET)) { processGet(context, request); } else { // error String msg = "Not supported method: " + request.getMethod(); LOG.error(msg); context.writeAndFlush(getBadRequest(msg)); } }
From source file:org.apache.tajo.worker.Fetcher.java
License:Apache License
public FileChunk get() throws IOException { if (useLocalFile) { LOG.info("Get pseudo fetch from local host"); startTime = System.currentTimeMillis(); finishTime = System.currentTimeMillis(); state = TajoProtos.FetcherState.FETCH_FINISHED; return fileChunk; }// ww w . j a v a 2s. co m LOG.info("Get real fetch from remote host"); this.startTime = System.currentTimeMillis(); this.state = TajoProtos.FetcherState.FETCH_FETCHING; ChannelFuture future = null; try { future = bootstrap.clone().connect(new InetSocketAddress(host, port)) .addListener(ChannelFutureListener.CLOSE_ON_FAILURE); // Wait until the connection attempt succeeds or fails. Channel channel = future.awaitUninterruptibly().channel(); if (!future.isSuccess()) { state = TajoProtos.FetcherState.FETCH_FAILED; throw new IOException(future.cause()); } String query = uri.getPath() + (uri.getRawQuery() != null ? "?" + uri.getRawQuery() : ""); // Prepare the HTTP request. HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, query); request.headers().set(HttpHeaders.Names.HOST, host); request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP); LOG.info("Status: " + getState() + ", URI:" + uri); // Send the HTTP request. ChannelFuture channelFuture = channel.writeAndFlush(request); // Wait for the server to close the connection. channel.closeFuture().awaitUninterruptibly(); channelFuture.addListener(ChannelFutureListener.CLOSE); fileChunk.setLength(fileChunk.getFile().length()); return fileChunk; } finally { if (future != null) { // Close the channel to exit. future.channel().close(); } this.finishTime = System.currentTimeMillis(); LOG.info("Fetcher finished:" + (finishTime - startTime) + " ms, " + getState() + ", URI:" + uri); } }
From source file:org.apache.tajo.worker.LocalFetcher.java
License:Apache License
private List<FileChunk> getChunksForRangeShuffle(final PullServerParams params, final Path queryBaseDir) throws IOException { final List<FileChunk> fileChunks = new ArrayList<>(); if (state == FetcherState.FETCH_INIT) { final ChannelInitializer<Channel> initializer = new HttpClientChannelInitializer(); bootstrap.handler(initializer);//w w w . j av a 2 s . com } this.state = FetcherState.FETCH_META_FETCHING; ChannelFuture future = null; try { future = bootstrap.clone().connect(new InetSocketAddress(host, port)) .addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); // Wait until the connection attempt succeeds or fails. Channel channel = future.awaitUninterruptibly().channel(); if (!future.isSuccess()) { endFetch(FetcherState.FETCH_FAILED); throw new IOException(future.cause()); } for (URI eachURI : createChunkMetaRequestURIs(host, port, params)) { String query = eachURI.getPath() + (eachURI.getRawQuery() != null ? "?" + eachURI.getRawQuery() : ""); HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, query); request.headers().set(HttpHeaders.Names.HOST, host); request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP); if (LOG.isDebugEnabled()) { LOG.debug("Status: " + getState() + ", URI:" + eachURI); } // Send the HTTP request. channel.writeAndFlush(request); } // Wait for the server to close the connection. throw exception if failed channel.closeFuture().syncUninterruptibly(); if (!state.equals(FetcherState.FETCH_META_FINISHED)) { endFetch(FetcherState.FETCH_FAILED); } else { state = FetcherState.FETCH_DATA_FETCHING; fileLen = fileNum = 0; for (FileChunkMeta eachMeta : chunkMetas) { Path outputPath = StorageUtil.concatPath(queryBaseDir, eachMeta.getTaskId(), "output"); if (!localDirAllocator.ifExists(outputPath.toString(), conf)) { LOG.warn("Range shuffle - file not exist. " + outputPath); continue; } Path path = localFileSystem .makeQualified(localDirAllocator.getLocalPathToRead(outputPath.toString(), conf)); File file = new File(URI.create(path.toUri() + "/output")); FileChunk chunk = new FileChunk(file, eachMeta.getStartOffset(), eachMeta.getLength()); chunk.setEbId(tableName); fileChunks.add(chunk); fileLen += chunk.length(); fileNum++; } endFetch(FetcherState.FETCH_DATA_FINISHED); } return fileChunks; } finally { if (future != null && future.channel().isOpen()) { // Close the channel to exit. future.channel().close().awaitUninterruptibly(); } } }
From source file:org.apache.tajo.worker.RemoteFetcher.java
License:Apache License
@Override public List<FileChunk> get() throws IOException { List<FileChunk> fileChunks = new ArrayList<>(); if (state == FetcherState.FETCH_INIT) { ChannelInitializer<Channel> initializer = new HttpClientChannelInitializer(fileChunk.getFile()); bootstrap.handler(initializer);//from ww w .ja v a 2 s . c om } this.startTime = System.currentTimeMillis(); this.state = FetcherState.FETCH_DATA_FETCHING; ChannelFuture future = null; try { future = bootstrap.clone().connect(new InetSocketAddress(host, port)) .addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); // Wait until the connection attempt succeeds or fails. Channel channel = future.awaitUninterruptibly().channel(); if (!future.isSuccess()) { state = TajoProtos.FetcherState.FETCH_FAILED; throw new IOException(future.cause()); } String query = uri.getPath() + (uri.getRawQuery() != null ? "?" + uri.getRawQuery() : ""); // Prepare the HTTP request. HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, query); request.headers().set(HttpHeaders.Names.HOST, host); request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP); if (LOG.isDebugEnabled()) { LOG.debug("Status: " + getState() + ", URI:" + uri); } // Send the HTTP request. channel.writeAndFlush(request); // Wait for the server to close the connection. throw exception if failed channel.closeFuture().syncUninterruptibly(); fileChunk.setLength(fileChunk.getFile().length()); long start = 0; for (Long eachChunkLength : chunkLengths) { if (eachChunkLength == 0) continue; FileChunk chunk = new FileChunk(fileChunk.getFile(), start, eachChunkLength); chunk.setEbId(fileChunk.getEbId()); chunk.setFromRemote(true); fileChunks.add(chunk); start += eachChunkLength; } return fileChunks; } finally { if (future != null && future.channel().isOpen()) { // Close the channel to exit. future.channel().close().awaitUninterruptibly(); } this.finishTime = System.currentTimeMillis(); long elapsedMills = finishTime - startTime; String transferSpeed; if (elapsedMills > 1000) { long bytePerSec = (fileChunk.length() * 1000) / elapsedMills; transferSpeed = FileUtils.byteCountToDisplaySize(bytePerSec); } else { transferSpeed = FileUtils.byteCountToDisplaySize(Math.max(fileChunk.length(), 0)); } LOG.info(String.format("Fetcher :%d ms elapsed. %s/sec, len:%d, state:%s, URL:%s", elapsedMills, transferSpeed, fileChunk.length(), getState(), uri)); } }
From source file:org.asciidoctor.maven.http.AsciidoctorHandler.java
License:Apache License
@Override public void channelRead0(final ChannelHandlerContext ctx, final FullHttpRequest msg) throws Exception { if (msg.getMethod() != HttpMethod.GET) { final DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.METHOD_NOT_ALLOWED, Unpooled.copiedBuffer("<html><body>Only GET method allowed</body></html>", CharsetUtil.UTF_8)); response.headers().set(HttpHeaders.Names.CONTENT_TYPE, HTML_MEDIA_TYPE); send(ctx, response);/* w ww .j av a2 s . c om*/ return; } final File file = deduceFile(msg.getUri()); final HttpResponseStatus status; final ByteBuf body; final String mediaType; if (file.exists()) { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final FileInputStream fileInputStream = new FileInputStream(file); IOUtils.copy(fileInputStream, baos); body = Unpooled.copiedBuffer(baos.toByteArray()); IOUtils.closeQuietly(fileInputStream); mediaType = mediaType(file.getName()); status = HttpResponseStatus.OK; } else { body = Unpooled.copiedBuffer("<body><html>File not found: " + file.getPath() + "<body></html>", CharsetUtil.UTF_8); status = HttpResponseStatus.NOT_FOUND; mediaType = HTML_MEDIA_TYPE; } final DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, body); response.headers().set(HttpHeaders.Names.CONTENT_TYPE, mediaType); send(ctx, response); }
From source file:org.asynchttpclient.Dsl.java
License:Open Source License
public static RequestBuilder get(String url) { return request(HttpMethod.GET.name(), url); }
From source file:org.asynchttpclient.providers.netty.handler.Protocol.java
License:Open Source License
protected boolean handleRedirectAndExit(Request request, NettyResponseFuture<?> future, HttpResponse response, final Channel channel) throws Exception { io.netty.handler.codec.http.HttpResponseStatus status = response.getStatus(); boolean redirectEnabled = request.isRedirectOverrideSet() ? request.isRedirectEnabled() : config.isRedirectEnabled(); boolean isRedirectStatus = status.equals(MOVED_PERMANENTLY) || status.equals(FOUND) || status.equals(SEE_OTHER) || status.equals(TEMPORARY_REDIRECT); if (redirectEnabled && isRedirectStatus) { if (future.incrementAndGetCurrentRedirectCount() >= config.getMaxRedirects()) { throw new MaxRedirectException("Maximum redirect reached: " + config.getMaxRedirects()); } else {// w w w. j av a 2 s . c om // We must allow 401 handling again. future.getAndSetAuth(false); String location = response.headers().get(HttpHeaders.Names.LOCATION); URI uri = AsyncHttpProviderUtils.getRedirectUri(future.getURI(), location); if (!uri.toString().equals(future.getURI().toString())) { final RequestBuilder requestBuilder = new RequestBuilder(future.getRequest()); if (config.isRemoveQueryParamOnRedirect()) { requestBuilder.setQueryParameters(null); } // FIXME why not do that for 301 and 307 too? // FIXME I think condition is wrong if ((status.equals(FOUND) || status.equals(SEE_OTHER)) && !(status.equals(FOUND) && config.isStrict302Handling())) { requestBuilder.setMethod(HttpMethod.GET.name()); } // in case of a redirect from HTTP to HTTPS, future attributes might change final boolean initialConnectionKeepAlive = future.isKeepAlive(); final String initialPoolKey = channels.getPoolKey(future); future.setURI(uri); String newUrl = uri.toString(); if (request.getURI().getScheme().startsWith(WEBSOCKET)) { newUrl = newUrl.replaceFirst(HTTP, WEBSOCKET); } logger.debug("Redirecting to {}", newUrl); if (future.getHttpHeaders().contains(HttpHeaders.Names.SET_COOKIE2)) { for (String cookieStr : future.getHttpHeaders().getAll(HttpHeaders.Names.SET_COOKIE2)) { Cookie c = CookieDecoder.decode(cookieStr, timeConverter); if (c != null) { requestBuilder.addOrReplaceCookie(c); } } } else if (future.getHttpHeaders().contains(HttpHeaders.Names.SET_COOKIE)) { for (String cookieStr : future.getHttpHeaders().getAll(HttpHeaders.Names.SET_COOKIE)) { Cookie c = CookieDecoder.decode(cookieStr, timeConverter); if (c != null) { requestBuilder.addOrReplaceCookie(c); } } } Callback callback = new Callback(future) { public void call() throws Exception { if (!(initialConnectionKeepAlive && channel.isActive() && channels.offerToPool(initialPoolKey, channel))) { channels.finishChannel(channel); } } }; if (HttpHeaders.isTransferEncodingChunked(response)) { // We must make sure there is no bytes left before // executing the next request. // FIXME investigate this Channels.setDefaultAttribute(channel, callback); } else { // FIXME don't understand: this offers the connection to the pool, or even closes it, while the // request has not been sent, right? callback.call(); } Request redirectRequest = requestBuilder.setUrl(newUrl).build(); // FIXME why not reuse the channel is same host? requestSender.sendNextRequest(redirectRequest, future); return true; } } } return false; }
From source file:org.asynchttpclient.providers.netty.request.NettyRequestSender.java
License:Apache License
private final boolean validateWebSocketRequest(Request request, AsyncHandler<?> asyncHandler) { return request.getMethod().equals(HttpMethod.GET.name()) && asyncHandler instanceof WebSocketUpgradeHandler; }
From source file:org.asynchttpclient.providers.netty4.request.NettyRequestSender.java
License:Open Source License
private boolean validateWebSocketRequest(Request request, AsyncHandler<?> asyncHandler) { return request.getMethod().equals(HttpMethod.GET.name()) && asyncHandler instanceof WebSocketUpgradeHandler; }
From source file:org.asynchttpclient.RequestBuilderTest.java
License:Apache License
@Test public void testDefaultMethod() { RequestBuilder requestBuilder = new RequestBuilder(); String defaultMethodName = HttpMethod.GET.name(); assertEquals(requestBuilder.method, defaultMethodName, "Default HTTP method should be " + defaultMethodName); }