Search in sources :

Example 1 with ArgumentCountException

use of org.structr.common.error.ArgumentCountException in project structr by structr.

the class AddToGroupFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    try {
        assertArrayHasLengthAndAllElementsNotNull(sources, 2);
        if (!(sources[0] instanceof Group)) {
            logParameterError(caller, sources, "Expected node of type Group as first argument!", ctx.isJavaScriptContext());
        } else if (!(sources[1] instanceof Principal)) {
            logParameterError(caller, sources, "Expected node of type Principal as second argument!", ctx.isJavaScriptContext());
        } else if ((sources[1] instanceof SuperUser)) {
            logParameterError(caller, sources, "Expected node of type Principal as second argument - SuperUser can not be member of a group!", ctx.isJavaScriptContext());
        } else {
            final Group group = (Group) sources[0];
            final Principal user = (Principal) sources[1];
            group.addMember(ctx.getSecurityContext(), user);
        }
    } catch (ArgumentNullException pe) {
    // silently ignore null arguments
    } catch (ArgumentCountException pe) {
        logParameterError(caller, sources, pe.getMessage(), ctx.isJavaScriptContext());
    }
    return "";
}
Also used : Group(org.structr.core.entity.Group) ArgumentNullException(org.structr.common.error.ArgumentNullException) SuperUser(org.structr.core.entity.SuperUser) Principal(org.structr.core.entity.Principal) ArgumentCountException(org.structr.common.error.ArgumentCountException)

Example 2 with ArgumentCountException

use of org.structr.common.error.ArgumentCountException in project structr by structr.

the class PDFEncryptFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    try {
        assertArrayHasMinLengthAndAllElementsNotNull(sources, 2);
        try {
            if (!(sources[0] instanceof File)) {
                logParameterError(caller, sources, "First parameter is not a file object.", ctx.isJavaScriptContext());
                return usage(ctx.isJavaScriptContext());
            }
            if (!(sources[1] instanceof String)) {
                logParameterError(caller, sources, "Second parameter is not a string.", ctx.isJavaScriptContext());
                return usage(ctx.isJavaScriptContext());
            }
            final File pdfFileObject = (File) sources[0];
            final String userPassword = (String) sources[1];
            final java.io.File fileOnDisk = pdfFileObject.getFileOnDisk();
            final PDDocument pdDocument = PDDocument.load(fileOnDisk);
            final AccessPermission accessPermission = new AccessPermission();
            accessPermission.setCanPrint(false);
            // Owner password (to open the file with all permissions) is the superuser password
            final StandardProtectionPolicy standardProtectionPolicy = new StandardProtectionPolicy(Settings.SuperUserPassword.getValue(), userPassword, accessPermission);
            standardProtectionPolicy.setEncryptionKeyLength(keyLength);
            standardProtectionPolicy.setPermissions(accessPermission);
            pdDocument.protect(standardProtectionPolicy);
            if (fileOnDisk.delete()) {
                pdDocument.save(fileOnDisk);
            }
            pdDocument.close();
        } catch (final IOException ioex) {
            logException(caller, ioex, sources);
        }
    } catch (final ArgumentNullException pe) {
        logParameterError(caller, sources, pe.getMessage(), ctx.isJavaScriptContext());
    } catch (final ArgumentCountException pe) {
        logParameterError(caller, sources, pe.getMessage(), ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
    return "";
}
Also used : ArgumentNullException(org.structr.common.error.ArgumentNullException) java.io(java.io) StandardProtectionPolicy(org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) AccessPermission(org.apache.pdfbox.pdmodel.encryption.AccessPermission) File(org.structr.web.entity.File) ArgumentCountException(org.structr.common.error.ArgumentCountException)

Example 3 with ArgumentCountException

use of org.structr.common.error.ArgumentCountException in project structr by structr.

the class GetIncomingRelationshipsFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    final List<AbstractRelationship> list = new ArrayList<>();
    try {
        assertArrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 2, 3);
        final Object source = sources[0];
        final Object target = sources[1];
        AbstractNode sourceNode = null;
        AbstractNode targetNode = null;
        if (source instanceof AbstractNode && target instanceof AbstractNode) {
            sourceNode = (AbstractNode) source;
            targetNode = (AbstractNode) target;
        } else {
            logger.warn("Error: entities are not nodes. Parameters: {}", getParametersAsString(sources));
            return "Error: entities are not nodes.";
        }
        if (sources.length == 2) {
            for (final AbstractRelationship rel : sourceNode.getIncomingRelationships()) {
                final NodeInterface s = rel.getSourceNode();
                final NodeInterface t = rel.getTargetNode();
                // We need to check if current user can see source and target node which is often not the case for OWNS or SECURITY rels
                if (s != null && t != null && s.equals(targetNode) && t.equals(sourceNode)) {
                    list.add(rel);
                }
            }
        } else if (sources.length == 3) {
            // dont try to create the relClass because we would need to do that both ways!!! otherwise it just fails if the nodes are in the "wrong" order (see tests:890f)
            final String relType = (String) sources[2];
            for (final AbstractRelationship rel : sourceNode.getIncomingRelationships()) {
                final NodeInterface s = rel.getSourceNode();
                final NodeInterface t = rel.getTargetNode();
                // We need to check if current user can see source and target node which is often not the case for OWNS or SECURITY rels
                if (s != null && t != null && rel.getRelType().name().equals(relType) && s.equals(targetNode) && t.equals(sourceNode)) {
                    list.add(rel);
                }
            }
        }
    } catch (ArgumentNullException pe) {
        logParameterError(caller, sources, pe.getMessage(), ctx.isJavaScriptContext());
    } catch (ArgumentCountException pe) {
        logParameterError(caller, sources, pe.getMessage(), ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
    return list;
}
Also used : AbstractNode(org.structr.core.entity.AbstractNode) ArgumentNullException(org.structr.common.error.ArgumentNullException) AbstractRelationship(org.structr.core.entity.AbstractRelationship) ArrayList(java.util.ArrayList) NodeInterface(org.structr.core.graph.NodeInterface) ArgumentCountException(org.structr.common.error.ArgumentCountException)

Example 4 with ArgumentCountException

use of org.structr.common.error.ArgumentCountException in project structr by structr.

the class HMACFunction method apply.

@Override
public Object apply(ActionContext ctx, Object caller, Object[] sources) throws FrameworkException {
    try {
        assertArrayHasMinLengthAndAllElementsNotNull(sources, 2);
        final String algorithmPrefix = "Hmac";
        final String value = (String) sources[0];
        final String secret = (String) sources[1];
        String algorithm = null;
        if (sources.length > 2) {
            algorithm = (String) sources[2];
        } else {
            algorithm = "SHA256";
        }
        algorithm = algorithmPrefix.concat(algorithm);
        Boolean returnRawHash = false;
        if (sources.length > 3) {
            returnRawHash = true;
        }
        SecretKeySpec key = new SecretKeySpec((secret).getBytes("UTF-8"), algorithm);
        Mac mac = Mac.getInstance(algorithm);
        mac.init(key);
        byte[] rawBytesHash = mac.doFinal(value.getBytes("UTF-8"));
        if (returnRawHash) {
            return rawBytesHash;
        }
        // Create Hex String and fill with 0 if needed
        StringBuffer hash = new StringBuffer();
        for (final byte b : rawBytesHash) {
            String hex = Integer.toHexString(0xFF & b);
            if (hex.length() == 1) {
                hash.append('0');
            }
            hash.append(hex);
        }
        return hash.toString();
    } catch (UnsupportedEncodingException e) {
        logParameterError(caller, sources, e.getMessage(), ctx.isJavaScriptContext());
    } catch (NoSuchAlgorithmException e) {
        logParameterError(caller, sources, e.getMessage(), ctx.isJavaScriptContext());
    } catch (InvalidKeyException e) {
        logParameterError(caller, sources, e.getMessage(), ctx.isJavaScriptContext());
    } catch (ArgumentNullException pe) {
        logParameterError(caller, sources, pe.getMessage(), ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    } catch (ArgumentCountException pe) {
        logParameterError(caller, sources, pe.getMessage(), ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
    return null;
}
Also used : ArgumentNullException(org.structr.common.error.ArgumentNullException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) Mac(javax.crypto.Mac) ArgumentCountException(org.structr.common.error.ArgumentCountException)

Example 5 with ArgumentCountException

use of org.structr.common.error.ArgumentCountException in project structr by structr.

the class HasIncomingRelationshipFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    try {
        assertArrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 2, 3);
        final Object source = sources[0];
        final Object target = sources[1];
        AbstractNode sourceNode = null;
        AbstractNode targetNode = null;
        if (source instanceof AbstractNode && target instanceof AbstractNode) {
            sourceNode = (AbstractNode) source;
            targetNode = (AbstractNode) target;
        } else {
            logger.warn("Error: entities are not nodes. Parameters: {}", getParametersAsString(sources));
            return "Error: entities are not nodes.";
        }
        if (sources.length == 2) {
            for (final AbstractRelationship rel : sourceNode.getIncomingRelationships()) {
                final NodeInterface s = rel.getSourceNode();
                final NodeInterface t = rel.getTargetNode();
                // We need to check if current user can see source and target node which is often not the case for OWNS or SECURITY rels
                if (s != null & t != null && s.equals(targetNode) && t.equals(sourceNode)) {
                    return true;
                }
            }
        } else if (sources.length == 3) {
            // dont try to create the relClass because we would need to do that both ways!!! otherwise it just fails if the nodes are in the "wrong" order (see tests:890f)
            final String relType = (String) sources[2];
            for (final AbstractRelationship rel : sourceNode.getIncomingRelationships()) {
                final NodeInterface s = rel.getSourceNode();
                final NodeInterface t = rel.getTargetNode();
                // We need to check if current user can see source and target node which is often not the case for OWNS or SECURITY rels
                if (s != null & t != null && rel.getRelType().name().equals(relType) && s.equals(targetNode) && t.equals(sourceNode)) {
                    return true;
                }
            }
        }
    } catch (ArgumentNullException pe) {
        logParameterError(caller, sources, pe.getMessage(), ctx.isJavaScriptContext());
    } catch (ArgumentCountException pe) {
        logParameterError(caller, sources, pe.getMessage(), ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
    return false;
}
Also used : AbstractNode(org.structr.core.entity.AbstractNode) ArgumentNullException(org.structr.common.error.ArgumentNullException) AbstractRelationship(org.structr.core.entity.AbstractRelationship) NodeInterface(org.structr.core.graph.NodeInterface) ArgumentCountException(org.structr.common.error.ArgumentCountException)

Aggregations

ArgumentCountException (org.structr.common.error.ArgumentCountException)79 ArgumentNullException (org.structr.common.error.ArgumentNullException)77 SecurityContext (org.structr.common.SecurityContext)16 List (java.util.List)11 Coordinate (com.vividsolutions.jts.geom.Coordinate)8 Map (java.util.Map)8 App (org.structr.core.app.App)8 StructrApp (org.structr.core.app.StructrApp)8 NodeInterface (org.structr.core.graph.NodeInterface)8 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)7 LinkedHashMap (java.util.LinkedHashMap)7 LinkedList (java.util.LinkedList)7 AbstractNode (org.structr.core.entity.AbstractNode)7 AbstractRelationship (org.structr.core.entity.AbstractRelationship)7 CoordinateList (com.vividsolutions.jts.geom.CoordinateList)6 Geometry (com.vividsolutions.jts.geom.Geometry)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 GraphObject (org.structr.core.GraphObject)6 GraphObjectMap (org.structr.core.GraphObjectMap)6