Search in sources :

Example 26 with Exchange

use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.

the class SOAPRESTHelper method getRequestXMLSource.

protected StreamSource getRequestXMLSource(Exchange exc) throws Exception {
    Request req = new Request(exc.getRequest());
    String res = req.toXml();
    log.debug("http-xml: " + res);
    return new StreamSource(new StringReader(res));
}
Also used : StreamSource(javax.xml.transform.stream.StreamSource) Request(com.predic8.membrane.core.http.xml.Request) StringReader(java.io.StringReader)

Example 27 with Exchange

use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.

the class WSDLPublisherInterceptor method handleRequest.

@Override
public Outcome handleRequest(final Exchange exc) throws Exception {
    if (!"GET".equals(exc.getRequest().getMethod()))
        return Outcome.CONTINUE;
    try {
        String resource = null;
        if (exc.getRequestURI().endsWith("?wsdl") || exc.getRequestURI().endsWith("?WSDL")) {
            processDocuments(exc);
            exc.setResponse(WebServerInterceptor.createResponse(router.getResolverMap(), resource = wsdl));
            exc.getResponse().getHeader().setContentType(MimeType.TEXT_XML);
        }
        if (exc.getRequestURI().contains("?xsd=")) {
            Map<String, String> params = URLParamUtil.getParams(router.getUriFactory(), exc);
            if (params.containsKey("xsd")) {
                int n = Integer.parseInt(params.get("xsd"));
                String path;
                processDocuments(exc);
                synchronized (paths) {
                    if (!paths.containsKey(n)) {
                        exc.setResponse(Response.forbidden("Unknown parameter. You may only retrieve documents referenced by the WSDL.").build());
                        return Outcome.ABORT;
                    }
                    path = paths.get(n);
                }
                exc.setResponse(WebServerInterceptor.createResponse(router.getResolverMap(), resource = path));
                exc.getResponse().getHeader().setContentType(MimeType.TEXT_XML);
            }
        }
        if (resource != null) {
            WSDLInterceptor wi = new WSDLInterceptor();
            wi.setRewriteEndpoint(false);
            wi.setPathRewriter(new RelativePathRewriter(exc, resource));
            wi.handleResponse(exc);
            return Outcome.RETURN;
        }
    } catch (NumberFormatException e) {
        exc.setResponse(HttpUtil.setHTMLErrorResponse(Response.internalServerError(), "Bad parameter format.", ""));
        return Outcome.ABORT;
    } catch (ResourceRetrievalException e) {
        exc.setResponse(Response.notFound().build());
        return Outcome.ABORT;
    }
    return Outcome.CONTINUE;
}
Also used : WSDLInterceptor(com.predic8.membrane.core.interceptor.WSDLInterceptor) ResourceRetrievalException(com.predic8.membrane.core.resolver.ResourceRetrievalException)

Example 28 with Exchange

use of com.predic8.membrane.core.exchange.Exchange in project service-proxy by membrane.

the class SwaggerRewriterInterceptor method handleRequest.

@Override
public Outcome handleRequest(Exchange exc) throws Exception {
    ((ServiceProxy) exc.getRule()).setTargetHost(swagger.getHost());
    URL url = new URL(swaggerUrl);
    exc.getDestinations().set(0, url.getProtocol() + "://" + url.getHost() + (url.getPort() < 0 ? "" : ":" + url.getPort()) + exc.getOriginalRequestUri());
    return super.handleRequest(exc);
}
Also used : ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) URL(java.net.URL)

Example 29 with Exchange

use of com.predic8.membrane.core.exchange.Exchange 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 30 with Exchange

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

Aggregations

Exchange (com.predic8.membrane.core.exchange.Exchange)107 Test (org.junit.Test)39 IOException (java.io.IOException)32 Request (com.predic8.membrane.core.http.Request)25 Outcome (com.predic8.membrane.core.interceptor.Outcome)24 Response (com.predic8.membrane.core.http.Response)16 AbstractInterceptor (com.predic8.membrane.core.interceptor.AbstractInterceptor)16 ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)16 HttpRouter (com.predic8.membrane.core.HttpRouter)14 Before (org.junit.Before)13 ServiceProxyKey (com.predic8.membrane.core.rules.ServiceProxyKey)12 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)11 Header (com.predic8.membrane.core.http.Header)10 HttpClient (com.predic8.membrane.core.transport.http.HttpClient)10 CacheBuilder (com.google.common.cache.CacheBuilder)9 Rule (com.predic8.membrane.core.rules.Rule)6 URISyntaxException (java.net.URISyntaxException)6 UnknownHostException (java.net.UnknownHostException)6 ArrayList (java.util.ArrayList)6 Session (com.predic8.membrane.core.interceptor.authentication.session.SessionManager.Session)5