Skip to content

Commit 4265479

Browse files
Reactive Media Common deprecated cleanup. (#6098)
- Remove the following methods in ContentWriters that are deprecated in 3.x: - byteArrayWriter - charSequenceWriter - charBufferWriter - byteChannelWriter (updated PathBodyWriter and FileBodyWriter to remove usage) - Updated ContentWriterTests and ContentReaderTests: consistent test method names (e.g. charSequenceWriter -> writeCharSequence) - Removed unused private code in MessageBodyWriterContext and MessageBodyReadableContent Fixes #4364
1 parent b6bbd5f commit 4265479

File tree

9 files changed

+30
-234
lines changed

9 files changed

+30
-234
lines changed

examples/webserver/tutorial/src/main/java/io/helidon/reactive/webserver/examples/tutorial/CommentService.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2022 Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2023 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -187,8 +187,7 @@ Flow.Publisher<DataChunk> publish(List<Comment> comments) {
187187
String str = comments.stream()
188188
.map(Comment::toString)
189189
.collect(Collectors.joining("\n"));
190-
return ContentWriters.charSequenceWriter(StandardCharsets.UTF_8)
191-
.apply(str + "\n");
190+
return ContentWriters.writeCharSequence(str + "\n", StandardCharsets.UTF_8);
192191
}
193192

194193
}

reactive/media/common/src/main/java/io/helidon/reactive/media/common/ContentReaders.java

