001/* 002 * Copyright 2002-2019 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.http.codec.protobuf; 018 019import java.io.IOException; 020import java.util.Collections; 021import java.util.List; 022import java.util.Map; 023import java.util.stream.Collectors; 024 025import com.google.protobuf.Message; 026import org.reactivestreams.Publisher; 027import reactor.core.publisher.Flux; 028import reactor.core.publisher.Mono; 029 030import org.springframework.core.ResolvableType; 031import org.springframework.core.io.buffer.DataBuffer; 032import org.springframework.core.io.buffer.DataBufferFactory; 033import org.springframework.core.io.buffer.DataBufferUtils; 034import org.springframework.http.MediaType; 035import org.springframework.http.codec.HttpMessageEncoder; 036import org.springframework.lang.Nullable; 037import org.springframework.util.MimeType; 038 039/** 040 * An {@code Encoder} that writes {@link com.google.protobuf.Message}s 041 * using <a href="https://developers.google.com/protocol-buffers/">Google Protocol Buffers</a>. 042 * 043 * <p>Flux are serialized using 044 * <a href="https://developers.google.com/protocol-buffers/docs/techniques?hl=en#streaming">delimited Protobuf messages</a> 045 * with the size of each message specified before the message itself. Single values are 046 * serialized using regular Protobuf message format (without the size prepended before the message). 047 * 048 * <p>To generate {@code Message} Java classes, you need to install the {@code protoc} binary. 049 * 050 * <p>This encoder requires Protobuf 3 or higher, and supports 051 * {@code "application/x-protobuf"} and {@code "application/octet-stream"} with the official 052 * {@code "com.google.protobuf:protobuf-java"} library. 053 * 054 * @author S茅bastien Deleuze 055 * @since 5.1 056 * @see ProtobufDecoder 057 */ 058public class ProtobufEncoder extends ProtobufCodecSupport implements HttpMessageEncoder<Message> { 059 060 private static final List<MediaType> streamingMediaTypes = MIME_TYPES 061 .stream() 062 .map(mimeType -> new MediaType(mimeType.getType(), mimeType.getSubtype(), 063 Collections.singletonMap(DELIMITED_KEY, DELIMITED_VALUE))) 064 .collect(Collectors.toList()); 065 066 067 @Override 068 public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType) { 069 return Message.class.isAssignableFrom(elementType.toClass()) && supportsMimeType(mimeType); 070 } 071 072 @Override 073 public Flux<DataBuffer> encode(Publisher<? extends Message> inputStream, DataBufferFactory bufferFactory, 074 ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) { 075 076 return Flux.from(inputStream).map(message -> 077 encodeValue(message, bufferFactory, !(inputStream instanceof Mono))); 078 } 079 080 @Override 081 public DataBuffer encodeValue(Message message, DataBufferFactory bufferFactory, 082 ResolvableType valueType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) { 083 084 return encodeValue(message, bufferFactory, false); 085 } 086 087 private DataBuffer encodeValue(Message message, DataBufferFactory bufferFactory, boolean delimited) { 088 089 DataBuffer buffer = bufferFactory.allocateBuffer(); 090 boolean release = true; 091 try { 092 if (delimited) { 093 message.writeDelimitedTo(buffer.asOutputStream()); 094 } 095 else { 096 message.writeTo(buffer.asOutputStream()); 097 } 098 release = false; 099 return buffer; 100 } 101 catch (IOException ex) { 102 throw new IllegalStateException("Unexpected I/O error while writing to data buffer", ex); 103 } 104 finally { 105 if (release) { 106 DataBufferUtils.release(buffer); 107 } 108 } 109 } 110 111 @Override 112 public List<MediaType> getStreamingMediaTypes() { 113 return streamingMediaTypes; 114 } 115 116 @Override 117 public List<MimeType> getEncodableMimeTypes() { 118 return getMimeTypes(); 119 } 120 121}