com.spotify.apollo.route.JsonSerializerMiddlewares Maven / Gradle / Ivy
The newest version!
/*
* -\-\-
* Spotify Apollo Extra
* --
* Copyright (C) 2013 - 2015 Spotify AB
* --
* 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 com.spotify.apollo.route;
import com.google.common.base.Throwables;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.spotify.apollo.Response;
import okio.ByteString;
public class JsonSerializerMiddlewares {
private JsonSerializerMiddlewares() {
// prevent instantiation
}
private static final String CONTENT_TYPE = "Content-Type";
private static final String JSON = "application/json; charset=UTF8";
private static ByteString serialize(ObjectWriter objectWriter, T object) {
try {
return ByteString.of(objectWriter.writeValueAsBytes(object));
} catch (JsonProcessingException e) {
throw Throwables.propagate(e);
}
}
private static Response serialize(ObjectWriter objectWriter, Response response){
if(!response.payload().isPresent()){
// Same as in com.spotify.apollo.route.Middlewares.serializePayload()
// no payload, so this cast is safe to do
//noinspection unchecked
return (Response) response;
} else {
return response.withPayload(serialize(objectWriter, response.payload().get()));
}
}
/**
* Middleware that serializes the result of the inner handler using the supplied
* {@link ObjectWriter}, and sets the Content-Type header to application/json.
*/
public static Middleware, AsyncHandler>>
jsonSerialize(ObjectWriter objectWriter) {
return handler ->
requestContext -> handler.invoke(requestContext)
.thenApply(result -> Response
.forPayload(serialize(objectWriter, result))
.withHeader(CONTENT_TYPE, JSON));
}
/**
* Middleware that serializes the payload of the result response of the inner handler using
* the supplied {@link ObjectWriter}, and sets the Content-Type header to application/json.
*/
public static Middleware>, AsyncHandler>>
jsonSerializeResponse(ObjectWriter objectWriter) {
return handler ->
requestContext -> handler.invoke(requestContext)
.thenApply(response -> serialize(objectWriter, response)
.withHeader(CONTENT_TYPE, JSON));
}
public static Middleware, AsyncHandler>>
jsonSerializeSync(ObjectWriter objectWriter) {
Middleware, AsyncHandler> syncToAsync = Middleware::syncToAsync;
return syncToAsync.and(jsonSerialize(objectWriter));
}
public static Middleware>, AsyncHandler>>
jsonSerializeResponseSync(ObjectWriter objectWriter) {
Middleware>, AsyncHandler>> syncToAsync = Middleware::syncToAsync;
return syncToAsync.and(jsonSerializeResponse(objectWriter));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy