Search in sources :

Example 1 with ConstraintField

use of com.sun.appserv.web.cache.mapping.ConstraintField in project Payara by payara.

the class DefaultCacheHelper method isCacheable.

/**
 * isCacheable: is the response to given request cachebale?
 *  @param request incoming <code>HttpServletRequest</code> object
 *  @return <code>true</code> if the response could be cached.
 *  or return <code>false</code> if the results of this request
 *  must not be cached.
 *
 *  Applies pre-configured cacheability constraints in the cache-mapping;
 *  all constraints must pass for this to be cacheable.
 */
public boolean isCacheable(HttpServletRequest request) {
    boolean result = false;
    // cache mapping associated with the request
    Optional<CacheMapping> mapping = lookupCacheMapping(request);
    if (mapping.isPresent()) {
        // check if the method is in the allowed methods list
        if (mapping.get().findMethod(request.getMethod())) {
            result = true;
            ConstraintField[] fields = mapping.get().getConstraintFields();
            // apply all the constraints
            for (ConstraintField field : fields) {
                if (!field.applyConstraints(context, request)) {
                    result = false;
                    break;
                }
            }
        }
    }
    return result;
}
Also used : ConstraintField(com.sun.appserv.web.cache.mapping.ConstraintField) CacheMapping(com.sun.appserv.web.cache.mapping.CacheMapping)

Example 2 with ConstraintField

use of com.sun.appserv.web.cache.mapping.ConstraintField in project Payara by payara.

the class CacheModule method configureCacheMapping.

/**
 * configure ias-web cache-mapping
 * @param Catalina context
 * @param bean ias-web app cache-mapping config bean
 * @throws Exception
 */
