- try {
- //
- // Copy the policy over
- //
- long num;
- Path policyFilePath = Paths.get(this.directory.toAbsolutePath().toString(), id);
-
- Path policyFile;
- if (Files.exists(policyFilePath)) {
- policyFile = policyFilePath;
- } else {
- policyFile = Files.createFile(policyFilePath);
- }
-
- try (OutputStream os = Files.newOutputStream(policyFile)) {
- num = ByteStreams.copy(policy, os);
- }
-
- logger.info("Copied " + num + " bytes for policy " + name);
-
- for (PDPPolicy p : policies) {
- if (p.getId().equals(id)) {
- // we just re-copied/refreshed/updated the policy file for a policy that already exists in this
- // group
- logger.info("Policy '" + id + "' already exists in group '" + getId() + "'");
- return;
- }
- }
-
- // policy is new to this group
- StdPDPPolicy tempRootPolicy = new StdPDPPolicy(id, true, name, policyFile.toUri());
- if (!tempRootPolicy.isValid()) {
- try {
- Files.delete(policyFile);
- } catch (Exception ee) {
- PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, ee, "StdPDPGroup",
- "Policy was invalid, could NOT delete it.");
- }
- throw new PAPException("Policy is invalid");
- }
- //
- // Add it in
- //
- this.policies.add(tempRootPolicy);
- //
- // We are changed
- //
- this.firePDPGroupChanged(this);
- } catch (IOException e) {
- PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "StdPDPGroup", "Failed to copyPolicyToFile");
- throw new PAPException("Failed to copy policy to file: " + e);
- }