001/*-
002 * #%L
003 * Smile CDR - CDR
004 * %%
005 * Copyright (C) 2016 - 2024 Smile CDR, Inc.
006 * %%
007 * All rights reserved.
008 * #L%
009 */
010package ca.cdr.api.security.permission;
011
012import jakarta.annotation.Nonnull;
013import jakarta.annotation.Nullable;
014import org.apache.commons.lang3.StringUtils;
015
016import java.util.Optional;
017
018/**
019 * A single String which contains Node ID and Module ID separated by a slash `/`.
020 * For eg. Master/subscription
021 */
022public class NodeAndModuleId extends PermissionArgumentValue
023                implements PermissionArgumentFormat.INodeAndModuleIdRestriction {
024
025        public static final String INVALID_METHOD_ARGUMENT_MESSAGE = "NodeId or ModuleId cannot be null or empty.";
026        private final String myNodeId;
027        private final String myModuleId;
028
029        static final String DELIMITER = "/";
030
031        private NodeAndModuleId(String theNodeId, String theModuleId) {
032                if (StringUtils.isBlank(theNodeId) || StringUtils.isBlank(theModuleId)) {
033                        throw new IllegalArgumentException(INVALID_METHOD_ARGUMENT_MESSAGE);
034                }
035                myNodeId = theNodeId;
036                myModuleId = theModuleId;
037        }
038
039        public static NodeAndModuleId fromNodeIdAndModuleId(String theNodeId, String theModuleId) {
040                return new NodeAndModuleId(theNodeId, theModuleId);
041        }
042
043        @Override
044        public String getNodeId() {
045                return myNodeId;
046        }
047
048        @Override
049        public String getModuleId() {
050                return myModuleId;
051        }
052
053        @Nonnull
054        @Override
055        String getStringValue() {
056                return StringUtils.join(myNodeId, DELIMITER, myModuleId);
057        }
058
059        static final class Format implements PermissionArgumentFormat<NodeAndModuleId> {
060                public static final String INVALID_FORMAT_MESSAGE =
061                                "Invalid format passed for permission. Expected permission to include node id and module id separated by slash (/) but found: ";
062
063                @Nonnull
064                public Optional<NodeAndModuleId> parse(@Nullable String theArgumentString, @Nonnull IdBuilder theIdBuilder) {
065                        if (StringUtils.isBlank(theArgumentString)) {
066                                throw new IllegalArgumentException(INVALID_FORMAT_MESSAGE + theArgumentString);
067                        }
068
069                        String[] nodeIdAndModuleId = theArgumentString.split(DELIMITER);
070                        if (nodeIdAndModuleId.length == 2) {
071                                final String nodeId = nodeIdAndModuleId[0];
072                                final String moduleId = nodeIdAndModuleId[1];
073                                return Optional.of(new NodeAndModuleId(nodeId, moduleId));
074                        } else {
075                                throw new IllegalArgumentException(INVALID_FORMAT_MESSAGE + theArgumentString);
076                        }
077                }
078
079                @Override
080                public Class<NodeAndModuleId> getType() {
081                        return NodeAndModuleId.class;
082                }
083
084                @Nonnull
085                @Override
086                public String format(@Nonnull NodeAndModuleId theArgument) {
087                        return theArgument.getStringValue();
088                }
089        }
090}