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:io.jsync.http.impl.DefaultHttpServerRequest.java

License:Open Source License

@Override
public HttpServerRequest expectMultiPart(boolean expect) {
    if (expect) {
        String contentType = request.headers().get(HttpHeaderNames.CONTENT_TYPE);
        if (contentType != null) {
            HttpMethod method = request.method();
            AsciiString lowerCaseContentType = new AsciiString(contentType.toLowerCase());
            boolean isURLEncoded = lowerCaseContentType
                    .startsWith(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED);
            if ((lowerCaseContentType.startsWith(HttpHeaderValues.MULTIPART_FORM_DATA) || isURLEncoded)
                    && (method.equals(HttpMethod.POST) || method.equals(HttpMethod.PUT)
                            || method.equals(HttpMethod.PATCH))) {
                decoder = new HttpPostRequestDecoder(new DataFactory(), request);
            }/*  w ww  . java2 s .  c  o  m*/
        }
    } else {
        decoder = null;
    }
    return this;
}

From source file:io.liveoak.container.protocols.http.HttpBinaryResourceRequestDecoderTest.java

License:Open Source License

@Test
public void testDecodePut() throws Exception {
    DefaultResourceRequest decoded = decode(HttpMethod.PUT, "/memory/data/file",
            "Some updated text to be saved!");

    assertThat(decoded.requestType()).isEqualTo(RequestType.UPDATE);

    assertThat(decoded.resourcePath().segments()).hasSize(3);
    assertThat(decoded.resourcePath().segments().get(0).name()).isEqualTo("memory");
    assertThat(decoded.resourcePath().segments().get(1).name()).isEqualTo("data");
    assertThat(decoded.resourcePath().segments().get(2).name()).isEqualTo("file");

    assertThat(decoded.state()).isNotNull();
    assertThat(decoded.state()).isInstanceOf(LazyResourceState.class);
    LazyResourceState state = (LazyResourceState) decoded.state();
    assertThat(state.contentAsByteBuf().toString(Charset.defaultCharset()))
            .isEqualTo("Some updated text to be saved!");

}

