Java Code Examples for org.apache.cxf.common.classloader.ClassLoaderUtils#getResourceAsStream()

The following examples show how to use org.apache.cxf.common.classloader.ClassLoaderUtils#getResourceAsStream() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JAXRS20HttpsBookTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private SSLContext createSSLContext() throws Exception {
    TLSClientParameters tlsParams = new TLSClientParameters();

    try (InputStream keystore = ClassLoaderUtils.getResourceAsStream("keys/Truststore.jks", this.getClass())) {
        KeyStore trustStore = loadStore(keystore, "password");

        TrustManagerFactory tmf =
            TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(trustStore);
        tlsParams.setTrustManagers(tmf.getTrustManagers());
    }

    try (InputStream keystore = ClassLoaderUtils.getResourceAsStream("keys/Morpit.jks", this.getClass())) {
        KeyStore keyStore = loadStore(keystore, "password");

        KeyManagerFactory kmf =
            KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(keyStore, "password".toCharArray());
        tlsParams.setKeyManagers(kmf.getKeyManagers());
    }

    return SSLUtils.getSSLContext(tlsParams);
}
 
Example 2
Source File: JAXRS20HttpsBookTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetBook() throws Exception {

    ClientBuilder builder = ClientBuilder.newBuilder();

    try (InputStream keystore = ClassLoaderUtils.getResourceAsStream("keys/Truststore.jks", this.getClass())) {
        KeyStore trustStore = loadStore(keystore, "password");
        builder.trustStore(trustStore);
    }
    builder.hostnameVerifier(new AllowAllHostnameVerifier());

    try (InputStream keystore = ClassLoaderUtils.getResourceAsStream("keys/Morpit.jks", this.getClass())) {
        KeyStore keyStore = loadStore(keystore, "password");
        builder.keyStore(keyStore, "password");
    }

    Client client = builder.build();
    client.register(new LoggingFeature());

    WebTarget target = client.target("https://localhost:" + PORT + "/bookstore/securebooks/123");
    Book b = target.request().accept(MediaType.APPLICATION_XML_TYPE).get(Book.class);
    assertEquals(123, b.getId());
}
 
Example 3
Source File: XSLTInterceptorsTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws TransformerConfigurationException {
    messageIS = ClassLoaderUtils.getResourceAsStream(MESSAGE_FILE, this.getClass());
    if (messageIS == null) {
        throw new IllegalArgumentException("Cannot load message from path: " + MESSAGE_FILE);
    }
    message = new MessageImpl();
    inInterceptor = new XSLTInInterceptor(TRANSFORMATION_XSL);
    outInterceptor = new XSLTOutInterceptor(TRANSFORMATION_XSL);
}
 
Example 4
Source File: SSLUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
private static InputStream getResourceAsStream(String resource) {
    InputStream is = ClassLoaderUtils.getResourceAsStream(resource, SSLUtils.class);
    if (is == null) {
        Bus bus = BusFactory.getThreadDefaultBus(true);
        ResourceManager rm = bus.getExtension(ResourceManager.class);
        if (rm != null) {
            is = rm.getResourceAsStream(resource);
        }
    }
    return is;
}
 
Example 5
Source File: TLSParameterJaxBUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
private static InputStream getResourceAsStream(String resource) {
    InputStream is = ClassLoaderUtils.getResourceAsStream(resource, TLSParameterJaxBUtils.class);
    if (is == null) {
        Bus bus = BusFactory.getThreadDefaultBus(true);
        ResourceManager rm = bus.getExtension(ResourceManager.class);
        if (rm != null) {
            is = rm.getResourceAsStream(resource);
        }
    }
    return is;
}
 
Example 6
Source File: AbstractXSLTInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
public AbstractXSLTInterceptor(String phase, Class<?> before, Class<?> after, String xsltPath) {
    super(phase);
    if (before != null) {
        addBefore(before.getName());
    }
    if (after != null) {
        addAfter(after.getName());
    }

    try {
        InputStream xsltStream = ClassLoaderUtils.getResourceAsStream(xsltPath, this.getClass());
        if (xsltStream == null) {
            throw new IllegalArgumentException("Cannot load XSLT from path: " + xsltPath);
        }
        Document doc = StaxUtils.read(xsltStream);

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        try {
            transformerFactory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
        } catch (javax.xml.transform.TransformerConfigurationException ex) {
            //
        }

        xsltTemplate = transformerFactory.newTemplates(new DOMSource(doc));
    } catch (TransformerConfigurationException | XMLStreamException e) {
        throw new IllegalArgumentException(
                                           String.format("Cannot create XSLT template from path: %s",
                                                         xsltPath), e);
    }
}
 
