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;
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
Aggregations