package at.gv.egovernment.moa.id.auth.validator.parep.client.szrgw; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import at.gv.egovernment.moa.id.auth.data.InfoboxValidatorParams; import at.gv.egovernment.moa.id.auth.validator.parep.ParepUtils; import at.gv.egovernment.moa.util.Constants; /** * This class implements a detailed CreateMandateRequest that * will be sent to SZR-gateway. * * @author Peter Danner */ public class CreateMandateRequest { /** * The Request. */ private Document document; /** * List of mandate representatives as XML element. */ private List representatives; /** * The mandator. */ private Element mandator; /** * The representative. */ private Element representative; /** * The signature to verify by the SZR-gateway */ private Element signature; /** * Creates the CreateMandateRequest element that will * be sent to SZR-gateway * * @return the CreateMandateRequest element. */ public Element toElement() throws SZRGWClientException{ this.document = ParepUtils.createEmptyDocument(); Element root = this.document.createElement(SZRGWConstants.SZRGW_PREFIX + SZRGWConstants.CREATE_MANDATE_REQUEST); root.setAttribute("xmlns" + SZRGWConstants.SZRGW_POSTFIX, SZRGWConstants.SZRGW_REQUEST_NS); root.setAttribute("xmlns" + SZRGWConstants.PD_POSTFIX, Constants.PD_NS_URI); if (this.representative!=null) root.appendChild(this.document.importNode(this.representative, true)); if (this.mandator!=null) root.appendChild(this.document.importNode(this.mandator, true)); if (this.signature!=null) root.appendChild(this.document.importNode(this.signature, true)); return root; } /** * Adds a representative. * * @param representative an XML representative to add. */ public void addRepresentative(Element representative) { if (representatives == null) { representatives = new ArrayList(); } representatives.add(representative); } /** * Gets the representative. * * @return the representative. */ public Element getRepresentative() { return representative; } /** * Gets the mandator. * * @return the mandator. */ public Element getMandator() { return mandator; } /** * Sets the mandator. * * @param mandator the mandator. */ public void setMandator(Element mandator) { this.mandator = mandator; } /** * Sets the Mandator. * * @param familyName the family name of the mandator. */ public void setMandator(String familyName, String givenName, String dateOfBirth, String postalCode, String municipality, String streetName, String buildingNumber, String unit, boolean physical, String cbFullName, String cbIdentificationType, String cbIdentificationValue) throws SZRGWClientException { Document mandatorDocument = ParepUtils.createEmptyDocument(); Element mandatorElem = mandatorDocument.createElementNS(SZRGWConstants.SZRGW_REQUEST_NS, SZRGWConstants.SZRGW_PREFIX + SZRGWConstants.MANDATOR); // mandatorElem.setAttribute("xmlns" + SZRGWConstants.PD_POSTFIX, Constants.PD_NS_URI); /// mandatorElem.setAttribute("xmlns" + SZRGWConstants.SZRGW_POSTFIX, SZRGWConstants.SZRGW_REQUEST_NS); if (physical) { Element physicalPersonElem = mandatorDocument.createElementNS(Constants.PD_NS_URI, SZRGWConstants.PD_PREFIX + SZRGWConstants.PHYSICALPERSON); physicalPersonElem.appendChild(createNameElem(mandatorDocument, givenName, familyName)); physicalPersonElem.appendChild(createPersonDataElem(mandatorDocument, SZRGWConstants.DATEOFBIRTH, dateOfBirth)); mandatorElem.appendChild(physicalPersonElem); Element postalAddressElement = createPostalAddressElem(mandatorDocument, postalCode, municipality, streetName, buildingNumber, unit); if (null!=postalAddressElement) mandatorElem.appendChild(postalAddressElement); } else { Element corporateBodyElem = mandatorDocument.createElementNS(Constants.PD_NS_URI, SZRGWConstants.PD_PREFIX + SZRGWConstants.CORPORATEBODY); corporateBodyElem.appendChild(createIdentificationElem(mandatorDocument, cbIdentificationType, cbIdentificationValue)); corporateBodyElem.appendChild(createPersonDataElem(mandatorDocument, SZRGWConstants.FULLNAME, cbFullName)); mandatorElem.appendChild(corporateBodyElem); } this.mandator = mandatorElem; } private Element createPersonDataElem(Document document, String elementName, String elementValue) { Element elem = document.createElementNS(Constants.PD_NS_URI, SZRGWConstants.PD_PREFIX + elementName); Node value = document.createTextNode(elementValue); elem.appendChild(value); return elem; } private Element createIdentificationElem(Document document, String identificationType, String identificationValue) { Element identificationElem = document.createElementNS(Constants.PD_NS_URI, SZRGWConstants.PD_PREFIX + SZRGWConstants.IDENTIFICATION); identificationElem.appendChild(createPersonDataElem(document, SZRGWConstants.VALUE, identificationValue)); identificationElem.appendChild(createPersonDataElem(document, SZRGWConstants.TYPE, identificationType)); return identificationElem; } private Element createNameElem(Document document, String givenName, String familyName) { Element nameElem = document.createElementNS(Constants.PD_NS_URI, SZRGWConstants.PD_PREFIX + SZRGWConstants.NAME); nameElem.appendChild(createPersonDataElem(document, SZRGWConstants.GIVENNAME, givenName)); nameElem.appendChild(createPersonDataElem(document, SZRGWConstants.FAMILYNAME, familyName)); return nameElem; } private Element createPostalAddressElem(Document document, String postalCode, String municipality, String streetName, String buildingNumber, String unit) { if (ParepUtils.isEmpty(postalCode) && ParepUtils.isEmpty(municipality) && ParepUtils.isEmpty(streetName) && ParepUtils.isEmpty(buildingNumber) && ParepUtils.isEmpty(unit)) return null; Element postalAddressElem = document.createElementNS(Constants.PD_NS_URI, SZRGWConstants.PD_PREFIX + SZRGWConstants.POSTALADDRESS); if (!ParepUtils.isEmpty(postalCode)) { postalAddressElem.appendChild(createPersonDataElem(document, SZRGWConstants.POSTALCODE, postalCode)); } if (!ParepUtils.isEmpty(municipality)) { postalAddressElem.appendChild(createPersonDataElem(document, SZRGWConstants.MUNICIPALITY, municipality)); } if (!ParepUtils.isEmpty(streetName) || !ParepUtils.isEmpty(buildingNumber) || !ParepUtils.isEmpty(unit)) { Element deliveryAddressElem = document.createElementNS(Constants.PD_NS_URI, SZRGWConstants.PD_PREFIX + SZRGWConstants.DELIVERYADDRESS); if (!ParepUtils.isEmpty(streetName)) { deliveryAddressElem.appendChild(createPersonDataElem(document, SZRGWConstants.STREETNAME, streetName)); } if (!ParepUtils.isEmpty(buildingNumber)) { deliveryAddressElem.appendChild(createPersonDataElem(document, SZRGWConstants.BUILDINGNUMBER, buildingNumber)); } if (!ParepUtils.isEmpty(unit)) { deliveryAddressElem.appendChild(createPersonDataElem(document, SZRGWConstants.UNIT, unit)); } postalAddressElem.appendChild(deliveryAddressElem); } return postalAddressElem; } /** * Sets the Representative. * * @param params InfoboxValidatorParams contain the data of the representative. * @param identificationType the type of the identification of the representative (has to be urn:publicid:gv.at:cdid). * @param identificationValue the identification value (bPK). */ public void setRepresentative(InfoboxValidatorParams params, String identificationType, String identificationValue) throws SZRGWClientException { Document representativeDocument = ParepUtils.createEmptyDocument(); Element representativeElem = representativeDocument.createElementNS(SZRGWConstants.SZRGW_REQUEST_NS, SZRGWConstants.SZRGW_PREFIX + SZRGWConstants.REPRESENTATIVE); // representativeElem.setAttribute("xmlns" + SZRGWConstants.PD_POSTFIX, Constants.PD_NS_URI); // representativeElem.setAttribute("xmlns" + SZRGWConstants.SZRGW_POSTFIX, SZRGWConstants.SZRGW_REQUEST_NS); representativeElem.appendChild(createIdentificationElem(representativeDocument, identificationType, identificationValue)); representativeElem.appendChild(createNameElem(representativeDocument, params.getGivenName(), params.getFamilyName())); representativeElem.appendChild(createPersonDataElem(representativeDocument, SZRGWConstants.DATEOFBIRTH, params.getDateOfBirth())); this.representative = representativeElem; } /** * @return the signature */ public Element getSignature() { return signature; } /** * @param signature the signature to set */ public void setSignature(Element signature) throws SZRGWClientException{ Document signatureDocument = ParepUtils.createEmptyDocument(); Element signatureElem = signatureDocument.createElementNS(SZRGWConstants.SZRGW_REQUEST_NS, SZRGWConstants.SZRGW_PREFIX + "Signature"); //SZR-gateway takes the first Signature //signatureElem.setAttribute("SignatureLocation", "//saml:Assertion/dsig:Signature"); signatureElem.appendChild(signatureDocument.importNode(signature, true)); this.signature = signatureElem; } }