From source file:io.liveoak.container.protocols.http.HttpResourceRequestDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, DefaultHttpRequest msg, List<Object> out) throws Exception {

    URI uri = new URI(msg.getUri());
    String query = uri.getRawQuery();
    if (query == null) {
        query = "?";
    } else {// ww w .j av  a 2 s  .c o m
        query = "?" + query;
    }

    QueryStringDecoder decoder = new QueryStringDecoder(query);

    String path = uri.getPath();

    int lastDotLoc = path.lastIndexOf('.');

    String extension = null;

    if (lastDotLoc > 0) {
        extension = path.substring(lastDotLoc + 1);
    }

    String acceptHeader = msg.headers().get(HttpHeaders.Names.ACCEPT);
    if (acceptHeader == null) {
        acceptHeader = "application/json";
    }
    MediaTypeMatcher mediaTypeMatcher = new DefaultMediaTypeMatcher(acceptHeader, extension);

    ResourceParams params = DefaultResourceParams.instance(decoder.parameters());

    // for cases when content is preset, and bypasses HttpRequestBodyHandler
    ByteBuf content = null;
    if (msg instanceof DefaultFullHttpRequest) {
        content = ((DefaultFullHttpRequest) msg).content().retain();
    }

    if (msg.getMethod().equals(HttpMethod.POST)) {
        String contentTypeHeader = msg.headers().get(HttpHeaders.Names.CONTENT_TYPE);
        MediaType contentType = new MediaType(contentTypeHeader);
        out.add(new DefaultResourceRequest.Builder(RequestType.CREATE, new ResourcePath(path))
                .resourceParams(params).mediaTypeMatcher(mediaTypeMatcher)
                .requestAttribute(HttpHeaders.Names.AUTHORIZATION,
                        msg.headers().get(HttpHeaders.Names.AUTHORIZATION))
                .requestAttribute(HttpHeaders.Names.CONTENT_TYPE, contentType)
                .requestAttribute(HTTP_REQUEST, msg)
                .resourceState(new DefaultLazyResourceState(codecManager, contentType, content)).build());
    } else if (msg.getMethod().equals(HttpMethod.GET)) {
        out.add(new DefaultResourceRequest.Builder(RequestType.READ, new ResourcePath(path))
                .resourceParams(params).mediaTypeMatcher(mediaTypeMatcher)
                .requestAttribute(HttpHeaders.Names.AUTHORIZATION,
                        msg.headers().get(HttpHeaders.Names.AUTHORIZATION))
                .requestAttribute(HttpHeaders.Names.ACCEPT, new MediaType(acceptHeader))
                .requestAttribute(HTTP_REQUEST, msg).pagination(decodePagination(params))
                .returnFields(decodeReturnFields(params)).sorting(decodeSorting(params)).build());
    } else if (msg.getMethod().equals(HttpMethod.PUT)) {
        String contentTypeHeader = msg.headers().get(HttpHeaders.Names.CONTENT_TYPE);
        MediaType contentType = new MediaType(contentTypeHeader);
        out.add(new DefaultResourceRequest.Builder(RequestType.UPDATE, new ResourcePath(path))
                .resourceParams(params).mediaTypeMatcher(mediaTypeMatcher)
                .requestAttribute(HttpHeaders.Names.AUTHORIZATION,
                        msg.headers().get(HttpHeaders.Names.AUTHORIZATION))
                .requestAttribute(HttpHeaders.Names.CONTENT_TYPE, contentType)
                .requestAttribute(HTTP_REQUEST, msg)
                .resourceState(new DefaultLazyResourceState(codecManager, contentType, content)).build());
    } else if (msg.getMethod().equals(HttpMethod.DELETE)) {
        out.add(new DefaultResourceRequest.Builder(RequestType.DELETE, new ResourcePath(path))
                .resourceParams(params).mediaTypeMatcher(mediaTypeMatcher)
                .requestAttribute(HttpHeaders.Names.AUTHORIZATION,
                        msg.headers().get(HttpHeaders.Names.AUTHORIZATION))
                .requestAttribute(HttpHeaders.Names.ACCEPT, new MediaType(acceptHeader))
                .requestAttribute(HTTP_REQUEST, msg).build());
    }
}

From source file:io.liveoak.container.protocols.http.HttpResourceRequestDecoderTest.java

License:Open Source License

@Test
public void testDecodePut() throws Exception {
    DefaultResourceRequest decoded = decode(HttpMethod.PUT, "/memory/people/bob",
            "{ name: 'bob', int: 1024, maxInt: 2147483647, minInt: -2147483648, longNeg: -2147483659, longPos: 2147483648  }");

    assertThat(decoded.requestType()).isEqualTo(RequestType.UPDATE);

    assertThat(decoded.resourcePath().segments()).hasSize(3);
    assertThat(decoded.resourcePath().segments().get(0).name()).isEqualTo("memory");
    assertThat(decoded.resourcePath().segments().get(1).name()).isEqualTo("people");
    assertThat(decoded.resourcePath().segments().get(2).name()).isEqualTo("bob");

    //assertThat(decoded.mediaType()).isEqualTo(MediaType.JSON);

    assertThat(decoded.requestContext().pagination()).isNotNull();
    assertThat(decoded.requestContext().pagination()).isEqualTo(Pagination.NONE);

    assertThat(decoded.state()).isNotNull();
    assertThat(decoded.state()).isInstanceOf(ResourceState.class);

    ResourceState state = decoded.state();

    assertThat(state.getProperty("name")).isEqualTo("bob");
    assertThat(state.getProperty("int")).isEqualTo(1024);
    assertThat(state.getProperty("maxInt")).isEqualTo(2147483647);
    assertThat(state.getProperty("minInt")).isEqualTo(-2147483648);
    assertThat(state.getProperty("longNeg")).isEqualTo(-2147483659L);
    assertThat(state.getProperty("longPos")).isEqualTo(2147483648L);
}