Example 7
Source File: AbstractHTTPServlet.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected InputStream getResourceAsStream(String path) {

        InputStream is = ClassLoaderUtils.getResourceAsStream(path, AbstractHTTPServlet.class);
        if (is == null && getBus() != null) {
            ResourceManager rm = getBus().getExtension(ResourceManager.class);
            if (rm != null) {
                is = rm.resolveResource(path, InputStream.class);
            }
        }
        return is;
    }
 
Example 8
Source File: ClientAuthTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@org.junit.Test
public void testSSLConnectionUsingJavaAPIs() throws Exception {
    URL service = new URL("https://localhost:" + PORT);
    HttpsURLConnection connection = (HttpsURLConnection) service.openConnection();

    connection.setHostnameVerifier(new DisableCNCheckVerifier());

    SSLContext sslContext = SSLContext.getInstance("TLS");

    KeyStore ts = KeyStore.getInstance("JKS");
    try (InputStream trustStore =
        ClassLoaderUtils.getResourceAsStream("keys/Truststore.jks", ClientAuthTest.class)) {
        ts.load(trustStore, "password".toCharArray());
    }

    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmf.init(ts);

    KeyStore ks = KeyStore.getInstance("JKS");
    try (InputStream keyStore =
        ClassLoaderUtils.getResourceAsStream("keys/Morpit.jks", ClientAuthTest.class)) {
        ks.load(keyStore, "password".toCharArray());
    }

    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(ks, "password".toCharArray());

    sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new java.security.SecureRandom());

    connection.setSSLSocketFactory(sslContext.getSocketFactory());

    connection.connect();

    connection.disconnect();
}
 
Example 9
Source File: ClientAuthTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@org.junit.Test
public void testDirectTrustUsingKeyManagers() throws Exception {

    URL url = SOAPService.WSDL_LOCATION;
    SOAPService service = new SOAPService(url, SOAPService.SERVICE);
    assertNotNull("Service is null", service);
    final Greeter port = service.getHttpsPort();
    assertNotNull("Port is null", port);

    updateAddressPort(port, PORT);

    // Enable Async
    if (async) {
        ((BindingProvider)port).getRequestContext().put("use.async.http.conduit", true);
    }

    // Set up KeyManagers/TrustManagers
    KeyStore ts = KeyStore.getInstance("JKS");
    try (InputStream trustStore =
        ClassLoaderUtils.getResourceAsStream("keys/Truststore.jks", ClientAuthTest.class)) {
        ts.load(trustStore, "password".toCharArray());
    }

    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmf.init(ts);

    KeyStore ks = KeyStore.getInstance("JKS");
    try (InputStream keyStore =
        ClassLoaderUtils.getResourceAsStream("keys/Morpit.jks", ClientAuthTest.class)) {
        ks.load(keyStore, "password".toCharArray());
    }

    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(ks, "password".toCharArray());

    TLSClientParameters tlsParams = new TLSClientParameters();
    tlsParams.setKeyManagers(kmf.getKeyManagers());
    tlsParams.setTrustManagers(tmf.getTrustManagers());
    tlsParams.setDisableCNCheck(true);

    Client client = ClientProxy.getClient(port);
    HTTPConduit http = (HTTPConduit) client.getConduit();
    http.setTlsClientParameters(tlsParams);

    assertEquals(port.greetMe("Kitty"), "Hello Kitty");

    ((java.io.Closeable)port).close();
}
 
Example 10
Source File: ResourceUtils.java    From cxf with Apache License 2.0 4 votes vote down vote up
public static InputStream getClasspathResourceStream(String path, Class<?> callingClass, Bus bus) {
    InputStream is = ClassLoaderUtils.getResourceAsStream(path, callingClass);
    return is == null ? getResource(path, InputStream.class, bus) : is;
}
 