+4-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2022 Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2023 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -81,13 +81,7 @@ public static Single<String> readURLEncodedString(Publisher<DataChunk> chunks,
8181
* Implementation of {@link Mapper} that converts a {@code byte[]} into
8282
* a {@link String} using a given {@link Charset}.
8383
*/
84-
private static final class BytesToString implements Mapper<byte[], String> {
85-
86-
private final Charset charset;
87-
88-
BytesToString(Charset charset) {
89-
this.charset = charset;
90-
}
84+
private record BytesToString(Charset charset) implements Mapper<byte[], String> {
9185

9286
@Override
9387
public String map(byte[] bytes) {
@@ -98,19 +92,14 @@ public String map(byte[] bytes) {
9892
/**
9993
* Mapper that applies URL decoding to a {@code String}.
10094
*/
101-
private static final class StringToDecodedString implements Mapper<String, String> {
102-
103-
private final Charset charset;
104-
105-
StringToDecodedString(Charset charset) {
106-
this.charset = charset;
107-
}
95+
private record StringToDecodedString(Charset charset) implements Mapper<String, String> {
10896

10997
@Override
11098
public String map(String s) {
11199
return URLDecoder.decode(s, charset);
112100
}
113101
}
102+
114103
/**
115104
* Implementation of {@link Collector} that collects chunks into a single
116105
* {@code byte[]}.

reactive/media/common/src/main/java/io/helidon/reactive/media/common/ContentWriters.java

+1-91
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2022 Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2023 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,15 +19,9 @@
1919
import java.io.PrintWriter;
2020
import java.io.StringWriter;
2121
import java.nio.ByteBuffer;
22-
import java.nio.channels.ReadableByteChannel;
2322
import java.nio.charset.Charset;
24-
import java.util.Objects;
25-
import java.util.concurrent.Flow.Publisher;
26-
import java.util.function.Function;
2723

2824
import io.helidon.common.http.DataChunk;
29-
import io.helidon.common.reactive.IoMulti;
30-
import io.helidon.common.reactive.RetrySchema;
3125
import io.helidon.common.reactive.Single;
3226

3327
/**
@@ -115,88 +109,4 @@ public static Single<DataChunk> writeStackTrace(Throwable throwable, Charset cha
115109
}
116110
return returnValue;
117111
}
118-
119-
/**
120-
* Returns a writer function for {@code byte[]}.
121-
* <p>
122-
* The {@code copy} variant is by default registered in
123-
* {@code ServerResponse}.
124-
*
125-
* @param copy a signal if byte array should be copied - set it {@code true}
126-
* if {@code byte[]} will be immediately reused.
127-
* @return a {@code byte[]} writer
128-
*
129-
* @deprecated since 2.0.0, use {@link #writeBytes(byte[], boolean)} instead
130-
*/
131-
@Deprecated(since = "2.0.0")
132-
public static Function<byte[], Publisher<DataChunk>> byteArrayWriter(boolean copy) {
133-
return (bytes) -> writeBytes(bytes, copy);
134-
}
135-
136-
/**
137-
* Returns a writer function for {@link CharSequence} using provided
138-
* standard {@code charset}.
139-
* <p>
140-
* An instance is by default registered in {@code ServerResponse} for all
141-
* standard charsets.
142-
*
143-
* @param charset a standard charset to use
144-
* @return a {@link String} writer
145-
* @throws NullPointerException if parameter {@code charset} is {@code null}
146-
* @deprecated since 2.0.0, use {@link #writeCharSequence(CharSequence, Charset)}
147-
* or {@link DefaultMediaSupport#charSequenceWriter()} instead
148-
*/
149-
@Deprecated(since = "2.0.0")
150-
public static Function<CharSequence, Publisher<DataChunk>> charSequenceWriter(Charset charset) {
151-
return (cs) -> writeCharSequence(cs, charset);
152-
}
153-
154-
/**
155-
* Returns a writer function for {@link CharBuffer} using provided standard
156-
* {@code charset}.
157-
* <p>
158-
* An instance is by default registered in {@code ServerResponse} for all
159-
* standard charsets.
160-
*
161-
* @param charset a standard charset to use
162-
* @return a {@link String} writer
163-
* @throws NullPointerException if parameter {@code charset} is {@code null}
164-
* @deprecated since 2.0.0, use {@link #writeCharBuffer(CharBuffer, Charset)} instead
165-
*/
166-
@Deprecated(since = "2.0.0")
167-
public static Function<CharBuffer, Publisher<DataChunk>> charBufferWriter(Charset charset) {
168-
return (buffer) -> writeCharBuffer(buffer, charset);
169-
}
170-
171-
/**
172-
* Returns a writer function for {@link ReadableByteChannel}. Created
173-
* publisher use provided {@link RetrySchema} to define delay between
174-
* unsuccessful read attempts.
175-
*
176-
* @param retrySchema a retry schema to use in case when {@code read}
177-
* operation reads {@code 0 bytes}
178-
* @return a {@link ReadableByteChannel} writer
179-
* @deprecated since 2.0.0, use {@link DefaultMediaSupport#byteChannelWriter(RetrySchema)}} instead
180-
*/
181-
@Deprecated(since = "2.0.0")
182-
public static Function<ReadableByteChannel, Publisher<DataChunk>> byteChannelWriter(RetrySchema retrySchema) {
183-
Objects.requireNonNull(retrySchema);
184-
185-
return channel -> IoMulti.multiFromByteChannelBuilder(channel)
186-
.retrySchema(retrySchema)
187-
.build()
188-
.map(DataChunk::create);
189-
}
190-
191-
/**
192-
* Returns a writer function for {@link ReadableByteChannel}.
193-
*
194-
* @return a {@link ReadableByteChannel} writer
195-
* @deprecated since 2.0.0, use {@link DefaultMediaSupport#byteChannelWriter()}} instead
196-
*/
197-
@Deprecated(since = "2.0.0")
198-
public static Function<ReadableByteChannel, Publisher<DataChunk>> byteChannelWriter() {
199-
return channel -> IoMulti.multiFromByteChannel(channel).map(DataChunk::create);
200-
}
201-
202112
}

reactive/media/common/src/main/java/io/helidon/reactive/media/common/FileBodyWriter.java

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2023 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727
import io.helidon.common.http.DataChunk;
2828
import io.helidon.common.mapper.Mapper;
2929
import io.helidon.common.media.type.MediaTypes;
30+
import io.helidon.common.reactive.IoMulti;
3031
import io.helidon.common.reactive.Single;
3132

3233
/**
@@ -67,13 +68,7 @@ static FileBodyWriter create() {
6768
* Implementation of {@link Mapper} that converts {@link File} to a
6869
* publisher of {@link DataChunk}.
6970
*/
70-
private static final class FileToChunks implements Mapper<File, Publisher<DataChunk>> {
71-
72-
private final MessageBodyWriterContext context;
73-
74-
FileToChunks(MessageBodyWriterContext context) {
75-
this.context = context;
76-
}
71+
private record FileToChunks(MessageBodyWriterContext context) implements Mapper<File, Publisher<DataChunk>> {
7772

7873
@Override
7974
public Publisher<DataChunk> map(File file) {
@@ -82,9 +77,9 @@ public Publisher<DataChunk> map(File file) {
8277
context.contentType(MediaTypes.APPLICATION_OCTET_STREAM);
8378
context.contentLength(Files.size(path));
8479
FileChannel fc = FileChannel.open(path, StandardOpenOption.READ);
85-
return ContentWriters.byteChannelWriter().apply(fc);
80+
return IoMulti.multiFromByteChannel(fc).map(DataChunk::create);
8681
} catch (IOException ex) {
87-
return Single.<DataChunk>error(ex);
82+
return Single.error(ex);
8883
}
8984
}
9085
}

reactive/media/common/src/main/java/io/helidon/reactive/media/common/MessageBodyReadableContent.java

+1-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2023 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,7 +27,6 @@
2727
/**
2828
* Readable {@link MessageBodyContent}.
2929
*/
30-
@SuppressWarnings("deprecation")
3130
public final class MessageBodyReadableContent
3231
implements MessageBodyReaders, MessageBodyFilters, MessageBodyContent, Multi<DataChunk> {
3332

@@ -46,16 +45,6 @@ public final class MessageBodyReadableContent
4645
this.context = context;
4746
}
4847

49-
/**
50-
* Copy constructor.
51-
* @param orig original context to be copied
52-
*/
53-
private MessageBodyReadableContent(MessageBodyReadableContent orig) {
54-
Objects.requireNonNull(orig, "orig is null!");
55-
this.publisher = orig.publisher;
56-
this.context = orig.context;
57-
}
58-
5948
/**
6049
* Get the reader context used to unmarshall data.
6150
*

reactive/media/common/src/main/java/io/helidon/reactive/media/common/MessageBodyWriterContext.java

+3-84
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2023 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,7 +23,6 @@
2323
import java.util.Objects;
2424
import java.util.Optional;
2525
import java.util.concurrent.Flow.Publisher;
26-
import java.util.function.Function;
2726
import java.util.function.Predicate;
2827

2928
import io.helidon.common.GenericType;
@@ -71,11 +70,7 @@ private MessageBodyWriterContext(MessageBodyWriterContext parent,
7170
super(parent, eventListener);
7271
Objects.requireNonNull(headers, "headers cannot be null!");
7372
this.headers = headers;
74-
if (acceptedTypes != null) {
75-
this.acceptedTypes = acceptedTypes;
76-
} else {
77-
this.acceptedTypes = List.of();
78-
}
73+
this.acceptedTypes = Objects.requireNonNullElseGet(acceptedTypes, List::of);
7974
if (parent != null) {
8075
this.writers = new MessageBodyOperators<>(parent.writers);
8176
this.swriters = new MessageBodyOperators<>(parent.swriters);
@@ -98,21 +93,6 @@ private MessageBodyWriterContext(WritableHeaders<?> headers) {
9893
this.acceptedTypes = List.of();
9994
}
10095

101-
/**
102-
* Create a new standalone (non parented) context.
103-
*/
104-
private MessageBodyWriterContext() {
105-
super(null, null);
106-
this.headers = WritableHeaders.create();
107-
this.writers = new MessageBodyOperators<>();
108-
this.swriters = new MessageBodyOperators<>();
109-
this.acceptedTypes = List.of();
110-
this.contentTypeCache = Optional.empty();
111-
this.contentTypeCached = true;
112-
this.charsetCache = DEFAULT_CHARSET;
113-
this.charsetCached = true;
114-
}
115-
11696
private MessageBodyWriterContext(MessageBodyWriterContext writerContext, WritableHeaders<?> headers) {
11797
super(writerContext);
11898
Objects.requireNonNull(headers, "headers cannot be null!");
@@ -378,7 +358,7 @@ public void contentType(HttpMediaType contentType) {
378358
*/
379359
public void contentType(MediaType mediaType) {
380360
Objects.requireNonNull(mediaType);
381-
headers.setIfAbsent(Http.Header.create(Http.Header.CONTENT_TYPE, false, false, mediaType.fullType()));
361+
headers.setIfAbsent(Http.Header.create(Http.Header.CONTENT_TYPE, false, false, mediaType.text()));
382362
}
383363

384364
/**
@@ -484,67 +464,6 @@ public Charset charset() throws IllegalStateException {
484464
return charsetCache;
485465
}
486466

487-
/**
488-
* Message body writer adapter for the old deprecated writer.
489-
* @param <T> writer type
490-
*/
491-
private static final class WriterAdapter<T> implements MessageBodyWriter<T> {
492-
493-
private final Function<T, Publisher<DataChunk>> function;
494-
private final Predicate predicate;
495-
private final Class<T> type;
496-
private final HttpMediaType contentType;
497-
498-
@SuppressWarnings("unchecked")
499-
WriterAdapter(Function<T, Publisher<DataChunk>> function, Predicate<?> predicate, HttpMediaType contentType) {
500-
Objects.requireNonNull(function, "function cannot be null!");
501-
Objects.requireNonNull(predicate, "predicate cannot be null!");
502-
this.function = function;
503-
this.predicate = predicate;
504-
this.contentType = contentType;
505-
this.type = null;
506-
}
507-
508-
@SuppressWarnings("unchecked")
509-
WriterAdapter(Function<? extends T, Publisher<DataChunk>> function, Class<T> type, HttpMediaType contentType) {
510-
Objects.requireNonNull(function, "function cannot be null!");
511-
Objects.requireNonNull(type, "type cannot be null!");
512-
this.function = (Function<T, Publisher<DataChunk>>) function;
513-
this.type = type;
514-
this.contentType = contentType;
515-
this.predicate = null;
516-
}
517-
518-
@Override
519-
@SuppressWarnings("unchecked")
520-
public PredicateResult accept(GenericType<?> type, MessageBodyWriterContext context) {
521-
if (this.type != null) {
522-
if (!this.type.isAssignableFrom(type.rawType())) {
523-
return PredicateResult.NOT_SUPPORTED;
524-
}
525-
} else {
526-
if (!predicate.test((Object) type.rawType())) {
527-
return PredicateResult.NOT_SUPPORTED;
528-
}
529-
}
530-
HttpMediaType ct = context.contentType().orElse(null);
531-
if (!(contentType != null && ct != null && !ct.test(contentType))) {
532-
if (contentType != null) {
533-
context.contentType(contentType);
534-
}
535-
return PredicateResult.SUPPORTED;
536-
}
537-
return PredicateResult.NOT_SUPPORTED;
538-
}
539-
540-
@Override
541-
public Publisher<DataChunk> write(Single<? extends T> single,
542-
GenericType<? extends T> type,
543-
MessageBodyWriterContext context) {
544-
return single.flatMap(function);
545-
}
546-
}
547-
548467
/**
549468
* Implementation of {@link Mapper} to convert {@code byte[]} to
550469
* a publisher of {@link DataChunk}.

0 commit comments

Comments
 (0)