Search in sources :

Example 1 with Connection

use of com.predic8.membrane.core.transport.http.Connection in project service-proxy by membrane.

the class Request method parseStartLine.

@Override
public void parseStartLine(InputStream in) throws IOException, EndOfStreamException {
    try {
        String firstLine = HttpUtil.readLine(in);
        Matcher matcher = pattern.matcher(firstLine);
        if (matcher.find()) {
            method = matcher.group(1);
            uri = matcher.group(2);
            version = matcher.group(3);
        } else if (stompPattern.matcher(firstLine).find()) {
            method = firstLine;
            uri = "";
            version = "STOMP";
        } else {
            throw new EOFWhileReadingFirstLineException(firstLine);
        }
    } catch (EOFWhileReadingLineException e) {
        if (e.getLineSoFar().length() == 0)
            // happens regularly at the end of a keep-alive connection
            throw new NoMoreRequestsException();
        throw new EOFWhileReadingFirstLineException(e.getLineSoFar());
    }
}
Also used : Matcher(java.util.regex.Matcher) NoMoreRequestsException(com.predic8.membrane.core.transport.http.NoMoreRequestsException) EOFWhileReadingFirstLineException(com.predic8.membrane.core.transport.http.EOFWhileReadingFirstLineException) EOFWhileReadingLineException(com.predic8.membrane.core.transport.http.EOFWhileReadingLineException)

Example 2 with Connection

use of com.predic8.membrane.core.transport.http.Connection 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 3 with Connection

use of com.predic8.membrane.core.transport.http.Connection 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 4 with Connection

use of com.predic8.membrane.core.transport.http.Connection in project service-proxy by membrane.

the class HttpUtil method createHeaders.

public static Header createHeaders(String contentType, String... headers) {
    Header header = new Header();
    if (contentType != null)
        header.setContentType(contentType);
    synchronized (GMT_DATE_FORMAT) {
        header.add("Date", GMT_DATE_FORMAT.format(new Date()));
    }
    header.add("Server", Constants.PRODUCT_NAME + " " + Constants.VERSION + ". See http://membrane-soa.org");
    header.add("Connection", Header.CLOSE);
    for (int i = 0; i < headers.length; i += 2) {
        header.add(headers[i], headers[i + 1]);
    }
    return header;
}
Also used : Header(com.predic8.membrane.core.http.Header) Date(java.util.Date)

Example 5 with Connection

use of com.predic8.membrane.core.transport.http.Connection in project service-proxy by membrane.

the class LoadBalancingInterceptorTest method setUp.

@Before
public void setUp() throws Exception {
    service1 = new HttpRouter();
    mockInterceptor1 = new DummyWebServiceInterceptor();
    ServiceProxy sp1 = new ServiceProxy(new ServiceProxyKey("localhost", "POST", ".*", 2000), "thomas-bayer.com", 80);
    sp1.getInterceptors().add(new AbstractInterceptor() {

        @Override
        public Outcome handleResponse(Exchange exc) throws Exception {
            exc.getResponse().getHeader().add("Connection", "close");
            return Outcome.CONTINUE;
        }
    });
    sp1.getInterceptors().add(mockInterceptor1);
    service1.getRuleManager().addProxyAndOpenPortIfNew(sp1);
    service1.init();
    service2 = new HttpRouter();
    mockInterceptor2 = new DummyWebServiceInterceptor();
    ServiceProxy sp2 = new ServiceProxy(new ServiceProxyKey("localhost", "POST", ".*", 3000), "thomas-bayer.com", 80);
    sp2.getInterceptors().add(new AbstractInterceptor() {

        @Override
        public Outcome handleResponse(Exchange exc) throws Exception {
            exc.getResponse().getHeader().add("Connection", "close");
            return Outcome.CONTINUE;
        }
    });
    sp2.getInterceptors().add(mockInterceptor2);
    service2.getRuleManager().addProxyAndOpenPortIfNew(sp2);
    service2.init();
    balancer = new HttpRouter();
    ServiceProxy sp3 = new ServiceProxy(new ServiceProxyKey("localhost", "POST", ".*", 7000), "thomas-bayer.com", 80);
    balancingInterceptor = new LoadBalancingInterceptor();
    balancingInterceptor.setName("Default");
    sp3.getInterceptors().add(balancingInterceptor);
    balancer.getRuleManager().addProxyAndOpenPortIfNew(sp3);
    enableFailOverOn5XX(balancer);
    balancer.init();
    BalancerUtil.lookupBalancer(balancer, "Default").up("Default", "localhost", 2000);
    BalancerUtil.lookupBalancer(balancer, "Default").up("Default", "localhost", 3000);
    roundRobinStrategy = new RoundRobinStrategy();
    byThreadStrategy = new ByThreadStrategy();
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) LoadBalancingInterceptor(com.predic8.membrane.core.interceptor.balancer.LoadBalancingInterceptor) ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) ByThreadStrategy(com.predic8.membrane.core.interceptor.balancer.ByThreadStrategy) Outcome(com.predic8.membrane.core.interceptor.Outcome) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) HttpRouter(com.predic8.membrane.core.HttpRouter) RoundRobinStrategy(com.predic8.membrane.core.interceptor.balancer.RoundRobinStrategy) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) DummyWebServiceInterceptor(com.predic8.membrane.core.services.DummyWebServiceInterceptor) Before(org.junit.Before)

Aggregations

IOException (java.io.IOException)7 Exchange (com.predic8.membrane.core.exchange.Exchange)3 EndOfStreamException (com.predic8.membrane.core.util.EndOfStreamException)3 Request (com.predic8.membrane.core.http.Request)2 AbstractInterceptor (com.predic8.membrane.core.interceptor.AbstractInterceptor)2 Outcome (com.predic8.membrane.core.interceptor.Outcome)2 EOFWhileReadingFirstLineException (com.predic8.membrane.core.transport.http.EOFWhileReadingFirstLineException)2 SSLProvider (com.predic8.membrane.core.transport.ssl.SSLProvider)2 Socket (java.net.Socket)2 SocketException (java.net.SocketException)2 URISyntaxException (java.net.URISyntaxException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 HttpRouter (com.predic8.membrane.core.HttpRouter)1 Header (com.predic8.membrane.core.http.Header)1 Response (com.predic8.membrane.core.http.Response)1 ByThreadStrategy (com.predic8.membrane.core.interceptor.balancer.ByThreadStrategy)1 LoadBalancingInterceptor (com.predic8.membrane.core.interceptor.balancer.LoadBalancingInterceptor)1 RoundRobinStrategy (com.predic8.membrane.core.interceptor.balancer.RoundRobinStrategy)1 User (com.predic8.membrane.core.interceptor.registration.entity.User)1 AbstractExchangeViewerListener (com.predic8.membrane.core.model.AbstractExchangeViewerListener)1