Search in sources :

Example 1 with Transport

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

the class HttpRouter method createTransport.

/**
 * Same as the default config from monitor-beans.xml
 */
private Transport createTransport(ProxyConfiguration proxyConfiguration) {
    Transport transport = new HttpTransport();
    List<Interceptor> interceptors = new ArrayList<Interceptor>();
    interceptors.add(new RuleMatchingInterceptor());
    interceptors.add(new DispatchingInterceptor());
    interceptors.add(new UserFeatureInterceptor());
    HTTPClientInterceptor httpClientInterceptor = new HTTPClientInterceptor();
    interceptors.add(httpClientInterceptor);
    transport.setInterceptors(interceptors);
    return transport;
}
Also used : HttpTransport(com.predic8.membrane.core.transport.http.HttpTransport) HTTPClientInterceptor(com.predic8.membrane.core.interceptor.HTTPClientInterceptor) ArrayList(java.util.ArrayList) HttpTransport(com.predic8.membrane.core.transport.http.HttpTransport) Transport(com.predic8.membrane.core.transport.Transport) HTTPClientInterceptor(com.predic8.membrane.core.interceptor.HTTPClientInterceptor) RuleMatchingInterceptor(com.predic8.membrane.core.interceptor.RuleMatchingInterceptor) Interceptor(com.predic8.membrane.core.interceptor.Interceptor) UserFeatureInterceptor(com.predic8.membrane.core.interceptor.UserFeatureInterceptor) DispatchingInterceptor(com.predic8.membrane.core.interceptor.DispatchingInterceptor) UserFeatureInterceptor(com.predic8.membrane.core.interceptor.UserFeatureInterceptor) RuleMatchingInterceptor(com.predic8.membrane.core.interceptor.RuleMatchingInterceptor) DispatchingInterceptor(com.predic8.membrane.core.interceptor.DispatchingInterceptor)

Example 2 with Transport

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

the class ServiceInvocationTest method createRouter.

private HttpRouter createRouter() throws Exception {
    HttpRouter router = new HttpRouter();
    router.getRuleManager().addProxyAndOpenPortIfNew(createFirstRule());
    router.getRuleManager().addProxyAndOpenPortIfNew(createServiceRule());
    router.getTransport().getInterceptors().add(router.getTransport().getInterceptors().size() - 1, new MockInterceptor("transport-log"));
    router.init();
    return router;
}
Also used : MockInterceptor(com.predic8.membrane.core.interceptor.MockInterceptor) HttpRouter(com.predic8.membrane.core.HttpRouter)

Example 3 with Transport

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

the class MembraneServlet method init.

@Override
public void init(ServletConfig config) throws ServletException {
    try {
        appCtx = new BaseLocationXmlWebApplicationContext();
        log.debug("loading beans configuration from: " + getProxiesXmlLocation(config));
        router = RouterUtil.initializeRoutersFromSpringWebContext(appCtx, config.getServletContext(), getProxiesXmlLocation(config));
        if (router == null)
            throw new RuntimeException("No <router> with a <servletTransport> was found. To use <router> with <transport>, use MembraneServletContextListener instead of MembraneServlet.");
    } catch (Exception e) {
        throw new ServletException(e);
    }
}
Also used : ServletException(javax.servlet.ServletException) BaseLocationXmlWebApplicationContext(com.predic8.membrane.servlet.config.spring.BaseLocationXmlWebApplicationContext) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 4 with Transport

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

the class AbstractHttpHandler method generateErrorResponse.

