Search in sources :

Example 1 with MissingExporterException

use of org.apache.sling.models.factory.MissingExporterException 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 MissingExporterException

use of org.apache.sling.models.factory.MissingExporterException in project sling by apache.

the class ExporterTest method testFailedExport.

@Test
public void testFailedExport() throws Exception {
    boolean thrown = false;
    try {
        ResourceResolver resolver = null;
        try {
            resolver = rrFactory.getAdministrativeResourceResolver(null);
            final Resource baseComponentResource = resolver.getResource(baseComponentPath);
            Assert.assertNotNull(baseComponentResource);
            String data = modelFactory.exportModelForResource(baseComponentResource, "jaxb", String.class, Collections.<String, String>emptyMap());
            Assert.fail("Should have thrown missing serializer error.");
        } finally {
            if (resolver != null && resolver.isLive()) {
                resolver.close();
            }
        }
    } catch (MissingExporterException e) {
        thrown = true;
        Assert.assertEquals("No exporter named jaxb supports java.lang.String.", e.getMessage());
    }
    Assert.assertTrue(thrown);
}
Also used : MissingExporterException(org.apache.sling.models.factory.MissingExporterException) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Aggregations

MissingExporterException (org.apache.sling.models.factory.MissingExporterException)2 Resource (org.apache.sling.api.resource.Resource)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)1 ExportException (org.apache.sling.models.factory.ExportException)1 ScriptHelper (org.apache.sling.scripting.core.ScriptHelper)1 Test (org.junit.Test)1