/*
* 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 java.util.Iterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import at.gv.egovernment.moa.spss.MOASystemException;
import at.gv.egovernment.moa.spss.api.xmlbind.ResponseBuilderUtils;
import at.gv.egovernment.moaspss.util.Base64Utils;
import at.gv.egovernment.moaspss.util.Constants;
/**
* Convert a CreateCMSSignatureResponse
API object into its XML
* representation, according to the MOA XML schema.
*
* @version $Id$
*/
public class CreatePDFSignatureResponseBuilder {
private static final String MOA_NS_URI = Constants.MOA_NS_URI;
private static final Logger logger = LoggerFactory.getLogger(CreatePDFSignatureResponseBuilder.class);
/** The XML document containing the response element. */
private Document responseDoc;
/** The response CreateCMSSignatureResponse
DOM element. */
private Element responseElem;
/**
* Create a new CreateCMSSignatureResponseBuilder
:
*
* @throws MOASystemException
* An error occurred setting up the resulting XML document.
*/
public CreatePDFSignatureResponseBuilder() throws MOASystemException {
responseDoc = ResponseBuilderUtils.createResponse("CreatePDFSignatureResponse");
responseElem = responseDoc.getDocumentElement();
}
/**
* Build a document containing a CreateCMSSignatureResponse
DOM
* element being the XML representation of the given
* CreateCMSSignatureResponse
API object.
*
* @param response
* The CreateCMSSignatureResponse
to convert to XML.
* @return A document containing the CreateCMSSignatureResponse
* DOM element.
*/
public Document build(CreatePDFRespone response) {
Iterator iter = response.getSignatureInfoList().iterator();
while (iter.hasNext()) {
SignedPDFInfo signedPDFInfo = iter.next();
addPDFSignature(signedPDFInfo);
}
return responseDoc;
}
/**
* Add a CMSSignature
element to the response.
*
* @param cmsSignatureResponse
* The content to put under the CMSSignature
* element.
*/
private void addPDFSignature(SignedPDFInfo signedPDFInfo) {
Element pdfSignature = responseDoc.createElementNS(MOA_NS_URI, "PDFSignature");
if(signedPDFInfo.getSignatureID() != null) {
Element signatureID = responseDoc.createElementNS(MOA_NS_URI, "SignatureID");
signatureID.setTextContent(signedPDFInfo.getSignatureID());
pdfSignature.appendChild(signatureID);
}
switch (signedPDFInfo.getResponseType()) {
case SignedPDFInfo.SUCCESS_SIGNATURE:
pdfSignature.appendChild(addSuccessSignature(signedPDFInfo));
break;
default:
pdfSignature.appendChild(addErrorResponse(signedPDFInfo));
break;
}
responseElem.appendChild(pdfSignature);
}
private Element addSuccessSignature(SignedPDFInfo signedPDFInfo) {
try {
String base64Value = Base64Utils.encode(signedPDFInfo.getPdfDocument());
Element cmsSignature = responseDoc.createElementNS(MOA_NS_URI, "PDFSignature");
cmsSignature.setTextContent(base64Value);
return cmsSignature;
} catch (IOException e) {
logger.error("Failed to encode pdf signature", e);
signedPDFInfo.setErrorCode(2300);
signedPDFInfo.setErrorInfo("Failed to encode the signed document");
return addErrorResponse(signedPDFInfo);
}
}
/**
* Add a ErrorResponse
element to the response.
*
* @param errorResponse
* The API object containing the information to put into the
* ErrorResponse
DOM element.
*/
private Element addErrorResponse(SignedPDFInfo signedPDFInfo) {
Element errorElem = responseDoc.createElementNS(MOA_NS_URI, "ErrorResponse");
Element errorCodeElem = responseDoc.createElementNS(MOA_NS_URI, "ErrorCode");
Element infoElem = responseDoc.createElementNS(MOA_NS_URI, "Info");
String errorCodeStr = Integer.toString(signedPDFInfo.getErrorCode());
errorCodeElem.appendChild(responseDoc.createTextNode(errorCodeStr));
errorElem.appendChild(errorCodeElem);
infoElem.appendChild(responseDoc.createTextNode(signedPDFInfo.getErrorInfo()));
errorElem.appendChild(errorCodeElem);
errorElem.appendChild(infoElem);
return errorElem;
}
}