com.github.tcurrie.rest.factory.RestExceptionAdaptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rest.factory Show documentation
Show all versions of rest.factory Show documentation
Simple rest client-server factory, you give it a url, it does the rest!
package com.github.tcurrie.rest.factory;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
public interface RestExceptionAdaptor {
interface Client {
class Factory {
private static final Logger LOGGER = Logger.getLogger(Client.class.getName());
public static Throwable create(final ResponseWrapper wrapper) {
try {
return (Throwable) wrapper.getExceptionType().getConstructor(String.class).newInstance(wrapper.getException());
} catch (final Exception e) {
throw RestFactoryException.create(LOGGER, Level.WARNING, "Failed to adapt result [{0}] to response.", e, wrapper.getException());
}
}
}
}
interface Service {
class Factory {
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final Logger LOGGER = Logger.getLogger(Service.class.getName());
public static Consumer apply(final Throwable exception) {
return response -> {
response.setStatus(HttpServletResponse.SC_OK);
response.setHeader("Content-Type", "application/json");
try {
LOGGER.log(Level.FINE, "Adapting exception result [{0}] to response.", new Object[]{exception});
MAPPER.writeValue(response.getWriter(), ResponseWrapper.createException(exception));
} catch (final IOException e) {
throw RestFactoryException.create(LOGGER, Level.WARNING, "Failed to adapt result [{0}] to response.", e, exception);
}
};
}
}
}
}