- public static synchronized void loadPolicy(Properties properties,
- StdPDPStatus status, String id, boolean isRoot) throws PAPException {
- PolicyDef policy = null;
- String location = null;
- URI locationURI = null;
- boolean isFile = false;
- boolean rougeFile = false;
- try {
- location = properties.getProperty(id + ".file");
- if(location != null){
- isFile = true;
- locationURI = Paths.get(location).toUri();
- try (InputStream is = Files.newInputStream(Paths.get(location))) {
- policy = DOMPolicyDef.load(is);
- } catch (Exception e){
- // This Happens if a any issue with the error policyFile. Lets remove it.
- try {
- LOGGER.error("Corrupted policy file, deleting: " + location + e);
- Files.delete(Paths.get(location));
- properties.remove(id + ".file");
- rougeFile = true;
- } catch (IOException e1) {
- LOGGER.error(e1);
- }
- }
- }
- if(location==null || rougeFile){
- if(rougeFile){
- rougeFile = false;
- }
- location = properties.getProperty(id + ".url");
- if (location != null) {
- //
- // Construct the URL
- //
- int errorCount=0;
- boolean error= false;
- do{
- error=false;
- PapUrlResolver papUrls = PapUrlResolver.getInstance();
- while(papUrls.hasMoreUrls()){
- String papID = papUrls.getUserId();
- String papPass = papUrls.getPass();
- Base64.Encoder encoder = Base64.getEncoder();
- String encoding = encoder.encodeToString((papID+":"+papPass).getBytes(StandardCharsets.UTF_8));
- locationURI = URI.create(papUrls.getUrl(PapUrlResolver.extractIdFromUrl(location)));
- URL url = locationURI.toURL();
- URLConnection urlConnection = null;
- try{
- urlConnection = url.openConnection();
- } catch (IOException e){
- LOGGER.error("Exception Occured while opening connection" +e);
- papUrls.failed();
- papUrls.getNext();
- break;
- }
- urlConnection.setRequestProperty(XacmlRestProperties.PROP_PDP_HTTP_HEADER_ID,
- XACMLProperties.getProperty(XacmlRestProperties.PROP_PDP_ID));
- urlConnection.setRequestProperty("Authorization", "Basic " + encoding);
- //
- // Now construct the output file name
- //
- Path outFile = Paths.get(getPDPConfig().toAbsolutePath()
- .toString(), id);
- //
- // Copy it to disk
- //
- try (FileOutputStream fos = new FileOutputStream(
- outFile.toFile())) {
- IOUtils.copy(urlConnection.getInputStream(), fos);
- } catch(IOException e){
- LOGGER.error("Exception Occured while Copying input stream" +e);
- papUrls.failed();
- papUrls.getNext();
- break;
- }
- //
- // Now try to load
- //
- isFile = true;
- try (InputStream fis = Files.newInputStream(outFile)) {
- policy = DOMPolicyDef.load(fis);
- }catch(Exception e){
- try {
- LOGGER.error("Corrupted policy file, deleting: " + location +e);
- Files.delete(outFile);
- error = true;
- errorCount++;
- break;
- } catch (IOException e1) {
- LOGGER.error(e1);
- }
- }
- //
- // Save it
- //
- properties.setProperty(id + ".file", outFile
- .toAbsolutePath().toString());
- error = false;
- break;
- }
- }while(error && errorCount>2);
- }
- }
- if (policy != null) {
- status.addLoadedPolicy(new StdPDPPolicy(id, isRoot,
- locationURI, properties));
- LOGGER.info("Loaded policy: " + policy.getIdentifier()
- + " version: " + policy.getVersion().stringValue());
- // Sending the policy objects to the Notification Controller.
- policyContainer.put(id, policy);
- } else {
- String error = "Failed to load policy " + location;
- LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + error);
- status.setStatus(PDPStatus.Status.LOAD_ERRORS);
- status.addLoadError(error);
- status.addFailedPolicy(new StdPDPPolicy(id, isRoot));
- }
- } catch (Exception e) {
- LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW +"Failed to load policy '" + id + "' from location '"
- + location + "'", e);
- status.setStatus(PDPStatus.Status.LOAD_ERRORS);
- status.addFailedPolicy(new StdPDPPolicy(id, isRoot));
- //
- // Is it a file?
- //
- if (isFile) {
- //
- // Let's remove it
- //
- try {
- LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Corrupted policy file, deleting: " + location);
- Files.delete(Paths.get(location));
+ /**
+ * Load a policy.
+ *
+ * @param properties the policy properties
+ * @param status the PDP status
+ * @param id the policy ID
+ * @param isRoot indicates if operation being done as root
+ * @throws PAPException on loading errors
+ */
+ public static synchronized void loadPolicy(Properties properties, StdPDPStatus status, String id, boolean isRoot)
+ throws PAPException {
+ PolicyDef policy = null;
+ String location = null;
+ URI locationUri = null;
+ boolean isFile = false;
+ boolean rougeFile = false;
+ try {
+ location = properties.getProperty(id + DOT_FILE);
+ if (location != null) {
+ isFile = true;
+ locationUri = Paths.get(location).toUri();
+ try (InputStream is = Files.newInputStream(Paths.get(location))) {
+ policy = DOMPolicyDef.load(is);
+ } catch (Exception e) {
+ // This Happens if a any issue with the error policyFile. Lets remove it.
+ try {
+ LOGGER.error(CORRUPTED_POLICY_FILE_DELETING + location + e);
+ Files.delete(Paths.get(location));
+ properties.remove(id + DOT_FILE);
+ rougeFile = true;
+ } catch (IOException e1) {
+ LOGGER.error(e1);
+ }
+ }
+ }
+ if (location == null || rougeFile) {
+ if (rougeFile) {
+ rougeFile = false;
+ }
+ location = properties.getProperty(id + ".url");
+ if (location != null) {
+ //
+ // Construct the URL
+ //
+ int errorCount = 0;
+ boolean error = false;
+ do {
+ error = false;
+ PapUrlResolver papUrls = PapUrlResolver.getInstance();
+ while (papUrls.hasMoreUrls()) {
+ String papID = papUrls.getUserId();
+ String papPass = papUrls.getPass();
+ Base64.Encoder encoder = Base64.getEncoder();
+ locationUri = URI.create(papUrls.getUrl(PapUrlResolver.extractIdFromUrl(location)));
+ URL url = locationUri.toURL();
+ URLConnection urlConnection = null;
+ try {
+ urlConnection = url.openConnection();
+ } catch (IOException e) {
+ LOGGER.error("Exception Occured while opening connection" + e);
+ papUrls.failed();
+ papUrls.getNext();
+ break;
+ }
+ String encoding =
+ encoder.encodeToString((papID + ":" + papPass).getBytes(StandardCharsets.UTF_8));
+ urlConnection.setRequestProperty(XacmlRestProperties.PROP_PDP_HTTP_HEADER_ID,
+ XACMLProperties.getProperty(XacmlRestProperties.PROP_PDP_ID));
+ urlConnection.setRequestProperty("Authorization", "Basic " + encoding);
+ //
+ // Now construct the output file name
+ //
+ Path outFile = Paths.get(getPDPConfig().toAbsolutePath().toString(), id);
+ //
+ // Copy it to disk
+ //
+ try (FileOutputStream fos = new FileOutputStream(outFile.toFile())) {
+ IOUtils.copy(urlConnection.getInputStream(), fos);
+ } catch (IOException e) {
+ LOGGER.error("Exception Occured while Copying input stream" + e);
+ papUrls.failed();
+ papUrls.getNext();
+ break;
+ }
+ //
+ // Now try to load
+ //
+ isFile = true;
+ try (InputStream fis = Files.newInputStream(outFile)) {
+ policy = DOMPolicyDef.load(fis);
+ } catch (Exception e) {
+ try {
+ LOGGER.error(CORRUPTED_POLICY_FILE_DELETING + location + e);
+ Files.delete(outFile);
+ error = true;
+ errorCount++;
+ break;
+ } catch (IOException e1) {
+ LOGGER.error(e1);
+ }
+ }
+ //
+ // Save it
+ //
+ properties.setProperty(id + DOT_FILE, outFile.toAbsolutePath().toString());
+ error = false;
+ break;
+ }
+ }
+ while (error && errorCount > 2);
+ }
+ }
+ if (policy != null) {
+ status.addLoadedPolicy(new StdPDPPolicy(id, isRoot, locationUri, properties));
+ LOGGER.info(
+ "Loaded policy: " + policy.getIdentifier() + " version: " + policy.getVersion().stringValue());
+ // Sending the policy objects to the Notification Controller.
+ policyContainer.put(id, policy);
+ } else {
+ String error = "Failed to load policy " + location;
+ LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + error);
+ status.setStatus(PDPStatus.Status.LOAD_ERRORS);
+ status.addLoadError(error);
+ status.addFailedPolicy(new StdPDPPolicy(id, isRoot));
+ }
+ } catch (Exception e) {
+ LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to load policy '" + id + "' from location '"
+ + location + "'", e);
+ status.setStatus(PDPStatus.Status.LOAD_ERRORS);
+ status.addFailedPolicy(new StdPDPPolicy(id, isRoot));
+ //
+ // Is it a file?
+ //
+ if (isFile) {
+ //
+ // Let's remove it
+ //
+ try {
+ LOGGER.error(
+ XACMLErrorConstants.ERROR_PROCESS_FLOW + CORRUPTED_POLICY_FILE_DELETING + location);
+ Files.delete(Paths.get(location));