diff options
Diffstat (limited to 'id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/auth/validator/parep/client/szrgw/SZRGWClient.java')
-rw-r--r-- | id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/auth/validator/parep/client/szrgw/SZRGWClient.java | 395 |
1 files changed, 395 insertions, 0 deletions
diff --git a/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/auth/validator/parep/client/szrgw/SZRGWClient.java b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/auth/validator/parep/client/szrgw/SZRGWClient.java new file mode 100644 index 000000000..e3457f4de --- /dev/null +++ b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/auth/validator/parep/client/szrgw/SZRGWClient.java @@ -0,0 +1,395 @@ +/* + * Copyright 2003 Federal Chancellery Austria + * MOA-ID 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.id.auth.validator.parep.client.szrgw;
+
+import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; + +import javax.net.ssl.SSLSocketFactory; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.commons.httpclient.protocol.Protocol; +import org.apache.xpath.XPathAPI; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.w3c.dom.Text; + +import at.gv.egovernment.moa.id.auth.validator.parep.ParepUtils; +import at.gv.egovernment.moa.util.Constants; +import at.gv.egovernment.moa.util.DOMUtils; + + + +/** + * This class implements a client for communication with the SZR-gateway + * <p> + * Two types of requests are supported + * <ol> + * <li>Basic Request</li> + * <li>Detailed Request</li> + * </ol> + * + * @author <a href="mailto:peter.danner@egiz.gv.at">Peter Danner</a> + */ +public class SZRGWClient { + /** + * The URL of the SZR-gateway webservice. + */ + private String address; + + /** + * The SSL socket factory when using a secure connection. + */ + private SSLSocketFactory sSLSocketFactory; + + /** + * Constructor + */ + public SZRGWClient() { + } + + /** + * Constructor + * + * @param address the URL of the SZR-gateway webservice. + */ + public SZRGWClient(String address) { + this.address = address; + } + /** + * Sets the SSL socket factory. + * + * @param factory the SSL socket factory. + */ + public void setSSLSocketFactory(SSLSocketFactory factory) { + this.sSLSocketFactory = factory; + } + + /** + * Sets the SZR webservice URL + * + * @param address the URL of the SZR-gateway webservice. + */ + public void setAddress(String address) { + this.address = address; + } + + /** + * Creates a mandate. + * + * @param reqElem the request. + * @return a SZR-gateway response containing the result + * @throws SZRGWException when an error occurs creating the mandate. + */ + public CreateMandateResponse createMandateResponse(Element reqElem) throws SZRGWClientException { + //Logger.info("Connecting to SZR-gateway."); + try { + if (address == null) { + throw new NullPointerException("Address (SZR-gateway ServiceURL) must not be null."); + } + HttpClient client = new HttpClient(); + PostMethod method = new PostMethod(address); + method.setRequestHeader("SOAPAction", ""); + + + // ssl settings + if (sSLSocketFactory != null) { + SZRGWSecureSocketFactory fac = new SZRGWSecureSocketFactory(sSLSocketFactory); + Protocol.registerProtocol("https", new Protocol("https", fac, 443)); + } + + // create soap body + Element soapBody = getSOAPBody(); + Document doc = soapBody.getOwnerDocument(); + soapBody.appendChild(doc.importNode(reqElem, true)); + Element requestElement = soapBody.getOwnerDocument().getDocumentElement(); + + //ParepUtils.saveElementToFile(requestElement, new File("c:/temp/szrRequest.xml")); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ParepUtils.serializeElementAsDocument(requestElement, bos); + + method.setRequestBody(new ByteArrayInputStream(bos.toByteArray())); + client.executeMethod(method); + CreateMandateResponse response = new CreateMandateResponse(); + + bos = new ByteArrayOutputStream(); + doc = ParepUtils.readDocFromIs(method.getResponseBodyAsStream()); + + //ParepUtils.saveElementToFile(doc.getDocumentElement(), new File("c:/temp/szrResponse.xml")); + response.parse(doc.getDocumentElement()); + + + return response; + } catch(Exception e) { + //e.printStackTrace(); + throw new SZRGWClientException(e); + } + } + + /** + * Gets a identity link. + * + * @param reqElem the request. + * @return a SZR-gateway response containing the result + * @throws SZRGWException when an error occurs creating the mandate. + */ + public CreateIdentityLinkResponse createIdentityLinkResponse(Element reqElem) throws SZRGWClientException { + + try { + if (address == null) { + throw new NullPointerException("Address (SZR-gateway ServiceURL) must not be null."); + } + HttpClient client = new HttpClient(); + PostMethod method = new PostMethod(address); + method.setRequestHeader("SOAPAction", ""); + + + // ssl settings + if (sSLSocketFactory != null) { + SZRGWSecureSocketFactory fac = new SZRGWSecureSocketFactory(sSLSocketFactory); + Protocol.registerProtocol("https", new Protocol("https", fac, 443)); + } + + // create soap body + Element soapBody = getSOAPBody(); + Document doc = soapBody.getOwnerDocument(); + soapBody.appendChild(doc.importNode(reqElem, true)); + Element requestElement = soapBody.getOwnerDocument().getDocumentElement(); + + //ParepUtils.saveElementToFile(requestElement, new File("c:/temp/szrRequest.xml")); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ParepUtils.serializeElementAsDocument(requestElement, bos); + + method.setRequestBody(new ByteArrayInputStream(bos.toByteArray())); + client.executeMethod(method); + CreateIdentityLinkResponse response = new CreateIdentityLinkResponse(); + + bos = new ByteArrayOutputStream(); + doc = ParepUtils.readDocFromIs(method.getResponseBodyAsStream()); + //ParepUtils.saveElementToFile(doc.getDocumentElement(), new File("c:/temp/szrResponse.xml")); + + NodeList list = doc.getElementsByTagNameNS(SZRGWConstants.SZRGW_REQUEST_NS, "ErrorResponse"); + if (list.getLength() > 0) { + // set error response + list = doc.getElementsByTagNameNS(SZRGWConstants.SZRGW_REQUEST_NS, "Info"); + String error = DOMUtils.getText(list.item(0)); + + response.setError(error); + } + else { + // set assertion + DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + Document newdoc = builder.newDocument(); + + Element nameSpaceNode = newdoc.createElement("NameSpaceNode"); + nameSpaceNode.setAttribute("xmlns:" + Constants.DSIG_PREFIX, Constants.DSIG_NS_URI); + nameSpaceNode.setAttribute("xmlns:" + Constants.SAML_PREFIX, Constants.SAML_NS_URI); + + Element samlAssertion = (Element)XPathAPI.selectSingleNode(doc, "//saml:Assertion[1]", nameSpaceNode); + + if (samlAssertion == null) + throw new SZRGWClientException("Could not found a saml:Assertion element in response."); + else + response.setAssertion(samlAssertion); + } + + return response; + + } catch(Exception e) { + throw new SZRGWClientException(e); + } + } + + + /* + * builds an XML soap envelope + */ + private Element getSOAPBody() throws SZRGWClientException { + Document doc_; + try { + doc_ = ParepUtils.createEmptyDocument(); + Element root = doc_.createElementNS(SOAPConstants.SOAP_ENV_NS, SOAPConstants.SOAP_ENV_PREFIX + SOAPConstants.ENVELOPE); + doc_.appendChild(root); + + root.setAttribute("xmlns" + SOAPConstants.SOAP_ENV_POSTFIX, SOAPConstants.SOAP_ENV_NS); + //root.setAttribute(SOAPConstants.SOAP_ENV_PREFIX + SOAPConstants.ENCODING_STYLE, SOAPConstants.SOAP_ENV_ENCODING_STYLE); + root.setAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema"); + root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); + + Element body = doc_.createElementNS(SOAPConstants.SOAP_ENV_NS, SOAPConstants.SOAP_ENV_PREFIX + SOAPConstants.BODY); + root.appendChild(body); + + return body; + } catch (SZRGWClientException e) { + throw new SZRGWClientException(e); + } + + } + + public Document buildGetIdentityLinkRequest(String PEPSIdentifier, String PEPSFirstname, String PEPSFamilyname, String PEPSDateOfBirth, Element signature) throws SZRGWClientException { + + String SZRGW_NS = "http://reference.e-government.gv.at/namespace/szrgw/20070807#"; + try { + DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document doc = builder.newDocument(); + + Element getIdentityLink = doc.createElementNS(SZRGW_NS, "szrgw:GetIdentityLinkRequest"); + getIdentityLink.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:szrgw", SZRGW_NS); + doc.appendChild(getIdentityLink); + + if ( (PEPSIdentifier != null) || (PEPSFirstname != null) || (PEPSFamilyname != null) || (PEPSDateOfBirth != null) ) { + + Element pepsDataElem = doc.createElementNS(SZRGW_NS, "szrgw:PEPSData"); + getIdentityLink.appendChild(pepsDataElem); + + if (PEPSIdentifier != null) { + Element elem = doc.createElementNS(SZRGW_NS, "szrgw:Identifier"); + pepsDataElem.appendChild(elem); + Text text= doc.createTextNode(PEPSIdentifier); + elem.appendChild(text); + } + if (PEPSFirstname != null) { + Element elem = doc.createElementNS(SZRGW_NS, "szrgw:Firstname"); + pepsDataElem.appendChild(elem); + Text text= doc.createTextNode(PEPSFirstname); + elem.appendChild(text); + } + + if (PEPSFamilyname != null) { + Element elem = doc.createElementNS(SZRGW_NS, "szrgw:Familyname"); + pepsDataElem.appendChild(elem); + Text text= doc.createTextNode(PEPSFamilyname); + elem.appendChild(text); + } + + if (PEPSDateOfBirth != null) { + Element elem = doc.createElementNS(SZRGW_NS, "szrgw:DateOfBirth"); + pepsDataElem.appendChild(elem); + Text text= doc.createTextNode(PEPSDateOfBirth); + elem.appendChild(text); + } + } + + if (signature == null) + throw new SZRGWClientException("Signature element must not be null!"); + else { + Element sig = doc.createElementNS(SZRGW_NS, "szrgw:Signature"); + Element xmlcontent = doc.createElementNS(SZRGW_NS, "szrgw:XMLContent"); + sig.appendChild(xmlcontent); + Node n = doc.importNode(signature, true); + getIdentityLink.appendChild(sig); + xmlcontent.appendChild(n); + } + + + return doc; + } catch (ParserConfigurationException e) { + throw new SZRGWClientException(e); + } /*catch (CertificateEncodingException e) { + throw new SZRGWClientException(e); + }*/ + + + } + + public Document buildGetIdentityLinkRequest(String PEPSIdentifier, String PEPSFirstname, String PEPSFamilyname, String PEPSDateOfBirth, String signature) throws SZRGWClientException { + + String SZRGW_NS = "http://reference.e-government.gv.at/namespace/szrgw/20070807#"; + + try { + DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document doc = builder.newDocument(); + + Element getIdentityLink = doc.createElementNS(SZRGW_NS, "szrgw:GetIdentityLinkRequest"); + getIdentityLink.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:szrgw", SZRGW_NS); + doc.appendChild(getIdentityLink); + + if ( (PEPSIdentifier != null) || (PEPSFirstname != null) || (PEPSFamilyname != null) || (PEPSDateOfBirth != null) ) { + + Element pepsDataElem = doc.createElementNS(SZRGW_NS, "szrgw:PEPSData"); + getIdentityLink.appendChild(pepsDataElem); + + if (PEPSIdentifier != null) { + Element elem = doc.createElementNS(SZRGW_NS, "szrgw:Identifier"); + pepsDataElem.appendChild(elem); + Text text= doc.createTextNode(PEPSIdentifier); + elem.appendChild(text); + } + if (PEPSFirstname != null) { + Element elem = doc.createElementNS(SZRGW_NS, "szrgw:Firstname"); + pepsDataElem.appendChild(elem); + Text text= doc.createTextNode(PEPSFirstname); + elem.appendChild(text); + } + + if (PEPSFamilyname != null) { + Element elem = doc.createElementNS(SZRGW_NS, "szrgw:Familyname"); + pepsDataElem.appendChild(elem); + Text text= doc.createTextNode(PEPSFamilyname); + elem.appendChild(text); + } + + if (PEPSDateOfBirth != null) { + Element elem = doc.createElementNS(SZRGW_NS, "szrgw:DateOfBirth"); + pepsDataElem.appendChild(elem); + Text text= doc.createTextNode(PEPSDateOfBirth); + elem.appendChild(text); + } + } + + if (signature == null) + throw new SZRGWClientException("Signature element must not be null!"); + else { + Element sig = doc.createElementNS(SZRGW_NS, "szrgw:Signature"); + Element base64content = doc.createElementNS(SZRGW_NS, "szrgw:Base64Content"); + sig.appendChild(base64content); + getIdentityLink.appendChild(sig); + Text text= doc.createTextNode(signature); + base64content.appendChild(text); + } + + return doc; + } catch (ParserConfigurationException e) { + throw new SZRGWClientException(e); + } /*catch (CertificateEncodingException e) { + throw new SZRGWClientException(e); + }*/ + + + } + +} + |