Search in sources :

Example 36 with Client

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

the class HttpKeepAliveTest method testTimeoutDefault.

@Test
public void testTimeoutDefault() throws Exception {
    HttpClient client = createHttpClient(1000);
    sp1.getInterceptors().add(0, new AbstractInterceptor() {

        @Override
        public Outcome handleResponse(Exchange exc) throws Exception {
            exc.getResponse().getHeader().add(Header.KEEP_ALIVE, "max=2");
            return Outcome.CONTINUE;
        }
    });
    assertEquals(200, issueRequest(client));
    assertEquals(1, set.size());
    Thread.sleep(1500);
    assertEquals(200, issueRequest(client));
    assertEquals(2, set.size());
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) Outcome(com.predic8.membrane.core.interceptor.Outcome) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Test(org.junit.Test)

Example 37 with Client

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

the class AccessControlInterceptorIntegrationTest method setInterceptor.

/*
	 * This test can only by run on a specific machine.
	 */
/*@Test
	public void testLocalhost() throws Exception {
		setInterceptor(FILE_CLIENTS_FROM_LOCALHOST);

		HttpClient client = new HttpClient();
		HostConfiguration config = new HostConfiguration();
		config.setLocalAddress(InetAddress.getByName("localhost"));
		client.setHostConfiguration(config);

		assertEquals(200, client.executeMethod(getBLZRequestMethod()));
	}*/
/*
	 * This test can only by run on a specific machine.
	 */
/*@Test
	public void test192_168_2_Star() throws Exception {
		setInterceptor(FILE_CLIENTS_FROM_192_168_2_STAR);
		assertEquals(200, getClient(FIXED_IP).executeMethod(getBLZRequestMethod()));
	}*/
private void setInterceptor(String fileName) throws Exception {
    AccessControlInterceptor interceptor = new AccessControlInterceptor();
    interceptor.setFile(fileName);
    router.addUserFeatureInterceptor(interceptor);
    router.init();
}
Also used : AccessControlInterceptor(com.predic8.membrane.core.interceptor.acl.AccessControlInterceptor)

Example 38 with Client

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

the class RevocationEndpointProcessor method process.

@Override
public Outcome process(Exchange exc) throws Exception {
    Map<String, String> params = URLParamUtil.getParams(uriFactory, exc);
    if (!params.containsKey("token")) {
        exc.setResponse(OAuth2Util.createParameterizedJsonErrorResponse(exc, jsonGen, "error", "invalid_request"));
        return Outcome.RETURN;
    }
    SessionManager.Session session = authServer.getSessionFinder().getSessionForToken(params.get("token"));
    if (session == null) {
        // token doesnt exist -> token is already invalid
        exc.setResponse(Response.ok().bodyEmpty().build());
        return Outcome.RETURN;
    }
    Client client;
    Map<String, String> userAttributes = session.getUserAttributes();
    synchronized (userAttributes) {
        try {
            client = authServer.getClientList().getClient(userAttributes.get(ParamNames.CLIENT_ID));
        } catch (Exception e) {
            // This should never happen
            exc.setResponse(Response.ok().bodyEmpty().build());
            return Outcome.RETURN;
        }
    }
    String paramClientId = params.get(ParamNames.CLIENT_ID);
    String paramClientSecret = params.get(ParamNames.CLIENT_SECRET);
    if ((paramClientId != null && !client.getClientId().equals(paramClientId)) || (paramClientSecret != null && !client.getClientSecret().equals(paramClientSecret))) {
        exc.setResponse(OAuth2Util.createParameterizedJsonErrorResponse(exc, jsonGen, "error", "invalid_grant"));
        return Outcome.RETURN;
    }
    try {
        authServer.getTokenGenerator().invalidateToken(params.get("token"), client.getClientId(), client.getClientSecret());
    } catch (Exception e) {
        exc.setResponse(OAuth2Util.createParameterizedJsonErrorResponse(exc, jsonGen, "error", "invalid_grant"));
        return Outcome.RETURN;
    }
    synchronized (session) {
        session.clear();
    }
    synchronized (authServer.getSessionManager()) {
        authServer.getSessionManager().removeSession(session);
    }
    synchronized (authServer.getSessionFinder()) {
        authServer.getSessionFinder().removeSessionForToken(params.get("token"));
    }
    exc.setResponse(Response.ok().bodyEmpty().build());
    return Outcome.RETURN;
}
Also used : SessionManager(com.predic8.membrane.core.interceptor.authentication.session.SessionManager) Client(com.predic8.membrane.core.interceptor.oauth2.Client)

Example 39 with Client

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

the class RegExReplaceInterceptorTest method testReplace.

@Test
public void testReplace() throws Exception {
    router = Router.init("src/test/resources/regex-monitor-beans.xml");
    Rule serverRule = new ServiceProxy(new ServiceProxyKey("localhost", "*", ".*", 3009), "www.predic8.de", 80);
    router.getRuleManager().addProxyAndOpenPortIfNew(serverRule);
    router.init();
    try {
        HttpClient client = new HttpClient();
        GetMethod method = new GetMethod("http://localhost:3009");
        method.setRequestHeader(Header.CONTENT_TYPE, MimeType.TEXT_XML_UTF8);
        method.setRequestHeader(Header.SOAP_ACTION, "");
        assertEquals(200, client.executeMethod(method));
        assertTrue(new String(method.getResponseBody()).contains("Membrane RegEx Replacement Is Cool"));
    } finally {
        router.shutdown();
    }
}
Also used : ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Rule(com.predic8.membrane.core.rules.Rule) Test(org.junit.Test)

Example 40 with Client

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

the class AMStatisticsCollector method sendJsonToElasticSearch.

private void sendJsonToElasticSearch(String path, String json) throws Exception {
    Response resp = null;
    synchronized (client) {
        Exchange exc = new Request.Builder().put(getElasticSearchPath(path)).body(json).buildExchange();
        if (clientId != null && clientSecret != null)
            exc.getRequest().getHeader().add(Header.AUTHORIZATION, "Basic " + new String(Base64.encodeBase64((clientId + ":" + clientSecret).getBytes("UTF-8")), "UTF-8"));
        resp = client.call(exc).getResponse();
    }
    if (!resp.isOk())
        log.warn("Could not send statistics to elastic search instance. Response: " + resp.getStatusCode() + " - " + resp.getStatusMessage() + " - " + resp.getBodyAsStringDecoded());
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange)

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