You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by Sam Lee <sk...@gmail.com> on 2014/08/14 23:45:12 UTC

easy way to dump JSON from JCR Node or Resource?

Hi,

I want to dump all JCR content to mongodb for analysis.
I see there is  https://issues.apache.org/jira/browse/SLING-3485 .
And org.apache.sling.servlets.get.impl.helpers.ResourceTraversor  does what
I want to do.

But I am stuck with older version of Sling.
Are there other ways to dump JSON of  a node tree?

I have the following code but it cannot be compiled because
ResourceTraversor cannot be found:

final Pattern ENTRY = Pattern.compile("/\\d+/\\d+/[^/]+/jcr:content$");
class JsonDumpNodeVisitor extends TraversingItemVisitor {
    private final ResourceResolver resolver;
    private final PrintWriter writer;

    public JsonDumpNodeVisitor(ResourceResolver resolver, PrintWriter
writer) {
        this.resolver = resolver;
        this.writer = writer;
    }

    @Override
    protected void entering(Property property, int level) throws
RepositoryException {
    }

    @Override
    protected void entering(Node node, int level) throws
RepositoryException {
        final String nodeType = node.getPrimaryNodeType().getName();
        final String path = node.getPath();
        final Matcher m = ENTRY.matcher(path);

        if (m.find() && "cq:PageContent".equals(nodeType)) {
            final ResourceTraversor traversor = new ResourceTraversor(-1,
Long.MAX_VALUE, resolver.getResource(path), false);
            traversor.collectResources();
            writer.write(traversor.getJSONObject().toString());
        }
    }

    @Override
    protected void leaving(Property property, int level) throws
RepositoryException {
    }

    @Override
    protected void leaving(Node node, int level) throws RepositoryException
{
    }
}

final Node node =
resourceResolver.getResource(rootPath).adaptTo(Node.class);
final TraversingItemVisitor visitor = new
JsonDumpNodeVisotor(resourceResolver, out);
node.accept(visitor);

Re: easy way to dump JSON from JCR Node or Resource?

Posted by Sam Lee <sk...@gmail.com>.
Ended up writing my own Node to mongo db object (json) routine :P
https://github.com/saml/jackrabbit-mongodb-dump/blob/eae1074e2cb59398348becb7d66dfdf4095ac241/src/main/scala/saml/mongo/package.scala#L21-L26

If anyone wants to dump JCR content into Mongo, here is how to use it:
https://github.com/saml/jackrabbit-mongodb-dump







On Fri, Aug 15, 2014 at 12:06 PM, Carsten Ziegeler <cz...@apache.org>
wrote:

> The code has recently moved to the commons json bundle, the easiest way
> would be to update that bundle. If that's not possible, just embed the
> classes in your bundle.
>
> Carsten
>
>
> 2014-08-14 23:45 GMT+02:00 Sam Lee <sk...@gmail.com>:
>
> > Hi,
> >
> > I want to dump all JCR content to mongodb for analysis.
> > I see there is  https://issues.apache.org/jira/browse/SLING-3485 .
> > And org.apache.sling.servlets.get.impl.helpers.ResourceTraversor  does
> what
> > I want to do.
> >
> > But I am stuck with older version of Sling.
> > Are there other ways to dump JSON of  a node tree?
> >
> > I have the following code but it cannot be compiled because
> > ResourceTraversor cannot be found:
> >
> > final Pattern ENTRY = Pattern.compile("/\\d+/\\d+/[^/]+/jcr:content$");
> > class JsonDumpNodeVisitor extends TraversingItemVisitor {
> >     private final ResourceResolver resolver;
> >     private final PrintWriter writer;
> >
> >     public JsonDumpNodeVisitor(ResourceResolver resolver, PrintWriter
> > writer) {
> >         this.resolver = resolver;
> >         this.writer = writer;
> >     }
> >
> >     @Override
> >     protected void entering(Property property, int level) throws
> > RepositoryException {
> >     }
> >
> >     @Override
> >     protected void entering(Node node, int level) throws
> > RepositoryException {
> >         final String nodeType = node.getPrimaryNodeType().getName();
> >         final String path = node.getPath();
> >         final Matcher m = ENTRY.matcher(path);
> >
> >         if (m.find() && "cq:PageContent".equals(nodeType)) {
> >             final ResourceTraversor traversor = new ResourceTraversor(-1,
> > Long.MAX_VALUE, resolver.getResource(path), false);
> >             traversor.collectResources();
> >             writer.write(traversor.getJSONObject().toString());
> >         }
> >     }
> >
> >     @Override
> >     protected void leaving(Property property, int level) throws
> > RepositoryException {
> >     }
> >
> >     @Override
> >     protected void leaving(Node node, int level) throws
> RepositoryException
> > {
> >     }
> > }
> >
> > final Node node =
> > resourceResolver.getResource(rootPath).adaptTo(Node.class);
> > final TraversingItemVisitor visitor = new
> > JsonDumpNodeVisotor(resourceResolver, out);
> > node.accept(visitor);
> >
>
>
>
> --
> Carsten Ziegeler
> Adobe Research Switzerland
> cziegeler@apache.org
>

Re: easy way to dump JSON from JCR Node or Resource?

Posted by Carsten Ziegeler <cz...@apache.org>.
The code has recently moved to the commons json bundle, the easiest way
would be to update that bundle. If that's not possible, just embed the
classes in your bundle.

Carsten


2014-08-14 23:45 GMT+02:00 Sam Lee <sk...@gmail.com>:

> Hi,
>
> I want to dump all JCR content to mongodb for analysis.
> I see there is  https://issues.apache.org/jira/browse/SLING-3485 .
> And org.apache.sling.servlets.get.impl.helpers.ResourceTraversor  does what
> I want to do.
>
> But I am stuck with older version of Sling.
> Are there other ways to dump JSON of  a node tree?
>
> I have the following code but it cannot be compiled because
> ResourceTraversor cannot be found:
>
> final Pattern ENTRY = Pattern.compile("/\\d+/\\d+/[^/]+/jcr:content$");
> class JsonDumpNodeVisitor extends TraversingItemVisitor {
>     private final ResourceResolver resolver;
>     private final PrintWriter writer;
>
>     public JsonDumpNodeVisitor(ResourceResolver resolver, PrintWriter
> writer) {
>         this.resolver = resolver;
>         this.writer = writer;
>     }
>
>     @Override
>     protected void entering(Property property, int level) throws
> RepositoryException {
>     }
>
>     @Override
>     protected void entering(Node node, int level) throws
> RepositoryException {
>         final String nodeType = node.getPrimaryNodeType().getName();
>         final String path = node.getPath();
>         final Matcher m = ENTRY.matcher(path);
>
>         if (m.find() && "cq:PageContent".equals(nodeType)) {
>             final ResourceTraversor traversor = new ResourceTraversor(-1,
> Long.MAX_VALUE, resolver.getResource(path), false);
>             traversor.collectResources();
>             writer.write(traversor.getJSONObject().toString());
>         }
>     }
>
>     @Override
>     protected void leaving(Property property, int level) throws
> RepositoryException {
>     }
>
>     @Override
>     protected void leaving(Node node, int level) throws RepositoryException
> {
>     }
> }
>
> final Node node =
> resourceResolver.getResource(rootPath).adaptTo(Node.class);
> final TraversingItemVisitor visitor = new
> JsonDumpNodeVisotor(resourceResolver, out);
> node.accept(visitor);
>



-- 
Carsten Ziegeler
Adobe Research Switzerland
cziegeler@apache.org