Java Code Examples for org.apache.cxf.transport.http.HTTPConduit#setAuthSupplier()

The following examples show how to use org.apache.cxf.transport.http.HTTPConduit#setAuthSupplier() . 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: JettyDigestAuthTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private HTTPConduit setupClient(boolean async) throws Exception {
    URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
    greeter = new SOAPService(wsdl, SERVICE_NAME).getPort(Greeter.class);
    BindingProvider bp = (BindingProvider)greeter;
    ClientProxy.getClient(greeter).getInInterceptors().add(new LoggingInInterceptor());
    ClientProxy.getClient(greeter).getOutInterceptors().add(new LoggingOutInterceptor());
    bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
                               ADDRESS);
    HTTPConduit cond = (HTTPConduit)ClientProxy.getClient(greeter).getConduit();
    HTTPClientPolicy client = new HTTPClientPolicy();
    cond.setClient(client);
    if (async) {
        if (cond instanceof AsyncHTTPConduit) {
            UsernamePasswordCredentials creds = new UsernamePasswordCredentials("ffang", "pswd");
            bp.getRequestContext().put(Credentials.class.getName(), creds);
            bp.getRequestContext().put(AsyncHTTPConduit.USE_ASYNC, Boolean.TRUE);
            client.setAutoRedirect(true);
        } else {
            fail("Not an async conduit");
        }
    } else {
        bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "ffang");
        bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "pswd");
        cond.setAuthSupplier(new DigestAuthSupplier());
    }

    ClientProxy.getClient(greeter).getOutInterceptors()
        .add(new AbstractPhaseInterceptor<Message>(Phase.PRE_STREAM_ENDING) {

            public void handleMessage(Message message) throws Fault {
                Map<String, ?> headers = CastUtils.cast((Map<?, ?>)message.get(Message.PROTOCOL_HEADERS));
                if (headers.containsKey("Proxy-Authorization")) {
                    throw new RuntimeException("Should not have Proxy-Authorization");
                }
            }
        });
    client.setAllowChunking(false);
    return cond;
}
 
Example 2
Source File: DigestAuthSupplierJettyTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void test() {
    WebClient client = WebClient.create("http://localhost:" + DigestAuthSupplierJettyServer.PORT, (String) null);

    assertThrows(NotAuthorizedException.class, () -> client.get(String.class));

    HTTPConduit conduit = WebClient.getConfig(client).getHttpConduit();
    conduit.setAuthSupplier(new DigestAuthSupplier());
    conduit.getAuthorization().setUserName(USER);
    conduit.getAuthorization().setPassword(PWD);

    assertEquals(TestServlet.RESPONSE, client.get(String.class));
}
 
Example 3
Source File: HttpConduitConfigApplier.java    From cxf with Apache License 2.0 6 votes vote down vote up
private void applyAuthSupplier(Dictionary<String, String> d, HTTPConduit c) {
    Enumeration<String> keys = d.keys();
    while (keys.hasMoreElements()) {
        String k = keys.nextElement();
        if (k.startsWith("authSupplier")) {
            String v = d.get(k);
            Object obj;
            try {
                obj = Class.forName(v).newInstance();
            } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
            if (obj instanceof HttpAuthSupplier) {
                c.setAuthSupplier((HttpAuthSupplier)obj);
            }
        }
    }
}
 
Example 4
Source File: DigestAuthSupplierSpringTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
    WebClient client = WebClient.create("http://localhost:" + port, (String) null);

    assertThrows(NotAuthorizedException.class, () -> client.get(String.class));

    HTTPConduit conduit = WebClient.getConfig(client).getHttpConduit();
    conduit.setAuthSupplier(new DigestAuthSupplier());
    conduit.getAuthorization().setUserName(USER);
    conduit.getAuthorization().setPassword(PWD);

    assertEquals(Controller.RESPONSE, client.get(String.class));
}
 
