-
Notifications
You must be signed in to change notification settings - Fork 580
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: David Kral <david.k.kral@oracle.com>
- Loading branch information
Showing
7 changed files
with
409 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
media/jackson/src/main/java/io/helidon/media/jackson/JacksonNdBodyStreamWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://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.helidon.media.jackson; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.concurrent.Flow; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import io.helidon.common.GenericType; | ||
import io.helidon.common.http.DataChunk; | ||
import io.helidon.common.http.MediaType; | ||
import io.helidon.common.reactive.Multi; | ||
import io.helidon.common.reactive.Single; | ||
import io.helidon.media.common.MessageBodyStreamWriter; | ||
import io.helidon.media.common.MessageBodyWriterContext; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
/** | ||
* Message body stream writer supporting object binding with Jackson. | ||
* This writer is for {@link MediaType#APPLICATION_X_NDJSON} media type. | ||
*/ | ||
class JacksonNdBodyStreamWriter implements MessageBodyStreamWriter<Object> { | ||
|
||
private static final byte[] NL = "\n".getBytes(StandardCharsets.UTF_8); | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
private JacksonNdBodyStreamWriter(ObjectMapper objectMapper) { | ||
this.objectMapper = Objects.requireNonNull(objectMapper); | ||
} | ||
|
||
static JacksonNdBodyStreamWriter create(ObjectMapper objectMapper) { | ||
return new JacksonNdBodyStreamWriter(objectMapper); | ||
} | ||
|
||
@Override | ||
public PredicateResult accept(GenericType<?> type, MessageBodyWriterContext context) { | ||
if (CharSequence.class.isAssignableFrom(type.rawType())) { | ||
return PredicateResult.NOT_SUPPORTED; | ||
} | ||
return context.contentType() | ||
.or(() -> findMediaType(context)) | ||
.filter(mediaType -> mediaType.equals(MediaType.APPLICATION_X_NDJSON)) | ||
.map(it -> PredicateResult.COMPATIBLE) | ||
.orElse(PredicateResult.NOT_SUPPORTED); | ||
} | ||
|
||
@Override | ||
public Multi<DataChunk> write(Flow.Publisher<?> publisher, GenericType<?> type, MessageBodyWriterContext context) { | ||
MediaType contentType = MediaType.APPLICATION_X_NDJSON; | ||
context.contentType(contentType); | ||
JacksonBodyWriter.ObjectToChunks objectToChunks = new JacksonBodyWriter.ObjectToChunks(objectMapper, context.charset()); | ||
AtomicBoolean first = new AtomicBoolean(true); | ||
return Multi.create(publisher) | ||
.flatMap(objectToChunks) | ||
.flatMap(dataChunk -> { | ||
if (first.getAndSet(false)) { | ||
return Single.just(dataChunk); | ||
} else { | ||
return Multi.just(DataChunk.create(NL), | ||
dataChunk); | ||
} | ||
}); | ||
} | ||
|
||
private Optional<MediaType> findMediaType(MessageBodyWriterContext context) { | ||
try { | ||
return Optional.of(context.findAccepted(MediaType.APPLICATION_X_NDJSON)); | ||
} catch (IllegalStateException ignore) { | ||
//Not supported. Ignore exception. | ||
return Optional.empty(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
media/jsonb/src/main/java/io/helidon/media/jsonb/JsonbNdBodyStreamWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://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.helidon.media.jsonb; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.concurrent.Flow; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import javax.json.bind.Jsonb; | ||
|
||
import io.helidon.common.GenericType; | ||
import io.helidon.common.http.DataChunk; | ||
import io.helidon.common.http.MediaType; | ||
import io.helidon.common.reactive.Multi; | ||
import io.helidon.common.reactive.Single; | ||
import io.helidon.media.common.MessageBodyStreamWriter; | ||
import io.helidon.media.common.MessageBodyWriterContext; | ||
|
||
/** | ||
* Message body stream writer supporting object binding with JSON-B. | ||
* This writer is for {@link MediaType#APPLICATION_X_NDJSON} media type. | ||
*/ | ||
class JsonbNdBodyStreamWriter implements MessageBodyStreamWriter<Object> { | ||
|
||
private static final byte[] NL = "\n".getBytes(StandardCharsets.UTF_8); | ||
|
||
private final Jsonb jsonb; | ||
|
||
private JsonbNdBodyStreamWriter(Jsonb jsonb) { | ||
this.jsonb = Objects.requireNonNull(jsonb); | ||
} | ||
|
||
static JsonbNdBodyStreamWriter create(Jsonb jsonb) { | ||
return new JsonbNdBodyStreamWriter(jsonb); | ||
} | ||
|
||
@Override | ||
public PredicateResult accept(GenericType<?> type, MessageBodyWriterContext context) { | ||
if (CharSequence.class.isAssignableFrom(type.rawType())) { | ||
return PredicateResult.NOT_SUPPORTED; | ||
} | ||
return context.contentType() | ||
.or(() -> findMediaType(context)) | ||
.filter(mediaType -> mediaType.equals(MediaType.APPLICATION_X_NDJSON)) | ||
.map(it -> PredicateResult.COMPATIBLE) | ||
.orElse(PredicateResult.NOT_SUPPORTED); | ||
} | ||
|
||
@Override | ||
public Multi<DataChunk> write(Flow.Publisher<?> publisher, GenericType<?> type, MessageBodyWriterContext context) { | ||
MediaType contentType = MediaType.APPLICATION_X_NDJSON; | ||
context.contentType(contentType); | ||
|
||
AtomicBoolean first = new AtomicBoolean(true); | ||
|
||
return Multi.create(publisher) | ||
.map(object -> DataChunk.create(jsonb.toJson(object).getBytes(StandardCharsets.UTF_8))) | ||
.flatMap(dataChunk -> { | ||
if (first.getAndSet(false)) { | ||
return Single.just(dataChunk); | ||
} else { | ||
return Multi.just(DataChunk.create(NL), | ||
dataChunk); | ||
} | ||
}); | ||
} | ||
|
||
private Optional<MediaType> findMediaType(MessageBodyWriterContext context) { | ||
try { | ||
return Optional.of(context.findAccepted(MediaType.APPLICATION_X_NDJSON)); | ||
} catch (IllegalStateException ignore) { | ||
//Not supported. Ignore exception. | ||
return Optional.empty(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
2f84730
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks awesome :)