private static void configureCacheMapping(org.glassfish.web.deployment.runtime.CacheMapping mapConfig, CacheMapping mapping, Logger logger) throws Exception {
    String name, scope, value, expr;
    /**
     * <cache-mapping  ((servlet-name|url-pattern)..)
     */
    mapping.setServletName(trim(mapConfig.getServletName()));
    mapping.setURLPattern(trim(mapConfig.getURLPattern()));
    // resolve the helper for this mapping
    String helperRef = mapConfig.getCacheHelperRef();
    if (helperRef == null) {
        helperRef = "default";
    }
    mapping.setHelperNameRef(helperRef);
    /**
     * <timeout>600</timeout>
     * <timeout name="cacheTimeout" scope="request.attribute" />
     */
    value = mapConfig.getTimeout();
    if (value != null) {
        try {
            mapping.setTimeout(Integer.parseInt(value.trim()));
        } catch (NumberFormatException e) {
            throw new Exception("invalid timeout", e);
        }
    } else {
        // XXX: get the timeout as a field?
        name = mapConfig.getAttributeValue(org.glassfish.web.deployment.runtime.CacheMapping.TIMEOUT, org.glassfish.web.deployment.runtime.CacheMapping.NAME);
        scope = mapConfig.getAttributeValue(org.glassfish.web.deployment.runtime.CacheMapping.TIMEOUT, org.glassfish.web.deployment.runtime.CacheMapping.SCOPE);
        if (name != null && scope != null)
            mapping.setTimeoutField(new Field(name, scope));
    }
    /**
     * <refresh-field name="refreshNow" scope="request.attribute" />
     */
    name = mapConfig.getAttributeValue(org.glassfish.web.deployment.runtime.CacheMapping.REFRESH_FIELD, org.glassfish.web.deployment.runtime.CacheMapping.NAME);
    scope = mapConfig.getAttributeValue(org.glassfish.web.deployment.runtime.CacheMapping.REFRESH_FIELD, org.glassfish.web.deployment.runtime.CacheMapping.SCOPE);
    if (name != null && scope != null) {
        Field refreshField = new Field(name, scope);
        mapping.setRefreshField(refreshField);
    }
    /**
     * <http-method> GET </http-method>
     *  <http-method> POST </http-method>
     */
    if (mapConfig.sizeHttpMethod() > 0) {
        mapping.setMethods(mapConfig.getHttpMethod());
    }
    /**
     * <key-field name="foo" scope="request.parameter"/>
     */
    for (int i = 0; i < mapConfig.sizeKeyField(); i++) {
        name = mapConfig.getAttributeValue(org.glassfish.web.deployment.runtime.CacheMapping.KEY_FIELD, i, org.glassfish.web.deployment.runtime.CacheMapping.NAME);
        scope = mapConfig.getAttributeValue(org.glassfish.web.deployment.runtime.CacheMapping.KEY_FIELD, i, org.glassfish.web.deployment.runtime.CacheMapping.SCOPE);
        if (name != null && scope != null) {
            mapping.addKeyField(new Field(name, scope));
            if (logger.isLoggable(Level.FINE)) {
                logger.log(Level.FINE, LogFacade.KEY_FIELD_ADDED, new Object[] { name, scope });
            }
        }
    }
    /**
     * <constraint-field name="foo" scope="request.parameter">
     *   <value match-expr="equals"> 200 </value>
     */
    for (int i = 0; i < mapConfig.sizeConstraintField(); i++) {
        org.glassfish.web.deployment.runtime.ConstraintField fieldConfig = mapConfig.getConstraintField(i);
        name = fieldConfig.getAttributeValue(org.glassfish.web.deployment.runtime.ConstraintField.NAME);
        scope = fieldConfig.getAttributeValue(org.glassfish.web.deployment.runtime.ConstraintField.SCOPE);
        ConstraintField constraintField = new ConstraintField(name, scope);
        value = fieldConfig.getAttributeValue(org.glassfish.web.deployment.runtime.ConstraintField.CACHE_ON_MATCH);
        if (value != null)
            constraintField.setCacheOnMatch(ConfigBeansUtilities.toBoolean(value));
        value = fieldConfig.getAttributeValue(org.glassfish.web.deployment.runtime.ConstraintField.CACHE_ON_MATCH_FAILURE);
        if (value != null)
            constraintField.setCacheOnMatchFailure(ConfigBeansUtilities.toBoolean(value));
        // now set the value's and the match expressions
        for (int j = 0; j < fieldConfig.sizeValue(); j++) {
            value = fieldConfig.getValue(j).trim();
            expr = fieldConfig.getAttributeValue(org.glassfish.web.deployment.runtime.ConstraintField.VALUE, j, org.glassfish.web.deployment.runtime.ConstraintField.MATCH_EXPR);
            ValueConstraint constraint = new ValueConstraint(value, expr);
            value = fieldConfig.getAttributeValue(org.glassfish.web.deployment.runtime.ConstraintField.VALUE, j, org.glassfish.web.deployment.runtime.ConstraintField.CACHE_ON_MATCH);
            if (value != null) {
                constraint.setCacheOnMatch(ConfigBeansUtilities.toBoolean(value));
            }
            value = fieldConfig.getAttributeValue(org.glassfish.web.deployment.runtime.ConstraintField.VALUE, j, org.glassfish.web.deployment.runtime.ConstraintField.CACHE_ON_MATCH_FAILURE);
            if (value != null) {
                constraint.setCacheOnMatchFailure(ConfigBeansUtilities.toBoolean(value));
            }
            constraintField.addConstraint(constraint);
            if (logger.isLoggable(Level.FINE)) {
                logger.log(Level.FINE, LogFacade.CONSTRAINT_ADDED, constraint.toString());
            }
        }
        mapping.addConstraintField(constraintField);
        if (logger.isLoggable(Level.FINE)) {
            logger.log(Level.FINE, LogFacade.CONSTRAINT_FIELD_ADDED, new Object[] { name, scope, constraintField.getCacheOnMatch(), constraintField.getCacheOnMatchFailure() });
        }
    }
}
Also used : Field(com.sun.appserv.web.cache.mapping.Field) ConstraintField(com.sun.appserv.web.cache.mapping.ConstraintField) ConstraintField(com.sun.appserv.web.cache.mapping.ConstraintField) ValueConstraint(com.sun.appserv.web.cache.mapping.ValueConstraint) org.glassfish.web.deployment.runtime(org.glassfish.web.deployment.runtime) ValueConstraint(com.sun.appserv.web.cache.mapping.ValueConstraint)

Aggregations

ConstraintField (com.sun.appserv.web.cache.mapping.ConstraintField)2 CacheMapping (com.sun.appserv.web.cache.mapping.CacheMapping)1 Field (com.sun.appserv.web.cache.mapping.Field)1 ValueConstraint (com.sun.appserv.web.cache.mapping.ValueConstraint)1 org.glassfish.web.deployment.runtime (org.glassfish.web.deployment.runtime)1