/* * The MIT License * * Original work sponsored and donated by National Board of e-Health (NSI), Denmark (http://www.nsi.dk) * * Copyright (C) 2011 National Board of e-Health (NSI), Denmark (http://www.nsi.dk) * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies * of the Software, and to permit persons to whom the Software is furnished to do * so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * * $HeadURL$ * $Id$ */ package com.trifork.sosigw.ws; import com.trifork.sosigw.Factory; import com.trifork.sosigw.cluster.IDCardClusterCacheImpl; import dk.sosi.gw._2007_09.RequestIdCardDigestForSigningResponse; import dk.sosi.gw._2007_09.SignIdCardRequestBody; import dk.sosi.gw._2007_09.SosiGWFacade; import dk.sosi.gw._2007_09.SosiGWFacadeService; import org.apache.log4j.Logger; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.mortbay.jetty.Server; import org.mortbay.jetty.handler.AbstractHandler; import org.mortbay.jetty.handler.ContextHandler; import org.mortbay.jetty.webapp.WebAppContext; import org.mortbay.resource.Resource; import org.oasis_open.docs.wss._2004._01.oasis_200401_wss_wssecurity_secext_1_0.Security; import org.w3._2000._09.xmldsig_.KeyInfo; import org.w3._2000._09.xmldsig_.X509Data; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.namespace.QName; import javax.xml.ws.BindingProvider; import java.io.*; import java.net.MalformedURLException; import java.security.KeyStore; import java.security.PrivateKey; import java.security.cert.Certificate; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; public class AbstractIntegrationTests { private static final Logger log = Logger.getLogger("com.trifork.sosigw.util.timing.Log4jTimeSink"); protected static final String SERVER_URL = "http://localhost:8586/sosigw"; private static final String TEST_RESPONSE = "#"; private static Server server; protected enum ServiceCalled { TEST, DCC } protected static ServiceCalled lastServiceCalled = null; protected static String lastSoapActionCalled = null; protected static StringBuffer lastRequest = null; protected static Map httpRequestHeaders; @Before public void clearLastServiceCalled() { lastServiceCalled = null; } @BeforeClass public static void configureServer() throws Exception { server = new Server(8586); System.setProperty("dk.sdsd.nsp.slalog.config.dir", new File(AbstractIntegrationTests.class.getResource("/etc/nspslalog-sosigw.properties").getFile()).getParent()); WebAppContext ctx = new WebAppContext() { public Resource getResource(String uriInContext) throws MalformedURLException { if ("/WEB-INF/wsdl/sosigw.wsdl".equals(uriInContext)) { try { return Resource.newResource(new File("etc/wsdl/sosigw.wsdl").toURI().toURL()); } catch (IOException e) { throw new RuntimeException(e); } } if ("/WEB-INF/wsdl/sosigw-restricted.wsdl".equals(uriInContext)) { try { return Resource.newResource(new File("etc/wsdl/sosigw-restricted.wsdl").toURI().toURL()); } catch (IOException e) { throw new RuntimeException(e); } } return super.getResource(uriInContext); } }; ctx.setWar("web"); ctx.setContextPath("/sosigw"); server.addHandler(ctx); AbstractHandler test = new AbstractHandler() { public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException { handleRequest(request, response, ServiceCalled.TEST); } }; ContextHandler testhandler = new ContextHandler("/stubservice"); testhandler.addHandler(test); server.addHandler(testhandler); AbstractHandler dcc = new AbstractHandler() { public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException { handleRequest(request, response, ServiceCalled.DCC); } }; ContextHandler dcchandler = new ContextHandler("/decoupling"); dcchandler.addHandler(dcc); server.addHandler(dcchandler); Factory.setTheIDCardCache(new IDCardClusterCacheImpl(null)); log.debug("Starting server"); server.start(); } private static void handleRequest(HttpServletRequest request, HttpServletResponse response, ServiceCalled serviceCalled) throws IOException { response.setContentType("text/xml; charset=utf-8"); PrintWriter pw = response.getWriter(); lastServiceCalled = serviceCalled; lastSoapActionCalled = request.getHeader("soapaction"); lastRequest = new StringBuffer(); BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream())); for (String line = in.readLine(); line != null; line = in.readLine()) { lastRequest.append(line); } pw.println(TEST_RESPONSE); pw.flush(); httpRequestHeaders = new HashMap(); Enumeration headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String key = (String) headerNames.nextElement(); String value = request.getHeader(key); httpRequestHeaders.put(key, value); } } @AfterClass public static void stopServer() throws Exception { if (server != null && server.isRunning()) { log.debug("Stopping server"); server.stop(); } server = null; } public static Map getHttpRequestHeaders() { return httpRequestHeaders; } protected SosiGWFacade getFacade() { SosiGWFacadeService service = new SosiGWFacadeService(getClass().getResource("wsdl/sosigw.wsdl"), new QName("http://sosi.dk/gw/2007.09.01", "SosiGWFacadeService")); SosiGWFacade proxy = service.getSosiGWSoapBinding(); BindingProvider bp = (BindingProvider)proxy; bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://localhost:8586/sosigw/service/sosigw"); return proxy; } protected void createSignedIdCard() throws Exception { Security requestIdCardDigestForSigningRequest = IdCardTestUtils.makeIDCardInSecurity(); SosiGWFacade sosiGWFacade = getFacade(); RequestIdCardDigestForSigningResponse response = sosiGWFacade.requestIdCardDigestForSigning(requestIdCardDigestForSigningRequest, ""); byte[] digestValue = response.getDigestValue(); SignIdCardRequestBody signIdCardRequestBody = new SignIdCardRequestBody(); String password = "Test1234"; KeyStore keystore = KeyStore.getInstance("JKS"); keystore.load(new FileInputStream("etc/validMocesVault.jks"), password.toCharArray()); Certificate certificate = keystore.getCertificate("SOSI:ALIAS_SYSTEM"); PrivateKey key = (PrivateKey)keystore.getKey("SOSI:ALIAS_SYSTEM", password.toCharArray()); byte[] signatureValue = IdCardTestUtils.signDigest(digestValue, key); signIdCardRequestBody.setSignatureValue(signatureValue); KeyInfo keyInfo = new KeyInfo(); X509Data x509Data = new X509Data(); x509Data.setX509Certificate(certificate.getEncoded()); keyInfo.setX509Data(x509Data); signIdCardRequestBody.setKeyInfo(keyInfo); sosiGWFacade.signIdCard(requestIdCardDigestForSigningRequest, signIdCardRequestBody); } }