Skip to content

Commit

Permalink
xacml4j-24 Marshalling to file results in com.sun.istack.internal.SAX…
Browse files Browse the repository at this point in the history
…Exception2

Fixes #24
  • Loading branch information
valdas-s committed Nov 4, 2014
1 parent d63f0df commit a32d61e
Show file tree
Hide file tree
Showing 13 changed files with 232 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,13 @@ public boolean equals(Object o){
if(o == this){
return true;
}
if(o == null){
return false;
}
if(!(o instanceof AttributeSelectorKey)){
return false;
}
AttributeSelectorKey s = (AttributeSelectorKey)o;
return category.equals(s.category) &&
dataType.equals(s.dataType) && xpath.equals(s.xpath) &&
dataType.equals(s.dataType) &&
xpath.equals(s.xpath) &&
Objects.equal(contextSelectorId, s.contextSelectorId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,22 @@ public boolean handleEvent(ValidationEvent event) {
});

JAXBElement<?> jaxbInstance = null;
if(source instanceof InputSource){
jaxbInstance = (JAXBElement<?>)u.unmarshal((InputSource)source);
if (source instanceof InputSource) {
jaxbInstance = (JAXBElement<?>) u.unmarshal((InputSource) source);
} else if (source instanceof InputStream) {
jaxbInstance = (JAXBElement<?>) u.unmarshal((InputStream) source);
} else if (source instanceof JAXBElement<?>) {
jaxbInstance = (JAXBElement<?>) source;
} else if (source instanceof XMLStreamReader) {
jaxbInstance = (JAXBElement<?>) u.unmarshal((XMLStreamReader) source);
} else if (source instanceof Node) {
jaxbInstance = (JAXBElement<?>) u.unmarshal((Node) source);
} else if (source instanceof Source) {
jaxbInstance = (JAXBElement<?>) u.unmarshal((Source) source);
} else if (source instanceof byte[]) {
jaxbInstance = (JAXBElement<?>) u.unmarshal(new ByteArrayInputStream((byte[]) source));
}
if(source instanceof InputStream){
jaxbInstance = (JAXBElement<?>)u.unmarshal((InputStream)source);
}
if(source instanceof JAXBElement<?>){
jaxbInstance = (JAXBElement<?>)source;
}
if(source instanceof XMLStreamReader){
jaxbInstance = (JAXBElement<?>)u.unmarshal((XMLStreamReader)source);
}
if(source instanceof Node){
jaxbInstance = (JAXBElement<?>)u.unmarshal((Node)source);
}
if(source instanceof Source){
jaxbInstance = (JAXBElement<?>)u.unmarshal((Source)source);
}
if(source instanceof byte[]){
jaxbInstance = (JAXBElement<?>)u.unmarshal(new ByteArrayInputStream((byte[])source));
}
if(jaxbInstance != null){
if (jaxbInstance != null) {
return create(jaxbInstance);
}
throw new IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@

public class JAXBContextUtil
{
private static JAXBContext INSTANCE;

private static final JAXBContext INSTANCE;
private static final char SEP = ':';
static{
StringBuilder b = new StringBuilder();
b.append(org.oasis.xacml.v30.jaxb.ObjectFactory.class.getPackage().getName()).append(":");
b.append(org.oasis.xacml.v20.jaxb.policy.ObjectFactory.class.getPackage().getName()).append(":");
b.append(org.oasis.xacml.v20.jaxb.context.ObjectFactory.class.getPackage().getName());
try{
INSTANCE = JAXBContext.newInstance(b.toString());
try{
INSTANCE = JAXBContext.newInstance(
org.oasis.xacml.v30.jaxb.ObjectFactory.class.getPackage().getName() +
SEP +
org.oasis.xacml.v20.jaxb.policy.ObjectFactory.class.getPackage().getName() +
SEP +
org.oasis.xacml.v20.jaxb.context.ObjectFactory.class.getPackage().getName());
}catch(JAXBException e){
e.printStackTrace(System.err);
throw new IllegalStateException("Failed to initialize JAXB context", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,39 +88,33 @@ public class Xacml30PolicyFromObjectModelToJaxbMapper

private final static ObjectFactory factory = new ObjectFactory();

private final static Map<Effect, EffectType> nativeToJaxbEffectMappings;
private final static Map<Effect, EffectType> nativeToJaxbEffectMappings = ImmutableMap.of(
Effect.DENY, EffectType.DENY,
Effect.PERMIT, EffectType.PERMIT);

static {
ImmutableMap.Builder<Effect, EffectType> nativeToJaxbB = ImmutableMap.builder();
nativeToJaxbB.put(Effect.DENY, EffectType.DENY);
nativeToJaxbB.put(Effect.PERMIT, EffectType.PERMIT);
nativeToJaxbEffectMappings = nativeToJaxbB.build();
}


public JAXBElement<?> toJaxb(CompositeDecisionRule d){
public JAXBElement<?> map(CompositeDecisionRule d){
if(d instanceof PolicySet){
return toJaxb((PolicySet)d);
return factory.createPolicySet(toJaxb((PolicySet)d));
}
if(d instanceof Policy){
return toJaxb((Policy)d);
return factory.createPolicy(toJaxb((Policy)d));
}
if(d instanceof Rule){
return toJaxb((Rule)d);
return factory.createRule(toJaxb((Rule)d));
}
if(d instanceof PolicyIDReference){
return toJaxb((PolicyIDReference)d);
return factory.createPolicyIdReference(toJaxb((PolicyIDReference)d));
}
if(d instanceof PolicySetIDReference){
return toJaxb((PolicySetIDReference)d);
return factory.createPolicySetIdReference(toJaxb((PolicySetIDReference)d));
}
throw new IllegalArgumentException(
String.format(
"Unsupported decision rule type=\"%s\"",
d.getClass().getName()));
}

private JAXBElement<?> toJaxb(PolicyIDReference ref)
private IdReferenceType toJaxb(PolicyIDReference ref)
{
IdReferenceType jaxbRef = factory.createIdReferenceType();
if(ref.getEarliestVersion() != null){
Expand All @@ -133,10 +127,10 @@ private JAXBElement<?> toJaxb(PolicyIDReference ref)
jaxbRef.setLatestVersion(ref.getVersion().getPattern());
}
jaxbRef.setValue(ref.getId());
return factory.createPolicyIdReference(jaxbRef);
return jaxbRef;
}

private JAXBElement<?> toJaxb(PolicySetIDReference ref)
private IdReferenceType toJaxb(PolicySetIDReference ref)
{
IdReferenceType jaxbRef = factory.createIdReferenceType();
if(ref.getEarliestVersion() != null){
Expand All @@ -149,10 +143,10 @@ private JAXBElement<?> toJaxb(PolicySetIDReference ref)
jaxbRef.setLatestVersion(ref.getVersion().getPattern());
}
jaxbRef.setValue(ref.getId());
return factory.createPolicySetIdReference(jaxbRef);
return jaxbRef;
}

private JAXBElement<?> toJaxb(Rule rule){
private RuleType toJaxb(Rule rule){
if(log.isDebugEnabled()){
log.debug("Mapping Rule id=\"{}\"", rule.getId());
}
Expand All @@ -170,14 +164,15 @@ private JAXBElement<?> toJaxb(Rule rule){
}
jaxbRule.setAdviceExpressions(toJaxbAdvices(rule.getAdviceExpressions()));
jaxbRule.setObligationExpressions(toJaxbObligations(rule.getObligationExpressions()));
return factory.createRule(jaxbRule);
return jaxbRule;
}

private JAXBElement<?> toJaxb(Policy p){
private PolicyType toJaxb(Policy p){
if(log.isDebugEnabled()){
log.debug("Mapping Policy id=\"{}\"", p.getId());
}
PolicyType jaxbPolicy = factory.createPolicyType();
jaxbPolicy.setVersion(p.getVersion().getValue());
jaxbPolicy.setPolicyId(p.getId());
jaxbPolicy.setPolicyIssuer(toJaxb(p.getIssuer()));
jaxbPolicy.setPolicyDefaults(toJaxb(p.getDefaults()));
Expand All @@ -197,15 +192,16 @@ private JAXBElement<?> toJaxb(Policy p){
for(Rule r : p.getRules()){
jaxbPolicy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(toJaxb(r));
}
return factory.createPolicy(jaxbPolicy);
return jaxbPolicy;
}

private JAXBElement<?> toJaxb(PolicySet ps){
private PolicySetType toJaxb(PolicySet ps){
if(log.isDebugEnabled()){
log.debug("Mapping PolicySet id=\"{}\"", ps.getId());
}
PolicySetType jaxbPolicySet = factory.createPolicySetType();
jaxbPolicySet.setPolicySetId(ps.getId());
jaxbPolicySet.setVersion(ps.getVersion().getValue());
jaxbPolicySet.setPolicyIssuer(toJaxb(ps.getIssuer()));
jaxbPolicySet.setPolicySetDefaults(toJaxb(ps.getDefaults()));
jaxbPolicySet.setDescription(ps.getDescription());
Expand All @@ -219,9 +215,9 @@ private JAXBElement<?> toJaxb(PolicySet ps){
jaxbPolicySet.setAdviceExpressions(toJaxbAdvices(ps.getAdviceExpressions()));
jaxbPolicySet.setObligationExpressions(toJaxbObligations(ps.getObligationExpressions()));
for(CompositeDecisionRule r : ps.getDecisions()){
jaxbPolicySet.getPolicySetOrPolicyOrPolicySetIdReference().add(toJaxb(r));
jaxbPolicySet.getPolicySetOrPolicyOrPolicySetIdReference().add(map(r));
}
return factory.createPolicySet(jaxbPolicySet);
return jaxbPolicySet;
}

private DefaultsType toJaxb(PolicyDefaults d)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
public class Xacml30PolicyMarshaller extends BaseJAXBMarshaller<CompositeDecisionRule>
implements PolicyMarshaller
{
private Xacml30PolicyFromObjectModelToJaxbMapper mapper;
private final Xacml30PolicyFromObjectModelToJaxbMapper mapper;

public Xacml30PolicyMarshaller() {
super(JAXBContextUtil.getInstance());
this.mapper = new Xacml30PolicyFromObjectModelToJaxbMapper();
}

@Override
public Object marshal(CompositeDecisionRule d) throws IOException {
return mapper.toJaxb(d);
return mapper.map(d);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,14 @@ public BagOfAttributeExp evaluate(EvaluationContext context)
}
return getDataType().bagType().createEmpty();
}
if((v == null || v.isEmpty()) &&
isMustBePresent()){
if((v == null || v.isEmpty()) && isMustBePresent()){
if(log.isDebugEnabled()){
log.debug("Failed to resolve attributeId=\"{}\", category=\"{}\"",
designatorKey.getAttributeId(), designatorKey.getCategory());
}
throw new AttributeReferenceEvaluationException(designatorKey);
}
return ((v == null)?getDataType().bagType().createEmpty():v);
return (v == null) ? getDataType().bagType().createEmpty() : v;
}

@Override
Expand All @@ -137,15 +136,12 @@ public boolean equals(Object o){
if(o == this){
return true;
}
if(o == null){
return false;
}
if(!(o instanceof AttributeDesignator)){
return false;
}
AttributeDesignator d = (AttributeDesignator)o;
return designatorKey.equals(d.designatorKey) &&
(isMustBePresent() ^ d.isMustBePresent());
isMustBePresent() == d.isMustBePresent();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,12 @@ public boolean equals(Object o){
if(o == this){
return true;
}
if(o == null){
return false;
}
if(!(o instanceof AttributeSelector)){
return false;
}
AttributeSelector s = (AttributeSelector)o;
return selectorKey.equals(s.selectorKey) &&
(isMustBePresent() ^ s.isMustBePresent());
isMustBePresent() == s.isMustBePresent();
}

@Override
Expand Down Expand Up @@ -113,7 +110,7 @@ public BagOfAttributeExp evaluate(EvaluationContext context)
&& isMustBePresent()){
if(log.isDebugEnabled()){
log.debug("Failed to resolve xpath=\"{}\", category=\"{}\"",
selectorKey.getPath(),
selectorKey.getPath(),
selectorKey.getCategory());
}
throw new AttributeReferenceEvaluationException(selectorKey);
Expand Down
3 changes: 0 additions & 3 deletions xacml-core/src/main/java/org/xacml4j/v30/pdp/MatchAnyOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ public int hashCode(){

@Override
public boolean equals(Object o){
if(o == null){
return false;
}
if(o == this){
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

public class VariableReference implements Expression
{
private VariableDefinition varDef;
private final VariableDefinition varDef;

/**
* Constructs variable reference with a given definition.
Expand Down Expand Up @@ -103,13 +103,10 @@ public String toString(){

@Override
public boolean equals(Object o){
if(o == this){
if (o == this) {
return true;
}
if(o == null){
return false;
}
if(!(o instanceof VariableReference)){
if (!(o instanceof VariableReference)) {
return false;
}
VariableReference r = (VariableReference)o;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,11 @@ public FunctionSpecImpl(
FunctionInvocation invocation,
FunctionParametersValidator validator,
boolean evaluateParameters){
Preconditions.checkNotNull(functionId);
Preconditions.checkNotNull(params);
Preconditions.checkNotNull(invocation);
Preconditions.checkNotNull(resolver);
this.functionId = functionId;
parameters.addAll(params);
this.resolver = resolver;
this.functionId = Preconditions.checkNotNull(functionId);
parameters.addAll(Preconditions.checkNotNull(params));
this.resolver = Preconditions.checkNotNull(resolver);
this.validator = validator;
this.invocation = invocation;
this.invocation = Preconditions.checkNotNull(invocation);
this.evaluateParameters = evaluateParameters;
this.legacyId = legacyId;
}
Expand Down Expand Up @@ -320,10 +316,10 @@ public <T extends ValueExpression> T invoke(EvaluationContext context,
functionId, normalizedArgs);
}
T result = (T)invocation.invoke(this, context,
isRequiresLazyParamEval()?normalizedArgs:evaluate(context, normalizedArgs));
evaluateParameters ?normalizedArgs:evaluate(context, normalizedArgs));
if(log.isDebugEnabled()){
log.debug("Function=\"{}\" " +
"invocation result=\"{}\"", getId(), result);
"invocation result=\"{}\"", functionId, result);
}
return result;
}
Expand All @@ -335,7 +331,7 @@ public <T extends ValueExpression> T invoke(EvaluationContext context,
log.debug("Failed to invoke function", e);
}
throw new FunctionInvocationException(this, e,
"Failed to invoke function=\"%s\"", getId());
"Failed to invoke function=\"%s\"", functionId);
}
}

Expand Down Expand Up @@ -552,6 +548,22 @@ public String toString(){
.add("params", parameters)
.toString();
}

@Override
public int hashCode() {
return functionId.hashCode();
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if(!(obj instanceof FunctionSpecImpl)) {
return false;
}
return functionId.equals(((FunctionSpecImpl) obj).functionId);
}
}

}
Loading

0 comments on commit a32d61e

Please sign in to comment.