Search in sources :

Example 6 with Key

use of com.predic8.membrane.core.interceptor.apimanagement.Key in project service-proxy by membrane.

the class Key method toString.

public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("Key: ").append(name).append("|");
    int c = 1;
    for (Policy p : policies) {
        builder.append(" [").append(c++).append("] ").append(p.getName());
    }
    return builder.toString();
}
Also used : Policy(com.predic8.membrane.core.interceptor.apimanagement.policy.Policy)

Example 7 with Key

use of com.predic8.membrane.core.interceptor.apimanagement.Key in project service-proxy by membrane.

the class AMQuota method addRequestEntry.

private void addRequestEntry(String apiKey, long sizeOfBytes) {
    synchronized (keyByteCounter) {
        if (!keyByteCounter.containsKey(apiKey)) {
            ApiKeyByteCounter value = new ApiKeyByteCounter();
            Key key = amc.getKeys().get(apiKey);
            for (Policy p : key.getPolicies()) {
                value.getPolicyByteCounters().put(p.getName(), new AtomicLong());
            }
            keyByteCounter.put(apiKey, value);
        }
    }
    ApiKeyByteCounter keyInfo = keyByteCounter.get(apiKey);
    for (AtomicLong counter : keyInfo.getPolicyByteCounters().values()) {
        counter.addAndGet(sizeOfBytes);
    }
}
Also used : Policy(com.predic8.membrane.core.interceptor.apimanagement.policy.Policy) AtomicLong(java.util.concurrent.atomic.AtomicLong) Key(com.predic8.membrane.core.interceptor.apimanagement.Key)

Example 8 with Key

use of com.predic8.membrane.core.interceptor.apimanagement.Key in project service-proxy by membrane.

the class EmbeddingJava method main.

public static void main(String[] args) throws Exception {
    String hostname = "*";
    String method = "GET";
    String path = ".*";
    int listenPort = 4000;
    ServiceProxyKey key = new ServiceProxyKey(hostname, method, path, listenPort);
    String targetHost = "predic8.com";
    int targetPort = 80;
    ServiceProxy sp = new ServiceProxy(key, targetHost, targetPort);
    sp.getInterceptors().add(new AddMyHeaderInterceptor());
    HttpRouter router = new HttpRouter();
    router.add(sp);
    router.init();
}
Also used : ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) HttpRouter(com.predic8.membrane.core.HttpRouter)

Example 9 with Key

use of com.predic8.membrane.core.interceptor.apimanagement.Key in project service-proxy by membrane.

the class MCUtil method addXML.

