You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ol...@apache.org on 2015/03/19 23:46:44 UTC
svn commit: r1667896 - in
/sling/trunk/samples/org.apache.sling.samples.jcr.contentloader: ./ src/
src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/
src/main/java/org/apache/sling/ src/main/java/org/apache/sling/samples/
src/main/ja...
Author: olli
Date: Thu Mar 19 22:46:43 2015
New Revision: 1667896
URL: http://svn.apache.org/r1667896
Log:
SLING-4526 add sample for custom content readers
Added:
sling/trunk/samples/org.apache.sling.samples.jcr.contentloader/
sling/trunk/samples/org.apache.sling.samples.jcr.contentloader/README.md
sling/trunk/samples/org.apache.sling.samples.jcr.contentloader/pom.xml
sling/trunk/samples/org.apache.sling.samples.jcr.contentloader/src/
sling/trunk/samples/org.apache.sling.samples.jcr.contentloader/src/main/
sling/trunk/samples/org.apache.sling.samples.jcr.contentloader/src/main/java/
sling/trunk/samples/org.apache.sling.samples.jcr.contentloader/src/main/java/org/
sling/trunk/samples/org.apache.sling.samples.jcr.contentloader/src/main/java/org/apache/
sling/trunk/samples/org.apache.sling.samples.jcr.contentloader/src/main/java/org/apache/sling/
sling/trunk/samples/org.apache.sling.samples.jcr.contentloader/src/main/java/org/apache/sling/samples/
sling/trunk/samples/org.apache.sling.samples.jcr.contentloader/src/main/java/org/apache/sling/samples/jcr/
sling/trunk/samples/org.apache.sling.samples.jcr.contentloader/src/main/java/org/apache/sling/samples/jcr/contentloader/
sling/trunk/samples/org.apache.sling.samples.jcr.contentloader/src/main/java/org/apache/sling/samples/jcr/contentloader/internal/
sling/trunk/samples/org.apache.sling.samples.jcr.contentloader/src/main/java/org/apache/sling/samples/jcr/contentloader/internal/GsonReader.java
Added: sling/trunk/samples/org.apache.sling.samples.jcr.contentloader/README.md
URL: http://svn.apache.org/viewvc/sling/trunk/samples/org.apache.sling.samples.jcr.contentloader/README.md?rev=1667896&view=auto
==============================================================================
--- sling/trunk/samples/org.apache.sling.samples.jcr.contentloader/README.md (added)
+++ sling/trunk/samples/org.apache.sling.samples.jcr.contentloader/README.md Thu Mar 19 22:46:43 2015
@@ -0,0 +1,4 @@
+Sling JCR Content Loader Sample
+===============================
+
+Basic sample to show how to provide a custom content reader and use it instead of the default one for JSON.
Added: sling/trunk/samples/org.apache.sling.samples.jcr.contentloader/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/samples/org.apache.sling.samples.jcr.contentloader/pom.xml?rev=1667896&view=auto
==============================================================================
--- sling/trunk/samples/org.apache.sling.samples.jcr.contentloader/pom.xml (added)
+++ sling/trunk/samples/org.apache.sling.samples.jcr.contentloader/pom.xml Thu Mar 19 22:46:43 2015
@@ -0,0 +1,117 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>sling</artifactId>
+ <version>22</version>
+ <relativePath>../../parent/pom.xml</relativePath>
+ </parent>
+
+ <groupId>org.apache.sling.samples</groupId>
+ <artifactId>org.apache.sling.samples.jcr.contentloader</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ <packaging>bundle</packaging>
+
+ <name>Apache Sling JCR Content Loader Sample</name>
+ <description>sample providing custom reader for Sling JCR Content Loader</description>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+ <sling.java.version>7</sling.java.version>
+ </properties>
+
+ <dependencies>
+ <!-- javax -->
+ <dependency>
+ <groupId>javax.jcr</groupId>
+ <artifactId>jcr</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <!-- OSGi -->
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.core</artifactId>
+ <version>4.3.1</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.compendium</artifactId>
+ <version>4.3.1</version>
+ <scope>provided</scope>
+ </dependency>
+ <!-- Apache Commons -->
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>2.4</version>
+ <scope>provided</scope>
+ </dependency>
+ <!-- Apache Sling -->
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.jcr.contentloader</artifactId>
+ <version>2.1.11-SNAPSHOT</version>
+ <scope>provided</scope>
+ </dependency>
+ <!-- Google Gson -->
+ <dependency>
+ <groupId>com.google.code.gson</groupId>
+ <artifactId>gson</artifactId>
+ <version>2.3.1</version>
+ <scope>provided</scope>
+ </dependency>
+ <!-- logging -->
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>3.1</version>
+ <configuration>
+ <source>1.7</source>
+ <target>1.7</target>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <extensions>true</extensions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-scr-plugin</artifactId>
+ </plugin>
+ </plugins>
+ </build>
+
+</project>
Added: sling/trunk/samples/org.apache.sling.samples.jcr.contentloader/src/main/java/org/apache/sling/samples/jcr/contentloader/internal/GsonReader.java
URL: http://svn.apache.org/viewvc/sling/trunk/samples/org.apache.sling.samples.jcr.contentloader/src/main/java/org/apache/sling/samples/jcr/contentloader/internal/GsonReader.java?rev=1667896&view=auto
==============================================================================
--- sling/trunk/samples/org.apache.sling.samples.jcr.contentloader/src/main/java/org/apache/sling/samples/jcr/contentloader/internal/GsonReader.java (added)
+++ sling/trunk/samples/org.apache.sling.samples.jcr.contentloader/src/main/java/org/apache/sling/samples/jcr/contentloader/internal/GsonReader.java Thu Mar 19 22:46:43 2015
@@ -0,0 +1,140 @@
+/*
+ * 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.samples.jcr.contentloader.internal;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonObject;
+import org.apache.commons.io.IOUtils;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Modified;
+import org.apache.felix.scr.annotations.Properties;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.PropertyUnbounded;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.jcr.contentloader.BaseContentReader;
+import org.apache.sling.jcr.contentloader.ContentCreator;
+import org.apache.sling.jcr.contentloader.ContentReader;
+import org.osgi.framework.Constants;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Component(
+ label = "Apache Sling JCR Content Loader Sample GsonReader",
+ description = "parses JSON content with Gson",
+ immediate = true,
+ metatype = true
+)
+@Service
+@Properties({
+ @Property(
+ name = Constants.SERVICE_VENDOR,
+ value = "The Apache Software Foundation"
+ ),
+ @Property(
+ name = Constants.SERVICE_DESCRIPTION,
+ value = "Apache Sling JCR Content Loader Sample GsonReader"
+ ),
+ @Property(
+ name = ContentReader.PROPERTY_EXTENSIONS,
+ value = {
+ "json"
+ },
+ unbounded = PropertyUnbounded.ARRAY
+ ),
+ @Property(
+ name = ContentReader.PROPERTY_CONTENTTYPES,
+ value = {
+ "application/json"
+ },
+ unbounded = PropertyUnbounded.ARRAY
+ ),
+ @Property(
+ name = Constants.SERVICE_RANKING,
+ intValue = 1,
+ propertyPrivate = false
+ )
+})
+public final class GsonReader extends BaseContentReader implements ContentReader {
+
+ private GsonBuilder gsonBuilder;
+
+ private final Logger logger = LoggerFactory.getLogger(GsonReader.class);
+
+ public GsonReader() {
+ }
+
+ @Activate
+ protected void activate(final ComponentContext componentContext) {
+ configure(componentContext);
+ gsonBuilder = new GsonBuilder();
+ }
+
+ @Modified
+ protected void modified(final ComponentContext componentContext) {
+ configure(componentContext);
+ }
+
+ @Deactivate
+ protected void deactivate(final ComponentContext componentContext) {
+ extensions = null;
+ contentTypes = null;
+ gsonBuilder = null;
+ }
+
+ @Override
+ public void parse(final URL url, final ContentCreator contentCreator) throws IOException, RepositoryException {
+ InputStream inputStream = null;
+ try {
+ inputStream = url.openStream();
+ parse(inputStream, contentCreator);
+ } finally {
+ IOUtils.closeQuietly(inputStream);
+ }
+ }
+
+ /**
+ * TODO well, implement real parsing...
+ */
+ @Override
+ public void parse(final InputStream inputStream, final ContentCreator contentCreator) throws IOException, RepositoryException {
+ final Gson gson = gsonBuilder.create();
+ final Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
+ final JsonObject jsonObject = gson.fromJson(reader, JsonObject.class);
+ final String json = jsonObject.toString();
+ logger.debug("json: {}", json);
+ contentCreator.createNode(null, "nt:unstructured", null);
+ contentCreator.createProperty("json", PropertyType.STRING, json);
+ contentCreator.finishNode();
+ }
+
+}