Example 11
Source File: ClientAuthTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@org.junit.Test
public void testDirectTrustUsingSSLContext() throws Exception {

    URL url = SOAPService.WSDL_LOCATION;
    SOAPService service = new SOAPService(url, SOAPService.SERVICE);
    assertNotNull("Service is null", service);
    final Greeter port = service.getHttpsPort();
    assertNotNull("Port is null", port);

    updateAddressPort(port, PORT);

    // Enable Async
    if (async) {
        ((BindingProvider)port).getRequestContext().put("use.async.http.conduit", true);
    }

    // Set up KeyManagers/TrustManagers
    KeyStore ts = KeyStore.getInstance("JKS");
    try (InputStream trustStore =
        ClassLoaderUtils.getResourceAsStream("keys/Truststore.jks", ClientAuthTest.class)) {
        ts.load(trustStore, "password".toCharArray());
    }

    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmf.init(ts);

    KeyStore ks = KeyStore.getInstance("JKS");
    try (InputStream keyStore =
        ClassLoaderUtils.getResourceAsStream("keys/Morpit.jks", ClientAuthTest.class)) {
        ks.load(keyStore, "password".toCharArray());
    }

    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(ks, "password".toCharArray());

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new java.security.SecureRandom());

    TLSClientParameters tlsParams = new TLSClientParameters();
    tlsParams.setSslContext(sslContext);
    tlsParams.setDisableCNCheck(true);

    Client client = ClientProxy.getClient(port);
    HTTPConduit http = (HTTPConduit) client.getConduit();
    http.setTlsClientParameters(tlsParams);

    assertEquals(port.greetMe("Kitty"), "Hello Kitty");

    // Enable Async
    ((BindingProvider)port).getRequestContext().put("use.async.http.conduit", true);

    assertEquals(port.greetMe("Kitty"), "Hello Kitty");

    ((java.io.Closeable)port).close();
}
 
Example 12
Source File: WSSCUnitTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testEndorsingSecureConverationViaCode() throws Exception {

    URL wsdl = WSSCUnitTest.class.getResource("DoubleItWSSC.wsdl");
    Service service = Service.create(wsdl, SERVICE_QNAME);
    QName portQName = new QName(NAMESPACE, "DoubleItTransportPort");
    DoubleItPortType port =
            service.getPort(portQName, DoubleItPortType.class);
    updateAddressPort(port, test.getPort());

    if (test.isStreaming()) {
        SecurityTestUtil.enableStreaming(port);
    }

    // TLS configuration
    TrustManagerFactory tmf =
        TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    final KeyStore ts = KeyStore.getInstance("JKS");
    try (InputStream trustStore =
        ClassLoaderUtils.getResourceAsStream("keys/Truststore.jks", WSSCUnitTest.class)) {
        ts.load(trustStore, "password".toCharArray());
    }
    tmf.init(ts);

    TLSClientParameters tlsParams = new TLSClientParameters();
    tlsParams.setTrustManagers(tmf.getTrustManagers());
    tlsParams.setDisableCNCheck(true);

    Client client = ClientProxy.getClient(port);
    HTTPConduit http = (HTTPConduit) client.getConduit();
    http.setTlsClientParameters(tlsParams);

    // STSClient configuration
    Bus clientBus = BusFactory.newInstance().createBus();
    STSClient stsClient = new STSClient(clientBus);
    stsClient.setTlsClientParameters(tlsParams);

    ((BindingProvider)port).getRequestContext().put("security.sts.client", stsClient);

    assertEquals(50, port.doubleIt(25));

    ((java.io.Closeable)port).close();
}
 
Example 13
Source File: TrustManagerTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@org.junit.Test
public void testOSCPOverride() throws Exception {
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = TrustManagerTest.class.getResource("client-trust.xml");

    Bus bus = bf.createBus(busFile.toString());
    BusFactory.setDefaultBus(bus);
    BusFactory.setThreadDefaultBus(bus);

    URL url = SOAPService.WSDL_LOCATION;
    SOAPService service = new SOAPService(url, SOAPService.SERVICE);
    assertNotNull("Service is null", service);
    final Greeter port = service.getHttpsPort();
    assertNotNull("Port is null", port);

    updateAddressPort(port, PORT2);

    // Enable Async
    if (async) {
        ((BindingProvider)port).getRequestContext().put("use.async.http.conduit", true);
    }

    // Read truststore
    KeyStore ts = KeyStore.getInstance("JKS");
    try (InputStream trustStore =
        ClassLoaderUtils.getResourceAsStream("keys/cxfca.jks", TrustManagerTest.class)) {
        ts.load(trustStore, "password".toCharArray());
    }

    try {
        Security.setProperty("ocsp.enable", "true");

        PKIXBuilderParameters param = new PKIXBuilderParameters(ts, new X509CertSelector());
        param.setRevocationEnabled(true);

        TrustManagerFactory tmf  =
            TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(new CertPathTrustManagerParameters(param));

        TLSClientParameters tlsParams = new TLSClientParameters();
        tlsParams.setTrustManagers(tmf.getTrustManagers());
        tlsParams.setDisableCNCheck(true);

        Client client = ClientProxy.getClient(port);
        HTTPConduit http = (HTTPConduit) client.getConduit();
        http.setTlsClientParameters(tlsParams);

        try {
            port.greetMe("Kitty");
            fail("Failure expected on an invalid OCSP responder URL");
        } catch (Exception ex) {
            // expected
        }

    } finally {
        Security.setProperty("ocsp.enable", "false");
    }

    ((java.io.Closeable)port).close();
    bus.shutdown(true);
}
 
