Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to core InputStreamByteBody #770

Merged
merged 10 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
micronaut = "4.5.4"
micronaut = "4.6.0"
micronaut-docs = "2.0.0"
micronaut-test = "4.0.1"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ import io.micronaut.context.annotation.Requires
import io.micronaut.core.async.annotation.SingleResult
import io.micronaut.http.HttpStatus
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Consumes
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import io.micronaut.http.client.exceptions.HttpClientResponseException
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import org.reactivestreams.Publisher
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import spock.lang.PendingFeature
import spock.lang.Specification

@MicronautTest
Expand All @@ -32,7 +30,6 @@ class JettyNotFoundSpec extends Specification {
Flux.from(client.streaming('notthere')).collectList().block() == []
}

@PendingFeature(reason = "https://github.com/micronaut-projects/micronaut-core/pull/9307")
void "test 404 handling with not streaming publisher"() {
when:
def exists = Mono.from(client.mono('1234')).block()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.servlet.http;

import io.micronaut.core.annotation.Internal;
import io.micronaut.core.io.buffer.ByteBuffer;
import io.micronaut.core.io.buffer.ByteBufferFactory;

/**
* {@link ByteBufferFactory} implementation based on simple byte arrays.
*
* @since 4.10.0
* @author Jonas Konrad
*/
@Internal
public class ByteArrayBufferFactory implements ByteBufferFactory<Void, byte[]> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be I core?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe eventually but not needed yet

public static final ByteArrayBufferFactory INSTANCE = new ByteArrayBufferFactory();

private ByteArrayBufferFactory() {
}

@Override
public Void getNativeAllocator() {
throw new UnsupportedOperationException("No native allocator");
}

@Override
public ByteBuffer<byte[]> buffer() {
return buffer(0);
}

@Override
public ByteBuffer<byte[]> buffer(int initialCapacity) {
return new ByteArrayByteBuffer<>(new byte[initialCapacity]);
}

@Override
public ByteBuffer<byte[]> buffer(int initialCapacity, int maxCapacity) {
return buffer(initialCapacity);
}

@Override
public ByteBuffer<byte[]> copiedBuffer(byte[] bytes) {
return wrap(bytes.clone());
}

@Override
public ByteBuffer<byte[]> copiedBuffer(java.nio.ByteBuffer nioBuffer) {
int pos = nioBuffer.position();
int lim = nioBuffer.limit();
byte[] arr = new byte[lim - pos];
nioBuffer.get(pos, arr, 0, arr.length);
return wrap(arr);
}

@Override
public ByteBuffer<byte[]> wrap(byte[] existing) {
return new ByteArrayByteBuffer<>(existing);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import io.micronaut.http.MutableHttpResponse;
import io.micronaut.http.annotation.Header;
import io.micronaut.http.annotation.Produces;
import io.micronaut.http.body.DynamicMessageBodyWriter;
import io.micronaut.http.body.MessageBodyHandlerRegistry;
import io.micronaut.http.body.MessageBodyWriter;
import io.micronaut.http.codec.CodecException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
*
* @author Jonas Konrad
* @since 4.9.0
* @deprecated Use {@link io.micronaut.http.body.stream.AvailableByteArrayBody} from core instead
*/
@Internal
@Deprecated(forRemoval = true)
yawkat marked this conversation as resolved.
Show resolved Hide resolved
public final class AvailableByteArrayBody extends AbstractServletByteBody implements CloseableAvailableByteBody {
private byte[] array;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
*
* @since 4.9.0
* @author Jonas Konrad
* @deprecated Use {@link io.micronaut.http.body.stream.InputStreamByteBody} from core instead
*/
@Internal
yawkat marked this conversation as resolved.
Show resolved Hide resolved
@Deprecated(forRemoval = true)
public final class InputStreamByteBody extends AbstractServletByteBody {
private final Context context;
private ExtendedInputStream stream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@
import io.micronaut.http.ServerHttpRequest;
import io.micronaut.http.body.ByteBody;
import io.micronaut.http.body.CloseableByteBody;
import io.micronaut.http.body.stream.InputStreamByteBody;
import io.micronaut.http.codec.MediaTypeCodecRegistry;
import io.micronaut.http.cookie.Cookies;
import io.micronaut.servlet.http.BodyBuilder;
import io.micronaut.servlet.http.ByteArrayBufferFactory;
import io.micronaut.servlet.http.ParsedBodyHolder;
import io.micronaut.servlet.http.ServletExchange;
import io.micronaut.servlet.http.ServletHttpRequest;
import io.micronaut.servlet.http.ServletHttpResponse;
import io.micronaut.servlet.http.StreamedServletMessage;
import io.micronaut.servlet.http.body.InputStreamByteBody;
import jakarta.servlet.AsyncContext;
import jakarta.servlet.ReadListener;
import jakarta.servlet.ServletInputStream;
Expand Down Expand Up @@ -135,7 +136,7 @@ protected DefaultServletHttpRequest(ConversionService conversionService,
this.delegate = delegate;
this.codecRegistry = codecRegistry;
long contentLengthLong = delegate.getContentLengthLong();
this.byteBody = InputStreamByteBody.create(new LazyDelegateInputStream(delegate), contentLengthLong < 0 ? OptionalLong.empty() : OptionalLong.of(contentLengthLong), ioExecutor);
this.byteBody = InputStreamByteBody.create(new LazyDelegateInputStream(delegate), contentLengthLong < 0 ? OptionalLong.empty() : OptionalLong.of(contentLengthLong), ioExecutor, ByteArrayBufferFactory.INSTANCE);

String requestURI = delegate.getRequestURI();

Expand Down
Loading