Search in sources :

Example 11 with RequestProperty

use of org.apache.sling.servlets.post.impl.helper.RequestProperty in project sling by apache.

the class RequestPropertyTest method testNullSingleValueWithDefaultToIgnore.

@Test
public void testNullSingleValueWithDefaultToIgnore() throws Throwable {
    Map<String, RequestProperty> props = collectContent(p("./param@DefaultValue", ":ignore"));
    assertEquals(1, props.size());
    RequestProperty prop = props.get("/test/path/param");
    assertFalse(prop.hasValues());
}
Also used : RequestProperty(org.apache.sling.servlets.post.impl.helper.RequestProperty) Test(org.junit.Test)

Example 12 with RequestProperty

use of org.apache.sling.servlets.post.impl.helper.RequestProperty in project sling by apache.

the class RequestPropertyTest method testMultiValueWithAllBlanks.

@Test
public void testMultiValueWithAllBlanks() throws Throwable {
    Map<String, RequestProperty> props = collectContent(p("./param", "", "", ""));
    assertEquals(1, props.size());
    RequestProperty prop = props.get("/test/path/param");
    assertTrue(prop.hasValues());
    assertFalse(prop.providesValue());
    assertEquals(3, prop.getStringValues().length);
    assertEquals("", prop.getStringValues()[0]);
    assertEquals("", prop.getStringValues()[1]);
    assertEquals("", prop.getStringValues()[2]);
}
Also used : RequestProperty(org.apache.sling.servlets.post.impl.helper.RequestProperty) Test(org.junit.Test)

Example 13 with RequestProperty

use of org.apache.sling.servlets.post.impl.helper.RequestProperty in project sling by apache.

the class RequestPropertyTest method testMultiValueWithBlank.

@Test
public void testMultiValueWithBlank() throws Throwable {
    Map<String, RequestProperty> props = collectContent(p("./param", "true", ""));
    assertEquals(1, props.size());
    RequestProperty prop = props.get("/test/path/param");
    assertTrue(prop.hasValues());
    assertTrue(prop.providesValue());
    assertEquals(2, prop.getStringValues().length);
    assertEquals("true", prop.getStringValues()[0]);
    assertEquals("", prop.getStringValues()[1]);
}
Also used : RequestProperty(org.apache.sling.servlets.post.impl.helper.RequestProperty) Test(org.junit.Test)

Example 14 with RequestProperty

use of org.apache.sling.servlets.post.impl.helper.RequestProperty 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 15 with RequestProperty

use of org.apache.sling.servlets.post.impl.helper.RequestProperty in project sling by apache.

the class ModifyOperation method doRun.

@Override
protected void doRun(final SlingHttpServletRequest request, final PostResponse response, final List<Modification> changes) throws PersistenceException {
    final Map<String, RequestProperty> reqProperties = collectContent(request, response);
    final VersioningConfiguration versioningConfiguration = getVersioningConfiguration(request);
    // do not change order unless you have a very good reason.
    // ensure root of new content
    processCreate(request.getResourceResolver(), reqProperties, response, changes, versioningConfiguration);
    // write content from existing content (@Move/CopyFrom parameters)
    processMoves(request.getResourceResolver(), reqProperties, changes, versioningConfiguration);
    processCopies(request.getResourceResolver(), reqProperties, changes, versioningConfiguration);
    // cleanup any old content (@Delete parameters)
    processDeletes(request.getResourceResolver(), reqProperties, changes, versioningConfiguration);
    // write content from form
    writeContent(request.getResourceResolver(), reqProperties, changes, versioningConfiguration);
    // order content
    final Resource newResource = request.getResourceResolver().getResource(response.getPath());
    this.jcrSsupport.orderNode(request, newResource, changes);
}
Also used : RequestProperty(org.apache.sling.servlets.post.impl.helper.RequestProperty) Resource(org.apache.sling.api.resource.Resource) VersioningConfiguration(org.apache.sling.servlets.post.VersioningConfiguration)

Aggregations

RequestProperty (org.apache.sling.servlets.post.impl.helper.RequestProperty)26 Test (org.junit.Test)12 RepositoryException (javax.jcr.RepositoryException)5 Resource (org.apache.sling.api.resource.Resource)5 HashMap (java.util.HashMap)4 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)4 UserManager (org.apache.jackrabbit.api.security.user.UserManager)4 RequestParameter (org.apache.sling.api.request.RequestParameter)4 Map (java.util.Map)3 Group (org.apache.jackrabbit.api.security.user.Group)3 Session (javax.jcr.Session)2 User (org.apache.jackrabbit.api.security.user.User)2 RequestParameterMap (org.apache.sling.api.request.RequestParameterMap)2 LoginException (org.apache.sling.api.resource.LoginException)2 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)2 PersistenceException (org.apache.sling.api.resource.PersistenceException)2 ResourceNotFoundException (org.apache.sling.api.resource.ResourceNotFoundException)2 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)2 VersioningConfiguration (org.apache.sling.servlets.post.VersioningConfiguration)2 IOException (java.io.IOException)1