Search in sources :

Example 16 with Response

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

the class DynamicAdminPageInterceptor method handleSystemRequest.

@Mapping("/admin/system/?(\\?.*)?")
public Response handleSystemRequest(final Map<String, String> params, String relativeRootPath) throws Exception {
    StringWriter writer = new StringWriter();
    return respond(new AdminPageBuilder(writer, router, relativeRootPath, params, readOnly) {

        static final int mb = 1024 * 1024;

        final DecimalFormat formatter = new DecimalFormat("#.00");

        private String formatMemoryValue(float value) {
            float newValue = value / (float) mb;
            String unit = "MB";
            if (newValue > 1024)
                unit = "GB";
            return formatter.format(newValue) + " " + unit;
        }

        @Override
        protected int getSelectedTab() {
            return TAB_ID_SYSTEM;
        }

        @Override
        protected void createTabContent() {
            h2().text("System").end();
            p().text("Availabe system memory: " + formatMemoryValue(Runtime.getRuntime().totalMemory())).end();
            p().text("Free system memory: " + formatMemoryValue(Runtime.getRuntime().freeMemory())).end();
            p().text("Membrane version: " + Constants.VERSION).end();
        // createLogConfigurationEditor();
        }
    }.createPage());
}
Also used : StringWriter(java.io.StringWriter) DecimalFormat(java.text.DecimalFormat) URLParamUtil.createQueryString(com.predic8.membrane.core.util.URLParamUtil.createQueryString)

Example 17 with Response

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

the class DynamicAdminPageInterceptor method handleServiceProxyShowRequest.

@Mapping("/admin/service-proxy/show/?(\\?.*)?")
public Response handleServiceProxyShowRequest(final Map<String, String> params, final String relativeRootPath) throws Exception {
    final StringWriter writer = new StringWriter();
    final AbstractServiceProxy rule = (AbstractServiceProxy) RuleUtil.findRuleByIdentifier(router, params.get("name"));
    return respond(new AdminPageBuilder(writer, router, relativeRootPath, params, readOnly) {

        @Override
        protected int getSelectedTab() {
            return TAB_ID_SERVICE_PROXIES;
        }

        @Override
        protected String getTitle() {
            return super.getTitle() + " " + rule.toString() + " ServiceProxy";
        }

        @Override
        protected void createTabContent() throws Exception {
            h1().text(rule.toString() + " ServiceProxy").end();
            script().raw("$(function() {\r\n" + "					$( \"#subtab\" ).tabs();\r\n" + "				});").end();
            div().id("subtab");
            ul();
            li().a().href("#tab1").text("Visualization").end(2);
            li().a().href("#tab2").text("Statistics").end(2);
            // li().a().href("#tab3").text("XML Configuration").end(2);
            end();
            div().id("tab1");
            createServiceProxyVisualization(rule, relativeRootPath);
            end();
            div().id("tab2");
            createStatusCodesTable(rule.getStatisticsByStatusCodes());
            br();
            createButton("View Messages", "calls", null, createQueryString("proxy", rule.toString()));
            end();
            end();
        }
    }.createPage());
}
Also used : AbstractServiceProxy(com.predic8.membrane.core.rules.AbstractServiceProxy) StringWriter(java.io.StringWriter)

Example 18 with Response

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

the class AdminRESTInterceptor method getBeautifiedBody.

@Mapping("/admin/web/exchanges/(-?\\d+)/(response|request)/body")
public Response getBeautifiedBody(QueryParameter params, String relativeRootPath) throws Exception {
    AbstractExchange exc = router.getExchangeStore().getExchangeById(params.getGroupInt(1));
    if (exc == null) {
        return Response.notFound().build();
    }
    Message msg = params.getGroup(2).equals("response") ? exc.getResponse() : exc.getRequest();
    if (msg == null || msg.isBodyEmpty()) {
        return Response.noContent().build();
    }
    return Response.ok().contentType(MimeType.TEXT_HTML_UTF8).body(TextUtil.formatXML(new InputStreamReader(msg.getBodyAsStreamDecoded(), msg.getCharset()), true)).build();
}
Also used : Message(com.predic8.membrane.core.http.Message) InputStreamReader(java.io.InputStreamReader) AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange)