Example 14
Source File: SSLv3Test.java    From cxf with Apache License 2.0 4 votes vote down vote up
@org.junit.Test
public void testSSLv3ServerAllowed() throws Exception {

    // Doesn't work with IBM JDK
    if ("IBM Corporation".equals(System.getProperty("java.vendor"))) {
        return;
    }

    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = SSLv3Test.class.getResource("sslv3-client.xml");

    Bus bus = bf.createBus(busFile.toString());
    BusFactory.setDefaultBus(bus);
    BusFactory.setThreadDefaultBus(bus);

    System.setProperty("https.protocols", "SSLv3");

    URL service = new URL("https://localhost:" + PORT2);
    HttpsURLConnection connection = (HttpsURLConnection) service.openConnection();

    connection.setHostnameVerifier(new DisableCNCheckVerifier());

    SSLContext sslContext = SSLContext.getInstance("SSL");
    KeyStore trustedCertStore = KeyStore.getInstance("jks");
    try (InputStream keystore = ClassLoaderUtils.getResourceAsStream("keys/Truststore.jks", SSLv3Test.class)) {
        trustedCertStore.load(keystore, null);
    }

    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmf.init(trustedCertStore);
    TrustManager[] trustManagers = tmf.getTrustManagers();

    sslContext.init(null, trustManagers, new java.security.SecureRandom());

    connection.setSSLSocketFactory(sslContext.getSocketFactory());

    connection.connect();

    connection.disconnect();

    System.clearProperty("https.protocols");

    bus.shutdown(true);
}
 
Example 15
Source File: SSLv3Test.java    From cxf with Apache License 2.0 4 votes vote down vote up
@org.junit.Test
public void testSSLv3ServerNotAllowedByDefault() throws Exception {

    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = SSLv3Test.class.getResource("sslv3-client.xml");

    Bus bus = bf.createBus(busFile.toString());
    BusFactory.setDefaultBus(bus);
    BusFactory.setThreadDefaultBus(bus);

    System.setProperty("https.protocols", "SSLv3");

    URL service = new URL("https://localhost:" + PORT);
    HttpsURLConnection connection = (HttpsURLConnection) service.openConnection();

    connection.setHostnameVerifier(new DisableCNCheckVerifier());

    SSLContext sslContext = SSLContext.getInstance("SSL");

    KeyStore trustedCertStore = KeyStore.getInstance("jks");
    try (InputStream keystore = ClassLoaderUtils.getResourceAsStream("keys/Truststore.jks", SSLv3Test.class)) {
        trustedCertStore.load(keystore, null);
    }

    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmf.init(trustedCertStore);
    TrustManager[] trustManagers = tmf.getTrustManagers();

    sslContext.init(null, trustManagers, new java.security.SecureRandom());
    connection.setSSLSocketFactory(sslContext.getSocketFactory());

    try {
        connection.connect();
        fail("Failure expected on an SSLv3 connection attempt");
    } catch (IOException ex) {
        // expected
    }

    System.clearProperty("https.protocols");

    bus.shutdown(true);
}
 
