PDPResponse.java
/*******************************************************************************
* Copyright 2018 IIT-CNR
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
******************************************************************************/
package it.cnr.iit.ucs.pdp;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBException;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import it.cnr.iit.utility.JAXBUtility;
import it.cnr.iit.utility.errorhandling.Reject;
import oasis.names.tc.xacml.core.schema.wd_17.DecisionType;
import oasis.names.tc.xacml.core.schema.wd_17.ObligationType;
import oasis.names.tc.xacml.core.schema.wd_17.ResponseType;
/**
* This is the result sent by the PDP
* The result of an evaluation is composed by a ResponseType that is one of those classes generated by the XJC tool
* starting from the xsd of the XACML definition. Furthermore we have an attribute that signals the id of the session to
* which the evaluation is referred to and a boolean that states if the object is in a correct state
*
* @author Antonio La Marra, Alessandro Rosetti
*/
@JsonIgnoreProperties( ignoreUnknown = true )
public final class PDPResponse implements PDPEvaluation {
private static Logger log = Logger.getLogger( PDPResponse.class.getName() );
// the response provided by the PDP object
private ResponseType responseType = null;
// list of firing rules
private ArrayList<Integer> firingRules = new ArrayList<>();
// states if the object has been correctly initialised
@JsonIgnore
private boolean initialized = false;
public PDPResponse() {}
public PDPResponse( ResponseType response ) {
setResponseType( response );
Reject.ifNull( responseType );
initialized = true;
}
private void setResponseType( ResponseType responseType ) {
this.responseType = responseType;
Reject.ifNull( responseType );
initialized = true;
}
public ResponseType getResponseType() {
return responseType;
}
@Override
@JsonIgnore
public String getResponse() {
try {
return JAXBUtility.marshalToString( ResponseType.class, responseType, "Response", JAXBUtility.SCHEMA );
} catch( JAXBException e ) {
log.log( Level.SEVERE, "Error marshalling to xml : {0}", e.getMessage() );
}
return "";
}
@Override
@JsonIgnore
public String getResult() {
Reject.ifFalse( initialized );
return responseType.getResult().get( 0 ).getDecision().value();
}
@Override
@JsonIgnore
public boolean isDecision( DecisionType decisionType ) {
return getResult().equalsIgnoreCase( decisionType.value() );
}
@Override
@JsonIgnore
public List<PDPObligationInterface> getPIPObligations() {
return new ArrayList<>();
}
@Override
@JsonIgnore
public List<PDPObligationInterface> getPEPObligations() {
return new ArrayList<>();
}
@Override
@JsonIgnore
public ArrayList<String> getObligations() {
ArrayList<String> obligations = new ArrayList<>();
if( responseType.getResult().get( 0 ).getObligations() == null ) {
return new ArrayList<>();
}
for( ObligationType obligation : responseType.getResult().get( 0 ).getObligations().getObligation() ) {
obligations.add( obligation.getObligationId() );
}
return obligations;
}
public void setFiringRules( List<Integer> firingRules ) {
this.firingRules = new ArrayList<>( firingRules );
}
@Override
public ArrayList<Integer> getFiringRules() {
return firingRules;
}
@JsonIgnore
public boolean isValid() {
return initialized;
}
}