Search in sources :

Example 1 with Mapping

use of com.predic8.membrane.core.interceptor.administration.Mapping in project service-proxy by membrane.

the class RESTInterceptor method dispatchRequest.

private Outcome dispatchRequest(Exchange exc) throws Exception {
    String path = router.getUriFactory().create(exc.getDestinations().get(0)).getPath();
    for (Method m : getClass().getMethods()) {
        Mapping a = m.getAnnotation(Mapping.class);
        if (a == null)
            continue;
        Matcher matcher = Pattern.compile(a.value()).matcher(path);
        if (matcher.matches()) {
            Object[] parameters;
            switch(m.getParameterTypes().length) {
                case 2:
                    parameters = new Object[] { new QueryParameter(URLParamUtil.getParams(router.getUriFactory(), exc), matcher), getRelativeRootPath(path) };
                    break;
                case 3:
                    parameters = new Object[] { new QueryParameter(URLParamUtil.getParams(router.getUriFactory(), exc), matcher), getRelativeRootPath(path), exc };
                    break;
                default:
                    throw new InvalidParameterException("@Mapping is supposed to annotate a 2-parameter method.");
            }
            exc.setResponse((Response) m.invoke(this, parameters));
            return Outcome.RETURN;
        }
    }
    return Outcome.CONTINUE;
}
Also used : InvalidParameterException(java.security.InvalidParameterException) Matcher(java.util.regex.Matcher) Mapping(com.predic8.membrane.core.interceptor.administration.Mapping) Method(java.lang.reflect.Method)

Example 2 with Mapping

use of com.predic8.membrane.core.interceptor.administration.Mapping in project service-proxy by membrane.

the class REST2SOAPInterceptor method handleResponse.

@Override
public Outcome handleResponse(Exchange exc) throws Exception {
    Mapping mapping = getRESTURL(exc);
    log.debug("restURL: " + mapping);
    if (getRESTURL(exc) == null)
        return Outcome.CONTINUE;
    if (log.isDebugEnabled())
        log.debug("response: " + new String(getTransformer(null).transform(getBodySource(exc), exc.getStringProperties()), Constants.UTF_8_CHARSET));
    exc.getResponse().setBodyContent(getTransformer(mapping.responseXSLT).transform(getBodySource(exc)));
    Header header = exc.getResponse().getHeader();
    header.removeFields(Header.CONTENT_TYPE);
    header.setContentType(MimeType.TEXT_XML_UTF8);
    XML2HTTP.unwrapMessageIfNecessary(exc.getResponse());
    convertResponseToJSONIfNecessary(exc.getRequest().getHeader(), mapping, exc.getResponse(), exc.getStringProperties());
    return Outcome.CONTINUE;
}
Also used : Header(com.predic8.membrane.core.http.Header)

Example 3 with Mapping

use of com.predic8.membrane.core.interceptor.administration.Mapping in project service-proxy by membrane.

the class REST2SOAPInterceptor method modifyRequest.

private void modifyRequest(AbstractExchange exc, Mapping mapping) {
    exc.getRequest().setMethod("POST");
    exc.getRequest().getHeader().setSOAPAction(mapping.soapAction);
    Header header = exc.getRequest().getHeader();
    header.removeFields(Header.CONTENT_TYPE);
    header.setContentType(isSOAP12(exc) ? MimeType.APPLICATION_SOAP : MimeType.TEXT_XML_UTF8);
    exc.setProperty("mapping", mapping);
    setServiceEndpoint(exc, mapping);
}
Also used : Header(com.predic8.membrane.core.http.Header)

Example 4 with Mapping

use of com.predic8.membrane.core.interceptor.administration.Mapping in project service-proxy by membrane.

the class DynamicAdminPageInterceptor method handleNodeSessionsRequest.

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

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

        @Override
        protected void createTabContent() throws Exception {
            h2().text("Node " + params.get("host") + ":" + params.get("port")).end();
            h3().text("Sessions").end();
            createSessionsTable(BalancerUtil.lookupBalancer(router, getBalancerParam(params)).getSessionsByNode(params.get("cluster"), new Node(params.get("host"), Integer.parseInt(params.get("port")))));
        }
    }.createPage());
}
Also used : StringWriter(java.io.StringWriter) Node(com.predic8.membrane.core.interceptor.balancer.Node)

Example 5 with Mapping

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

Aggregations

StringWriter (java.io.StringWriter)7 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)5 Rule (com.predic8.membrane.core.rules.Rule)5 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)4 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)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 IOException (java.io.IOException)4 SQLException (java.sql.SQLException)4 HttpRouter (com.predic8.membrane.core.HttpRouter)3 Mapping (com.predic8.membrane.core.interceptor.administration.Mapping)3 Mapping (com.predic8.membrane.core.interceptor.rewrite.RewriteInterceptor.Mapping)3 AbstractServiceProxy (com.predic8.membrane.core.rules.AbstractServiceProxy)3 Header (com.predic8.membrane.core.http.Header)2 Node (com.predic8.membrane.core.interceptor.balancer.Node)2 ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)2 ServiceProxyKey (com.predic8.membrane.core.rules.ServiceProxyKey)2 URLParamUtil.createQueryString (com.predic8.membrane.core.util.URLParamUtil.createQueryString)2 Definitions (com.predic8.wsdl.Definitions)2