Search in sources :

Example 6 with ContentImportListener

use of org.apache.sling.jcr.contentloader.ContentImportListener in project sling by apache.

the class DefaultContentCreatorTest method createNodeWithPrimaryType.

@Test
public void createNodeWithPrimaryType() throws RepositoryException {
    final String newNodeName = uniqueId();
    final List<String> createdNodes = new ArrayList<String>();
    final ContentImportListener listener = mockery.mock(ContentImportListener.class);
    this.mockery.checking(new Expectations() {

        {
            oneOf(listener).onCreate(with(any(String.class)));
        }
    });
    contentCreator.init(ImportOptionsFactory.createImportOptions(true, false, true, false, false), new HashMap<String, ContentReader>(), createdNodes, listener);
    contentCreator.prepareParsing(parentNode, DEFAULT_NAME);
    int createdNodesSize = createdNodes.size();
    contentCreator.createNode(newNodeName, NodeType.NT_UNSTRUCTURED, null);
    assertEquals(createdNodesSize + 1, createdNodes.size());
    Node createdNode = parentNode.getNode(newNodeName);
    assertNotNull(createdNode);
    assertTrue(createdNode.getPrimaryNodeType().isNodeType(NodeType.NT_UNSTRUCTURED));
    mockery.assertIsSatisfied();
}
Also used : Expectations(org.jmock.Expectations) ContentReader(org.apache.sling.jcr.contentloader.ContentReader) ContentImportListener(org.apache.sling.jcr.contentloader.ContentImportListener) Test(org.junit.Test)

Example 7 with ContentImportListener

use of org.apache.sling.jcr.contentloader.ContentImportListener in project sling by apache.

the class ImportOperation method doRun.