From source file:io.reactivex.netty.protocol.http.client.HttpClientRequest.java

License:Apache License

public static HttpClientRequest<ByteBuf> createPut(String uri) {
    return create(HttpMethod.PUT, uri);
}

From source file:io.urmia.api.handler.RestApiHandler.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {

    if (msg instanceof HttpRequest) {

        if (requestState != RequestState.EXPECT_REQUEST) {
            sendError(ctx, ERROR_BAD_REQUEST);
            return;
        }/* w w w  .  j  a v a2 s  .  c  o  m*/

        request = (HttpRequest) msg;

        log.info("received HttpRequest: {} {}", request.getMethod(), request.getUri());

        final ObjectRequest r;

        try {
            r = new ObjectRequest(request);
        } catch (ExceptionInInitializerError e) {
            log.warn("unable to parse the request into a command: {}", request.getUri());
            return;
        }

        objectRequest = r;
        httpRequest = request;

        if (HttpMethod.PUT.equals(request.getMethod())) {
            if (objectRequest.isNotDirectory()) // make dir request
                setupProxyToPUT(ctx, objectRequest);

            requestState = RequestState.EXPECT_CONTENT_OR_LAST;
        } else {
            requestState = RequestState.EXPECT_LAST;
            ctx.read();
        }

        return;
    }

    if (objectRequest == null) {
        String uri = request == null ? "" : request.getUri();
        log.warn("not expecting an empty objectRequest. parse error maybe: {}", uri);
        ByteBuf body = request == null || request.getMethod().equals(HttpMethod.HEAD) ? null
                : errorBody("ResourceNotFoundError", uri + " does not exist");
        sendError(ctx, HttpResponseStatus.NOT_FOUND, body);
        return;
    }

    if (msg instanceof HttpContent) {

        if (requestState != RequestState.EXPECT_LAST && requestState != RequestState.EXPECT_CONTENT_OR_LAST) {
            log.warn("not expecting LAST or CONTENT, requestState: {}", requestState);
            sendError(ctx, HttpResponseStatus.NOT_EXTENDED);
            return;
        }

        final boolean last = msg instanceof LastHttpContent;
        final boolean emptyLast = last && msg == LastHttpContent.EMPTY_LAST_CONTENT;

        if (proxyMode && !emptyLast) // todo: the emptyLast was added for mln
            ctx.fireChannelRead(msg);

        // example of reading only if at the end
        if (last) {

            log.debug("received LastHttpContent: {}", msg);
            requestState = RequestState.EXPECT_REQUEST;

            final HttpRequest request = httpRequest;

            if (HttpMethod.HEAD.equals(request.getMethod())) {
                handleHEAD(ctx, objectRequest);
                return;
            }

            if (HttpMethod.GET.equals(request.getMethod())) {
                handleGET(ctx, objectRequest);
                return;
            }

            if (HttpMethod.DELETE.equals(request.getMethod())) {
                handleDELETE(ctx, objectRequest);
                return;
            }

            if (HttpMethod.PUT.equals(request.getMethod())) {
                if (proxyMode)
                    log.info("finished file upload: {}", objectRequest);
                else
                    handlePUTmkdir(ctx, objectRequest);
                return;
            }

            log.warn("unknown request: {}", request);
            sendError(ctx, HttpResponseStatus.BAD_REQUEST);
        }

        return;
    }

    log.warn("unexpected msg type: {}", msg);
    sendError(ctx, HttpResponseStatus.BAD_REQUEST);
}

