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

Reactive Media Common deprecated cleanup #6098

Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2022 Oracle and/or its affiliates.
* Copyright (c) 2017, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -187,8 +187,7 @@ Flow.Publisher<DataChunk> publish(List<Comment> comments) {
String str = comments.stream()
.map(Comment::toString)
.collect(Collectors.joining("\n"));
return ContentWriters.charSequenceWriter(StandardCharsets.UTF_8)
.apply(str + "\n");
return ContentWriters.writeCharSequence(str + "\n", StandardCharsets.UTF_8);
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2022 Oracle and/or its affiliates.
* Copyright (c) 2017, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -81,13 +81,7 @@ public static Single<String> readURLEncodedString(Publisher<DataChunk> chunks,
* Implementation of {@link Mapper} that converts a {@code byte[]} into
* a {@link String} using a given {@link Charset}.
*/
private static final class BytesToString implements Mapper<byte[], String> {

private final Charset charset;

BytesToString(Charset charset) {
this.charset = charset;
}
private record BytesToString(Charset charset) implements Mapper<byte[], String> {

@Override
public String map(byte[] bytes) {
Expand All @@ -98,19 +92,14 @@ public String map(byte[] bytes) {
/**
* Mapper that applies URL decoding to a {@code String}.
*/
private static final class StringToDecodedString implements Mapper<String, String> {

private final Charset charset;

StringToDecodedString(Charset charset) {
this.charset = charset;
}
private record StringToDecodedString(Charset charset) implements Mapper<String, String> {

@Override
public String map(String s) {
return URLDecoder.decode(s, charset);
}
}

/**
* Implementation of {@link Collector} that collects chunks into a single
* {@code byte[]}.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2022 Oracle and/or its affiliates.
* Copyright (c) 2017, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,15 +19,9 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
import java.util.Objects;
import java.util.concurrent.Flow.Publisher;
import java.util.function.Function;

import io.helidon.common.http.DataChunk;
import io.helidon.common.reactive.IoMulti;
import io.helidon.common.reactive.RetrySchema;
import io.helidon.common.reactive.Single;

/**
Expand Down Expand Up @@ -115,88 +109,4 @@ public static Single<DataChunk> writeStackTrace(Throwable throwable, Charset cha
}
return returnValue;
}

/**
* Returns a writer function for {@code byte[]}.
* <p>
* The {@code copy} variant is by default registered in
* {@code ServerResponse}.
*
* @param copy a signal if byte array should be copied - set it {@code true}
* if {@code byte[]} will be immediately reused.
* @return a {@code byte[]} writer
*
* @deprecated since 2.0.0, use {@link #writeBytes(byte[], boolean)} instead
*/
@Deprecated(since = "2.0.0")
public static Function<byte[], Publisher<DataChunk>> byteArrayWriter(boolean copy) {
return (bytes) -> writeBytes(bytes, copy);
}

/**
* Returns a writer function for {@link CharSequence} using provided
* standard {@code charset}.
* <p>
* An instance is by default registered in {@code ServerResponse} for all
* standard charsets.
*
* @param charset a standard charset to use
* @return a {@link String} writer
* @throws NullPointerException if parameter {@code charset} is {@code null}
* @deprecated since 2.0.0, use {@link #writeCharSequence(CharSequence, Charset)}
* or {@link DefaultMediaSupport#charSequenceWriter()} instead
*/
@Deprecated(since = "2.0.0")
public static Function<CharSequence, Publisher<DataChunk>> charSequenceWriter(Charset charset) {
return (cs) -> writeCharSequence(cs, charset);
}

/**
* Returns a writer function for {@link CharBuffer} using provided standard
* {@code charset}.
* <p>
* An instance is by default registered in {@code ServerResponse} for all
* standard charsets.
*
* @param charset a standard charset to use
* @return a {@link String} writer
* @throws NullPointerException if parameter {@code charset} is {@code null}
* @deprecated since 2.0.0, use {@link #writeCharBuffer(CharBuffer, Charset)} instead
*/
@Deprecated(since = "2.0.0")
public static Function<CharBuffer, Publisher<DataChunk>> charBufferWriter(Charset charset) {
return (buffer) -> writeCharBuffer(buffer, charset);
}

/**
* Returns a writer function for {@link ReadableByteChannel}. Created
* publisher use provided {@link RetrySchema} to define delay between
* unsuccessful read attempts.
*
* @param retrySchema a retry schema to use in case when {@code read}
* operation reads {@code 0 bytes}
* @return a {@link ReadableByteChannel} writer
* @deprecated since 2.0.0, use {@link DefaultMediaSupport#byteChannelWriter(RetrySchema)}} instead
*/
@Deprecated(since = "2.0.0")
public static Function<ReadableByteChannel, Publisher<DataChunk>> byteChannelWriter(RetrySchema retrySchema) {
Objects.requireNonNull(retrySchema);

return channel -> IoMulti.multiFromByteChannelBuilder(channel)
.retrySchema(retrySchema)
.build()
.map(DataChunk::create);
}

/**
* Returns a writer function for {@link ReadableByteChannel}.
*
* @return a {@link ReadableByteChannel} writer
* @deprecated since 2.0.0, use {@link DefaultMediaSupport#byteChannelWriter()}} instead
*/
@Deprecated(since = "2.0.0")
public static Function<ReadableByteChannel, Publisher<DataChunk>> byteChannelWriter() {
return channel -> IoMulti.multiFromByteChannel(channel).map(DataChunk::create);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
* Copyright (c) 2020, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,7 @@
import io.helidon.common.http.DataChunk;
import io.helidon.common.mapper.Mapper;
import io.helidon.common.media.type.MediaTypes;
import io.helidon.common.reactive.IoMulti;
import io.helidon.common.reactive.Single;

/**
Expand Down Expand Up @@ -67,13 +68,7 @@ static FileBodyWriter create() {
* Implementation of {@link Mapper} that converts {@link File} to a
* publisher of {@link DataChunk}.
*/
private static final class FileToChunks implements Mapper<File, Publisher<DataChunk>> {

private final MessageBodyWriterContext context;

FileToChunks(MessageBodyWriterContext context) {
this.context = context;
}
private record FileToChunks(MessageBodyWriterContext context) implements Mapper<File, Publisher<DataChunk>> {

@Override
public Publisher<DataChunk> map(File file) {
Expand All @@ -82,9 +77,9 @@ public Publisher<DataChunk> map(File file) {
context.contentType(MediaTypes.APPLICATION_OCTET_STREAM);
context.contentLength(Files.size(path));
FileChannel fc = FileChannel.open(path, StandardOpenOption.READ);
return ContentWriters.byteChannelWriter().apply(fc);
return IoMulti.multiFromByteChannel(fc).map(DataChunk::create);
} catch (IOException ex) {
return Single.<DataChunk>error(ex);
return Single.error(ex);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
* Copyright (c) 2020, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,7 +27,6 @@
/**
* Readable {@link MessageBodyContent}.
*/
@SuppressWarnings("deprecation")
public final class MessageBodyReadableContent
implements MessageBodyReaders, MessageBodyFilters, MessageBodyContent, Multi<DataChunk> {

Expand All @@ -46,16 +45,6 @@ public final class MessageBodyReadableContent
this.context = context;
}

/**
* Copy constructor.
* @param orig original context to be copied
*/
private MessageBodyReadableContent(MessageBodyReadableContent orig) {
Objects.requireNonNull(orig, "orig is null!");
this.publisher = orig.publisher;
this.context = orig.context;
}

/**
* Get the reader context used to unmarshall data.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
* Copyright (c) 2020, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,7 +23,6 @@
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.Flow.Publisher;
import java.util.function.Function;
import java.util.function.Predicate;

import io.helidon.common.GenericType;
Expand Down Expand Up @@ -71,11 +70,7 @@ private MessageBodyWriterContext(MessageBodyWriterContext parent,
super(parent, eventListener);
Objects.requireNonNull(headers, "headers cannot be null!");
this.headers = headers;
if (acceptedTypes != null) {
this.acceptedTypes = acceptedTypes;
} else {
this.acceptedTypes = List.of();
}
this.acceptedTypes = Objects.requireNonNullElseGet(acceptedTypes, List::of);
if (parent != null) {
this.writers = new MessageBodyOperators<>(parent.writers);
this.swriters = new MessageBodyOperators<>(parent.swriters);
Expand All @@ -98,21 +93,6 @@ private MessageBodyWriterContext(WritableHeaders<?> headers) {
this.acceptedTypes = List.of();
}

/**
* Create a new standalone (non parented) context.
*/
private MessageBodyWriterContext() {
super(null, null);
this.headers = WritableHeaders.create();
this.writers = new MessageBodyOperators<>();
this.swriters = new MessageBodyOperators<>();
this.acceptedTypes = List.of();
this.contentTypeCache = Optional.empty();
this.contentTypeCached = true;
this.charsetCache = DEFAULT_CHARSET;
this.charsetCached = true;
}

private MessageBodyWriterContext(MessageBodyWriterContext writerContext, WritableHeaders<?> headers) {
super(writerContext);
Objects.requireNonNull(headers, "headers cannot be null!");
Expand Down Expand Up @@ -378,7 +358,7 @@ public void contentType(HttpMediaType contentType) {
*/
public void contentType(MediaType mediaType) {
Objects.requireNonNull(mediaType);
headers.setIfAbsent(Http.Header.create(Http.Header.CONTENT_TYPE, false, false, mediaType.fullType()));
headers.setIfAbsent(Http.Header.create(Http.Header.CONTENT_TYPE, false, false, mediaType.text()));
}

/**
Expand Down Expand Up @@ -484,67 +464,6 @@ public Charset charset() throws IllegalStateException {
return charsetCache;
}

/**
* Message body writer adapter for the old deprecated writer.
* @param <T> writer type
*/
private static final class WriterAdapter<T> implements MessageBodyWriter<T> {

private final Function<T, Publisher<DataChunk>> function;
private final Predicate predicate;
private final Class<T> type;
private final HttpMediaType contentType;

@SuppressWarnings("unchecked")
WriterAdapter(Function<T, Publisher<DataChunk>> function, Predicate<?> predicate, HttpMediaType contentType) {
Objects.requireNonNull(function, "function cannot be null!");
Objects.requireNonNull(predicate, "predicate cannot be null!");
this.function = function;
this.predicate = predicate;
this.contentType = contentType;
this.type = null;
}

@SuppressWarnings("unchecked")
WriterAdapter(Function<? extends T, Publisher<DataChunk>> function, Class<T> type, HttpMediaType contentType) {
Objects.requireNonNull(function, "function cannot be null!");
Objects.requireNonNull(type, "type cannot be null!");
this.function = (Function<T, Publisher<DataChunk>>) function;
this.type = type;
this.contentType = contentType;
this.predicate = null;
}

@Override
@SuppressWarnings("unchecked")
public PredicateResult accept(GenericType<?> type, MessageBodyWriterContext context) {
if (this.type != null) {
if (!this.type.isAssignableFrom(type.rawType())) {
return PredicateResult.NOT_SUPPORTED;
}
} else {
if (!predicate.test((Object) type.rawType())) {
return PredicateResult.NOT_SUPPORTED;
}
}
HttpMediaType ct = context.contentType().orElse(null);
if (!(contentType != null && ct != null && !ct.test(contentType))) {
if (contentType != null) {
context.contentType(contentType);
}
return PredicateResult.SUPPORTED;
}
return PredicateResult.NOT_SUPPORTED;
}

@Override
public Publisher<DataChunk> write(Single<? extends T> single,
GenericType<? extends T> type,
MessageBodyWriterContext context) {
return single.flatMap(function);
}
}

/**
* Implementation of {@link Mapper} to convert {@code byte[]} to
* a publisher of {@link DataChunk}.
Expand Down
Loading