summaryrefslogtreecommitdiff
path: root/bkucommon/src/test/java/moaspss/SLClient.java
diff options
context:
space:
mode:
authormcentner <mcentner@8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4>2009-11-30 15:05:21 +0000
committermcentner <mcentner@8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4>2009-11-30 15:05:21 +0000
commitebede55b888e8232cef37a2f4b40dbb0746dd28a (patch)
tree2091eee0c88bed1bcff5a6531d94bac92dc33ba2 /bkucommon/src/test/java/moaspss/SLClient.java
parent2573a0bde88b31d08db21a57f296bacf7c99dc14 (diff)
downloadmocca-ebede55b888e8232cef37a2f4b40dbb0746dd28a.tar.gz
mocca-ebede55b888e8232cef37a2f4b40dbb0746dd28a.tar.bz2
mocca-ebede55b888e8232cef37a2f4b40dbb0746dd28a.zip
Added test classes for creating a signature with MOCCA and sending it to MOA-SP for verification.
git-svn-id: https://joinup.ec.europa.eu/svn/mocca/trunk@557 8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4
Diffstat (limited to 'bkucommon/src/test/java/moaspss/SLClient.java')
-rw-r--r--bkucommon/src/test/java/moaspss/SLClient.java175
1 files changed, 175 insertions, 0 deletions
diff --git a/bkucommon/src/test/java/moaspss/SLClient.java b/bkucommon/src/test/java/moaspss/SLClient.java
new file mode 100644
index 00000000..c3561bfe
--- /dev/null
+++ b/bkucommon/src/test/java/moaspss/SLClient.java
@@ -0,0 +1,175 @@
+package moaspss;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.UnsupportedEncodingException;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.ProtocolException;
+import java.net.URL;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBElement;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+import at.gv.egiz.bku.utils.URLEncodingWriter;
+
+public class SLClient {
+
+ private static class JAXBContextHolder {
+
+ private static JAXBContext context;
+
+ {
+ String slPkg = at.buergerkarte.namespaces.securitylayer._1.ObjectFactory.class.getPackage().getName();
+ String xmldsigPkg = org.w3._2000._09.xmldsig_.ObjectFactory.class.getPackage().getName();
+ String samlPkg = oasis.names.tc.saml._1_0.assertion.ObjectFactory.class.getPackage().getName();
+ String prPkg = at.buergerkarte.namespaces.personenbindung._20020506_.ObjectFactory.class.getPackage().getName();
+ try {
+ context = JAXBContext.newInstance(slPkg + ":" + xmldsigPkg + ":" + samlPkg + ":" + prPkg);
+ } catch (JAXBException e) {
+ throw new RuntimeException("Failed to setup JAXBContext.", e);
+ }
+ }
+
+ }
+
+ public static JAXBContext getJAXBContext() {
+ return JAXBContextHolder.context;
+ }
+
+ private URL slUrl;
+
+ private URL slUrlSSL;
+
+ private boolean useSSL = false;
+
+ public SLClient() {
+ try {
+ slUrl = new URL("http://localhost:3495/http-security-layer-request");
+ slUrlSSL = new URL("https://localhost:3496/https-security-layer-request");
+ } catch (MalformedURLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public Object submitRequest(Element request, Class<?> responseType)
+ throws SLException, TransformerException, IOException {
+
+ URL url = (useSSL) ? slUrlSSL : slUrl;
+
+ HttpURLConnection connection;
+ int responseCode;
+ try {
+ connection = (HttpURLConnection) url.openConnection();
+ connection.setRequestMethod("POST");
+ connection.setDoOutput(true);
+ connection.setDoInput(true);
+ connection.connect();
+
+ OutputStream outputStream = connection.getOutputStream();
+ OutputStreamWriter streamWriter = new OutputStreamWriter(outputStream, "ISO-8859-1");
+ streamWriter.write("XMLRequest=");
+ URLEncodingWriter urlEnc = new URLEncodingWriter(streamWriter);
+
+ TransformerFactory tf = TransformerFactory.newInstance();
+ Transformer transformer = tf.newTransformer();
+ Source source = new DOMSource(request);
+ Result result = new StreamResult(urlEnc);
+ transformer.transform(source, result);
+ urlEnc.flush();
+ streamWriter.flush();
+ outputStream.close();
+ responseCode = connection.getResponseCode();
+
+ } catch (ProtocolException e) {
+ throw new RuntimeException(e);
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException(e);
+ }
+
+ if (responseCode == 200) {
+ String[] contentType = connection.getContentType().split(";", 2);
+
+ if ("text/xml".equals(contentType[0])) {
+
+ Reader streamReader;
+ try {
+ InputStream inputStream = connection.getInputStream();
+
+ String charset = "ISO-8859-1";
+ if (contentType.length > 1
+ && (contentType[1].trim()).startsWith("charset=")) {
+ charset = contentType[1].split("=", 2)[1];
+ }
+
+ streamReader = new InputStreamReader(inputStream, charset);
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException(e);
+ }
+
+ if (JAXBElement.class.isAssignableFrom(responseType)) {
+ Object obj;
+ try {
+ Unmarshaller unmarshaller = getJAXBContext().createUnmarshaller();
+ obj = unmarshaller.unmarshal(streamReader);
+ } catch (JAXBException e) {
+ throw new SLException(9000, e);
+ }
+ if (obj instanceof JAXBElement<?>) {
+ return obj;
+ } else {
+ throw new SLException(9000, "Got unexpected response.");
+ }
+ } else if (Element.class.isAssignableFrom(responseType)) {
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ dbf.setNamespaceAware(true);
+ Document doc;
+ try {
+ DocumentBuilder db = dbf.newDocumentBuilder();
+ doc = db.parse(new InputSource(streamReader));
+ } catch (ParserConfigurationException e) {
+ throw new SLException(9000, e);
+ } catch (SAXException e) {
+ throw new SLException(9000, e);
+ } catch (IOException e) {
+ throw new SLException(9000, e);
+ }
+ return doc.getDocumentElement();
+ } else {
+ throw new SLException(9000, "Unsupported response type "
+ + responseType);
+ }
+
+ } else {
+ throw new SLException(9000, "Got unexpected content type "
+ + contentType + ".");
+ }
+ } else {
+ throw new SLException(9000, "Got unexpected response code "
+ + responseCode + ".");
+ }
+
+ }
+
+}