Search in sources :

Example 51 with ResourceResolver

use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.

the class ValidationModelRetrieverImpl method getValidationModel.

/*
     * (non-Javadoc)
     * 
     * @see org.apache.sling.validation.impl.ValidationModelRetriever#getModels(java.lang.String, java.lang.String)
     */
@CheckForNull
public ValidationModel getValidationModel(@Nonnull String resourceType, String resourcePath, boolean considerResourceSuperTypeModels) {
    // first get model for exactly the requested resource type
    ValidationModel baseModel = getModel(resourceType, resourcePath);
    String currentResourceType = resourceType;
    if (considerResourceSuperTypeModels) {
        Collection<ValidationModel> modelsToMerge = new ArrayList<ValidationModel>();
        ResourceResolver resourceResolver = null;
        try {
            resourceResolver = resourceResolverFactory.getServiceResourceResolver(null);
            while ((currentResourceType = resourceResolver.getParentResourceType(currentResourceType)) != null) {
                LOG.debug("Retrieving validation models for resource super type {}...", currentResourceType);
                ValidationModel modelToMerge = getModel(currentResourceType, resourcePath);
                if (modelToMerge != null) {
                    if (baseModel == null) {
                        baseModel = modelToMerge;
                    } else {
                        modelsToMerge.add(modelToMerge);
                    }
                }
            }
            if (!modelsToMerge.isEmpty()) {
                return new MergedValidationModel(baseModel, modelsToMerge.toArray(new ValidationModel[modelsToMerge.size()]));
            }
        } catch (LoginException e) {
            throw new IllegalStateException("Could not get service resource resolver", e);
        } finally {
            if (resourceResolver != null) {
                resourceResolver.close();
            }
        }
    }
    return baseModel;
}
Also used : MergedValidationModel(org.apache.sling.validation.impl.model.MergedValidationModel) ValidationModel(org.apache.sling.validation.model.ValidationModel) MergedValidationModel(org.apache.sling.validation.impl.model.MergedValidationModel) ArrayList(java.util.ArrayList) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) LoginException(org.apache.sling.api.resource.LoginException) CheckForNull(javax.annotation.CheckForNull)

Example 52 with ResourceResolver

use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.

the class ExporterTest method testExportToJSON.

