Skip to content

Instantly share code, notes, and snippets.

@chrismrgn
Last active July 14, 2016 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrismrgn/ed1c13c3ec72e8c6a813705b8bfc2317 to your computer and use it in GitHub Desktop.
Save chrismrgn/ed1c13c3ec72e8c6a813705b8bfc2317 to your computer and use it in GitHub Desktop.
DataBinder, based on the DD4T 2 Java pattern, to get other types of pages from SDL
public interface CustomDataBinder {
<T extends Sitemap> T buildSitemap(String source, final Class<T> aClass) throws SerializationException;
}
public class CustomDataBinderImpl implements CustomDataBinder {
private static final Logger LOG = LoggerFactory.getLogger(CustomDataBinderImpl.class);
private static final ObjectMapper GENERIC_MAPPER = new ObjectMapper();
static {
GENERIC_MAPPER.configure(MapperFeature.IGNORE_DUPLICATE_MODULE_REGISTRATIONS,true);
GENERIC_MAPPER.registerModule(new JodaModule());
GENERIC_MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
}
private static final CustomDataBinderImpl INSTANCE = new CustomDataBinderImpl();
private CustomDataBinderImpl () {
LOG.info("CustomDataBinderImpl init.");
}
public static CustomDataBinderImpl getInstance () {
if (null == INSTANCE) {
LOG.error("DataBinderImpl not properly instantiated!");
}
return INSTANCE;
}
@Override
public <T extends Sitemap> T buildSitemap(String source, final Class<T> aClass) throws SerializationException {
try {
return GENERIC_MAPPER.readValue(source, aClass);
} catch (IOException e) {
LOG.error(DataBindConstants.MESSAGE_ERROR_DESERIALIZING, e);
throw new SerializationException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment