Search in sources :

Example 6 with Request

use of ratpack.http.Request in project ratpack by ratpack.

the class DefaultBlockingExecTimingInterceptor method intercept.

@Override
public void intercept(Execution execution, ExecType type, Block executionSegment) throws Exception {
    if (type == ExecType.BLOCKING) {
        Optional<Request> requestOpt = execution.maybeGet(Request.class);
        if (requestOpt.isPresent()) {
            Request request = requestOpt.get();
            String tag = buildBlockingTimerTag(request.getPath(), request.getMethod().getName());
            Timer.Context timer = metricRegistry.timer(tag).time();
            try {
                executionSegment.execute();
            } finally {
                timer.stop();
            }
            return;
        }
    }
    executionSegment.execute();
}
Also used : Timer(com.codahale.metrics.Timer) Request(ratpack.http.Request)

Example 7 with Request

use of ratpack.http.Request in project ratpack by ratpack.

the class FormDecoder method parseForm.

@SuppressWarnings("deprecation")
public static Form parseForm(Context context, TypedData body, MultiValueMap<String, String> base) throws RuntimeException {
    Request request = context.getRequest();
    HttpMethod method = io.netty.handler.codec.http.HttpMethod.valueOf(request.getMethod().getName());
    HttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, method, request.getUri());
    nettyRequest.headers().add(HttpHeaderNames.CONTENT_TYPE, body.getContentType().toString());
    HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(nettyRequest);
    HttpContent content = new DefaultHttpContent(body.getBuffer());
    decoder.offer(content);
    decoder.offer(LastHttpContent.EMPTY_LAST_CONTENT);
    Map<String, List<String>> attributes = new LinkedHashMap<>(base.getAll());
    Map<String, List<UploadedFile>> files = new LinkedHashMap<>();
    try {
        InterfaceHttpData data = decoder.next();
        while (data != null) {
            if (data.getHttpDataType().equals(InterfaceHttpData.HttpDataType.Attribute)) {
                List<String> values = attributes.get(data.getName());
                if (values == null) {
                    values = new ArrayList<>(1);
                    attributes.put(data.getName(), values);
                }
                try {
                    values.add(((Attribute) data).getValue());
                } catch (IOException e) {
                    throw uncheck(e);
                } finally {
                    data.release();
                }
            } else if (data.getHttpDataType().equals(InterfaceHttpData.HttpDataType.FileUpload)) {
                List<UploadedFile> values = files.get(data.getName());
                if (values == null) {
                    values = new ArrayList<>(1);
                    files.put(data.getName(), values);
                }
                try {
                    FileUpload nettyFileUpload = (FileUpload) data;
                    final ByteBuf byteBuf = nettyFileUpload.getByteBuf();
                    byteBuf.retain();
                    context.onClose(ro -> byteBuf.release());
                    MediaType contentType;
                    String rawContentType = nettyFileUpload.getContentType();
                    if (rawContentType == null) {
                        contentType = null;
                    } else {
                        Charset charset = nettyFileUpload.getCharset();
                        if (charset == null) {
                            contentType = DefaultMediaType.get(rawContentType);
                        } else {
                            contentType = DefaultMediaType.get(rawContentType + ";charset=" + charset);
                        }
                    }
                    UploadedFile fileUpload = new DefaultUploadedFile(new ByteBufBackedTypedData(byteBuf, contentType), nettyFileUpload.getFilename());
                    values.add(fileUpload);
                } catch (IOException e) {
                    throw uncheck(e);
                } finally {
                    data.release();
                }
            }
            data = decoder.next();
        }
    } catch (HttpPostRequestDecoder.EndOfDataDecoderException ignore) {
    // ignore
    } finally {
        decoder.destroy();
    }
    return new DefaultForm(new ImmutableDelegatingMultiValueMap<>(attributes), new ImmutableDelegatingMultiValueMap<>(files));
}
Also used : Context(ratpack.handling.Context) FileUpload(io.netty.handler.codec.http.multipart.FileUpload) MultiValueMap(ratpack.util.MultiValueMap) Form(ratpack.form.Form) ImmutableDelegatingMultiValueMap(ratpack.util.internal.ImmutableDelegatingMultiValueMap) Exceptions.uncheck(ratpack.util.Exceptions.uncheck) DefaultMediaType(ratpack.http.internal.DefaultMediaType) IOException(java.io.IOException) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) UploadedFile(ratpack.form.UploadedFile) ArrayList(java.util.ArrayList) TypedData(ratpack.http.TypedData) LinkedHashMap(java.util.LinkedHashMap) Attribute(io.netty.handler.codec.http.multipart.Attribute) io.netty.handler.codec.http(io.netty.handler.codec.http) Request(ratpack.http.Request) List(java.util.List) ByteBuf(io.netty.buffer.ByteBuf) Charset(java.nio.charset.Charset) MediaType(ratpack.http.MediaType) ByteBufBackedTypedData(ratpack.http.internal.ByteBufBackedTypedData) Map(java.util.Map) HttpPostRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) LinkedHashMap(java.util.LinkedHashMap) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) DefaultMediaType(ratpack.http.internal.DefaultMediaType) MediaType(ratpack.http.MediaType) ArrayList(java.util.ArrayList) List(java.util.List) ByteBufBackedTypedData(ratpack.http.internal.ByteBufBackedTypedData) FileUpload(io.netty.handler.codec.http.multipart.FileUpload) Request(ratpack.http.Request) Charset(java.nio.charset.Charset) IOException(java.io.IOException) HttpPostRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder) UploadedFile(ratpack.form.UploadedFile)

Example 8 with Request

use of ratpack.http.Request in project ratpack by ratpack.

the class DefaultDevelopmentErrorHandler method error.

/**
   * Prints the string "Client error «statusCode»" to the response as text with the given status code.
   *
   * @param ctx The ctx
   * @param statusCode The 4xx status code that explains the problem
   */
@Override
public void error(Context ctx, int statusCode) throws Exception {
    HttpResponseStatus status = HttpResponseStatus.valueOf(statusCode);
    Request request = ctx.getRequest();
    LOGGER.error(statusCode + " client error for request to " + request.getRawUri());
    ctx.getResponse().status(statusCode);
    ctx.byContent(s -> s.plainText(() -> ctx.render("Client error " + statusCode)).html(() -> new ErrorPageRenderer() {

        protected void render() {
            render(ctx, status.reasonPhrase(), w -> messages(w, "Client Error", () -> meta(w, m -> m.put("URI:", request.getRawUri()).put("Method:", request.getMethod().getName()).put("Status Code:", status.code()).put("Phrase:", status.reasonPhrase()))));
        }
    }).noMatch("text/plain"));
}
Also used : Request(ratpack.http.Request) Logger(org.slf4j.Logger) Context(ratpack.handling.Context) LoggerFactory(org.slf4j.LoggerFactory) Throwables(com.google.common.base.Throwables) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Request(ratpack.http.Request)

Example 9 with Request

use of ratpack.http.Request in project ratpack by ratpack.

the class ClientSideSessionStore method getCookieStorage.

private CookieStorage getCookieStorage() {
    Request request = this.request.get();
    Optional<CookieStorage> cookieStorageOpt = request.maybeGet(CookieStorage.class);
    CookieStorage cookieStorage;
    if (cookieStorageOpt.isPresent()) {
        cookieStorage = cookieStorageOpt.get();
    } else {
        cookieStorage = new CookieStorage(readCookies(latCookieOrdering, request), readCookies(dataCookieOrdering, request));
        request.add(CookieStorage.class, cookieStorage);
    }
    return cookieStorage;
}
Also used : Request(ratpack.http.Request)

Aggregations

Request (ratpack.http.Request)9 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 Context (ratpack.handling.Context)2 Timer (com.codahale.metrics.Timer)1 Throwables (com.google.common.base.Throwables)1 HostAndPort (com.google.common.net.HostAndPort)1 ByteBuf (io.netty.buffer.ByteBuf)1 Channel (io.netty.channel.Channel)1 io.netty.handler.codec.http (io.netty.handler.codec.http)1 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 HttpMethod (io.netty.handler.codec.http.HttpMethod)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 Attribute (io.netty.handler.codec.http.multipart.Attribute)1 FileUpload (io.netty.handler.codec.http.multipart.FileUpload)1 HttpPostRequestDecoder (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)1 InterfaceHttpData (io.netty.handler.codec.http.multipart.InterfaceHttpData)1 IOException (java.io.IOException)1 Charset (java.nio.charset.Charset)1