Example 16
Source File: XKMSTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@org.junit.Test
public void testRegisterUnitTest() throws Exception {
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = XKMSTest.class.getResource("client.xml");

    Bus bus = bf.createBus(busFile.toString());
    BusFactory.setDefaultBus(bus);
    BusFactory.setThreadDefaultBus(bus);

    URL wsdl = //XKMSTest.class.getResource("xkms.wsdl");
        new URL("https://localhost:" + PORT2 + "/XKMS?wsdl");

    String ns = "http://www.w3.org/2002/03/xkms#wsdl";
    QName serviceQName = new QName(ns, "XKMSService");
    Service service = Service.create(wsdl, serviceQName);
    QName portQName = new QName(NAMESPACE, "XKMSPort");
    XKMSPortType port =
            service.getPort(portQName, XKMSPortType.class);
    //updateAddressPort(port, PORT2);

    // First try to locate - which should fail

    LocateRequestType locateRequest = new LocateRequestType();
    locateRequest.setId("_xyz");
    locateRequest.setService("http://cxf.apache.org/services/XKMS/");
    QueryKeyBindingType queryKeyBinding = new QueryKeyBindingType();
    UseKeyWithType useKeyWithType = new UseKeyWithType();
    useKeyWithType.setApplication("urn:ietf:rfc:2459");
    useKeyWithType.setIdentifier("CN=client");
    queryKeyBinding.getUseKeyWith().add(useKeyWithType);
    locateRequest.setQueryKeyBinding(queryKeyBinding);

    LocateResultType locateResultType = port.locate(locateRequest);
    assertTrue(locateResultType.getResultMajor().endsWith("Success"));
    assertTrue(locateResultType.getResultMinor().endsWith("NoMatch"));

    // Now register

    RegisterRequestType registerRequest = new RegisterRequestType();
    registerRequest.setId("_xyz");
    registerRequest.setService("http://cxf.apache.org/services/XKMS/");

    PrototypeKeyBindingType prototypeKeyBinding = new PrototypeKeyBindingType();
    CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
    InputStream certInputStream = ClassLoaderUtils.getResourceAsStream("xkmstest.cer", this.getClass());
    Certificate certificate =
        certificateFactory.generateCertificate(certInputStream);
    KeyInfoType keyInfo = X509Utils.getKeyInfo((X509Certificate)certificate);
    prototypeKeyBinding.setKeyInfo(keyInfo);

    prototypeKeyBinding.getUseKeyWith().add(useKeyWithType);
    registerRequest.setPrototypeKeyBinding(prototypeKeyBinding);

    RegisterResultType registerResult = port.register(registerRequest);
    assertTrue(registerResult.getResultMajor().endsWith("Success"));
    assertFalse(registerResult.getKeyBinding().isEmpty());

    // Now locate again - which should work

    locateResultType = port.locate(locateRequest);
    assertTrue(locateResultType.getResultMajor().endsWith("Success"));
    assertFalse(locateResultType.getUnverifiedKeyBinding().isEmpty());

    // Delete the certificate so that the test works when run again
    Path path = FileSystems.getDefault().getPath("target/test-classes/certs/xkms/CN-client.cer");
    Files.delete(path);

}
 
Example 17
Source File: UsernameTokenTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@org.junit.Test
public void testPlaintextWSDLOverHTTPSViaCode() throws Exception {

    TrustManagerFactory tmf =
        TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    final KeyStore ts = KeyStore.getInstance("JKS");
    try (InputStream trustStore =
        ClassLoaderUtils.getResourceAsStream("keys/Truststore.jks", UsernameTokenTest.class)) {
        ts.load(trustStore, "password".toCharArray());
    }
    tmf.init(ts);

    TLSClientParameters tlsParams = new TLSClientParameters();
    tlsParams.setTrustManagers(tmf.getTrustManagers());
    tlsParams.setDisableCNCheck(true);

    HTTPConduitConfigurer myHttpConduitConfig = new HTTPConduitConfigurer() {
        public void configure(String name, String address, HTTPConduit c) {
            if ("{http://cxf.apache.org}TransportURIResolver.http-conduit".equals(name)) {
                c.setTlsClientParameters(tlsParams);
            }
        }
    };

    BusFactory busFactory = BusFactory.newInstance();
    bus = busFactory.createBus();
    bus.setExtension(myHttpConduitConfig, HTTPConduitConfigurer.class);
    BusFactory.setThreadDefaultBus(bus);

    URL wsdl = new URL("https://localhost:" + PORT + "/DoubleItUTPlaintext?wsdl");
    Service service = Service.create(wsdl, SERVICE_QNAME);
    QName portQName = new QName(NAMESPACE, "DoubleItPlaintextPort");
    DoubleItPortType utPort =
            service.getPort(portQName, DoubleItPortType.class);
    updateAddressPort(utPort, test.getPort());

    if (test.isStreaming()) {
        SecurityTestUtil.enableStreaming(utPort);
    }

    ((BindingProvider)utPort).getRequestContext().put(SecurityConstants.USERNAME, "Alice");

    ((BindingProvider)utPort).getRequestContext().put(SecurityConstants.CALLBACK_HANDLER,
                                                      "org.apache.cxf.systest.ws.common.UTPasswordCallback");

    Client client = ClientProxy.getClient(utPort);
    HTTPConduit http = (HTTPConduit) client.getConduit();
    http.setTlsClientParameters(tlsParams);

    assertEquals(50, utPort.doubleIt(25));

    ((java.io.Closeable)utPort).close();
}
 
