All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.tcurrie.rest.factory.RestResponseAdaptor Maven / Gradle / Ivy

There is a newer version: 0.2.70
Show newest version
package com.github.tcurrie.rest.factory;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.tcurrie.rest.factory.model.ResponseWrapper;
import com.github.tcurrie.rest.factory.model.RestFactoryException;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.function.Consumer;
import java.util.function.Function;

public interface RestResponseAdaptor {
    interface Client {
        T apply(String s) throws Throwable;

        final class Factory {
            private Factory() {
                throw RestFactoryException.create("Can not construct instance of Factory class.");
            }

            private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(Factory.class);
            private static final ObjectMapper MAPPER = new ObjectMapper();

            public static  Client create(final Method method) {
                @SuppressWarnings("unchecked")
                final Class methodResult = (Class) method.getReturnType();
                final JavaType type;
                if (void.class.equals(methodResult)) {
                    type = new ObjectMapper().getTypeFactory().constructParametrizedType(ResponseWrapper.class, ResponseWrapper.class, Object.class);
                } else {
                    type = new ObjectMapper().getTypeFactory().constructParametrizedType(ResponseWrapper.class, ResponseWrapper.class,
                            new ObjectMapper().getTypeFactory().constructType(method.getGenericReturnType()));
                }

                return result -> {
                    final ResponseWrapper wrapper;
                    LOGGER.info("Adapting result from wrapper [{}]", result);
                    try {
                        wrapper = MAPPER.readValue(result, type);
                    } catch (final IOException e) {
                        LOGGER.warn("Failed to adapt result [{}] from response.", result, e);
                        throw RestFactoryException.create(Strings.format("Failed to adapt result [{}] from response.", result), e);
                    }
                    if (wrapper.isSuccess()) {
                        return wrapper.getResult();
                    } else {
                        throw RestExceptionAdaptor.Client.Factory.create(wrapper.getException());
                    }
                };
            }
        }
    }

    interface Service  extends Function> {
        final class Factory {
            private Factory() {
                throw RestFactoryException.create("Can not construct instance of Factory class.");
            }

            private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(Factory.class);
            private static final ObjectMapper MAPPER = new ObjectMapper();

            public static  Service create() {
                return result -> (response -> {
                    response.setStatus(HttpServletResponse.SC_OK);
                    response.setHeader("Content-Type", "application/json");
                    try {
                        LOGGER.debug("Adapting result [{}] to response.", result);
                        MAPPER.writeValue(response.getWriter(), ResponseWrapper.create(result));
                    } catch (final IOException e) {
                        LOGGER.warn("Failed to adapt result [{}] to response.", result, e);
                        throw RestFactoryException.create(Strings.format("Failed to adapt result [{}] to response.", result), e);
                    }
                });
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy