org.certificateservices.messages.csmessages.BasePayloadParser Maven / Gradle / Ivy
/**
*
*/
package org.certificateservices.messages.csmessages;
import java.util.List;
import java.util.Properties;
import javax.xml.bind.JAXBElement;
import org.certificateservices.messages.MessageContentException;
import org.certificateservices.messages.MessageProcessingException;
import org.certificateservices.messages.MessageSecurityProvider;
import org.certificateservices.messages.csmessages.jaxb.CSMessage;
import org.certificateservices.messages.csmessages.jaxb.CSResponse;
import org.certificateservices.messages.csmessages.jaxb.Credential;
import org.certificateservices.messages.csmessages.jaxb.IsApprovedResponseType;
import org.certificateservices.messages.csmessages.jaxb.ObjectFactory;
import org.certificateservices.messages.csmessages.jaxb.RequestStatus;
/**
* Base implementation of a PayLoadParser that other implementations might inherit.
*
* @author Philip Vendil
*
*/
public abstract class BasePayloadParser implements PayloadParser {
protected Properties config;
protected MessageSecurityProvider secProv;
protected CSMessageParser customCSMessageParser;
protected ObjectFactory csMessageObjectFactory = new ObjectFactory();
protected String payloadVersion = getDefaultPayloadVersion();
/**
* Default initializer setting the parser and config properties.
*
* @see org.certificateservices.messages.csmessages.PayloadParser#init(java.util.Properties, MessageSecurityProvider)
*/
public void init(Properties config, MessageSecurityProvider secProv)
throws MessageProcessingException {
this.config = config;
this.secProv = secProv;
}
/**
* Alternativ initializer setting the parser and config properties and custom CSMessageParser.
* Usen mainly during testing.
*
* @see org.certificateservices.messages.csmessages.PayloadParser#init(java.util.Properties, MessageSecurityProvider)
*/
public void init(Properties config, MessageSecurityProvider secProv, CSMessageParser customCSMessageParser)
throws MessageProcessingException {
this.config = config;
this.secProv = secProv;
this.customCSMessageParser = customCSMessageParser;
}
protected CSMessageParser getCSMessageParser() throws MessageProcessingException {
if(customCSMessageParser != null){
return customCSMessageParser;
}
return CSMessageParserManager.getCSMessageParser();
}
/**
* Method to parse a message into a CSMessage and verify that it fulfills the registred schemas.
*
* This method will parse all registered payloads and not only sys config payload messages.
*
* This method always validates and authorizes the signing certificate.
*
* @param messageData the data to parse into a CSMessage
* @return a parsed CS Message object.
*
* @throws MessageContentException if input data contained invalid format.
* @throws MessageProcessingException if internal problems occurred processing the cs message.
*/
public CSMessage parseMessage(byte[] messageData) throws MessageContentException, MessageProcessingException{
return getCSMessageParser().parseMessage(messageData);
}
/**
* Method to parse a message into a CSMessage and verify that it fulfills the registred schemas.
*
* This method will parse all registered payloads and not only sys config payload messages.
*
* @param messageData the data to parse into a CSMessage
* @param performValidation true if the message security provider should perform
* validate that the signing certificate is valid and authorized for related organisation.
* Otherwise must validation be performed manually after the message is parsed
* @return a parsed CS Message object.
*
* @throws MessageContentException if input data contained invalid format.
* @throws MessageProcessingException if internal problems occurred processing the cs message.
*/
public CSMessage parseMessage(byte[] messageData, boolean performValidation) throws MessageContentException, MessageProcessingException{
return getCSMessageParser().parseMessage(messageData, performValidation);
}
/**
* Method to parse a message into a CSMessage and verify that it fulfills the registred schemas.
*
* @param messageData the data to parse into a CSMessage
* @param performValidation true if the message security provider should perform
* validate that the signing certificate is valid and authorized for related organisation.
* Otherwise must validation be performed manually after the message is parsed.
* @param requireSignature if signature should be required.
* @return a parsed CS Message object.
*
* @throws MessageContentException if input data contained invalid format.
* @throws MessageProcessingException if internal problems occurred processing the cs message.
*/
public CSMessage parseMessage(byte[] messageData, boolean performValidation, boolean requireSignature)
throws MessageContentException, MessageProcessingException{
return getCSMessageParser().parseMessage(messageData, performValidation, requireSignature);
}
/**
* Help method to get the request status from a CS response message.
* @param csMessage containing a CS response message.
* @return the request status.
*
* @throws MessageContentException if message content was illegal.
*/
@SuppressWarnings("unchecked")
public RequestStatus getResponseStatus(CSMessage csMessage) throws MessageContentException{
try{
Object responsePayload = csMessage.getPayload().getAny();
if(responsePayload instanceof JAXBElement && ((JAXBElement) responsePayload).getValue() instanceof CSResponse){
return ((JAXBElement) responsePayload).getValue().getStatus();
}
if(responsePayload instanceof CSResponse){
return ((CSResponse) responsePayload).getStatus();
}
}catch(Exception e){
throw new MessageContentException("Error parsing CSResponse status from message: " + e.getMessage(),e);
}
throw new MessageContentException("Error parsing CSResponse status from message, make sure it is a CSResponse.");
}
/**
* Help method to get the payload of a message.
* @param csMessage containing a CS message payload.
* @return the payload object
*
* @throws MessageContentException if message content was illegal.
*/
public Object getPayload(CSMessage csMessage) throws MessageContentException{
try{
Object responsePayload = csMessage.getPayload().getAny();
if(responsePayload instanceof JAXBElement){
return ((JAXBElement) csMessage.getPayload().getAny()).getValue();
}
return responsePayload;
}catch(Exception e){
throw new MessageContentException("Error parsing payload from message: " + e.getMessage(),e);
}
}
/**
* Help method to retrieve the assertions from an approved IsApprovedResponseType payload
*
* @param isApprovedResponse the payload if a IsApprovedResponse or GetApprovedResponse
* @return the list of assertions or null if no assertions could be found.
*/
public List