@Override
protected void doRun(SlingHttpServletRequest request, PostResponse response, final List<Modification> changes) throws PersistenceException {
    try {
        Object importer = contentImporter;
        if (importer == null) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Missing content importer for import");
            return;
        }
        Map<String, RequestProperty> reqProperties = collectContent(request, response);
        VersioningConfiguration versioningConfiguration = getVersioningConfiguration(request);
        // do not change order unless you have a very good reason.
        Session session = request.getResourceResolver().adaptTo(Session.class);
        processCreate(request.getResourceResolver(), reqProperties, response, changes, versioningConfiguration);
        String path = response.getPath();
        Node node = null;
        try {
            node = (Node) session.getItem(path);
        } catch (RepositoryException e) {
            log.warn(e.getMessage(), e);
        // was not able to resolve the node
        } catch (ClassCastException e) {
            log.warn(e.getMessage(), e);
        // it was not a node
        }
        if (node == null) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND, "Missing target node " + path + " for import");
            return;
        }
        String contentType = getRequestParamAsString(request, SlingPostConstants.RP_CONTENT_TYPE);
        if (contentType == null) {
            response.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED, "Required :contentType parameter is missing");
            return;
        }
        //import options passed as request parameters.
        final boolean replace = "true".equalsIgnoreCase(getRequestParamAsString(request, SlingPostConstants.RP_REPLACE));
        final boolean replaceProperties = "true".equalsIgnoreCase(getRequestParamAsString(request, SlingPostConstants.RP_REPLACE_PROPERTIES));
        final boolean checkin = "true".equalsIgnoreCase(getRequestParamAsString(request, SlingPostConstants.RP_CHECKIN));
        final boolean autoCheckout = "true".equalsIgnoreCase(getRequestParamAsString(request, SlingPostConstants.RP_AUTO_CHECKOUT));
        String basePath = getResourcePath(request);
        if (basePath.endsWith("/")) {
            //remove the trailing slash
            basePath = basePath.substring(0, basePath.length() - 1);
        }
        // default to creating content
        response.setCreateRequest(true);
        final String targetName;
        //check if a name was posted to use as the name of the imported root node
        if (getRequestParamAsString(request, SlingPostConstants.RP_NODE_NAME) != null) {
            // exact name
            targetName = getRequestParamAsString(request, SlingPostConstants.RP_NODE_NAME);
            if (targetName.length() > 0 && node.hasNode(targetName)) {
                if (replace) {
                    response.setCreateRequest(false);
                } else {
                    response.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED, "Cannot import " + path + "/" + targetName + ": node exists");
                    return;
                }
            }
        } else if (getRequestParamAsString(request, SlingPostConstants.RP_NODE_NAME_HINT) != null) {
            // node name hint only
            String nodePath = generateName(request, basePath);
            String name = nodePath.substring(nodePath.lastIndexOf('/') + 1);
            targetName = name;
        } else {
            // no name posted, so the import won't create a root node
            targetName = "";
        }
        final String contentRootName = targetName + "." + contentType;
        try {
            InputStream contentStream = null;
            RequestParameter contentParameter = request.getRequestParameter(SlingPostConstants.RP_CONTENT);
            if (contentParameter != null) {
                contentStream = contentParameter.getInputStream();
            } else {
                RequestParameter contentFile = request.getRequestParameter(SlingPostConstants.RP_CONTENT_FILE);
                if (contentFile != null) {
                    contentStream = contentFile.getInputStream();
                }
            }
            if (contentStream == null) {
                response.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED, "Missing content for import");
                return;
            } else {
                ((ContentImporter) importer).importContent(node, contentRootName, contentStream, new ImportOptions() {

                    @Override
                    public boolean isCheckin() {
                        return checkin;
                    }

                    @Override
                    public boolean isAutoCheckout() {
                        return autoCheckout;
                    }

                    @Override
                    public boolean isIgnoredImportProvider(String extension) {
                        // this probably isn't important in this context.
                        return false;
                    }

                    @Override
                    public boolean isOverwrite() {
                        return replace;
                    }

                    /* (non-Javadoc)
                                 * @see org.apache.sling.jcr.contentloader.ImportOptions#isPropertyOverwrite()
                                 */
                    @Override
                    public boolean isPropertyOverwrite() {
                        return replaceProperties;
                    }
                }, new ContentImportListener() {

                    @Override
                    public void onReorder(String orderedPath, String beforeSibbling) {
                        changes.add(Modification.onOrder(orderedPath, beforeSibbling));
                    }

                    @Override
                    public void onMove(String srcPath, String destPath) {
                        changes.add(Modification.onMoved(srcPath, destPath));
                    }

                    @Override
                    public void onModify(String srcPath) {
                        changes.add(Modification.onModified(srcPath));
                    }

                    @Override
                    public void onDelete(String srcPath) {
                        changes.add(Modification.onDeleted(srcPath));
                    }

                    @Override
                    public void onCreate(String srcPath) {
                        changes.add(Modification.onCreated(srcPath));
                    }

                    @Override
                    public void onCopy(String srcPath, String destPath) {
                        changes.add(Modification.onCopied(srcPath, destPath));
                    }

                    @Override
                    public void onCheckin(String srcPath) {
                        changes.add(Modification.onCheckin(srcPath));
                    }

                    @Override
                    public void onCheckout(String srcPath) {
                        changes.add(Modification.onCheckout(srcPath));
                    }
                });
            }
            if (!changes.isEmpty()) {
                //fill in the data for the response report
                Modification modification = changes.get(0);
                if (modification.getType() == ModificationType.CREATE) {
                    String importedPath = modification.getSource();
                    response.setLocation(externalizePath(request, importedPath));
                    response.setPath(importedPath);
                    int lastSlashIndex = importedPath.lastIndexOf('/');
                    if (lastSlashIndex != -1) {
                        String parentPath = importedPath.substring(0, lastSlashIndex);
                        response.setParentLocation(externalizePath(request, parentPath));
                    }
                }
            }
        } catch (IOException e) {
            throw new PersistenceException(e.getMessage(), e);
        }
    } catch (final RepositoryException re) {
        throw new PersistenceException(re.getMessage(), re);
    }
}
Also used : Modification(org.apache.sling.servlets.post.Modification) InputStream(java.io.InputStream) RequestParameter(org.apache.sling.api.request.RequestParameter) Node(javax.jcr.Node) ContentImportListener(org.apache.sling.jcr.contentloader.ContentImportListener) RepositoryException(javax.jcr.RepositoryException) VersioningConfiguration(org.apache.sling.servlets.post.VersioningConfiguration) IOException(java.io.IOException) ContentImporter(org.apache.sling.jcr.contentloader.ContentImporter) RequestProperty(org.apache.sling.servlets.post.impl.helper.RequestProperty) PersistenceException(org.apache.sling.api.resource.PersistenceException) ImportOptions(org.apache.sling.jcr.contentloader.ImportOptions) Session(javax.jcr.Session)

Example 8 with ContentImportListener

use of org.apache.sling.jcr.contentloader.ContentImportListener in project sling by apache.

the class DefaultContentCreatorTest method createDateProperty.