private static void addXML(Object object, String id, XMLStreamWriter xew, SerializationContext sc) throws XMLStreamException {
    if (object == null)
        throw new InvalidParameterException("'object' must not be null.");
    Class<? extends Object> clazz = object.getClass();
    MCElement e = clazz.getAnnotation(MCElement.class);
    if (e == null)
        throw new IllegalArgumentException("'object' must be @MCElement-annotated.");
    BeanWrapperImpl src = new BeanWrapperImpl(object);
    xew.writeStartElement(e.name());
    if (id != null)
        xew.writeAttribute("id", id);
    HashSet<String> attributes = new HashSet<String>();
    for (Method m : clazz.getMethods()) {
        if (!m.getName().startsWith("set"))
            continue;
        String propertyName = AnnotUtils.dejavaify(m.getName().substring(3));
        MCAttribute a = m.getAnnotation(MCAttribute.class);
        if (a != null) {
            Object value = src.getPropertyValue(propertyName);
            String str;
            if (value == null)
                continue;
            else if (value instanceof String)
                str = (String) value;
            else if (value instanceof Boolean)
                str = ((Boolean) value).toString();
            else if (value instanceof Integer)
                str = ((Integer) value).toString();
            else if (value instanceof Long)
                str = ((Long) value).toString();
            else if (value instanceof Enum<?>)
                str = value.toString();
            else {
                MCElement el = value.getClass().getAnnotation(MCElement.class);
                if (el != null) {
                    str = defineBean(sc, value, null, true);
                } else {
                    str = "?";
                    sc.incomplete = true;
                }
            }
            if (a.attributeName().length() > 0)
                propertyName = a.attributeName();
            attributes.add(propertyName);
            xew.writeAttribute(propertyName, str);
        }
    }
    for (Method m : clazz.getMethods()) {
        if (!m.getName().startsWith("set"))
            continue;
        String propertyName = AnnotUtils.dejavaify(m.getName().substring(3));
        MCOtherAttributes o = m.getAnnotation(MCOtherAttributes.class);
        if (o != null) {
            Object value = src.getPropertyValue(propertyName);
            if (value instanceof Map<?, ?>) {
                Map<?, ?> map = (Map<?, ?>) value;
                for (Map.Entry<?, ?> entry : map.entrySet()) {
                    Object key = entry.getKey();
                    Object val = entry.getValue();
                    if (!(key instanceof String) || !(val instanceof String)) {
                        sc.incomplete = true;
                        key = "incompleteAttributes";
                        val = "?";
                    }
                    if (attributes.contains(key))
                        continue;
                    attributes.add((String) key);
                    xew.writeAttribute((String) key, (String) val);
                }
            } else {
                xew.writeAttribute("incompleteAttributes", "?");
                sc.incomplete = true;
            }
        }
    }
    List<Method> childElements = new ArrayList<Method>();
    for (Method m : clazz.getMethods()) {
        if (!m.getName().startsWith("set"))
            continue;
        String propertyName = AnnotUtils.dejavaify(m.getName().substring(3));
        MCChildElement c = m.getAnnotation(MCChildElement.class);
        if (c != null) {
            childElements.add(m);
        }
        MCTextContent t = m.getAnnotation(MCTextContent.class);
        if (t != null) {
            Object value = src.getPropertyValue(propertyName);
            if (value == null) {
                continue;
            } else if (value instanceof String) {
                xew.writeCharacters((String) value);
            } else {
                xew.writeCharacters("?");
                sc.incomplete = true;
            }
        }
    }
    Collections.sort(childElements, new Comparator<Method>() {

        @Override
        public int compare(Method o1, Method o2) {
            MCChildElement c1 = o1.getAnnotation(MCChildElement.class);
            MCChildElement c2 = o2.getAnnotation(MCChildElement.class);
            return c1.order() - c2.order();
        }
    });
    for (Method m : childElements) {
        String propertyName = AnnotUtils.dejavaify(m.getName().substring(3));
        Object value = src.getPropertyValue(propertyName);
        if (value != null) {
            if (value instanceof Collection<?>) {
                for (Object item : (Collection<?>) value) addXML(item, null, xew, sc);
            } else {
                addXML(value, null, xew, sc);
            }
        }
    }
    xew.writeEndElement();
}
Also used : MCChildElement(com.predic8.membrane.annot.MCChildElement) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) ArrayList(java.util.ArrayList) InvalidParameterException(java.security.InvalidParameterException) MCElement(com.predic8.membrane.annot.MCElement) MCAttribute(com.predic8.membrane.annot.MCAttribute) HashSet(java.util.HashSet) MCTextContent(com.predic8.membrane.annot.MCTextContent) Method(java.lang.reflect.Method) MCOtherAttributes(com.predic8.membrane.annot.MCOtherAttributes) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with Key

use of com.predic8.membrane.core.interceptor.apimanagement.Key in project service-proxy by membrane.

the class EtcdBasedConfigurator method setUpModuleServiceProxy.

private ServiceProxy setUpModuleServiceProxy(String name, int port, String path) {
    log.info("Creating serviceProxy for module: " + path);
    ServiceProxyKey key = new ServiceProxyKey("*", "*", path, port);
    key.setUsePathPattern(true);
    key.setPathRegExp(false);
    ServiceProxy sp = new ServiceProxy(key, null, 0);
    sp.getInterceptors().add(new LoadBalancingInterceptor());
    try {
        sp.init(router);
        router.add(sp);
        runningServiceProxyForModule.put(path, sp);
    } catch (Exception ignored) {
    }
    return sp;
}
Also used : LoadBalancingInterceptor(com.predic8.membrane.core.interceptor.balancer.LoadBalancingInterceptor) ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) BeansException(org.springframework.beans.BeansException)

Aggregations

Policy (com.predic8.membrane.core.interceptor.apimanagement.policy.Policy)5 RuleKey (com.predic8.membrane.core.rules.RuleKey)3 ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)3 ServiceProxyKey (com.predic8.membrane.core.rules.ServiceProxyKey)3 IOException (java.io.IOException)3 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)2 Request (com.predic8.membrane.core.http.Request)2 Key (com.predic8.membrane.core.interceptor.apimanagement.Key)2 ResourceRetrievalException (com.predic8.membrane.core.resolver.ResourceRetrievalException)2 StatisticCollector (com.predic8.membrane.core.rules.StatisticCollector)2 MalformedURLException (java.net.MalformedURLException)2 MCAttribute (com.predic8.membrane.annot.MCAttribute)1 MCChildElement (com.predic8.membrane.annot.MCChildElement)1 MCElement (com.predic8.membrane.annot.MCElement)1 MCOtherAttributes (com.predic8.membrane.annot.MCOtherAttributes)1 MCTextContent (com.predic8.membrane.annot.MCTextContent)1 MainInfo (com.predic8.membrane.annot.model.MainInfo)1 HttpRouter (com.predic8.membrane.core.HttpRouter)1 SSLParser (com.predic8.membrane.core.config.security.SSLParser)1 Exchange (com.predic8.membrane.core.exchange.Exchange)1