Man hat ein Schema, z.B. das von XACML3,
http://docs.oasis-open.org/xacml/3.0/xacml-core-v3-schema-wd-17.xsd
und möchte davon mit Java eine Instanz abfüllen.
Nichts einfacher als das mit JAXB:
Man stelle das Schema in /src/main/xsd und lasse einen mvn install laufen, mit folgendem Plugin konfiguriert:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<plugin> | |
<groupId>org.codehaus.mojo</groupId> | |
<artifactId>jaxb2-maven-plugin</artifactId> | |
<version>1.5</version> | |
<executions> | |
<execution> | |
<id>xjc</id> | |
<goals> | |
<goal>xjc</goal> | |
</goals> | |
</execution> | |
</executions> | |
</plugin> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.StringReader; | |
import java.io.StringWriter; | |
import javax.xml.bind.JAXB; | |
import oasis.names.tc.xacml._3_0.core.schema.wd_17.DecisionType; | |
import oasis.names.tc.xacml._3_0.core.schema.wd_17.RequestType; | |
import oasis.names.tc.xacml._3_0.core.schema.wd_17.ResponseType; | |
import oasis.names.tc.xacml._3_0.core.schema.wd_17.ResultType; | |
import oasis.names.tc.xacml._3_0.core.schema.wd_17.StatusType; | |
public class XacmlMarshallUtil { | |
public static Result extractResultFromXacmlResponseString(String xacmlResponseAsString) { | |
StringReader reader = new StringReader(xacmlResponseAsString); | |
ResponseType responseType = JAXB.unmarshal(reader, ResponseType.class); | |
ResultType resultType = responseType.getResult().get(0); | |
DecisionType decision = resultType.getDecision(); | |
StatusType status = resultType.getStatus(); | |
Result result = mapToOurResult(decision, status); | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.axa.ch.authorization.client.pep.facade; | |
import static org.junit.Assert.assertEquals; | |
import java.io.IOException; | |
import org.junit.Test; | |
import org.xml.sax.SAXException; | |
import junit.framework.Assert; | |
public class XacmlMarshallUtilTest { | |
@Test | |
public void testExtractResultFromXacmlResponseString() { | |
Result result = XacmlMarshallUtil.extractResultFromXacmlResponseString(createResponse()); | |
assertEquals(Result.Decision.Deny, result.getDecision()); | |
} | |
private String createResponse() { | |
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?> " | |
+ "<Response xmlns=\"urn:oasis:names:tc:xacml:3.0:core:schema:wd-17\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"urn:oasis:names:tc:xacml:3.0:core:schema:wd-17 http://docs.oasis-open.org/xacml/3.0/xacml-core-v3-schema-wd-17.xsd\">" | |
+ "<Result><Decision>Deny</Decision></Result></Response>"; | |
} | |
} |
Die generierten Klassen landen in target und werden von eclipse automatisch in den build-path genommen. Fertig.
Die Klassen können dann ganz einfach benützt werden: hier wird z.B. ein XACML-String von JAXB geparst und in Java umgewandelt:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.StringReader; | |
import java.io.StringWriter; | |
import javax.xml.bind.JAXB; | |
import oasis.names.tc.xacml._3_0.core.schema.wd_17.DecisionType; | |
import oasis.names.tc.xacml._3_0.core.schema.wd_17.RequestType; | |
import oasis.names.tc.xacml._3_0.core.schema.wd_17.ResponseType; | |
import oasis.names.tc.xacml._3_0.core.schema.wd_17.ResultType; | |
import oasis.names.tc.xacml._3_0.core.schema.wd_17.StatusType; | |
public class XacmlMarshallUtil { | |
public static Result extractResultFromXacmlResponseString(String xacmlResponseAsString) { | |
StringReader reader = new StringReader(xacmlResponseAsString); | |
ResponseType responseType = JAXB.unmarshal(reader, ResponseType.class); | |
ResultType resultType = responseType.getResult().get(0); | |
DecisionType decision = resultType.getDecision(); | |
StatusType status = resultType.getStatus(); | |
Result result = mapToOurResult(decision, status); | |
return result; | |
} |
Keine Kommentare:
Kommentar veröffentlichen