|
| 1 | +/* |
| 2 | + * Copyright 2002-2014 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.baeldung.config.converter; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | + |
| 21 | +import org.baeldung.web.dto.Foo; |
| 22 | +import org.springframework.http.HttpInputMessage; |
| 23 | +import org.springframework.http.HttpOutputMessage; |
| 24 | +import org.springframework.http.MediaType; |
| 25 | +import org.springframework.http.converter.AbstractHttpMessageConverter; |
| 26 | + |
| 27 | +import com.esotericsoftware.kryo.Kryo; |
| 28 | +import com.esotericsoftware.kryo.io.Input; |
| 29 | +import com.esotericsoftware.kryo.io.Output; |
| 30 | + |
| 31 | +/** |
| 32 | + * An {@code HttpMessageConverter} that can read and write Kryo messages. |
| 33 | + */ |
| 34 | +public class KryoHttpMessageConverter extends AbstractHttpMessageConverter<Object> { |
| 35 | + |
| 36 | + public static final MediaType KRYO = new MediaType("application", "x-kryo"); |
| 37 | + |
| 38 | + private static final ThreadLocal<Kryo> kryoThreadLocal = new ThreadLocal<Kryo>() { |
| 39 | + @Override |
| 40 | + protected Kryo initialValue() { |
| 41 | + final Kryo kryo = new Kryo(); |
| 42 | + kryo.register(Foo.class, 1); |
| 43 | + return kryo; |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + public KryoHttpMessageConverter() { |
| 48 | + super(KRYO); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + protected boolean supports(final Class<?> clazz) { |
| 53 | + return Object.class.isAssignableFrom(clazz); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + protected Object readInternal(final Class<? extends Object> clazz, final HttpInputMessage inputMessage) throws IOException { |
| 58 | + final Input input = new Input(inputMessage.getBody()); |
| 59 | + return kryoThreadLocal.get().readClassAndObject(input); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + protected void writeInternal(final Object object, final HttpOutputMessage outputMessage) throws IOException { |
| 64 | + final Output output = new Output(outputMessage.getBody()); |
| 65 | + kryoThreadLocal.get().writeClassAndObject(output, object); |
| 66 | + output.flush(); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + protected MediaType getDefaultContentType(final Object object) { |
| 71 | + return KRYO; |
| 72 | + } |
| 73 | +} |
0 commit comments