/* * Copyright 2003 Federal Chancellery Austria * MOA-SPSS has been developed in a cooperation between BRZ, the Federal * Chancellery Austria - ICT staff unit, and Graz University of Technology. * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://www.osor.eu/eupl/ * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. */ package at.gv.egovernment.moa.spss.server.xmlbind; import java.io.IOException; import org.w3c.dom.Element; import org.w3c.dom.traversal.NodeIterator; import at.gv.egovernment.moa.spss.MOAApplicationException; import at.gv.egovernment.moaspss.util.Base64Utils; import at.gv.egovernment.moaspss.util.Constants; import at.gv.egovernment.moaspss.util.DOMUtils; import at.gv.egovernment.moaspss.util.XPathUtils; /** * A parser to parse CreateCMSSignatureRequest DOM trees into * CreateCMSSignatureRequest API objects. * * @author Patrick Peck * @version $Id$ */ public class CreatePDFSignatureRequestParser { // // XPath expresssions to select elements in the CreateCMSSignatureRequest // private static final String MOA = Constants.MOA_PREFIX + ":"; private static final String KEY_IDENTIFIER_XPATH = "/" + MOA + "CreatePDFSignatureRequest/" + MOA + "KeyIdentifier"; private static final String SINGLE_SIGNATURE_INFO_XPATH = "/" + MOA + "CreatePDFSignatureRequest/" + MOA + "SingleSignatureInfo"; private static final String SIGNATURE_PROFILE_XPATH = MOA + "SignatureProfile"; private static final String SIGNATURE_POSITION_XPATH = MOA + "SignaturePosition"; private static final String SIGNATURE_ID_XPATH = MOA + "SignatureID"; private static final String PDF_DOCUMENT_XPATH = MOA + "PDFDocument"; /** * Create a new CreateCMSSignatureRequestParser. */ public CreatePDFSignatureRequestParser() { } /** * Parse a CreateCMSSignatureRequest DOM element, as defined by the * MOA schema. * * @param requestElem The CreateCMSSignatureRequest to parse. The * request must have been successfully parsed against the * schema for this method to succeed. * @return A CreateCMSSignatureRequest API object containing the * data from the DOM element. * @throws MOAApplicationException An error occurred parsing the request. */ public CreatePDFRequest parse(Element requestElem) throws MOAApplicationException { final String keyIdentifier = XPathUtils.getElementValue(requestElem, KEY_IDENTIFIER_XPATH, null); final CreatePDFRequest createPDFRequest = new CreatePDFRequest(keyIdentifier); parseSingleSignatureInfos(requestElem, createPDFRequest); return createPDFRequest; } /** * Parse all SingleSignatureInfo elements of the * CreateCMSSignatureRequest. * * @param requestElem The CreateCMSSignatureRequest to parse. * @return A List of SingleSignatureInfo API objects. * @throws MOAApplicationException An error occurred parsing on of the * SingleSignatureInfo elements. */ private void parseSingleSignatureInfos(Element requestElem, CreatePDFRequest createPDFRequest) throws MOAApplicationException { final NodeIterator sigInfoElems = XPathUtils.selectNodeIterator(requestElem, SINGLE_SIGNATURE_INFO_XPATH); Element sigInfoElem; while ((sigInfoElem = (Element) sigInfoElems.nextNode()) != null) { createPDFRequest.getSignatureInfoList().add(parsePDFSignatureInfo(sigInfoElem)); } } /** * Parse a SingleSignatureInfo DOM element. * * @param sigInfoElem The SingleSignatureInfo DOM element to parse. * @return A SingleSignatureInfo API object containing the * information of sigInfoElem. * @throws MOAApplicationException An error occurred parsing the * SingleSignatureInfo. */ private PDFSignatureInfo parsePDFSignatureInfo(Element sigInfoElem) throws MOAApplicationException { final String signatureProfile = XPathUtils.getElementValue(sigInfoElem, SIGNATURE_PROFILE_XPATH, null); final String signaturePosition = XPathUtils.getElementValue(sigInfoElem, SIGNATURE_POSITION_XPATH, null); final String signatureID = XPathUtils.getElementValue(sigInfoElem, SIGNATURE_ID_XPATH, null); final Element base64ContentElem = (Element) XPathUtils.selectSingleNode(sigInfoElem, PDF_DOCUMENT_XPATH); final String base64Str = DOMUtils.getText(base64ContentElem); try { final byte[] inputPDF = Base64Utils.decode(base64Str, true); final PDFSignatureInfo PDFSignatureInfo = new PDFSignatureInfo(inputPDF, signatureProfile, signaturePosition, signatureID); return PDFSignatureInfo; } catch (final IOException e) { throw new MOAApplicationException("2244", null, e); } } }