io.micronaut.http.server.netty.binders.NettyPartUploadAnnotationBinder Maven / Gradle / Ivy
/*
* Copyright 2017-2022 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.http.server.netty.binders;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.bind.annotation.Bindable;
import io.micronaut.core.convert.ArgumentConversionContext;
import io.micronaut.core.convert.ConversionError;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.type.Argument;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.annotation.Part;
import io.micronaut.http.bind.binders.AnnotatedRequestArgumentBinder;
import io.micronaut.http.bind.binders.PendingRequestBindingResult;
import io.micronaut.http.bind.binders.RequestArgumentBinder;
import io.micronaut.http.server.netty.NettyHttpRequest;
import io.micronaut.http.server.netty.converters.NettyConverters;
import reactor.core.publisher.Mono;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
/**
* Bind values annotated with {@link Part}.
*
* @param The part type
* @author Denis Stepanov
* @since 4.0.0
*/
final class NettyPartUploadAnnotationBinder implements AnnotatedRequestArgumentBinder, RequestArgumentBinder {
private final ConversionService conversionService;
private final NettyCompletedFileUploadBinder completedFileUploadBinder;
private final NettyPublisherPartUploadBinder publisherPartUploadBinder;
NettyPartUploadAnnotationBinder(ConversionService conversionService,
NettyCompletedFileUploadBinder completedFileUploadBinder,
NettyPublisherPartUploadBinder publisherPartUploadBinder) {
this.conversionService = conversionService;
this.completedFileUploadBinder = completedFileUploadBinder;
this.publisherPartUploadBinder = publisherPartUploadBinder;
}
@Override
public BindingResult bind(ArgumentConversionContext context, HttpRequest> request) {
if (!(request instanceof NettyHttpRequest> nettyRequest) || !nettyRequest.isFormOrMultipartData()) {
return BindingResult.unsatisfied();
}
if (completedFileUploadBinder.matches(context.getArgument().getType())) {
return completedFileUploadBinder.bind((ArgumentConversionContext) context, request);
}
if (publisherPartUploadBinder.matches(context.getArgument().getType())) {
return publisherPartUploadBinder.bind((ArgumentConversionContext) context, request);
}
Argument argument = context.getArgument();
String inputName = argument.getAnnotationMetadata().stringValue(Bindable.NAME).orElse(argument.getName());
return bindPart(conversionService, context, nettyRequest, inputName, false);
}
@NonNull
static BindingResult bindPart(ConversionService conversionService, ArgumentConversionContext context, NettyHttpRequest> nettyRequest, String inputName, boolean skipClaimed) {
if (skipClaimed && nettyRequest.formRouteCompleter().isClaimed(inputName)) {
return BindingResult.unsatisfied();
}
CompletableFuture> completableFuture = Mono.from(nettyRequest.formRouteCompleter().claimFieldsComplete(inputName))
.map(d -> NettyConverters.refCountAwareConvert(conversionService, d, context))
.toFuture();
return new PendingRequestBindingResult<>() {
@Override
public boolean isPending() {
return !completableFuture.isDone();
}
@Override
public List getConversionErrors() {
return context.getLastError().map(List::of).orElseGet(List::of);
}
@Override
public Optional getValue() {
Optional res = completableFuture.getNow(Optional.empty());
//noinspection OptionalAssignedToNull
if (res == null) {
// tricky: If the Mono completes without an element, the future will return null here.
res = Optional.empty();
}
return res;
}
};
}
@Override
public Class getAnnotationType() {
return Part.class;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy