Search in sources :

Example 1 with DecodingException

use of cn.taketoday.core.codec.DecodingException in project today-infrastructure by TAKETODAY.

the class ProtobufDecoder method decode.

@Override
public Message decode(DataBuffer dataBuffer, ResolvableType targetType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) throws DecodingException {
    try {
        Message.Builder builder = getMessageBuilder(targetType.toClass());
        ByteBuffer buffer = dataBuffer.asByteBuffer();
        builder.mergeFrom(CodedInputStream.newInstance(buffer), this.extensionRegistry);
        return builder.build();
    } catch (IOException ex) {
        throw new DecodingException("I/O error while parsing input stream", ex);
    } catch (Exception ex) {
        throw new DecodingException("Could not read Protobuf message: " + ex.getMessage(), ex);
    } finally {
        DataBufferUtils.release(dataBuffer);
    }
}
Also used : Message(com.google.protobuf.Message) DecodingException(cn.taketoday.core.codec.DecodingException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) DataBufferLimitException(cn.taketoday.core.io.buffer.DataBufferLimitException) DecodingException(cn.taketoday.core.codec.DecodingException)

Example 2 with DecodingException

use of cn.taketoday.core.codec.DecodingException in project today-infrastructure by TAKETODAY.

the class ProtobufHttpMessageWriter method write.

@Override
public Mono<Void> write(Publisher<? extends Message> inputStream, ResolvableType elementType, @Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) {
    try {
        HttpHeaders headers = message.getHeaders();
        Message.Builder builder = getMessageBuilder(elementType.toClass());
        Descriptors.Descriptor descriptor = builder.getDescriptorForType();
        headers.add(X_PROTOBUF_SCHEMA_HEADER, descriptor.getFile().getName());
        headers.add(X_PROTOBUF_MESSAGE_HEADER, descriptor.getFullName());
        if (inputStream instanceof Flux) {
            if (mediaType == null) {
                headers.setContentType(((HttpMessageEncoder<?>) getEncoder()).getStreamingMediaTypes().get(0));
            } else if (!ProtobufEncoder.DELIMITED_VALUE.equals(mediaType.getParameters().get(ProtobufEncoder.DELIMITED_KEY))) {
                Map<String, String> parameters = new HashMap<>(mediaType.getParameters());
                parameters.put(ProtobufEncoder.DELIMITED_KEY, ProtobufEncoder.DELIMITED_VALUE);
                headers.setContentType(new MediaType(mediaType.getType(), mediaType.getSubtype(), parameters));
            }
        }
        return super.write(inputStream, elementType, mediaType, message, hints);
    } catch (Exception ex) {
        return Mono.error(new DecodingException("Could not read Protobuf message: " + ex.getMessage(), ex));
    }
}
Also used : HttpHeaders(cn.taketoday.http.HttpHeaders) ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) Message(com.google.protobuf.Message) Flux(reactor.core.publisher.Flux) HttpMessageEncoder(cn.taketoday.http.codec.HttpMessageEncoder) MediaType(cn.taketoday.http.MediaType) DecodingException(cn.taketoday.core.codec.DecodingException) Descriptors(com.google.protobuf.Descriptors) ConcurrentReferenceHashMap(cn.taketoday.util.ConcurrentReferenceHashMap) HashMap(java.util.HashMap) Map(java.util.Map) DecodingException(cn.taketoday.core.codec.DecodingException)

Example 3 with DecodingException

use of cn.taketoday.core.codec.DecodingException in project today-infrastructure by TAKETODAY.

the class DefaultPartHttpMessageReader method read.

@Override
public Flux<Part> read(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) {
    return Flux.defer(() -> {
        byte[] boundary = boundary(message);
        if (boundary == null) {
            return Flux.error(new DecodingException("No multipart boundary found in Content-Type: \"" + message.getHeaders().getContentType() + "\""));
        }
        Flux<MultipartParser.Token> tokens = MultipartParser.parse(message.getBody(), boundary, this.maxHeadersSize, this.headersCharset);
        return PartGenerator.createParts(tokens, this.maxParts, this.maxInMemorySize, this.maxDiskUsagePerPart, this.streaming, this.fileStorage.directory(), this.blockingOperationScheduler);
    });
}
Also used : DecodingException(cn.taketoday.core.codec.DecodingException)

Example 4 with DecodingException

use of cn.taketoday.core.codec.DecodingException in project today-framework by TAKETODAY.

the class ProtobufHttpMessageWriter method write.

@Override
public Mono<Void> write(Publisher<? extends Message> inputStream, ResolvableType elementType, @Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) {
    try {
        HttpHeaders headers = message.getHeaders();
        Message.Builder builder = getMessageBuilder(elementType.toClass());
        Descriptors.Descriptor descriptor = builder.getDescriptorForType();
        headers.add(X_PROTOBUF_SCHEMA_HEADER, descriptor.getFile().getName());
        headers.add(X_PROTOBUF_MESSAGE_HEADER, descriptor.getFullName());
        if (inputStream instanceof Flux) {
            if (mediaType == null) {
                headers.setContentType(((HttpMessageEncoder<?>) getEncoder()).getStreamingMediaTypes().get(0));
            } else if (!ProtobufEncoder.DELIMITED_VALUE.equals(mediaType.getParameters().get(ProtobufEncoder.DELIMITED_KEY))) {
                Map<String, String> parameters = new HashMap<>(mediaType.getParameters());
                parameters.put(ProtobufEncoder.DELIMITED_KEY, ProtobufEncoder.DELIMITED_VALUE);
                headers.setContentType(new MediaType(mediaType.getType(), mediaType.getSubtype(), parameters));
            }
        }
        return super.write(inputStream, elementType, mediaType, message, hints);
    } catch (Exception ex) {
        return Mono.error(new DecodingException("Could not read Protobuf message: " + ex.getMessage(), ex));
    }
}
Also used : HttpHeaders(cn.taketoday.http.HttpHeaders) ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) Message(com.google.protobuf.Message) Flux(reactor.core.publisher.Flux) HttpMessageEncoder(cn.taketoday.http.codec.HttpMessageEncoder) MediaType(cn.taketoday.http.MediaType) DecodingException(cn.taketoday.core.codec.DecodingException) Descriptors(com.google.protobuf.Descriptors) ConcurrentReferenceHashMap(cn.taketoday.util.ConcurrentReferenceHashMap) HashMap(java.util.HashMap) Map(java.util.Map) DecodingException(cn.taketoday.core.codec.DecodingException)

Example 5 with DecodingException

use of cn.taketoday.core.codec.DecodingException in project today-framework by TAKETODAY.

the class DefaultPartHttpMessageReader method read.

@Override
public Flux<Part> read(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) {
    return Flux.defer(() -> {
        byte[] boundary = MultipartUtils.boundary(message, this.headersCharset);
        if (boundary == null) {
            return Flux.error(new DecodingException("No multipart boundary found in Content-Type: \"" + message.getHeaders().getContentType() + "\""));
        }
        Flux<MultipartParser.Token> allPartsTokens = MultipartParser.parse(message.getBody(), boundary, maxHeadersSize, headersCharset);
        AtomicInteger partCount = new AtomicInteger();
        return allPartsTokens.windowUntil(MultipartParser.Token::isLast).concatMap(partsTokens -> {
            if (tooManyParts(partCount)) {
                return Mono.error(new DecodingException("Too many parts (" + partCount.get() + "/" + this.maxParts + " allowed)"));
            } else {
                return PartGenerator.createPart(partsTokens, this.maxInMemorySize, this.maxDiskUsagePerPart, this.streaming, this.fileStorage.directory(), this.blockingOperationScheduler);
            }
        });
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DecodingException(cn.taketoday.core.codec.DecodingException)

Aggregations

DecodingException (cn.taketoday.core.codec.DecodingException)6 Message (com.google.protobuf.Message)4 DataBufferLimitException (cn.taketoday.core.io.buffer.DataBufferLimitException)2 HttpHeaders (cn.taketoday.http.HttpHeaders)2 MediaType (cn.taketoday.http.MediaType)2 ReactiveHttpOutputMessage (cn.taketoday.http.ReactiveHttpOutputMessage)2 HttpMessageEncoder (cn.taketoday.http.codec.HttpMessageEncoder)2 ConcurrentReferenceHashMap (cn.taketoday.util.ConcurrentReferenceHashMap)2 Descriptors (com.google.protobuf.Descriptors)2 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Flux (reactor.core.publisher.Flux)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1