Example 5
Source File: UndertowDigestAuthTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
private HTTPConduit setupClient(boolean async) throws Exception {
    URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
    greeter = new SOAPService(wsdl, SERVICE_NAME).getPort(Greeter.class);
    BindingProvider bp = (BindingProvider)greeter;
    ClientProxy.getClient(greeter).getInInterceptors().add(new LoggingInInterceptor());
    ClientProxy.getClient(greeter).getOutInterceptors().add(new LoggingOutInterceptor());
    bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
                               ADDRESS);
    HTTPConduit cond = (HTTPConduit)ClientProxy.getClient(greeter).getConduit();
    HTTPClientPolicy client = new HTTPClientPolicy();
    client.setConnectionTimeout(600000);
    client.setReceiveTimeout(600000);
    cond.setClient(client);
    if (async) {
        if (cond instanceof AsyncHTTPConduit) {
            UsernamePasswordCredentials creds = new UsernamePasswordCredentials("ffang", "pswd");
            bp.getRequestContext().put(Credentials.class.getName(), creds);
            bp.getRequestContext().put(AsyncHTTPConduit.USE_ASYNC, Boolean.TRUE);
            client.setAutoRedirect(true);
        } else {
            fail("Not an async conduit");
        }
    } else {
        bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "ffang");
        bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "pswd");
        cond.setAuthSupplier(new DigestAuthSupplier());
    }

    ClientProxy.getClient(greeter).getOutInterceptors()
        .add(new AbstractPhaseInterceptor<Message>(Phase.PRE_STREAM_ENDING) {

            public void handleMessage(Message message) throws Fault {
                Map<String, ?> headers = CastUtils.cast((Map<?, ?>)message.get(Message.PROTOCOL_HEADERS));
                if (headers.containsKey("Proxy-Authorization")) {
                    throw new RuntimeException("Should not have Proxy-Authorization");
                }
            }
        });
    client.setAllowChunking(false);
    return cond;
}
 
Example 6
Source File: HTTPSConduitTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
/**
 * This tests redirects through Gordy to Bethal. Bethal will
 * supply a series of 401s. See PushBack401.
 */
@Test
public void testHttpsRedirect401Response() throws Exception {
    startServer("Gordy");
    startServer("Bethal");

    URL wsdl = getClass().getResource("greeting.wsdl");
    assertNotNull("WSDL is null", wsdl);

    SOAPService service = new SOAPService(wsdl, serviceName);
    assertNotNull("Service is null", service);

    Greeter gordy = service.getPort(gordyQ, Greeter.class);
    assertNotNull("Port is null", gordy);
    updateAddressPort(gordy, getPort("PORT3"));

    // Okay, I'm sick of configuration files.
    // This also tests dynamic configuration of the conduit.
    Client client = ClientProxy.getClient(gordy);

    HTTPConduit http =
        (HTTPConduit) client.getConduit();

    HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();

    httpClientPolicy.setAutoRedirect(true);
    http.setClient(httpClientPolicy);
    http.setTlsClientParameters(tlsClientParameters);

    // We get redirected from Gordy, to Bethal.
    http.setTrustDecider(
            new MyHttpsTrustDecider(
                    new String[] {"Gordy", "Bethal"}));

    // Without preemptive user/pass Bethal returns a
    // 401 for realm Cronus. If we supply any name other
    // than Edward, George, or Mary, with the pass of "password"
    // we should succeed.
    http.setAuthSupplier(
            new MyBasicAuthSupplier("Cronus", "Betty", "password"));

    // We actually get our answer from Bethal at the end of the
    // redirects.
    String answer = gordy.sayHi();
    assertTrue("Unexpected answer: " + answer,
            "Bonjour from Bethal".equals(answer));

    // The loop auth supplier,
    // We should die with looping realms.
    http.setAuthSupplier(new MyBasicAuthSupplier());

    try {
        answer = gordy.sayHi();
        fail("Unexpected answer from Gordy: " + answer);
    } catch (Exception e) {
        //e.printStackTrace();
    }
}