Example 18
Source File: UsernameTokenTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@org.junit.Test
public void testPlaintextCodeFirst() throws Exception {

    String address = "https://localhost:" + PORT + "/DoubleItUTPlaintext";
    QName portQName = new QName(NAMESPACE, "DoubleItPlaintextPort");

    WSPolicyFeature policyFeature = new WSPolicyFeature();
    Element policyElement =
        StaxUtils.read(getClass().getResourceAsStream("plaintext-pass-timestamp-policy.xml")).getDocumentElement();
    policyFeature.setPolicyElements(Collections.singletonList(policyElement));

    JaxWsProxyFactoryBean clientFactoryBean = new JaxWsProxyFactoryBean();
    clientFactoryBean.setFeatures(Collections.singletonList(policyFeature));
    clientFactoryBean.setAddress(address);
    clientFactoryBean.setServiceName(SERVICE_QNAME);
    clientFactoryBean.setEndpointName(portQName);
    clientFactoryBean.setServiceClass(DoubleItPortType.class);

    DoubleItPortType port = (DoubleItPortType)clientFactoryBean.create();

    if (test.isStreaming()) {
        SecurityTestUtil.enableStreaming(port);
    }

    ((BindingProvider)port).getRequestContext().put(SecurityConstants.USERNAME, "Alice");

    ((BindingProvider)port).getRequestContext().put(SecurityConstants.CALLBACK_HANDLER,
                                                      "org.apache.cxf.systest.ws.common.UTPasswordCallback");

    TrustManagerFactory tmf =
        TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    final KeyStore ts = KeyStore.getInstance("JKS");
    try (InputStream trustStore =
        ClassLoaderUtils.getResourceAsStream("keys/Truststore.jks", UsernameTokenTest.class)) {
        ts.load(trustStore, "password".toCharArray());
    }
    tmf.init(ts);

    TLSClientParameters tlsParams = new TLSClientParameters();
    tlsParams.setTrustManagers(tmf.getTrustManagers());
    tlsParams.setDisableCNCheck(true);

    Client client = ClientProxy.getClient(port);
    HTTPConduit http = (HTTPConduit) client.getConduit();
    http.setTlsClientParameters(tlsParams);

    assertEquals(50, port.doubleIt(25));

    ((java.io.Closeable)port).close();
}
 
Example 19
Source File: UsernameTokenTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@org.junit.Test
public void testPlaintextTLSConfigViaCode() throws Exception {

    URL wsdl = UsernameTokenTest.class.getResource("DoubleItUt.wsdl");
    // URL wsdl = new URL("https://localhost:" + PORT + "/DoubleItUTPlaintext?wsdl");
    Service service = Service.create(wsdl, SERVICE_QNAME);
    QName portQName = new QName(NAMESPACE, "DoubleItPlaintextPort");
    DoubleItPortType utPort =
            service.getPort(portQName, DoubleItPortType.class);
    updateAddressPort(utPort, test.getPort());

    if (test.isStreaming()) {
        SecurityTestUtil.enableStreaming(utPort);
    }

    ((BindingProvider)utPort).getRequestContext().put(SecurityConstants.USERNAME, "Alice");

    ((BindingProvider)utPort).getRequestContext().put(SecurityConstants.CALLBACK_HANDLER,
                                                      "org.apache.cxf.systest.ws.common.UTPasswordCallback");

    TrustManagerFactory tmf =
        TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    final KeyStore ts = KeyStore.getInstance("JKS");
    try (InputStream trustStore =
        ClassLoaderUtils.getResourceAsStream("keys/Truststore.jks", UsernameTokenTest.class)) {
        ts.load(trustStore, "password".toCharArray());
    }
    tmf.init(ts);

    TLSClientParameters tlsParams = new TLSClientParameters();
    tlsParams.setTrustManagers(tmf.getTrustManagers());
    tlsParams.setDisableCNCheck(true);

    Client client = ClientProxy.getClient(utPort);
    HTTPConduit http = (HTTPConduit) client.getConduit();
    http.setTlsClientParameters(tlsParams);

    assertEquals(50, utPort.doubleIt(25));

    ((java.io.Closeable)utPort).close();
}