@Test
public void testExportToJSON() throws Exception {
    ResourceResolver resolver = null;
    try {
        resolver = rrFactory.getAdministrativeResourceResolver(null);
        final Resource baseComponentResource = resolver.getResource(baseComponentPath);
        Assert.assertNotNull(baseComponentResource);
        String jsonData = modelFactory.exportModelForResource(baseComponentResource, "jackson", String.class, Collections.<String, String>emptyMap());
        Assert.assertTrue("JSON Data should contain the property value", StringUtils.contains(jsonData, "baseTESTValue"));
        JsonObject parsed = Json.createReader(new StringReader(jsonData)).readObject();
        JsonObject resource = parsed.getJsonObject("resource");
        Assert.assertEquals(3, resource.getJsonArray("sampleArray").size());
        Assert.assertEquals(1.0d, resource.getJsonNumber("sampleDoubleValue").doubleValue(), .1);
        Assert.assertEquals(2, resource.getJsonArray(":sampleBinaryArray").size());
        Assert.assertTrue(resource.getBoolean("sampleBooleanValue"));
        Assert.assertEquals(1, resource.getInt("sampleLongValue"));
        Assert.assertEquals(3, resource.getInt(":sampleBinary"));
        Assert.assertEquals(0, resource.getJsonArray("sampleEmptyArray").size());
        final Resource extendedComponentResource = resolver.getResource(extendedComponentPath);
        Assert.assertNotNull(extendedComponentResource);
        jsonData = modelFactory.exportModelForResource(extendedComponentResource, "jackson", String.class, Collections.<String, String>emptyMap());
        Assert.assertTrue("JSON Data should contain the property value", StringUtils.contains(jsonData, "extendedTESTValue"));
        final Resource interfaceComponentResource = resolver.getResource(interfaceComponentPath);
        Assert.assertNotNull(baseComponentResource);
        jsonData = modelFactory.exportModelForResource(interfaceComponentResource, "jackson", String.class, Collections.<String, String>emptyMap());
        Assert.assertTrue("JSON Data should contain the property value", StringUtils.contains(jsonData, "interfaceTESTValue"));
    } finally {
        if (resolver != null && resolver.isLive()) {
            resolver.close();
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 53 with ResourceResolver

use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.

the class ExporterTest method testResourceServlets.

@Test
public void testResourceServlets() throws Exception {
    ResourceResolver resolver = null;
    try {
        resolver = rrFactory.getAdministrativeResourceResolver(null);
        FakeResponse response = new FakeResponse();
        slingRequestProcessor.processRequest(new FakeRequest(baseComponentPath + ".model.json"), response, resolver);
        JsonObject obj = Json.createReader(new StringReader((response.getStringWriter().toString()))).readObject();
        Assert.assertEquals("application/json", response.getContentType());
        Assert.assertEquals("BASETESTVALUE", obj.getString("UPPER"));
        Assert.assertEquals(baseComponentPath, obj.getString("id"));
        response = new FakeResponse();
        slingRequestProcessor.processRequest(new FakeRequest(extendedComponentPath + ".model.json"), response, resolver);
        obj = Json.createReader(new StringReader((response.getStringWriter().toString()))).readObject();
        Assert.assertEquals("application/json", response.getContentType());
        Assert.assertEquals(extendedComponentPath, obj.getString("id"));
        Assert.assertEquals(testDate.getTimeInMillis(), obj.getJsonNumber("date").longValue());
        response = new FakeResponse();
        slingRequestProcessor.processRequest(new FakeRequest(interfaceComponentPath + ".model.json"), response, resolver);
        obj = Json.createReader(new StringReader((response.getStringWriter().toString()))).readObject();
        Assert.assertEquals("application/json", response.getContentType());
        Assert.assertEquals(interfaceComponentPath, obj.getString("id"));
        Assert.assertEquals("interfaceTESTValue", obj.getString("sampleValue"));
    } finally {
        if (resolver != null && resolver.isLive()) {
            resolver.close();
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 54 with ResourceResolver

use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.

the class ResourceTypeBindingTest method testClientModelCreateFromResource.

@Test
public void testClientModelCreateFromResource() throws LoginException {
    ResourceResolver resolver = null;
    try {
        resolver = rrFactory.getAdministrativeResourceResolver(null);
        final Resource baseComponentResource = resolver.getResource(baseComponentPath);
        Assert.assertNotNull(baseComponentResource);
        final Object baseModel = modelFactory.getModelFromResource(baseComponentResource);
        Assert.assertNotNull("Model should not be null", baseModel);
        Assert.assertTrue("Model should be a BaseComponent", baseModel instanceof BaseComponent);
        final Resource childComponentResource = resolver.getResource(childComponentPath);
        Assert.assertNotNull(childComponentResource);
        final Object childModel = modelFactory.getModelFromResource(childComponentResource);
        Assert.assertNotNull("Model should not be null", childModel);
        Assert.assertTrue("Model should be a BaseComponent", childModel instanceof BaseComponent);
        final Resource child2ComponentResource = resolver.getResource(child2ComponentPath);
        Assert.assertNotNull(child2ComponentResource);
        final Object child2Model = modelFactory.getModelFromResource(child2ComponentResource);
        Assert.assertNotNull("Model should not be null", child2Model);
        Assert.assertTrue("Model should be a BaseComponent", child2Model instanceof BaseComponent);
        final Resource extendedComponentResource = resolver.getResource(extendedComponentPath);
        Assert.assertNotNull(extendedComponentResource);
        final Object extendedModel = modelFactory.getModelFromResource(extendedComponentResource);
        Assert.assertNotNull("Model should not be null", extendedModel);
        Assert.assertTrue("Model should be a BaseComponent", extendedModel instanceof BaseComponent);
        Assert.assertTrue("Model should be an ExtendedComponent", extendedModel instanceof ExtendedComponent);
    } finally {
        if (resolver != null && resolver.isLive()) {
            resolver.close();
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 55 with ResourceResolver

use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.

the class ResourceTypePickerTest method setup.

@Before
public void setup() throws LoginException, PersistenceException {
    ResourceResolver adminResolver = null;
    try {
        adminResolver = rrFactory.getAdministrativeResourceResolver(null);
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put(SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_TYPE, "sling/rtpicker/base");
        ResourceUtil.getOrCreateResource(adminResolver, baseComponentPath, properties, null, false);
        properties.clear();
        properties.put(SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_TYPE, "sling/rtpicker/sub");
        properties.put(SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_SUPER_TYPE, "sling/rtpicker/base");
        ResourceUtil.getOrCreateResource(adminResolver, childComponentPath, properties, null, false);
        properties.clear();
        adminResolver.commit();
    } finally {
        if (adminResolver != null && adminResolver.isLive()) {
            adminResolver.close();
        }
    }
}
Also used : HashMap(java.util.HashMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Before(org.junit.Before)

Aggregations

ResourceResolver (org.apache.sling.api.resource.ResourceResolver)339 Resource (org.apache.sling.api.resource.Resource)168 Test (org.junit.Test)131 HashMap (java.util.HashMap)65 LoginException (org.apache.sling.api.resource.LoginException)53 PersistenceException (org.apache.sling.api.resource.PersistenceException)52 Session (javax.jcr.Session)31 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)29 ValueMap (org.apache.sling.api.resource.ValueMap)27 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)26 ArrayList (java.util.ArrayList)23 DistributionRequest (org.apache.sling.distribution.DistributionRequest)23 DistributionPackage (org.apache.sling.distribution.packaging.DistributionPackage)21 Map (java.util.Map)19 Before (org.junit.Before)19 IOException (java.io.IOException)17 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)17 ChildResource (org.apache.sling.validation.model.ChildResource)17 HashSet (java.util.HashSet)16 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)15