Search in sources :

Example 11 with Outcome

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

the class STOMPClient method handleRequest.

@Override
public Outcome handleRequest(Exchange exc) throws Exception {
    String login = exc.getRequest().getHeader().getFirstValue("login");
    String passcode = exc.getRequest().getHeader().getFirstValue("passcode");
    String host = exc.getRequest().getHeader().getFirstValue("host");
    String acceptVersion = exc.getRequest().getHeader().getFirstValue("accept-version");
    boolean isStomp1_0 = login != null && passcode != null;
    boolean isStomp1_1orAbove = host != null && acceptVersion != null;
    if (isStomp1_0 || isStomp1_1orAbove) {
        Connection c = connectionManager.getConnection(this.host, port, connectionConfiguration.getLocalAddr(), sslOutboundProvider, connectionConfiguration.getTimeout());
        exc.getRequest().writeSTOMP(c.out);
        HttpClient.setupConnectionForwarding(exc, c, "STOMP", getRouter().getStatistics().getStreamPumpStats());
    } else {
        exc.setResponse(Response.badRequest().build());
    }
    return Outcome.RETURN;
}
Also used : Connection(com.predic8.membrane.core.transport.http.Connection)

Example 12 with Outcome

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

the class RegistrationInterceptor method handleRequest.

@Override
public Outcome handleRequest(Exchange exc) throws Exception {
    Request request = exc.getRequest();
    if (!request.isPOSTRequest())
        return ErrorMessages.returnErrorBadRequest(exc);
    User user;
    try {
        user = new ObjectMapper().readValue(request.getBodyAsStringDecoded(), User.class);
    } catch (IOException e) {
        return ErrorMessages.returnErrorBadRequest(exc);
    }
    try (Connection connection = userDataProvider.getDatasource().getConnection()) {
        try (ResultSet rs = connection.createStatement().executeQuery(getIsAccountNameAvailableSQL(user))) {
            if (rs.next() && rs.getInt(1) != 0)
                return ErrorMessages.returnErrorUserAlreadyExists(exc);
        }
        if (!SecurityUtils.isHashedPassword(user.getPassword()))
            user.setPassword(SecurityUtils.createPasswdCompatibleHash(user.getPassword()));
        connection.createStatement().executeUpdate(getInsertAccountIntoDatabaseSQL(user));
    }
    // TODO: Save user mit flag if confirmated
    // TODO: Send Confirmation Email
    // TODO: PreparedStatements gegen SQL-Injection verwenden??????
    exc.setResponse(Response.ok().build());
    return Outcome.RETURN;
}
Also used : User(com.predic8.membrane.core.interceptor.registration.entity.User) Request(com.predic8.membrane.core.http.Request) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 13 with Outcome

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

the class REST2SOAPInterceptor method handleResponse.

@Override
public Outcome handleResponse(Exchange exc) throws Exception {
    Mapping mapping = getRESTURL(exc);
    log.debug("restURL: " + mapping);
    if (getRESTURL(exc) == null)
        return Outcome.CONTINUE;
    if (log.isDebugEnabled())
        log.debug("response: " + new String(getTransformer(null).transform(getBodySource(exc), exc.getStringProperties()), Constants.UTF_8_CHARSET));
    exc.getResponse().setBodyContent(getTransformer(mapping.responseXSLT).transform(getBodySource(exc)));
    Header header = exc.getResponse().getHeader();
    header.removeFields(Header.CONTENT_TYPE);
    header.setContentType(MimeType.TEXT_XML_UTF8);
    XML2HTTP.unwrapMessageIfNecessary(exc.getResponse());
    convertResponseToJSONIfNecessary(exc.getRequest().getHeader(), mapping, exc.getResponse(), exc.getStringProperties());
    return Outcome.CONTINUE;
}
Also used : Header(com.predic8.membrane.core.http.Header)

Example 14 with Outcome

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

the class LargeBodyTest method setup.

public void setup() throws Exception {
    // streaming only works for maxRetries = 1
    hcc = new HttpClientConfiguration();
    hcc.setMaxRetries(1);
    Rule rule = new ServiceProxy(new ServiceProxyKey("localhost", "POST", ".*", 3040), "thomas-bayer.com", 80);
    rule.getInterceptors().add(new AbstractInterceptor() {

        @Override
        public Outcome handleRequest(Exchange exc) throws Exception {
            exc.setResponse(Response.ok().body("").build());
            return Outcome.RETURN;
        }
    });
    router = new HttpRouter();
    ((HTTPClientInterceptor) router.getTransport().getInterceptors().get(3)).setHttpClientConfig(hcc);
    router.getRuleManager().addProxyAndOpenPortIfNew(rule);
    router.init();
    Rule rule1 = new ServiceProxy(new ServiceProxyKey("localhost", "POST", ".*", 3041), "localhost", 3040);
    router2 = new HttpRouter();
    ((HTTPClientInterceptor) router2.getTransport().getInterceptors().get(3)).setHttpClientConfig(hcc);
    router2.getRuleManager().addProxyAndOpenPortIfNew(rule1);
    router2.init();
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) HTTPClientInterceptor(com.predic8.membrane.core.interceptor.HTTPClientInterceptor) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) Outcome(com.predic8.membrane.core.interceptor.Outcome) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) Rule(com.predic8.membrane.core.rules.Rule) HttpRouter(com.predic8.membrane.core.HttpRouter) HttpClientConfiguration(com.predic8.membrane.core.transport.http.client.HttpClientConfiguration) IOException(java.io.IOException)

Example 15 with Outcome

use of com.predic8.membrane.core.interceptor.Outcome 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)

Aggregations

Outcome (com.predic8.membrane.core.interceptor.Outcome)25 Exchange (com.predic8.membrane.core.exchange.Exchange)19 AbstractInterceptor (com.predic8.membrane.core.interceptor.AbstractInterceptor)16 ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)14 IOException (java.io.IOException)14 ServiceProxyKey (com.predic8.membrane.core.rules.ServiceProxyKey)10 Test (org.junit.Test)9 HttpRouter (com.predic8.membrane.core.HttpRouter)8 URISyntaxException (java.net.URISyntaxException)7 Before (org.junit.Before)6 ArrayList (java.util.ArrayList)4 Header (com.predic8.membrane.core.http.Header)3 Session (com.predic8.membrane.core.interceptor.authentication.session.SessionManager.Session)3 Rule (com.predic8.membrane.core.rules.Rule)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Body (com.predic8.membrane.core.http.Body)2 HttpClientConfiguration (com.predic8.membrane.core.transport.http.client.HttpClientConfiguration)2 MalformedURLException (java.net.MalformedURLException)2 InvalidParameterException (java.security.InvalidParameterException)2