Search in sources :

Example 1 with DigestGenerator

use of org.wso2.carbon.mediator.cache.digest.DigestGenerator in project carbon-mediation by wso2.

the class CacheMediatorFactory method createSpecificMediator.

/**
 * {@inheritDoc}
 */
protected Mediator createSpecificMediator(OMElement elem, Properties properties) {
    if (!CachingConstants.CACHE_Q.equals(elem.getQName())) {
        handleException("Unable to create the cache mediator. Unexpected element as the cache mediator configuration");
    }
    CacheMediator cache = new CacheMediator(cacheManager);
    OMAttribute collectorAttr = elem.getAttribute(ATT_COLLECTOR);
    if (collectorAttr != null && collectorAttr.getAttributeValue() != null) {
        if ("true".equals(collectorAttr.getAttributeValue())) {
            cache.setCollector(true);
            OMAttribute scopeAttribute = elem.getAttribute(ATT_SCOPE);
            if (scopeAttribute != null && scopeAttribute.getAttributeValue() != null) {
                cache.setScope(scopeAttribute.getAttributeValue().trim());
            }
        } else if ("false".equals(collectorAttr.getAttributeValue())) {
            cache.setCollector(false);
            OMAttribute timeoutAttr = elem.getAttribute(ATT_TIMEOUT);
            if (timeoutAttr != null && timeoutAttr.getAttributeValue() != null) {
                cache.setTimeout(Long.parseLong(timeoutAttr.getAttributeValue().trim()));
            } else {
                cache.setTimeout(CachingConstants.DEFAULT_TIMEOUT);
            }
            OMAttribute maxMessageSizeAttr = elem.getAttribute(ATT_MAX_MSG_SIZE);
            if (maxMessageSizeAttr != null && maxMessageSizeAttr.getAttributeValue() != null) {
                cache.setMaxMessageSize(Integer.parseInt(maxMessageSizeAttr.getAttributeValue().trim()));
            } else {
                cache.setMaxMessageSize(-1);
            }
            OMAttribute idAttribute = elem.getAttribute(ATT_ID);
            if (idAttribute != null && idAttribute.getAttributeValue() != null) {
                cache.setId(idAttribute.getAttributeValue().trim());
            }
            OMAttribute hashGeneratorAttribute = elem.getAttribute(ATT_HASH_GENERATOR);
            if (hashGeneratorAttribute != null && hashGeneratorAttribute.getAttributeValue() != null) {
                cache.setHashGenerator(hashGeneratorAttribute.getAttributeValue().trim());
            }
            OMAttribute scopeAttribute = elem.getAttribute(ATT_SCOPE);
            if (scopeAttribute != null && scopeAttribute.getAttributeValue() != null) {
                cache.setScope(scopeAttribute.getAttributeValue().trim());
            }
            String className = null;
            OMElement protocolElem = elem.getFirstChildWithName(PROTOCOL_Q);
            Map<String, Object> props = new HashMap<>();
            if (protocolElem != null) {
                OMAttribute typeAttr = protocolElem.getAttribute(ATT_TYPE);
                if (typeAttr != null && typeAttr.getAttributeValue() != null) {
                    OMElement hashGeneratorElem = protocolElem.getFirstChildWithName(HASH_GENERATOR_Q);
                    if (hashGeneratorElem != null) {
                        className = hashGeneratorElem.getText();
                    }
                    String protocolType = typeAttr.getAttributeValue().toUpperCase().trim();
                    cache.setProtocolType(protocolType);
                    if (CachingConstants.HTTP_PROTOCOL_TYPE.equals(protocolType)) {
                        OMElement methodElem = protocolElem.getFirstChildWithName(HTTP_METHODS_TO_CACHE_Q);
                        if (methodElem != null) {
                            String[] methods = methodElem.getText().split(",");
                            if (!"".equals(methods[0])) {
                                for (int i = 0; i < methods.length; i++) {
                                    methods[i] = methods[i].toUpperCase().trim();
                                    if (!(PassThroughConstants.HTTP_POST.equals(methods[i]) || PassThroughConstants.HTTP_GET.equals(methods[i]) || PassThroughConstants.HTTP_HEAD.equals(methods[i]) || PassThroughConstants.HTTP_PUT.equals(methods[i]) || PassThroughConstants.HTTP_DELETE.equals(methods[i]) || PassThroughConstants.HTTP_OPTIONS.equals(methods[i]) || PassThroughConstants.HTTP_CONNECT.equals(methods[i]) || "PATCH".equals(methods[i]) || CachingConstants.ALL.equals(methods[i]))) {
                                        handleException("Unexpected method type: " + methods[i]);
                                    }
                                }
                                cache.setHTTPMethodsToCache(methods);
                            }
                        } else {
                            cache.setHTTPMethodsToCache(CachingConstants.ALL);
                        }
                        OMElement headersToIncludeInHash = protocolElem.getFirstChildWithName(HEADERS_TO_INCLUDE_IN_HASH_Q);
                        if (headersToIncludeInHash != null) {
                            String[] headers = headersToIncludeInHash.getText().split(",");
                            for (int i = 0; i < headers.length; i++) {
                                headers[i] = headers[i].trim();
                            }
                            cache.setHeadersToIncludeInHash(headers);
                        } else {
                            cache.setHeadersToIncludeInHash("");
                        }
                        OMElement headersToExcludeInHash = protocolElem.getFirstChildWithName(HEADERS_TO_EXCLUDE_IN_HASH_Q);
                        if (headersToExcludeInHash != null) {
                            String[] headers = headersToExcludeInHash.getText().split(",");
                            for (int i = 0; i < headers.length; i++) {
                                headers[i] = headers[i].trim();
                            }
                            cache.setHeadersToExcludeInHash(headers);
                        } else {
                            cache.setHeadersToExcludeInHash("");
                        }
                        OMElement responseCodesElem = protocolElem.getFirstChildWithName(RESPONSE_CODES_Q);
                        if (responseCodesElem != null) {
                            String responses = responseCodesElem.getText();
                            if (!"".equals(responses) && responses != null) {
                                cache.setResponseCodes(responses);
                            }
                        } else {
                            cache.setResponseCodes(CachingConstants.ANY_RESPONSE_CODE);
                        }
                        OMElement enableCacheControlElem = protocolElem.getFirstChildWithName(ENABLE_CACHE_CONTROL_Q);
                        if (enableCacheControlElem != null) {
                            String cacheControlElemText = enableCacheControlElem.getText();
                            if (StringUtils.isNotEmpty(cacheControlElemText)) {
                                cache.setCacheControlEnabled(Boolean.parseBoolean(cacheControlElemText));
                            }
                        } else {
                            cache.setCacheControlEnabled(CachingConstants.DEFAULT_ENABLE_CACHE_CONTROL);
                        }
                        OMElement addAgeHeaderElem = protocolElem.getFirstChildWithName(INCLUDE_AGE_HEADER_Q);
                        if (addAgeHeaderElem != null) {
                            String addAgeHeaderElemText = addAgeHeaderElem.getText();
                            if (StringUtils.isNotEmpty(addAgeHeaderElemText)) {
                                cache.setAddAgeHeaderEnabled(Boolean.parseBoolean(addAgeHeaderElemText));
                            }
                        } else {
                            cache.setCacheControlEnabled(CachingConstants.DEFAULT_ADD_AGE_HEADER);
                        }
                        props.put(CachingConstants.INCLUDED_HEADERS_PROPERTY, cache.getHeadersToIncludeInHash());
                        props.put(CachingConstants.EXCLUDED_HEADERS_PROPERTY, cache.getHeadersToExcludeInHash());
                    }
                } else {
                    cache.setProtocolType(CachingConstants.HTTP_PROTOCOL_TYPE);
                }
            } else {
                OMAttribute hashGeneratorAttr = elem.getAttribute(ATT_HASH_GENERATOR);
                if (hashGeneratorAttr != null && hashGeneratorAttr.getAttributeValue() != null) {
                    className = hashGeneratorAttr.getAttributeValue();
                }
            }
            if (className != null && !"".equals(className)) {
                try {
                    Class generator = Class.forName(className);
                    Object o = generator.newInstance();
                    if (o instanceof DigestGenerator) {
                        cache.setDigestGenerator((DigestGenerator) o);
                    } else {
                        handleException("Specified class for the hashGenerator is not a " + "DigestGenerator. It *must* implement " + "org.wso2.carbon.mediator.cache.digest.DigestGenerator interface");
                    }
                } catch (ClassNotFoundException e) {
                    handleException("Unable to load the hash generator class", e);
                } catch (IllegalAccessException e) {
                    handleException("Unable to access the hash generator class", e);
                } catch (InstantiationException e) {
                    handleException("Unable to instantiate the hash generator class", e);
                }
            } else {
                cache.setDigestGenerator(CachingConstants.DEFAULT_HASH_GENERATOR);
            }
            props.put(CachingConstants.PERMANENTLY_EXCLUDED_HEADERS_STRING, CachingConstants.PERMANENTLY_EXCLUDED_HEADERS);
            cache.getDigestGenerator().init(props);
            OMElement onCacheHitElem = elem.getFirstChildWithName(ON_CACHE_HIT_Q);
            if (onCacheHitElem != null) {
                OMAttribute sequenceAttr = onCacheHitElem.getAttribute(ATT_SEQUENCE);
                if (sequenceAttr != null && sequenceAttr.getAttributeValue() != null) {
                    cache.setOnCacheHitRef(sequenceAttr.getAttributeValue());
                } else if (onCacheHitElem.getFirstElement() != null) {
                    cache.setOnCacheHitSequence(new SequenceMediatorFactory().createAnonymousSequence(onCacheHitElem, properties));
                }
            } else {
                cache.setOnCacheHitRef(null);
                cache.setOnCacheHitSequence(null);
            }
            OMElement implElem = elem.getFirstChildWithName(IMPLEMENTATION_Q);
            if (implElem != null) {
                OMAttribute sizeAttr = implElem.getAttribute(ATT_SIZE);
                if (sizeAttr != null && sizeAttr.getAttributeValue() != null) {
                    cache.setInMemoryCacheSize(Integer.parseInt(sizeAttr.getAttributeValue().trim()));
                } else {
                    cache.setInMemoryCacheSize(-1);
                }
                OMAttribute typeAttribute = implElem.getAttribute(ATT_TYPE);
                if (typeAttribute != null && typeAttribute.getAttributeValue() != null) {
                    cache.setImplementationType(typeAttribute.getAttributeValue().trim());
                }
            }
        } else {
            handleException("The value for collector has to be either true or false");
        }
    } else {
        handleException("The collector attribute must be specified");
    }
    addAllCommentChildrenToList(elem, cache.getCommentsList());
    return cache;
}
Also used : OMElement(org.apache.axiom.om.OMElement) DigestGenerator(org.wso2.carbon.mediator.cache.digest.DigestGenerator) SequenceMediatorFactory(org.apache.synapse.config.xml.SequenceMediatorFactory) OMAttribute(org.apache.axiom.om.OMAttribute) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 OMAttribute (org.apache.axiom.om.OMAttribute)1 OMElement (org.apache.axiom.om.OMElement)1 SequenceMediatorFactory (org.apache.synapse.config.xml.SequenceMediatorFactory)1 DigestGenerator (org.wso2.carbon.mediator.cache.digest.DigestGenerator)1