Example usage for io.netty.handler.codec.http HttpMethod PUT

List of usage examples for io.netty.handler.codec.http HttpMethod PUT

Introduction

In this page you can find the example usage for io.netty.handler.codec.http HttpMethod PUT.

Prototype

HttpMethod PUT

To view the source code for io.netty.handler.codec.http HttpMethod PUT.

Click Source Link

Document

The PUT method requests that the enclosed entity be stored under the supplied Request-URI.

Usage

From source file:cn.wantedonline.puppy.httpserver.component.HttpRequest.java

License:Apache License

public HttpPostRequestDecoder getHttpPostRequestDecoder() {
    if (!httpPostRequestDecoderInit) {
        HttpMethod method = getMethod();
        if (method.equals(HttpMethod.POST) || method.equals(HttpMethod.PUT)) {
            try {
                httpPostRequestDecoder = new HttpPostRequestDecoder(factory, this, charset4ContentDecoder);
            } catch (HttpPostRequestDecoder.ErrorDataDecoderException e) {
                log.error("request postDataDecode error:{}", this, e);
            } catch (HttpPostRequestDecoder.IncompatibleDataDecoderException e) {
            }//from   www.  j av a  2 s. c  o m
        }
        httpPostRequestDecoderInit = true;
    }
    return httpPostRequestDecoder;
}

From source file:com.barchart.netty.rest.server.RestHandlerBase.java

License:BSD License

@Override
public void handle(final HttpServerRequest request) throws IOException {

    final HttpMethod method = request.getMethod();

    try {//w w  w.  ja va 2 s . c o  m
        if (method == HttpMethod.GET) {
            get(request);
        } else if (method == HttpMethod.POST) {
            post(request);
        } else if (method == HttpMethod.PUT) {
            put(request);
        } else if (method == HttpMethod.DELETE) {
            delete(request);
        } else {
            complete(request.response(), HttpResponseStatus.METHOD_NOT_ALLOWED,
                    method.name() + " not implemented");
        }
    } catch (final Throwable t) {
        complete(request.response(), HttpResponseStatus.INTERNAL_SERVER_ERROR, t.getMessage());
    }

}

From source file:com.barchart.netty.rest.server.TestRestHandler.java

License:BSD License

@Test
public void testPut() throws Exception {

    request.method = HttpMethod.PUT;
    service.handle(request);/*w  ww.  ja v a 2  s . co  m*/

    assertEquals(1, handler.requests);
    assertEquals(0, handler.get);
    assertEquals(1, handler.put);
    assertEquals(0, handler.post);
    assertEquals(0, handler.delete);
    assertEquals(0, handler.exceptions);

}

From source file:com.couchbase.client.core.endpoint.search.SearchHandler.java

License:Apache License

@Override
protected HttpRequest encodeRequest(ChannelHandlerContext ctx, SearchRequest msg) throws Exception {
    HttpMethod httpMethod = HttpMethod.GET;
    if (msg instanceof UpsertSearchIndexRequest) {
        httpMethod = HttpMethod.PUT;
    } else if (msg instanceof RemoveSearchIndexRequest) {
        httpMethod = HttpMethod.DELETE;//  w  w w  . j  a  v  a 2  s  .  c  om
    } else if (msg instanceof SearchQueryRequest) {
        httpMethod = HttpMethod.POST;
    }

    ByteBuf content;
    if (msg instanceof UpsertSearchIndexRequest) {
        content = Unpooled.copiedBuffer(((UpsertSearchIndexRequest) msg).payload(), CharsetUtil.UTF_8);
    } else if (msg instanceof SearchQueryRequest) {
        content = Unpooled.copiedBuffer(((SearchQueryRequest) msg).payload(), CharsetUtil.UTF_8);
    } else {
        content = Unpooled.EMPTY_BUFFER;
    }

    FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, httpMethod, msg.path(), content);
    request.headers().set(HttpHeaders.Names.USER_AGENT, env().userAgent());
    if (msg instanceof UpsertSearchIndexRequest || msg instanceof SearchQueryRequest) {
        request.headers().set(HttpHeaders.Names.ACCEPT, "*/*");
        request.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json");
    }
    request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, content.readableBytes());
    request.headers().set(HttpHeaders.Names.HOST, remoteHttpHost(ctx));

    addHttpBasicAuth(ctx, request, msg.bucket(), msg.password());
    return request;
}

