X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ONAP-XACML%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fxacml%2Futil%2FXACMLPolicyWriter.java;h=2aaa4e12461ea34fb4ca452f9ea9b2db43f3724e;hb=1134bd40da28d3833a0dd4f821e75ec938f6061f;hp=928165909e7b0894f9295a344faac48ce0e9c01d;hpb=5ad6783ca5a09c0061b661a48210f6d8e22cd7d3;p=policy%2Fengine.git diff --git a/ONAP-XACML/src/main/java/org/onap/policy/xacml/util/XACMLPolicyWriter.java b/ONAP-XACML/src/main/java/org/onap/policy/xacml/util/XACMLPolicyWriter.java index 928165909..2aaa4e124 100644 --- a/ONAP-XACML/src/main/java/org/onap/policy/xacml/util/XACMLPolicyWriter.java +++ b/ONAP-XACML/src/main/java/org/onap/policy/xacml/util/XACMLPolicyWriter.java @@ -137,11 +137,28 @@ public class XACMLPolicyWriter { * * */ - public static InputStream getXmlAsInputStream(PolicyType policy) { - JAXBElement policyElement = new ObjectFactory().createPolicy(policy); + public static InputStream getXmlAsInputStream(Object policy) { + JAXBElement policyElement; + if (policy instanceof PolicyType) { + policyElement = new ObjectFactory().createPolicy((PolicyType) policy); + return getByteArrayInputStream(policyElement, PolicyType.class); + } else if (policy instanceof PolicySetType) { + policyElement = new ObjectFactory().createPolicySet((PolicySetType) policy); + return getByteArrayInputStream(policyElement, PolicySetType.class); + } + return null; + } + + /** + * Helper static class that reads the JAXB element and return policy input stream. + * @param policyElement + * @param className (PolicyType or PolicySetType ?). + * @return ByteArrayInputStream. + */ + public static InputStream getByteArrayInputStream(JAXBElement policyElement, Class className) { try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - JAXBContext context = JAXBContext.newInstance(PolicyType.class); + JAXBContext context = JAXBContext.newInstance(className); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); m.marshal(policyElement, byteArrayOutputStream); @@ -149,7 +166,7 @@ public class XACMLPolicyWriter { } catch (JAXBException e) { PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPolicyWriter", "writePolicyFile failed"); throw new IllegalArgumentException("XACMLPolicyWriter writePolicyFile failed", e); - } + } } /** * Helper static class that does the work to write a policy set to an output stream.