package at.gv.egovernment.moa.ss.erechtclient.moainvoker; import java.io.InputStream; import java.util.Properties; import javax.servlet.ServletContext; import org.apache.log4j.Logger; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import at.gv.egovernment.moa.ss.erechtclient.ERechtClientException; import at.gv.egovernment.moa.ss.erechtclient.init.Constants; import at.gv.egovernment.moa.ss.erechtclient.util.DOMUtils; import at.gv.egovernment.moa.ss.erechtclient.util.Utils; import at.gv.egovernment.moa.ss.erechtclient.util.XPathUtils; public class RequestBuilder { private static final String XPATH_KEYID_ = "/" + Constants.NSPRE_MOA_13_ + ":" + "CreateXMLSignatureRequest" + "/" + Constants.NSPRE_MOA_13_ + ":" + "KeyIdentifier"; private static final String XPATH_XMLDOC_ = "/" + Constants.NSPRE_MOA_13_ + ":" + "CreateXMLSignatureRequest" + "/" + Constants.NSPRE_MOA_13_ + ":" + "SingleSignatureInfo" + "/" + Constants.NSPRE_MOA_13_ + ":" + "DataObjectInfo" + "/" + Constants.NSPRE_MOA_13_ + ":" + "DataObject" + "/" + Constants.NSPRE_MOA_13_ + ":" + "LocRefContent"; private static final String XPATH_STYLESHEET_ = "/" + Constants.NSPRE_MOA_13_ + ":" + "CreateXMLSignatureRequest" + "/" + Constants.NSPRE_MOA_13_ + ":" + "SingleSignatureInfo" + "/" + Constants.NSPRE_MOA_13_ + ":" + "DataObjectInfo" + "/" + Constants.NSPRE_MOA_13_ + ":" + "CreateTransformsInfoProfile" + "/" + Constants.NSPRE_MOA_13_ + ":" + "Supplement" + "/" + Constants.NSPRE_MOA_13_ + ":" + "Content" + "[@Reference=\"stylesheet.xsl\"]" + "/" + Constants.NSPRE_MOA_13_ + ":" + "LocRefContent"; private static final String XPATH_IMAGE_ = "/" + Constants.NSPRE_MOA_13_ + ":" + "CreateXMLSignatureRequest" + "/" + Constants.NSPRE_MOA_13_ + ":" + "SingleSignatureInfo"; private static final String ELEM_NAME_DOI_ = "DataObjectInfo"; private static final String ATTR_VALUE_DO_STRUCTURE_ = "detached"; private static final String ELEM_NAME_DO_ = "DataObject"; private static final String ATTR_NAME_DOI_STRUCTURE_ = "Structure"; private static final String ATTR_NAME_DO_REFERENCE_ = "Reference"; private static final String ELEM_NAME_CTIP_ = "CreateTransformsInfoProfile"; private static final String ELEM_NAME_CTI_ = "CreateTransformsInfo"; private static final String ELEM_NAME_FDMI_ = "FinalDataMetaInfo"; private static final String ELEM_NAME_FDMIMIMETYPE_ = "MimeType"; private static final String ELEM_VALUE_FDMIMIMETYPE_ = "image"; private static final String ELEM_NAME_FDMITYPE_ = "Type"; private static final String ELEM_VALUE_FDMITYPE_ = "http://www.buergerkarte.at/specifications/" + "Security-Layer/20031113?Name=SignedImage&InstanceDocRef=0"; private static final String ELEM_NAME_LOCREF_ = "LocRefContent"; private Document requestDoc_; private String contextPath_; private Properties initProps_; private ServletContext context_; private static Logger logger_ = Logger.getLogger(Constants.LH_MOAINVOKER_); public RequestBuilder(ServletContext context, String contextPath) throws ERechtClientException { contextPath_ = contextPath; context_ = context; initProps_ = (Properties) context.getAttribute(Constants.WSCP_INIT_PROPS_); // Load template String requestTemplateLocation = Utils.readInitProperty(initProps_, Constants.IP_SS_REQUEST_TEMPLATE_, logger_); try { InputStream requestTemplateIS = context_.getResourceAsStream(requestTemplateLocation); requestDoc_ = DOMUtils.parseWellFormed(requestTemplateIS); } catch (Exception e) { String message = "Could not parse MOA SS signature creation request template."; logger_.error(message, e); throw new ERechtClientException(message, e); } // Insert key identifier Element keyIdentifierElem = xPathGetElement(XPATH_KEYID_, requestDoc_); String keyIdentifierStr = Utils.readInitProperty(initProps_, Constants.IP_SS_KEYID_, logger_); keyIdentifierElem.appendChild(requestDoc_.createTextNode(keyIdentifierStr)); } public void setXMLDocument(String locationRef) throws ERechtClientException { // Set XML document location reference Element xMLDocLocRefElem = xPathGetElement(XPATH_XMLDOC_, requestDoc_); String webAppHostPortFromMOASS = Utils.readInitProperty(initProps_, Constants.IP_WEBAPP_HOST_PORT_FROM_MOA_SS_, logger_); xMLDocLocRefElem.appendChild(requestDoc_.createTextNode(webAppHostPortFromMOASS + contextPath_ + locationRef)); } public void setStylesheet(String locationRef) throws ERechtClientException { Element stylesheetLocRefElem = xPathGetElement(XPATH_STYLESHEET_, requestDoc_); String webAppHostPortFromMOASS = Utils.readInitProperty(initProps_, Constants.IP_WEBAPP_HOST_PORT_FROM_MOA_SS_, logger_); String stylesheetLocRefStr = webAppHostPortFromMOASS + contextPath_ + locationRef; stylesheetLocRefElem.appendChild(requestDoc_.createTextNode(webAppHostPortFromMOASS + contextPath_ + locationRef)); logger_.debug("Setting stylesheet location to: " + stylesheetLocRefStr); } public void useDefaultStylesheet() throws ERechtClientException { // Insert stylesheet reference, if it has not been set explicitly Element stylesheetLocRefElem = xPathGetElement(XPATH_STYLESHEET_, requestDoc_); String stylesheetLocRefStr = Utils.readInitProperty(initProps_, Constants.IP_SS_STYLESHEET_, logger_); String webAppHostPortFromMOASS = Utils.readInitProperty(initProps_, Constants.IP_WEBAPP_HOST_PORT_FROM_MOA_SS_, logger_); stylesheetLocRefStr = webAppHostPortFromMOASS + contextPath_ + stylesheetLocRefStr; stylesheetLocRefElem.appendChild(requestDoc_.createTextNode(stylesheetLocRefStr)); logger_.debug("No stylesheet set explicitly, using default one: " + stylesheetLocRefStr); } public void addImage(String name, String locationRef) throws ERechtClientException { // Add supplement for image to be signed Element parentElem = xPathGetElement(XPATH_IMAGE_, requestDoc_); String webAppHostPortFromMOASS = Utils.readInitProperty(initProps_, Constants.IP_WEBAPP_HOST_PORT_FROM_MOA_SS_, logger_); Element dataObjInfo = requestDoc_.createElementNS(Constants.NSURI_MOA_13_, ELEM_NAME_DOI_); dataObjInfo.setAttributeNS(null, ATTR_NAME_DOI_STRUCTURE_, ATTR_VALUE_DO_STRUCTURE_); Element dataObj = requestDoc_.createElementNS(Constants.NSURI_MOA_13_, ELEM_NAME_DO_); Element locRefCont = requestDoc_.createElementNS(Constants.NSURI_MOA_13_, ELEM_NAME_LOCREF_); locRefCont.appendChild(requestDoc_.createTextNode(webAppHostPortFromMOASS + contextPath_ + locationRef)); dataObj.appendChild(locRefCont); dataObj.setAttributeNS(null, ATTR_NAME_DO_REFERENCE_, name); dataObjInfo.appendChild(dataObj); Element createTIProf = requestDoc_.createElementNS(Constants.NSURI_MOA_13_, ELEM_NAME_CTIP_); Element createTI = requestDoc_.createElementNS(Constants.NSURI_MOA_13_, ELEM_NAME_CTI_); Element finalDataMI = requestDoc_.createElementNS(Constants.NSURI_MOA_13_, ELEM_NAME_FDMI_); Element finalDataMIMimeType = requestDoc_.createElementNS(Constants.NSURI_MOA_13_, ELEM_NAME_FDMIMIMETYPE_); finalDataMIMimeType.appendChild(requestDoc_.createTextNode(ELEM_VALUE_FDMIMIMETYPE_)); finalDataMI.appendChild(finalDataMIMimeType); Element finalDataMIType = requestDoc_.createElementNS(Constants.NSURI_MOA_13_, ELEM_NAME_FDMITYPE_); finalDataMIType.appendChild(requestDoc_.createTextNode(ELEM_VALUE_FDMITYPE_)); finalDataMI.appendChild(finalDataMIType); createTI.appendChild(finalDataMI); createTIProf.appendChild(createTI); dataObjInfo.appendChild(createTIProf); parentElem.appendChild(dataObjInfo); } public Document getRequest() { return requestDoc_; } private Element xPathGetElement(String xPath, Document doc) throws ERechtClientException { try { XPathUtils xpUtils = new XPathUtils(); String additionalNSPrefixes = Constants.NSPRE_MOA_13_ + " " + Constants.NSURI_MOA_13_; xpUtils.setupContext(xPath, doc.getDocumentElement(), additionalNSPrefixes); NodeList nodeList = xpUtils.selectNodeSet(doc); if (nodeList == null) return null; if (nodeList.getLength() < 1) return null; if (!(nodeList.item(0) instanceof Element)) return null; return (Element) nodeList.item(0); } catch (Exception e) { String message = "MOA SS signature request template seems to be corrupted."; logger_.error(message, e); throw new ERechtClientException(message, e); } } }