}
Config(GadgetSpec spec, Config defaultConfig) {
this.onlyAllowExcludes = defaultConfig.onlyAllowExcludes;
Feature f = spec.getModulePrefs().getFeatures().get("content-rewrite");
// Include overrides.
// Note: Shindig originally supported the plural versions with regular
// expressions. But the OpenSocial specification v0.9 allows for singular
// spellings, with multiple values. Plus they are case insensitive substrings.
// For backward compatibility, if the singular versions are present they
// will override the plural versions. 10/6/09
String includeRegex = defaultConfig.includes.param;
Collection<String> includeUrls = Lists.newArrayList();
if (f != null && !onlyAllowExcludes) {
if (f.getParams().containsKey(INCLUDE_URLS)) {
includeRegex = f.getParam(INCLUDE_URLS);
}
Collection<String> paramUrls = f.getParamCollection(INCLUDE_URL);
for (String url : paramUrls) {
includeUrls.add(url.trim().toLowerCase());
}
}
this.includes = getMatchBundle(includeRegex, includeUrls);
// Exclude overrides. Only use the exclude regex specified by the
// gadget spec if !onlyAllowExcludes.
String excludeRegex = defaultConfig.excludes.param;
Collection<String> excludeUrls = Lists.newArrayList();
if (f != null) {
if (f.getParams().containsKey(EXCLUDE_URLS)) {
excludeRegex = f.getParam(EXCLUDE_URLS);
}
Collection<String> eParamUrls = f.getParamCollection(EXCLUDE_URL);
for (String url : eParamUrls) {
excludeUrls.add(url.trim().toLowerCase());
}
}
this.excludes = getMatchBundle(excludeRegex, excludeUrls);
// Spec-specified include tags.
Set<String> tagsVal = null;
if (f != null && f.getParams().containsKey(INCLUDE_TAGS)) {
tagsVal = Sets.newTreeSet();
for (String tag : StringUtils.split(f.getParam(INCLUDE_TAGS), ',')) {
if (!StringUtils.isEmpty(tag)) {
tagsVal.add(tag.trim().toLowerCase());
}
}
if (onlyAllowExcludes) {
// Only excludes are allowed. Keep only subset of
// specified tags that are in the defaults.
tagsVal.retainAll(defaultConfig.includeTags);
}
} else {
tagsVal = ImmutableSortedSet.copyOf(defaultConfig.includeTags);
}
this.includeTags = tagsVal;
// Let spec/feature override if present and smaller than default.
int expiresVal = defaultConfig.expires;
if (f != null && f.getParams().containsKey(EXPIRES)) {
try {
int overrideVal = Integer.parseInt(f.getParam(EXPIRES));
expiresVal = Math.min(overrideVal, expiresVal);
} catch (NumberFormatException e) {
// Falls through to default.
if ("HTTP".equalsIgnoreCase(f.getParam(EXPIRES).trim())) {
expiresVal = EXPIRES_DEFAULT;
}
}
}
this.expires = expiresVal;