io.micronaut.http.server.codec.TextStreamCodec Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of micronaut-http-server Show documentation
Show all versions of micronaut-http-server Show documentation
Core components supporting the Micronaut Framework
/*
* Copyright 2017-2020 original authors
*
* 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.micronaut.http.server.codec;
import io.micronaut.context.annotation.BootstrapContextCompatible;
import io.micronaut.core.annotation.Internal;
import io.micronaut.http.codec.CodecConfiguration;
import io.micronaut.core.io.buffer.ByteBuffer;
import io.micronaut.core.io.buffer.ByteBufferFactory;
import io.micronaut.core.type.Argument;
import io.micronaut.http.MediaType;
import io.micronaut.http.codec.CodecException;
import io.micronaut.http.codec.MediaTypeCodec;
import io.micronaut.http.codec.MediaTypeCodecRegistry;
import io.micronaut.http.sse.Event;
import io.micronaut.runtime.ApplicationConfiguration;
import edu.umd.cs.findbugs.annotations.Nullable;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* A {@link MediaTypeCodec} that will encode {@link Event} objects in order to support Server Sent Events.
*
* @author Graeme Rocher
* @since 1.0
*/
@Singleton
@Internal
@BootstrapContextCompatible
public class TextStreamCodec implements MediaTypeCodec {
public static final String CONFIGURATION_QUALIFIER = "text-stream";
private static final byte[] DATA_PREFIX = "data: ".getBytes(StandardCharsets.UTF_8);
private static final byte[] EVENT_PREFIX = "event: ".getBytes(StandardCharsets.UTF_8);
private static final byte[] ID_PREFIX = "id: ".getBytes(StandardCharsets.UTF_8);
private static final byte[] RETRY_PREFIX = "retry: ".getBytes(StandardCharsets.UTF_8);
private static final byte[] COMMENT_PREFIX = ": ".getBytes(StandardCharsets.UTF_8);
private static final byte[] NEWLINE = "\n".getBytes(StandardCharsets.UTF_8);
private final Provider codecRegistryProvider;
private final ByteBufferFactory byteBufferFactory;
private final List additionalTypes;
private final Charset defaultCharset;
private MediaTypeCodecRegistry codecRegistry;
/**
* @param applicationConfiguration The application configuration
* @param byteBufferFactory A byte buffer factory
* @param codecRegistryProvider A media type codec registry
* @param codecConfiguration The configuration for the codec
*/
@Inject
public TextStreamCodec(
ApplicationConfiguration applicationConfiguration,
ByteBufferFactory byteBufferFactory,
Provider codecRegistryProvider,
@Named(CONFIGURATION_QUALIFIER) @Nullable CodecConfiguration codecConfiguration) {
this(applicationConfiguration.getDefaultCharset(), byteBufferFactory, codecRegistryProvider, codecConfiguration);
}
/**
* @param defaultCharset The default charset
* @param byteBufferFactory A byte buffer factory
* @param codecRegistryProvider A media type codec registry
* @param codecConfiguration The configuration for the codec
*/
protected TextStreamCodec(
Charset defaultCharset,
ByteBufferFactory byteBufferFactory,
Provider codecRegistryProvider,
@Named(CONFIGURATION_QUALIFIER) @Nullable CodecConfiguration codecConfiguration) {
this.defaultCharset = defaultCharset;
this.byteBufferFactory = byteBufferFactory;
this.codecRegistryProvider = codecRegistryProvider;
if (codecConfiguration != null) {
this.additionalTypes = codecConfiguration.getAdditionalTypes();
} else {
this.additionalTypes = Collections.emptyList();
}
}
@Override
public Collection getMediaTypes() {
List mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.TEXT_EVENT_STREAM_TYPE);
mediaTypes.addAll(additionalTypes);
return mediaTypes;
}
@Override
public T decode(Argument type, InputStream inputStream) throws CodecException {
throw new UnsupportedOperationException("This codec currently only supports encoding");
}
@Override
public T decode(Class type, InputStream inputStream) throws CodecException {
throw new UnsupportedOperationException("This codec currently only supports encoding");
}
@Override
public void encode(T object, OutputStream outputStream) throws CodecException {
try {
outputStream.write(encode(object));
} catch (IOException e) {
throw new CodecException("I/O error occurred encoding object to output stream: " + e.getMessage(), e);
}
}
@Override
public byte[] encode(T object) throws CodecException {
ByteBuffer buffer = encode(object, byteBufferFactory);
return buffer.toByteArray();
}
@SuppressWarnings("MagicNumber")
@Override
public ByteBuffer encode(T object, ByteBufferFactory, B> allocator) throws CodecException {
Event