Search in sources :

Example 16 with Client

use of com.predic8.membrane.core.interceptor.oauth2.Client in project service-proxy by membrane.

the class DynamicRegistration method registerWithCallbackAt.

public Client registerWithCallbackAt(String callbackUri, String registrationEndpoint) throws Exception {
    Exchange exc = new Request.Builder().post(registrationEndpoint).header(Header.CONTENT_TYPE, MimeType.APPLICATION_JSON_UTF8).body(getRegistrationBody(callbackUri)).buildExchange();
    Response response = doRequest(exc);
    HashMap<String, String> json = Util.parseSimpleJSONResponse(response);
    if (!json.containsKey("client_id") || !json.containsKey("client_secret"))
        throw new RuntimeException("Registration endpoint didn't return clientId/clientSecret");
    return new Client(json.get("client_id"), json.get("client_secret"), "");
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) Response(com.predic8.membrane.core.http.Response) Client(com.predic8.membrane.core.interceptor.oauth2.Client) HttpClient(com.predic8.membrane.core.transport.http.HttpClient)

Example 17 with Client

use of com.predic8.membrane.core.interceptor.oauth2.Client in project service-proxy by membrane.

the class LoadBalancingInterceptorTest method testFailOverOnConnectionRefused.

@Test
public void testFailOverOnConnectionRefused() throws Exception {
    balancingInterceptor.setDispatchingStrategy(roundRobinStrategy);
    HttpClient client = new HttpClient();
    client.getParams().setParameter(HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(1, mockInterceptor1.getCount());
    assertEquals(0, mockInterceptor2.getCount());
    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(1, mockInterceptor1.getCount());
    assertEquals(1, mockInterceptor2.getCount());
    service1.shutdown();
    Thread.sleep(1000);
    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(1, mockInterceptor1.getCount());
    assertEquals(2, mockInterceptor2.getCount());
    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(3, mockInterceptor2.getCount());
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) Test(org.junit.Test) Http11Test(com.predic8.membrane.integration.Http11Test)

Example 18 with Client

use of com.predic8.membrane.core.interceptor.oauth2.Client in project service-proxy by membrane.

the class LoadBalancingInterceptorTest method testFailOverOnStatus500.

@Test
public void testFailOverOnStatus500() throws Exception {
    balancingInterceptor.setDispatchingStrategy(roundRobinStrategy);
    HttpClient client = new HttpClient();
    client.getParams().setParameter(HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(1, mockInterceptor1.getCount());
    assertEquals(0, mockInterceptor2.getCount());
    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(1, mockInterceptor1.getCount());
    assertEquals(1, mockInterceptor2.getCount());
    ((ServiceProxy) service1.getRuleManager().getRules().get(0)).getInterceptors().add(0, new AbstractInterceptor() {

        @Override
        public Outcome handleRequest(Exchange exc) throws Exception {
            exc.setResponse(Response.internalServerError().build());
            return Outcome.ABORT;
        }
    });
    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(1, mockInterceptor1.getCount());
    assertEquals(2, mockInterceptor2.getCount());
    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(3, mockInterceptor2.getCount());
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) Outcome(com.predic8.membrane.core.interceptor.Outcome) HttpClient(org.apache.commons.httpclient.HttpClient) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) Test(org.junit.Test) Http11Test(com.predic8.membrane.integration.Http11Test)

Example 19 with Client

use of com.predic8.membrane.core.interceptor.oauth2.Client in project service-proxy by membrane.

the class LoadBalancerSession3Test method test.

/**
 * The test as described in README.txt, but "wsimport" (previously called by ant)
 * was removed and is run directly from this test before everything else. Thereby
 * we can use a Maven dependency on wsimport and do not have to download it ourselves.
 */
@Test
public void test() throws IOException, InterruptedException {
    File base = getExampleDir("loadbalancer-session-3");
    AssertUtils.replaceInFile(new File(base, "proxies.xml"), "8080", "3023");
    AssertUtils.replaceInFile(new File(base, "src/com/predic8/chat/Client.java"), "8080", "3023");
    AssertUtils.replaceInFile(new File(base, "data/ChatService.wsdl"), "8080", "3023");
    Process2 sl = new Process2.Builder().in(base).script("service-proxy").waitForMembrane().start();
    try {
        File buildXML = new File(base, "build.xml");
        // remove <exec...</exec> from build.xml
        String s = Pattern.compile("<exec.*</exec>", Pattern.DOTALL).matcher(FileUtils.readFileToString(buildXML)).replaceAll("");
        FileUtils.writeStringToFile(buildXML, s);
        File classes = new File(base, "build" + File.separator + "classes");
        classes.mkdirs();
        File source = new File(base, "src");
        source.mkdirs();
        // run "wsimport" generating java sources
        Assert.assertTrue(new com.sun.tools.ws.wscompile.WsimportTool(System.out).run(new String[] { "-quiet", "-Xnocompile", new File(base, "data" + File.separator + "ChatService.wsdl").getAbsolutePath(), "-s", source.getAbsolutePath() }));
        // call "ant compile" now so that both antNodeX processes do call it at the same time
        BufferLogger loggerCompile = new BufferLogger();
        Process2 antCompile = new Process2.Builder().in(base).withWatcher(loggerCompile).executable("ant compile").start();
        try {
            int result = antCompile.waitFor(60000);
            if (result != 0)
                throw new AssertionError("'ant compile' returned non-zero " + result + ":\r\n" + loggerCompile.toString());
        } finally {
            antCompile.killScript();
        }
        BufferLogger loggerNode1 = new BufferLogger();
        BufferLogger loggerNode2 = new BufferLogger();
        Process2 antNode1 = new Process2.Builder().in(base).withWatcher(loggerNode1).executable("ant run-node1").start();
        try {
            Process2 antNode2 = new Process2.Builder().in(base).withWatcher(loggerNode2).executable("ant run-node2").start();
            try {
                LoadBalancerUtil.addLBNodeViaHTML("http://localhost:9000/admin/", "localhost", 4000);
                LoadBalancerUtil.addLBNodeViaHTML("http://localhost:9000/admin/", "localhost", 4001);
                // wait for nodes to come up
                Thread.sleep(1000);
                Process2 antClient = new Process2.Builder().in(base).executable("ant run-client -Dlogin=jim").start();
                try {
                    antClient.waitFor(60000);
                } finally {
                    antClient.killScript();
                }
            } finally {
                antNode2.killScript();
            }
        } finally {
            antNode1.killScript();
        }
        AssertUtils.assertContains("Hallo World", loggerNode1.toString());
        AssertUtils.assertContainsNot("Hallo World", loggerNode2.toString());
    } finally {
        sl.killScript();
    }
}
Also used : Process2(com.predic8.membrane.examples.Process2) BufferLogger(com.predic8.membrane.examples.util.BufferLogger) File(java.io.File) Test(org.junit.Test)

Example 20 with Client

use of com.predic8.membrane.core.interceptor.oauth2.Client in project service-proxy by membrane.

the class LoadBalancingInterceptorTest method testExpect100Continue.

@Test
public void testExpect100Continue() throws Exception {
    balancingInterceptor.setDispatchingStrategy(roundRobinStrategy);
    HttpClient client = new HttpClient();
    Http11Test.initExpect100ContinueWithFastFail(client);
    PostMethod vari = getPostMethod();
    int status = client.executeMethod(vari);
    assertEquals(200, status);
    assertEquals(1, mockInterceptor1.getCount());
    assertEquals(0, mockInterceptor2.getCount());
    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(1, mockInterceptor1.getCount());
    assertEquals(1, mockInterceptor2.getCount());
    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(2, mockInterceptor1.getCount());
    assertEquals(1, mockInterceptor2.getCount());
    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(2, mockInterceptor1.getCount());
    assertEquals(2, mockInterceptor2.getCount());
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) Test(org.junit.Test) Http11Test(com.predic8.membrane.integration.Http11Test)

Aggregations

Test (org.junit.Test)27 Client (org.orcid.jaxb.model.client_v2.Client)18 Exchange (com.predic8.membrane.core.exchange.Exchange)12 ClientDetailsEntity (org.orcid.persistence.jpa.entities.ClientDetailsEntity)10 IOException (java.io.IOException)8 AbstractInterceptor (com.predic8.membrane.core.interceptor.AbstractInterceptor)6 Outcome (com.predic8.membrane.core.interceptor.Outcome)6 HashSet (java.util.HashSet)6 BaseTest (org.orcid.core.BaseTest)6 ClientRedirectUri (org.orcid.jaxb.model.client_v2.ClientRedirectUri)6 URISyntaxException (java.net.URISyntaxException)5 HttpClient (org.apache.commons.httpclient.HttpClient)5 ScopePathType (org.orcid.jaxb.model.message.ScopePathType)5 Request (com.predic8.membrane.core.http.Request)4 Response (com.predic8.membrane.core.http.Response)4 Process2 (com.predic8.membrane.examples.Process2)4 Http11Test (com.predic8.membrane.integration.Http11Test)4 SessionManager (com.predic8.membrane.core.interceptor.authentication.session.SessionManager)3 Client (com.predic8.membrane.core.interceptor.oauth2.Client)3 HttpClient (com.predic8.membrane.core.transport.http.HttpClient)3