You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 10:25:17 UTC
[sling-org-apache-sling-testing-sling-mock] 10/14: SLING-6595
switch to latest contentparser API
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-1.9.6
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-sling-mock.git
commit 29ca6a10c06dee280a9d60a916f2834fbb539436
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Fri Mar 17 22:28:32 2017 +0000
SLING-6595 switch to latest contentparser API
git-svn-id: https://svn.apache.org/repos/asf/sling/branches/testing/mocks/sling-mock-1.x@1787514 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 ac6de5c..d21d49d 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
"commits@sling.apache.org" <co...@sling.apache.org>.