This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.testing.sling-mock-2.2.10 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-sling-mock.git
commit 7e3d73c20b70274d268d9925a154f52534e9dd27 Author: Stefan Seifert <[email protected]> AuthorDate: Fri Mar 17 22:24:27 2017 +0000 SLING-6595 switch to latest contentparser API git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/sling-mock@1787513 13f79535-47bb-0310-9956-ffa450edef68 --- .../testing/mock/sling/loader/ContentLoader.java | 44 +--------- .../mock/sling/loader/LoaderContentHandler.java | 94 ++++++++++++++++++++++ 2 files changed, 97 insertions(+), 41 deletions(-) diff --git a/src/main/java/org/apache/sling/testing/mock/sling/loader/ContentLoader.java b/src/main/java/org/apache/sling/testing/mock/sling/loader/ContentLoader.java index 73120f9..b205812 100644 --- a/src/main/java/org/apache/sling/testing/mock/sling/loader/ContentLoader.java +++ b/src/main/java/org/apache/sling/testing/mock/sling/loader/ContentLoader.java @@ -18,7 +18,6 @@ */ package org.apache.sling.testing.mock.sling.loader; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; @@ -27,7 +26,6 @@ import java.util.Set; import org.apache.commons.lang3.StringUtils; import org.apache.jackrabbit.JcrConstants; -import org.apache.sling.api.resource.ModifiableValueMap; import org.apache.sling.api.resource.PersistenceException; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceResolver; @@ -51,7 +49,6 @@ import com.google.common.collect.ImmutableSet; public final class ContentLoader { private static final String CONTENTTYPE_OCTET_STREAM = "application/octet-stream"; - private static final String JCR_DATA_PLACEHOLDER = ":jcr:data"; private static final Set<String> IGNORED_NAMES = ImmutableSet.of( JcrConstants.JCR_MIXINTYPES, @@ -177,12 +174,12 @@ public final class ContentLoader { throw new IllegalArgumentException("Resource does already exist: " + destPath); } - Map<String,Object> content = JSON_PARSER.parse(inputStream); - Resource resource = this.createResource(parentResource, childName, content); + LoaderContentHandler contentHandler = new LoaderContentHandler(destPath, resourceResolver); + JSON_PARSER.parse(contentHandler, inputStream); if (autoCommit) { resourceResolver.commit(); } - return resource; + return resourceResolver.getResource(destPath); } catch (ParseException ex) { throw new RuntimeException(ex); } catch (IOException ex) { @@ -208,41 +205,6 @@ public final class ContentLoader { } } - @SuppressWarnings("unchecked") - private Resource createResource(Resource parentResource, String childName, Map<String,Object> content) throws IOException { - - // collect all properties first - boolean hasJcrData = false; - Map<String, Object> props = new HashMap<String, Object>(); - for (Map.Entry<String,Object> entry : content.entrySet()) { - final String name = entry.getKey(); - if (StringUtils.equals(name, JCR_DATA_PLACEHOLDER)) { - hasJcrData = true; - } - else if (!(entry.getValue() instanceof Map)) { - props.put(name, entry.getValue()); - } - } - - // create resource - Resource resource = resourceResolver.create(parentResource, childName, props); - - if (hasJcrData) { - ModifiableValueMap valueMap = resource.adaptTo(ModifiableValueMap.class); - // we cannot import binary data here - but to avoid complaints by JCR we create it with empty binary data - valueMap.put(JcrConstants.JCR_DATA, new ByteArrayInputStream(new byte[0])); - } - - // add child resources - for (Map.Entry<String,Object> entry : content.entrySet()) { - if (entry.getValue() instanceof Map) { - createResource(resource, entry.getKey(), (Map<String,Object>)entry.getValue()); - } - } - - return resource; - } - /** * Import binary file as nt:file binary node into repository. Auto-creates * parent hierarchies as nt:unstrucured nodes if missing. Mime type is diff --git a/src/main/java/org/apache/sling/testing/mock/sling/loader/LoaderContentHandler.java b/src/main/java/org/apache/sling/testing/mock/sling/loader/LoaderContentHandler.java new file mode 100644 index 0000000..9077cd4 --- /dev/null +++ b/src/main/java/org/apache/sling/testing/mock/sling/loader/LoaderContentHandler.java @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.sling.testing.mock.sling.loader; + +import java.io.ByteArrayInputStream; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.lang3.StringUtils; +import org.apache.jackrabbit.JcrConstants; +import org.apache.sling.api.resource.ModifiableValueMap; +import org.apache.sling.api.resource.PersistenceException; +import org.apache.sling.api.resource.Resource; +import org.apache.sling.api.resource.ResourceResolver; +import org.apache.sling.api.resource.ResourceUtil; +import org.apache.sling.jcr.contentparser.ContentHandler; + +final class LoaderContentHandler implements ContentHandler { + + private static final String JCR_DATA_PLACEHOLDER = ":jcr:data"; + + private final String rootPath; + private final ResourceResolver resourceResolver; + + public LoaderContentHandler(String rootPath, ResourceResolver resourceResolver) { + this.rootPath = rootPath; + this.resourceResolver = resourceResolver; + } + + @Override + public void resource(String path, Map<String, Object> properties) { + String fullPath = rootPath; + if (!StringUtils.equals(path, "/")) { + fullPath += path; + } + String parentPath = ResourceUtil.getParent(fullPath); + String name = ResourceUtil.getName(fullPath); + + Resource parentResource = resourceResolver.getResource(parentPath); + if (parentResource == null) { + throw new RuntimeException("Parent resource '" + parentPath + "' not found."); + } + try { + createResource(parentResource, name, properties); + } + catch (PersistenceException ex) { + throw new RuntimeException("Unable to create resource at '" + fullPath + "'.", ex); + } + } + + private Resource createResource(Resource parentResource, String childName, Map<String,Object> content) throws PersistenceException { + + // collect all properties first + boolean hasJcrData = false; + Map<String, Object> props = new HashMap<String, Object>(); + for (Map.Entry<String,Object> entry : content.entrySet()) { + final String name = entry.getKey(); + if (StringUtils.equals(name, JCR_DATA_PLACEHOLDER)) { + hasJcrData = true; + } + else { + props.put(name, entry.getValue()); + } + } + + // create resource + Resource resource = resourceResolver.create(parentResource, childName, props); + + if (hasJcrData) { + ModifiableValueMap valueMap = resource.adaptTo(ModifiableValueMap.class); + // we cannot import binary data here - but to avoid complaints by JCR we create it with empty binary data + valueMap.put(JcrConstants.JCR_DATA, new ByteArrayInputStream(new byte[0])); + } + + return resource; + } + +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
