use of org.apache.sling.models.factory.ExportException in project sling by apache.
the class ExportServlet method doGet.
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
Map<String, String> options = createOptionMap(request);
ScriptHelper scriptHelper = new ScriptHelper(bundleContext, null, request, response);
try {
addScriptBindings(scriptHelper, request, response);
String exported;
try {
exported = accessor.getExportedString(request, options, modelFactory, exporterName);
} catch (ExportException e) {
logger.error("Could not perform export with " + exporterName + " requested by model.", e);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
} catch (MissingExporterException e) {
logger.error("Could not get exporter " + exporterName + " requested by model.", e);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
if (exported == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
response.setContentType(request.getResponseContentType());
response.getWriter().write(exported);
} finally {
scriptHelper.cleanup();
}
}
use of org.apache.sling.models.factory.ExportException in project sling by apache.
the class JacksonExporter method export.
@Override
public <T> T export(@Nonnull Object model, @Nonnull Class<T> clazz, @Nonnull Map<String, String> options) throws ExportException {
ObjectMapper mapper = new ObjectMapper();
for (Map.Entry<String, String> optionEntry : options.entrySet()) {
String key = optionEntry.getKey();
if (key.startsWith(SERIALIZATION_FEATURE_PREFIX)) {
String enumName = key.substring(SERIALIZATION_FEATURE_PREFIX_LENGTH);
try {
SerializationFeature feature = SerializationFeature.valueOf(enumName);
mapper.configure(feature, Boolean.valueOf(optionEntry.getValue()));
} catch (IllegalArgumentException e) {
log.warn("Bad SerializationFeature option");
}
} else if (key.startsWith(MAPPER_FEATURE_PREFIX)) {
String enumName = key.substring(MAPPER_FEATURE_PREFIX_LENGTH);
try {
MapperFeature feature = MapperFeature.valueOf(enumName);
mapper.configure(feature, Boolean.valueOf(optionEntry.getValue()));
} catch (IllegalArgumentException e) {
log.warn("Bad SerializationFeature option");
}
}
}
for (ModuleProvider moduleProvider : moduleProviders) {
mapper.registerModule(moduleProvider.getModule());
}
if (clazz.equals(Map.class)) {
return (T) mapper.convertValue(model, Map.class);
} else if (clazz.equals(String.class)) {
final JsonFactory f = new JsonFactory();
f.setCharacterEscapes(new EscapeCloseScriptBlocks());
StringWriter writer = new StringWriter();
JsonGenerator jgen;
final boolean printTidy;
if (options.containsKey("tidy")) {
printTidy = Boolean.valueOf(options.get("tidy"));
} else {
printTidy = false;
}
try {
jgen = f.createGenerator(writer);
if (printTidy) {
mapper.writerWithDefaultPrettyPrinter().writeValue(jgen, model);
} else {
mapper.writeValue(jgen, model);
}
} catch (final IOException e) {
throw new ExportException(e);
}
return (T) writer.toString();
} else {
return null;
}
}
Aggregations