use of org.structr.common.error.ArgumentNullException 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 "";
}
use of org.structr.common.error.ArgumentNullException 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 "";
}
use of org.structr.common.error.ArgumentNullException 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;
}
use of org.structr.common.error.ArgumentNullException 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;
}
use of org.structr.common.error.ArgumentNullException 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;
}
Aggregations