From source file:com.couchbase.client.core.endpoint.view.ViewHandler.java

License:Apache License

@Override
protected HttpRequest encodeRequest(final ChannelHandlerContext ctx, final ViewRequest msg) throws Exception {
    if (msg instanceof KeepAliveRequest) {
        FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.HEAD, "/",
                Unpooled.EMPTY_BUFFER);//  w w  w  .j  av  a  2  s  .  co  m
        request.headers().set(HttpHeaders.Names.USER_AGENT, env().userAgent());
        request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, 0);
        return request;
    }

    StringBuilder path = new StringBuilder();

    HttpMethod method = HttpMethod.GET;
    ByteBuf content = null;
    if (msg instanceof ViewQueryRequest) {
        ViewQueryRequest queryMsg = (ViewQueryRequest) msg;
        path.append("/").append(msg.bucket()).append("/_design/");
        path.append(queryMsg.development() ? "dev_" + queryMsg.design() : queryMsg.design());
        if (queryMsg.spatial()) {
            path.append("/_spatial/");
        } else {
            path.append("/_view/");
        }
        path.append(queryMsg.view());

        int queryLength = queryMsg.query() == null ? 0 : queryMsg.query().length();
        int keysLength = queryMsg.keys() == null ? 0 : queryMsg.keys().length();
        boolean hasQuery = queryLength > 0;
        boolean hasKeys = keysLength > 0;

        if (hasQuery || hasKeys) {
            if (queryLength + keysLength < MAX_GET_LENGTH) {
                //the query is short enough for GET
                //it has query, query+keys or keys only
                if (hasQuery) {
                    path.append("?").append(queryMsg.query());
                    if (hasKeys) {
                        path.append("&keys=").append(encodeKeysGet(queryMsg.keys()));
                    }
                } else {
                    //it surely has keys if not query
                    path.append("?keys=").append(encodeKeysGet(queryMsg.keys()));
                }
            } else {
                //the query is too long for GET, use the keys as JSON body
                if (hasQuery) {
                    path.append("?").append(queryMsg.query());
                }
                String keysContent = encodeKeysPost(queryMsg.keys());

                //switch to POST
                method = HttpMethod.POST;
                //body is "keys" but in JSON
                content = ctx.alloc().buffer(keysContent.length());
                content.writeBytes(keysContent.getBytes(CHARSET));
            }
        }
    } else if (msg instanceof GetDesignDocumentRequest) {
        GetDesignDocumentRequest queryMsg = (GetDesignDocumentRequest) msg;
        path.append("/").append(msg.bucket()).append("/_design/");
        path.append(queryMsg.development() ? "dev_" + queryMsg.name() : queryMsg.name());
    } else if (msg instanceof UpsertDesignDocumentRequest) {
        method = HttpMethod.PUT;
        UpsertDesignDocumentRequest queryMsg = (UpsertDesignDocumentRequest) msg;
        path.append("/").append(msg.bucket()).append("/_design/");
        path.append(queryMsg.development() ? "dev_" + queryMsg.name() : queryMsg.name());
        content = Unpooled.copiedBuffer(queryMsg.body(), CHARSET);
    } else if (msg instanceof RemoveDesignDocumentRequest) {
        method = HttpMethod.DELETE;
        RemoveDesignDocumentRequest queryMsg = (RemoveDesignDocumentRequest) msg;
        path.append("/").append(msg.bucket()).append("/_design/");
        path.append(queryMsg.development() ? "dev_" + queryMsg.name() : queryMsg.name());
    } else {
        throw new IllegalArgumentException("Unknown incoming ViewRequest type " + msg.getClass());
    }

    if (content == null) {
        content = Unpooled.buffer(0);
    }
    FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, path.toString(),
            content);
    request.headers().set(HttpHeaders.Names.USER_AGENT, env().userAgent());
    request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, content.readableBytes());
    request.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json");
    request.headers().set(HttpHeaders.Names.HOST, remoteHttpHost(ctx));
    addHttpBasicAuth(ctx, request, msg.bucket(), msg.password());

    return request;
}