From source file:io.urmia.st.StorageServerHandler.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {

    if (msg instanceof HttpRequest) {

        requestStartMS = System.currentTimeMillis();

        request = (HttpRequest) msg;//from   w ww  . ja  va 2 s . com

        log.info("received HttpRequest: {} {}", request.getMethod(), request.getUri());

        // todo: what's this?
        //if (is100ContinueExpected(request)) {
        //    send100Continue(ctx);
        //}

        if (HttpMethod.PUT.equals(request.getMethod())) {
            // start doing the fs put. next msg is not a LastHttpContent
            handlePUT(ctx, request);
            //return;
        }

        ctx.read();
        return;
    }

    if (msg instanceof HttpContent) {

        // New chunk is received
        HttpContent chunk = (HttpContent) msg;

        //log.info("chunk {} of size: {}", chunk, chunk.content().readableBytes());

        if (fileChannel != null) {
            writeToFile(chunk.content());
        }

        // example of reading only if at the end
        if (chunk instanceof LastHttpContent) {

            log.trace("received LastHttpContent: {}", chunk);

            if (HttpMethod.HEAD.equals(request.getMethod())) {
                handleHEAD(ctx, request);
                ctx.read();
                return;
            }

            if (HttpMethod.GET.equals(request.getMethod())) {
                handleGET(ctx, request);
                ctx.read();
                return;
            }

            if (HttpMethod.DELETE.equals(request.getMethod())) {
                handleDELETE(ctx, request);
                ctx.read();
                return;
            }

            if (HttpMethod.PUT.equals(request.getMethod())) {
                // TODO: reset() if exception catch or timeout (i.e. no LastHttpContent)
                writeResponse(ctx, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK),
                        true); // close the connection after upload (mput) done
                reset();
                access.success(ctx, "PUT", uri, requestStartMS);
                ctx.read();
                return;
            }

            log.warn("unknown request: {}", request);
            sendError(ctx, HttpResponseStatus.BAD_REQUEST);
        }

        ctx.read();
        return;
    }

    log.warn("unknown msg type: {}", msg);
}

From source file:io.vertx.core.http.impl.HttpServerRequestImpl.java

License:Open Source License

@Override
public HttpServerRequest setExpectMultipart(boolean expect) {
    synchronized (conn) {
        checkEnded();/*from   w w w .  j  av  a  2 s .c  o  m*/
        if (expect) {
            if (decoder == null) {
                String contentType = request.headers().get(HttpHeaders.Names.CONTENT_TYPE);
                if (contentType != null) {
                    HttpMethod method = request.getMethod();
                    String lowerCaseContentType = contentType.toLowerCase();
                    boolean isURLEncoded = lowerCaseContentType
                            .startsWith(HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED);
                    if ((lowerCaseContentType.startsWith(HttpHeaders.Values.MULTIPART_FORM_DATA)
                            || isURLEncoded)
                            && (method.equals(HttpMethod.POST) || method.equals(HttpMethod.PUT)
                                    || method.equals(HttpMethod.PATCH) || method.equals(HttpMethod.DELETE))) {
                        decoder = new HttpPostRequestDecoder(
                                new NettyFileUploadDataFactory(conn.vertx(), this, () -> uploadHandler),
                                request);
                    }
                }
            }
        } else {
            decoder = null;
        }
        return this;
    }
}

From source file:io.vertx.core.http.impl.HttpUtils.java

License:Open Source License

static HttpMethod toNettyHttpMethod(io.vertx.core.http.HttpMethod method, String rawMethod) {
    switch (method) {
    case CONNECT: {
        return HttpMethod.CONNECT;
    }//from www .j a  va  2s.c om
    case GET: {
        return HttpMethod.GET;
    }
    case PUT: {
        return HttpMethod.PUT;
    }
    case POST: {
        return HttpMethod.POST;
    }
    case DELETE: {
        return HttpMethod.DELETE;
    }
    case HEAD: {
        return HttpMethod.HEAD;
    }
    case OPTIONS: {
        return HttpMethod.OPTIONS;
    }
    case TRACE: {
        return HttpMethod.TRACE;
    }
    case PATCH: {
        return HttpMethod.PATCH;
    }
    default: {
        return HttpMethod.valueOf(rawMethod);
    }
    }
}

From source file:net.anyflow.menton.http.HttpClient.java

License:Apache License

@Override
public HttpResponse put(final MessageReceiver receiver) {
    httpRequest().setMethod(HttpMethod.PUT);

    return request(receiver);
}