Search in sources :

Example 1 with ExportException

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();
    }
}
Also used : MissingExporterException(org.apache.sling.models.factory.MissingExporterException) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) ScriptHelper(org.apache.sling.scripting.core.ScriptHelper) ExportException(org.apache.sling.models.factory.ExportException)

Example 2 with ExportException

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;
    }
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) SerializableString(com.fasterxml.jackson.core.SerializableString) IOException(java.io.IOException) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) ExportException(org.apache.sling.models.factory.ExportException) ModuleProvider(org.apache.sling.models.jacksonexporter.ModuleProvider) StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) MapperFeature(com.fasterxml.jackson.databind.MapperFeature) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ExportException (org.apache.sling.models.factory.ExportException)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 SerializableString (com.fasterxml.jackson.core.SerializableString)1 MapperFeature (com.fasterxml.jackson.databind.MapperFeature)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SerializationFeature (com.fasterxml.jackson.databind.SerializationFeature)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Map (java.util.Map)1 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)1 MissingExporterException (org.apache.sling.models.factory.MissingExporterException)1 ModuleProvider (org.apache.sling.models.jacksonexporter.ModuleProvider)1 ScriptHelper (org.apache.sling.scripting.core.ScriptHelper)1