private Response generateErrorResponse(Exception e) {
    String msg;
    boolean printStackTrace = transport.isPrintStackTrace();
    if (printStackTrace) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        msg = sw.toString();
    } else {
        msg = e.toString();
    }
    String comment = "Stack traces can be " + (printStackTrace ? "dis" : "en") + "abled by setting the " + "@printStackTrace attribute on <a href=\"https://membrane-soa.org/service-proxy-doc/current/configuration/reference/transport.htm\">transport</a>. " + "More details might be found in the log.";
    Response error = null;
    ResponseBuilder b = null;
    if (e instanceof URISyntaxException)
        b = Response.badRequest();
    if (b == null)
        b = Response.internalServerError();
    switch(ContentTypeDetector.detect(exchange.getRequest()).getEffectiveContentType()) {
        case XML:
            error = b.header(HttpUtil.createHeaders(MimeType.TEXT_XML_UTF8)).body(("<error><message>" + StringEscapeUtils.escapeXml(msg) + "</message><comment>" + StringEscapeUtils.escapeXml(comment) + "</comment></error>").getBytes(Constants.UTF_8_CHARSET)).build();
            break;
        case JSON:
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {
                JsonGenerator jg = new JsonFactory().createGenerator(baos);
                jg.writeStartObject();
                jg.writeFieldName("error");
                jg.writeString(msg);
                jg.writeFieldName("comment");
                jg.writeString(comment);
                jg.close();
            } catch (Exception f) {
                log.error("Error generating JSON error response", f);
            }
            error = b.header(HttpUtil.createHeaders(MimeType.APPLICATION_JSON_UTF8)).body(baos.toByteArray()).build();
            break;
        case SOAP:
            error = b.header(HttpUtil.createHeaders(MimeType.TEXT_XML_UTF8)).body(HttpUtil.getFaultSOAPBody("Internal Server Error", msg + " " + comment).getBytes(Constants.UTF_8_CHARSET)).build();
            break;
        case UNKNOWN:
            error = HttpUtil.setHTMLErrorResponse(b, msg, comment);
            break;
    }
    return error;
}
Also used : Response(com.predic8.membrane.core.http.Response) StringWriter(java.io.StringWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) URISyntaxException(java.net.URISyntaxException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ResponseBuilder(com.predic8.membrane.core.http.Response.ResponseBuilder) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) EndOfStreamException(com.predic8.membrane.core.util.EndOfStreamException) PrintWriter(java.io.PrintWriter)

Example 5 with Transport

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

the class HttpEndpointListener method run.

@Override
public void run() {
    while (!closed) {
        try {
            Socket socket = serverSocket.accept();
            openSockets.put(socket, Boolean.TRUE);
            try {
                if (log.isDebugEnabled())
                    log.debug("Accepted connection from " + socket.getRemoteSocketAddress());
                transport.getExecutorService().execute(new HttpServerHandler(socket, this));
            } catch (RejectedExecutionException e) {
                openSockets.remove(socket);
                log.error("HttpServerHandler execution rejected. Might be due to a proxies.xml hot deployment in progress or a low" + " value for <transport maxThreadPoolSize=\"...\">.");
                socket.close();
            }
        } catch (SocketException e) {
            String message = e.getMessage();
            if (message != null && (message.endsWith("socket closed") || message.endsWith("Socket closed"))) {
                log.debug("socket closed.");
                break;
            } else
                log.error("", e);
        } catch (NullPointerException e) {
        // Ignore this. serverSocket variable is set null during a loop in the process of closing server socket.
        } catch (Exception e) {
            log.error("", e);
        }
    }
}
Also used : SocketException(java.net.SocketException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IOException(java.io.IOException) BindException(java.net.BindException) PortOccupiedException(com.predic8.membrane.core.transport.PortOccupiedException) SocketException(java.net.SocketException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) InvalidParameterException(java.security.InvalidParameterException)

Aggregations

IOException (java.io.IOException)3 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 HttpRouter (com.predic8.membrane.core.HttpRouter)1 Response (com.predic8.membrane.core.http.Response)1 ResponseBuilder (com.predic8.membrane.core.http.Response.ResponseBuilder)1 DispatchingInterceptor (com.predic8.membrane.core.interceptor.DispatchingInterceptor)1 HTTPClientInterceptor (com.predic8.membrane.core.interceptor.HTTPClientInterceptor)1 Interceptor (com.predic8.membrane.core.interceptor.Interceptor)1 MockInterceptor (com.predic8.membrane.core.interceptor.MockInterceptor)1 RuleMatchingInterceptor (com.predic8.membrane.core.interceptor.RuleMatchingInterceptor)1 UserFeatureInterceptor (com.predic8.membrane.core.interceptor.UserFeatureInterceptor)1 PortOccupiedException (com.predic8.membrane.core.transport.PortOccupiedException)1 Transport (com.predic8.membrane.core.transport.Transport)1 HttpTransport (com.predic8.membrane.core.transport.http.HttpTransport)1 EndOfStreamException (com.predic8.membrane.core.util.EndOfStreamException)1 BaseLocationXmlWebApplicationContext (com.predic8.membrane.servlet.config.spring.BaseLocationXmlWebApplicationContext)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1