Example 19 with Response

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

the class AdminRESTInterceptor method getExchanges.

@Mapping("/admin/rest/exchanges(/?\\?.*)?")
public Response getExchanges(QueryParameter params, String relativeRootPath) throws Exception {
    if (params.getString("waitForModification") != null) {
        getRouter().getExchangeStore().waitForModification(params.getLong("waitForModification"));
    }
    List<AbstractExchange> exchanges;
    synchronized (getRouter().getExchangeStore().getAllExchangesAsList()) {
        exchanges = new ArrayList<AbstractExchange>(getRouter().getExchangeStore().getAllExchangesAsList());
    }
    exchanges = filter(params, exchanges);
    Collections.sort(exchanges, ComparatorFactory.getAbstractExchangeComparator(params.getString("sort", "time"), params.getString("order", "desc")));
    int offset = params.getInt("offset", 0);
    int max = params.getInt("max", exchanges.size());
    final int total = exchanges.size();
    final List<AbstractExchange> paginated = exchanges.subList(offset, Math.min(offset + max, exchanges.size()));
    return json(new JSONContent() {

        public void write(JsonGenerator gen) throws Exception {
            gen.writeStartObject();
            gen.writeArrayFieldStart("exchanges");
            for (AbstractExchange e : paginated) {
                writeExchange(e, gen);
            }
            gen.writeEndArray();
            gen.writeNumberField("total", total);
            gen.writeNumberField("lastModified", getRouter().getExchangeStore().getLastModified());
            gen.writeEndObject();
        }
    });
}
Also used : JSONContent(com.predic8.membrane.core.interceptor.rest.JSONContent) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange) SQLException(java.sql.SQLException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) IOException(java.io.IOException)

Example 20 with Response

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

the class AdminRESTInterceptor method getRequestHeader.

@Mapping("/admin/rest/exchanges/(-?\\d+)/(response|request)/header")
public Response getRequestHeader(QueryParameter params, String relativeRootPath) throws Exception {
    final AbstractExchange exc = router.getExchangeStore().getExchangeById(params.getGroupInt(1));
    if (exc == null) {
        return Response.notFound().build();
    }
    final Message msg = params.getGroup(2).equals("response") ? exc.getResponse() : exc.getRequest();
    if (msg == null) {
        return Response.noContent().build();
    }
    return json(new JSONContent() {

        public void write(JsonGenerator gen) throws Exception {
            gen.writeStartObject();
            gen.writeArrayFieldStart("headers");
            for (HeaderField hf : msg.getHeader().getAllHeaderFields()) {
                gen.writeStartObject();
                gen.writeStringField("name", hf.getHeaderName().toString());
                gen.writeStringField("value", hf.getValue());
                gen.writeEndObject();
            }
            gen.writeEndArray();
            gen.writeEndObject();
        }
    });
}
Also used : Message(com.predic8.membrane.core.http.Message) JSONContent(com.predic8.membrane.core.interceptor.rest.JSONContent) HeaderField(com.predic8.membrane.core.http.HeaderField) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange) SQLException(java.sql.SQLException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) IOException(java.io.IOException)

Aggregations

Response (com.predic8.membrane.core.http.Response)29 Exchange (com.predic8.membrane.core.exchange.Exchange)14 IOException (java.io.IOException)14 StringWriter (java.io.StringWriter)9 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)7 Request (com.predic8.membrane.core.http.Request)7 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)6 Header (com.predic8.membrane.core.http.Header)6 Test (org.junit.Test)6 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)4 Message (com.predic8.membrane.core.http.Message)4 JSONContent (com.predic8.membrane.core.interceptor.rest.JSONContent)4 ProxyRule (com.predic8.membrane.core.rules.ProxyRule)4 HttpClient (com.predic8.membrane.core.transport.http.HttpClient)4 SQLException (java.sql.SQLException)4 Element (org.w3c.dom.Element)4 NodeList (org.w3c.dom.NodeList)4 JsonFactory (com.fasterxml.jackson.core.JsonFactory)3 MCElement (com.predic8.membrane.annot.MCElement)3 ResponseBuilder (com.predic8.membrane.core.http.Response.ResponseBuilder)3