Skip to content

Commit

Permalink
Updated according to changes in #1806.
Browse files Browse the repository at this point in the history
Changed generic declaration in API to be more open for users.

Signed-off-by: Tomas Langer <tomas.langer@oracle.com>
  • Loading branch information
tomas-langer committed May 17, 2020
1 parent 6d18747 commit 845fba7
Show file tree
Hide file tree
Showing 13 changed files with 231 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020 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 @@ -49,8 +49,9 @@ public boolean accept(GenericType<?> type, MessageBodyWriterContext context) {
}

@Override
public Publisher<DataChunk> write(Single<ReadableByteChannel> content, GenericType<? extends ReadableByteChannel> type,
MessageBodyWriterContext context) {
public Publisher<DataChunk> write(Single<? extends ReadableByteChannel> content,
GenericType<? extends ReadableByteChannel> type,
MessageBodyWriterContext context) {

context.contentType(MediaType.APPLICATION_OCTET_STREAM);
return content.flatMap(mapper);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020 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 @@ -41,8 +41,9 @@ public boolean accept(GenericType<?> type, MessageBodyWriterContext context) {
}

@Override
public Publisher<DataChunk> write(Single<CharSequence> content, GenericType<? extends CharSequence> type,
MessageBodyWriterContext context) {
public Publisher<DataChunk> write(Single<? extends CharSequence> content,
GenericType<? extends CharSequence> type,
MessageBodyWriterContext context) {

context.contentType(MediaType.TEXT_PLAIN);
return content.flatMap(new CharSequenceToChunks(context.charset()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020 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 @@ -49,7 +49,9 @@ public boolean accept(GenericType<?> type, MessageBodyWriterContext context) {
}

@Override
public Publisher<DataChunk> write(Single<File> content, GenericType<? extends File> type, MessageBodyWriterContext context) {
public Publisher<DataChunk> write(Single<? extends File> content,
GenericType<? extends File> type,
MessageBodyWriterContext context) {
return content.flatMap(new FileToChunks(DEFAULT_RETRY_SCHEMA, context));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Flow;
import java.util.concurrent.Flow.Publisher;
import java.util.concurrent.Flow.Subscriber;
import java.util.concurrent.Flow.Subscription;
Expand Down Expand Up @@ -153,6 +154,13 @@ public <T> Single<T> unmarshall(Publisher<DataChunk> payload, GenericType<T> typ
if (payload == null) {
return Single.<T>empty();
}

// Flow.Publisher - can only be supported by streaming media
if (Flow.Publisher.class.isAssignableFrom(type.rawType())) {
throw new IllegalStateException("This method does not support unmarshalling of Flow.Publisher. Please use "
+ "a stream unmarshalling method.");
}

try {
Publisher<DataChunk> filteredPayload = applyFilters(payload, type);
if (byte[].class.equals(type.rawType())) {
Expand Down Expand Up @@ -185,6 +193,13 @@ public <T> Single<T> unmarshall(Publisher<DataChunk> payload, Class<? extends Me
if (payload == null) {
return Single.<T>empty();
}

// Flow.Publisher - can only be supported by streaming media
if (Flow.Publisher.class.isAssignableFrom(type.rawType())) {
throw new IllegalStateException("This method does not support unmarshalling of Flow.Publisher. Please use "
+ "a stream unmarshalling method.");
}

try {
Publisher<DataChunk> filteredPayload = applyFilters(payload, type);
MessageBodyReader<T> reader = (MessageBodyReader<T>) readers.get(readerType);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020 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 @@ -35,5 +35,5 @@ public interface MessageBodyStreamWriter<T> extends MessageBodyOperator<MessageB
* @param context writer context
* @return HTTP payload publisher
*/
Publisher<DataChunk> write(Publisher<T> publisher, GenericType<? extends T> type, MessageBodyWriterContext context);
Publisher<DataChunk> write(Publisher<? extends T> publisher, GenericType<? extends T> type, MessageBodyWriterContext context);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020 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 @@ -36,5 +36,5 @@ public interface MessageBodyWriter<T> extends MessageBodyOperator<MessageBodyWri
* @param context the context providing the headers abstraction
* @return Publisher of objects
*/
Publisher<DataChunk> write(Single<T> single, GenericType<? extends T> type, MessageBodyWriterContext context);
Publisher<DataChunk> write(Single<? extends T> single, GenericType<? extends T> type, MessageBodyWriterContext context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import io.helidon.common.GenericType;
import io.helidon.common.http.DataChunk;
import io.helidon.common.http.HashParameters;
import io.helidon.common.http.Http;
import io.helidon.common.http.MediaType;
import io.helidon.common.http.Parameters;
Expand Down Expand Up @@ -197,7 +198,7 @@ public static MessageBodyWriterContext create(MessageBodyWriterContext parent, P
* @return MessageBodyWriterContext
*/
public static MessageBodyWriterContext create() {
return new MessageBodyWriterContext(ReadOnlyParameters.empty());
return new MessageBodyWriterContext(HashParameters.create());
}

@Override
Expand Down Expand Up @@ -296,6 +297,14 @@ public <T> Publisher<DataChunk> marshall(Single<T> content, GenericType<T> type,
if (byte[].class.equals(type.rawType())) {
return applyFilters(((Single<byte[]>) content).flatMap(BYTES_MAPPER));
}

// Flow.Publisher - can only be supported by streaming media
if (Publisher.class.isAssignableFrom(type.rawType())) {
throw new IllegalStateException("This method does not support marshalling of Flow.Publisher. Please use "
+ "a method that accepts Flow.Publisher and type for stream marshalling"
+ ".");
}

MessageBodyWriter<T> writer;
if (fallback != null) {
writer = (MessageBodyWriter<T>) writers.select(type, this, fallback.writers);
Expand Down Expand Up @@ -604,7 +613,9 @@ public boolean accept(GenericType<?> type, MessageBodyWriterContext context) {
}

@Override
public Publisher<DataChunk> write(Single<T> single, GenericType<? extends T> type, MessageBodyWriterContext context) {
public Publisher<DataChunk> write(Single<? extends T> single,
GenericType<? extends T> type,
MessageBodyWriterContext context) {
return single.flatMap(function);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020 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 @@ -48,7 +48,9 @@ public boolean accept(GenericType<?> type, MessageBodyWriterContext context) {
}

@Override
public Publisher<DataChunk> write(Single<Path> content, GenericType<? extends Path> type, MessageBodyWriterContext context) {
public Publisher<DataChunk> write(Single<? extends Path> content,
GenericType<? extends Path> type,
MessageBodyWriterContext context) {
return content.flatMap(new PathToChunks(DEFAULT_RETRY_SCHEMA, context));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020 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 @@ -45,7 +45,7 @@ public boolean accept(GenericType<?> type, MessageBodyWriterContext context) {
}

@Override
public Publisher<DataChunk> write(Single<Throwable> content,
public Publisher<DataChunk> write(Single<? extends Throwable> content,
GenericType<? extends Throwable> type,
MessageBodyWriterContext context) {
context.contentType(MediaType.TEXT_PLAIN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import java.nio.charset.Charset;
import java.util.Objects;
import java.util.concurrent.Flow;
import java.util.concurrent.Flow.Publisher;

import javax.json.bind.Jsonb;
Expand Down Expand Up @@ -49,18 +48,11 @@ private JsonbBodyWriter(Jsonb jsonb) {
public boolean accept(GenericType<?> type,
MessageBodyWriterContext context) {

// We are excluding the following types from support:
// 1. any char sequence
// 2. Flow.Publisher - that can only be supported by streaming media
if (Flow.Publisher.class.isAssignableFrom(type.rawType())) {
return false;
}

return !CharSequence.class.isAssignableFrom(type.rawType());
}

@Override
public Publisher<DataChunk> write(Single<Object> content, GenericType<? extends Object> type,
public Publisher<DataChunk> write(Single<? extends Object> content, GenericType<? extends Object> type,
MessageBodyWriterContext context) {

MediaType contentType = context.findAccepted(MediaType.JSON_PREDICATE, MediaType.APPLICATION_JSON);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public boolean accept(GenericType<?> type, MessageBodyWriterContext context) {
}

@Override
public Publisher<DataChunk> write(Publisher<JsonStructure> publisher,
GenericType<? extends JsonStructure> type,
MessageBodyWriterContext context) {
public Multi<DataChunk> write(Publisher<? extends JsonStructure> publisher,
GenericType<? extends JsonStructure> type,
MessageBodyWriterContext context) {

MediaType contentType = context.findAccepted(MediaType.JSON_PREDICATE, MediaType.APPLICATION_JSON);
context.contentType(contentType);
Expand All @@ -64,25 +64,18 @@ public Publisher<DataChunk> write(Publisher<JsonStructure> publisher,
JsonStructureToChunks jsonToChunks = new JsonStructureToChunks(jsonWriterFactory,
context.charset());

// we also do not have an append operator
Multi<DataChunk> stream = Multi.concat(
Single.just(DataChunk.create(ARRAY_JSON_BEGIN_BYTES)),
Multi.from(publisher)
.map(jsonToChunks)
.flatMap(it -> {
if (first.getAndSet(false)) {
// first record, do not prepend a comma
return Single.just(it);
} else {
// any subsequent record starts with a comma
return Multi.just(DataChunk.create(COMMA_BYTES), it);
}
}));

// append ] at the end of the stream
stream = Multi.concat(stream,
Single.just(DataChunk.create(ARRAY_JSON_END_BYTES)));

return stream;
return Single.just(DataChunk.create(ARRAY_JSON_BEGIN_BYTES))
.onCompleteResumeWith(Multi.from(publisher)
.map(jsonToChunks)
.flatMap(it -> {
if (first.getAndSet(false)) {
// first record, do not prepend a comma
return Single.just(it);
} else {
// any subsequent record starts with a comma
return Multi.just(DataChunk.create(COMMA_BYTES), it);
}
}))
.onCompleteResume(DataChunk.create(ARRAY_JSON_END_BYTES));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public boolean accept(GenericType<?> type, MessageBodyWriterContext context) {
}

@Override
public Publisher<DataChunk> write(Single<JsonStructure> content, GenericType<? extends JsonStructure> type,
public Publisher<DataChunk> write(Single<? extends JsonStructure> content,
GenericType<? extends JsonStructure> type,
MessageBodyWriterContext context) {

MediaType contentType = context.findAccepted(MediaType.JSON_PREDICATE, MediaType.APPLICATION_JSON);
Expand Down
Loading

0 comments on commit 845fba7

Please sign in to comment.