Search in sources :

Example 1 with JSONContent

use of com.predic8.membrane.core.interceptor.rest.JSONContent 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 2 with JSONContent

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

Example 3 with JSONContent

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

the class AdminRESTInterceptor method getProxies.

@Mapping("/admin/rest/proxies(/?\\?.*)?")
public Response getProxies(final QueryParameter params, String relativeRootPath) throws Exception {
    final List<AbstractServiceProxy> proxies = getServiceProxies();
    if ("order".equals(params.getString("sort"))) {
        if (params.getString("order", "asc").equals("desc"))
            Collections.reverse(proxies);
    } else {
        Collections.sort(proxies, ComparatorFactory.getAbstractServiceProxyComparator(params.getString("sort", "name"), params.getString("order", "asc")));
    }
    final int offset = params.getInt("offset", 0);
    int max = params.getInt("max", proxies.size());
    final List<AbstractServiceProxy> paginated = proxies.subList(offset, Math.min(offset + max, proxies.size()));
    return json(new JSONContent() {

        public void write(JsonGenerator gen) throws Exception {
            gen.writeStartObject();
            gen.writeArrayFieldStart("proxies");
            int i = offset;
            if (params.getString("order", "asc").equals("desc"))
                i = proxies.size() - i + 1;
            for (AbstractServiceProxy p : paginated) {
                gen.writeStartObject();
                gen.writeNumberField("order", i += params.getString("order", "asc").equals("desc") ? -1 : 1);
                gen.writeStringField("name", p.toString());
                gen.writeBooleanField("active", p.isActive());
                if (!p.isActive())
                    gen.writeStringField("error", p.getErrorState());
                gen.writeNumberField("listenPort", p.getKey().getPort());
                gen.writeStringField("virtualHost", p.getKey().getHost());
                gen.writeStringField("method", p.getKey().getMethod());
                gen.writeStringField("path", p.getKey().getPath());
                gen.writeStringField("targetHost", p.getTargetHost());
                gen.writeNumberField("targetPort", p.getTargetPort());
                gen.writeNumberField("count", p.getCount());
                gen.writeObjectFieldStart("actions");
                if (!isReadOnly()) {
                    gen.writeStringField("delete", "/admin/service-proxy/delete?name=" + URLEncoder.encode(RuleUtil.getRuleIdentifier(p), "UTF-8"));
                }
                if (!p.isActive())
                    gen.writeStringField("start", "/admin/service-proxy/start?name=" + URLEncoder.encode(RuleUtil.getRuleIdentifier(p), "UTF-8"));
                gen.writeEndObject();
                gen.writeEndObject();
            }
            gen.writeEndArray();
            gen.writeNumberField("total", proxies.size());
            gen.writeEndObject();
        }
    });
}
Also used : AbstractServiceProxy(com.predic8.membrane.core.rules.AbstractServiceProxy) JSONContent(com.predic8.membrane.core.interceptor.rest.JSONContent) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) SQLException(java.sql.SQLException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) IOException(java.io.IOException)

Example 4 with JSONContent

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

the class AdminRESTInterceptor method getClients.

@Mapping("/admin/rest/clients(/?\\?.*)?")
public Response getClients(QueryParameter params, String relativeRootPath) throws Exception {
    final List<? extends ClientStatistics> clients = getRouter().getExchangeStore().getClientStatistics();
    Collections.sort(clients, ComparatorFactory.getClientStatisticsComparator(params.getString("sort", "name"), params.getString("order", "asc")));
    int offset = params.getInt("offset", 0);
    int max = params.getInt("max", clients.size());
    final int total = clients.size();
    final List<? extends ClientStatistics> paginated = clients.subList(offset, Math.min(offset + max, clients.size()));
    return json(new JSONContent() {

        public void write(JsonGenerator gen) throws Exception {
            gen.writeStartObject();
            gen.writeArrayFieldStart("clients");
            for (ClientStatistics s : paginated) {
                gen.writeStartObject();
                gen.writeStringField("name", s.getClient());
                gen.writeNumberField("count", s.getCount());
                gen.writeNumberField("min", s.getMinDuration());
                gen.writeNumberField("max", s.getMaxDuration());
                gen.writeNumberField("avg", s.getAvgDuration());
                gen.writeEndObject();
            }
            gen.writeEndArray();
            gen.writeNumberField("total", total);
            gen.writeEndObject();
        }
    });
}
Also used : JSONContent(com.predic8.membrane.core.interceptor.rest.JSONContent) ClientStatistics(com.predic8.membrane.core.exchangestore.ClientStatistics) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) SQLException(java.sql.SQLException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) IOException(java.io.IOException)

Aggregations

JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)4 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)4 JSONContent (com.predic8.membrane.core.interceptor.rest.JSONContent)4 IOException (java.io.IOException)4 SQLException (java.sql.SQLException)4 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)2 ClientStatistics (com.predic8.membrane.core.exchangestore.ClientStatistics)1 HeaderField (com.predic8.membrane.core.http.HeaderField)1 Message (com.predic8.membrane.core.http.Message)1 AbstractServiceProxy (com.predic8.membrane.core.rules.AbstractServiceProxy)1