@Test
public void createDateProperty() throws RepositoryException, ParseException {
    final String propName = "dateProp";
    final String[] propValues = { "2012-10-01T09:45:00.000+02:00", "2011-02-13T09:45:00.000+02:00" };
    final ContentImportListener listener = mockery.mock(ContentImportListener.class);
    this.mockery.checking(new Expectations() {

        {
            oneOf(listener).onCreate(with(any(String.class)));
        }
    });
    parentNode.addMixin("mix:versionable");
    contentCreator.init(ImportOptionsFactory.createImportOptions(false, false, true, false, false), new HashMap<String, ContentReader>(), null, listener);
    contentCreator.prepareParsing(parentNode, null);
    assertFalse(parentNode.hasProperty(propName));
    contentCreator.createProperty(propName, PropertyType.DATE, propValues);
    assertTrue(parentNode.hasProperty(propName));
    Property dateProp = parentNode.getProperty(propName);
    assertTrue(dateProp.isNew());
    assertEquals(propValues.length, dateProp.getValues().length);
    mockery.assertIsSatisfied();
}
Also used : Expectations(org.jmock.Expectations) ContentReader(org.apache.sling.jcr.contentloader.ContentReader) ContentImportListener(org.apache.sling.jcr.contentloader.ContentImportListener) Test(org.junit.Test)

Example 9 with ContentImportListener

use of org.apache.sling.jcr.contentloader.ContentImportListener in project sling by apache.

the class DefaultContentCreatorTest method finishNodeWithSingleProperty.

@Test
public void finishNodeWithSingleProperty() throws RepositoryException, NoSuchFieldException {
    final String propName = uniqueId();
    final String underTestNodeName = uniqueId();
    final ContentImportListener listener = mockery.mock(ContentImportListener.class);
    this.mockery.checking(new Expectations() {

        {
            exactly(2).of(listener).onCreate(with(any(String.class)));
        }
    });
    contentCreator.init(ImportOptionsFactory.createImportOptions(false, false, false, false, false), new HashMap<String, ContentReader>(), null, listener);
    contentCreator.prepareParsing(parentNode, null);
    contentCreator.createProperty(propName, PropertyType.REFERENCE, underTestNodeName);
    contentCreator.createNode(underTestNodeName, null, null);
    Node underTest = parentNode.getNode(underTestNodeName);
    underTest.addMixin("mix:referenceable");
    contentCreator.finishNode();
    assertEquals(underTest.getUUID(), parentNode.getProperty(propName).getString());
    mockery.assertIsSatisfied();
}
Also used : Expectations(org.jmock.Expectations) ContentReader(org.apache.sling.jcr.contentloader.ContentReader) ContentImportListener(org.apache.sling.jcr.contentloader.ContentImportListener) Test(org.junit.Test)

Example 10 with ContentImportListener

use of org.apache.sling.jcr.contentloader.ContentImportListener in project sling by apache.

the class DefaultContentCreatorTest method testCreatePropertyWithNewPropertyType.

@Test
public void testCreatePropertyWithNewPropertyType() throws RepositoryException, ParseException {
    final String propertyName = "foo";
    final String propertyValue = "bar";
    final Integer propertyType = -1;
    final ContentImportListener listener = mockery.mock(ContentImportListener.class);
    parentNode = mockery.mock(Node.class);
    prop = mockery.mock(Property.class);
    this.mockery.checking(new Expectations() {

        {
            oneOf(parentNode).hasProperty(with(any(String.class)));
            oneOf(parentNode).getProperty(propertyName);
            will(returnValue(prop));
            oneOf(parentNode).setProperty(propertyName, propertyValue, propertyType);
            oneOf(prop).getPath();
            will(returnValue(""));
            oneOf(listener).onCreate(with(any(String.class)));
        }
    });
    contentCreator.init(ImportOptionsFactory.createImportOptions(false, false, false, false, false), new HashMap<String, ContentReader>(), null, listener);
    contentCreator.prepareParsing(parentNode, null);
    contentCreator.createProperty(propertyName, propertyType, propertyValue);
    mockery.assertIsSatisfied();
}
Also used : Expectations(org.jmock.Expectations) ContentReader(org.apache.sling.jcr.contentloader.ContentReader) ContentImportListener(org.apache.sling.jcr.contentloader.ContentImportListener) Test(org.junit.Test)

Aggregations

ContentImportListener (org.apache.sling.jcr.contentloader.ContentImportListener)11 ContentReader (org.apache.sling.jcr.contentloader.ContentReader)10 Expectations (org.jmock.Expectations)10 Test (org.junit.Test)10 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Node (javax.jcr.Node)1 RepositoryException (javax.jcr.RepositoryException)1 Session (javax.jcr.Session)1 RequestParameter (org.apache.sling.api.request.RequestParameter)1 PersistenceException (org.apache.sling.api.resource.PersistenceException)1 ContentImporter (org.apache.sling.jcr.contentloader.ContentImporter)1 ImportOptions (org.apache.sling.jcr.contentloader.ImportOptions)1 Modification (org.apache.sling.servlets.post.Modification)1 VersioningConfiguration (org.apache.sling.servlets.post.VersioningConfiguration)1 RequestProperty (org.apache.sling.servlets.post.impl.helper.RequestProperty)1