From source file:com.github.jonbonazza.puni.core.mux.DefaultMuxer.java

License:Apache License

public DefaultMuxer() {
    methodMap.put(HttpMethod.CONNECT, new HashMap<>());
    methodMap.put(HttpMethod.DELETE, new HashMap<>());
    methodMap.put(HttpMethod.GET, new HashMap<>());
    methodMap.put(HttpMethod.HEAD, new HashMap<>());
    methodMap.put(HttpMethod.OPTIONS, new HashMap<>());
    methodMap.put(HttpMethod.PATCH, new HashMap<>());
    methodMap.put(HttpMethod.POST, new HashMap<>());
    methodMap.put(HttpMethod.PUT, new HashMap<>());
    methodMap.put(HttpMethod.TRACE, new HashMap<>());
}

From source file:com.google.devtools.build.lib.remote.blobstore.http.HttpUploadHandler.java

License:Open Source License

private HttpRequest buildRequest(UploadCommand msg) {
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PUT,
            constructPath(msg.uri(), msg.hash(), msg.casUpload()));
    request.headers().set(HttpHeaderNames.HOST, constructHost(msg.uri()));
    request.headers().set(HttpHeaderNames.ACCEPT, "*/*");
    request.headers().set(HttpHeaderNames.CONTENT_LENGTH, msg.contentLength());
    request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    return request;
}

From source file:com.google.devtools.build.lib.remote.blobstore.http.HttpUploadHandlerTest.java

License:Open Source License

private void uploadsShouldWork(boolean casUpload, EmbeddedChannel ch, HttpResponseStatus status)
        throws Exception {
    ByteArrayInputStream data = new ByteArrayInputStream(new byte[] { 1, 2, 3, 4, 5 });
    ChannelPromise writePromise = ch.newPromise();
    ch.writeOneOutbound(new UploadCommand(CACHE_URI, casUpload, "abcdef", data, 5), writePromise);

    HttpRequest request = ch.readOutbound();
    assertThat(request.method()).isEqualTo(HttpMethod.PUT);
    assertThat(request.headers().get(HttpHeaders.CONNECTION)).isEqualTo(HttpHeaderValues.KEEP_ALIVE.toString());

    HttpChunkedInput content = ch.readOutbound();
    assertThat(content.readChunk(ByteBufAllocator.DEFAULT).content().readableBytes()).isEqualTo(5);

    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status);
    response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);

    ch.writeInbound(response);/*from ww w  .  j ava2s .c o m*/

    assertThat(writePromise.isDone()).isTrue();
    assertThat(ch.isOpen()).isTrue();
}

From source file:com.google.devtools.build.remote.worker.HttpCacheServerHandler.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) {
    if (!request.decoderResult().isSuccess()) {
        sendError(ctx, request, HttpResponseStatus.BAD_REQUEST);
        return;//from  w  w  w.ja  v  a  2  s.co  m
    }

    if (request.method() == HttpMethod.GET) {
        handleGet(ctx, request);
    } else if (request.method() == HttpMethod.PUT) {
        handlePut(ctx, request);
    } else {
        sendError(ctx, request, HttpResponseStatus.METHOD_NOT_ALLOWED);
    }
}

From source file:com.king.platform.net.http.netty.NettyHttpClient.java

License:Apache License

@Override
public HttpClientRequestWithBody createPut(String uri) {
    if (!started.get()) {
        throw new IllegalStateException("Http client is not started!");
    }//from w  ww.j  a v  a2 s.c om

    return new HttpClientRequestBuilder(this, HttpVersion.HTTP_1_1, HttpMethod.PUT, uri, confMap);
}