use of org.craftercms.core.util.url.ContentBundleUrl in project core by craftercms.
the class ContentBundleMergeStrategy method getDescriptors.
@Override
public List<MergeableDescriptor> getDescriptors(Context context, CachingOptions cachingOptions, String mainDescriptorUrl, Document mainDescriptorDom, boolean mainDescriptorOptional) throws XmlMergeException {
List<MergeableDescriptor> descriptors = new ArrayList<>();
List<MergeableDescriptor> tmp;
ContentBundleUrl parsedUrl = urlParser.getContentBundleUrl(mainDescriptorUrl);
// prefix = folder1/
String prefix = parsedUrl.getPrefix();
// baseNameAndExtensionToken =
String baseNameAndExtensionToken = parsedUrl.getBaseNameAndExtensionToken();
// folder2_es
// suffix = /file.xml
String suffix = parsedUrl.getSuffix();
// If the prefix is the same length as the initial URI, ignore, otherwise process
if (prefix.length() < mainDescriptorUrl.length()) {
// Get the index of the delimiter that separates the base name from the extension token (the _ in
// folder2_es).
String baseName = baseNameAndExtensionToken;
int delimiterIdx = baseName.lastIndexOf(baseDelimiter);
boolean baseFound = false;
while (delimiterIdx > 0 && !baseFound) {
// baseName = folder2
baseName = baseName.substring(0, delimiterIdx);
// baseDescriptor = folder1/folder2/file.xml
String baseDescriptor = prefix + baseName + suffix;
Document baseDescriptorDom;
baseDescriptorDom = getDescriptorDom(context, cachingOptions, baseDescriptor);
if (baseDescriptorDom != null) {
baseFound = true;
} else if (logger.isDebugEnabled()) {
logger.debug("No base descriptor " + baseDescriptor + " was found");
}
if (baseFound) {
// This can recurse if the selected strategy is also an ContentBundleMergeStrategy and the base
// descriptor path has more families.
DescriptorMergeStrategy baseMergeStrategy = baseMergeStrategyResolver.getStrategy(baseDescriptor, baseDescriptorDom);
if (baseMergeStrategy == null) {
throw new XmlMergeException("No merge strategy for descriptor " + baseDescriptor);
}
tmp = baseMergeStrategy.getDescriptors(context, cachingOptions, baseDescriptor, baseDescriptorDom, true);
descriptors.addAll(tmp);
} else {
delimiterIdx = baseName.lastIndexOf(baseDelimiter);
}
}
// Keep only after the prefix (folder2_es/file.xml)
String noPrefix = mainDescriptorUrl.substring(prefix.length(), mainDescriptorUrl.length());
// Add all level descriptors after the prefix using the regular strategy (this is up to us/part of our spec)
tmp = regularMergeStrategy.getDescriptors(context, cachingOptions, noPrefix, mainDescriptorDom, mainDescriptorOptional);
// if it's not already in the results.
for (MergeableDescriptor descriptor : tmp) {
descriptor.setUrl(prefix + descriptor.getUrl());
if (!descriptors.contains(descriptor)) {
descriptors.add(descriptor);
}
}
} else {
descriptors.add(new MergeableDescriptor(mainDescriptorUrl, mainDescriptorOptional));
}
if (logger.isDebugEnabled()) {
logger.debug("Final merge list for " + mainDescriptorUrl + ": " + descriptors);
}
return descriptors;
}
use of org.craftercms.core.util.url.ContentBundleUrl in project core by craftercms.
the class ContentBundleMergeStrategyTest method setUpTestStrategy.
private void setUpTestStrategy() {
ContentBundleUrl contentBundleUrl = mock(ContentBundleUrl.class);
when(contentBundleUrl.getPrefix()).thenReturn(MAIN_DESCRIPTOR_URL_PREFIX);
when(contentBundleUrl.getBaseNameAndExtensionToken()).thenReturn(MAIN_DESCRIPTOR_URL_BASE_NAME_AND_EXT_TOKEN);
when(contentBundleUrl.getSuffix()).thenReturn(MAIN_DESCRIPTOR_URL_SUFFIX);
ContentBundleUrlParser contentBundleUrlParser = mock(ContentBundleUrlParser.class);
when(contentBundleUrlParser.getContentBundleUrl(MAIN_DESCRIPTOR_URL)).thenReturn(contentBundleUrl);
DescriptorMergeStrategy baseStrategy = mock(DescriptorMergeStrategy.class);
when(baseStrategy.getDescriptors(eq(context), eq(DEFAULT_CACHING_OPTIONS), eq(BASE_DESCRIPTOR_URL), any(Document.class), anyBoolean())).thenAnswer(new Answer<List<MergeableDescriptor>>() {
@Override
public List<MergeableDescriptor> answer(InvocationOnMock invocation) throws Throwable {
boolean isOptionalForMerging = (Boolean) invocation.getArguments()[4];
return Arrays.asList(new MergeableDescriptor(BASE_DESCRIPTOR_URL, isOptionalForMerging));
}
});
DescriptorMergeStrategyResolver baseResolver = mock(DescriptorMergeStrategyResolver.class);
when(baseResolver.getStrategy(eq(BASE_DESCRIPTOR_URL), Matchers.<Document>anyObject())).thenReturn(baseStrategy);
DescriptorMergeStrategy regularStrategy = mock(DescriptorMergeStrategy.class);
when(regularStrategy.getDescriptors(any(Context.class), any(CachingOptions.class), eq(NO_PREFIX_DESCRIPTOR_URL), any(Document.class), anyBoolean())).thenAnswer(new Answer<List<MergeableDescriptor>>() {
@Override
public List<MergeableDescriptor> answer(InvocationOnMock invocation) throws Throwable {
boolean isOptionalForMerging = (Boolean) invocation.getArguments()[4];
return Arrays.asList(new MergeableDescriptor(NO_PREFIX_DESCRIPTOR_URL, isOptionalForMerging));
}
});
strategy = new ContentBundleMergeStrategy();
strategy.setUrlParser(contentBundleUrlParser);
strategy.setBaseDelimiter(BASE_DELIMITER);
strategy.setBaseMergeStrategyResolver(baseResolver);
strategy.setRegularMergeStrategy(regularStrategy);
}
use of org.craftercms.core.util.url.ContentBundleUrl in project core by craftercms.
the class ContentBundleShortToLongUrlTransformer method getLongUrlLookingInBundle.
private String getLongUrlLookingInBundle(Context context, CachingOptions cachingOptions, String shortUrl) throws UrlTransformationException {
ContentBundleUrl parsedUrl = urlParser.getContentBundleUrl(shortUrl);
String prefix = parsedUrl.getPrefix();
String originalBase = parsedUrl.getBaseNameAndExtensionToken();
String suffix = parsedUrl.getSuffix();
if (originalBase == null) {
originalBase = "";
}
if (suffix == null) {
suffix = "";
}
String base = originalBase;
int delimiterIdx;
do {
String currentShortUrl = prefix + base + suffix;
String longUrl = getLongUrl(context, cachingOptions, currentShortUrl, false);
if (StringUtils.isNotEmpty(longUrl)) {
if (!originalBase.equals(base)) {
// Put original base back in long URL.
longUrl = longUrl.replace(base, originalBase);
}
return longUrl;
}
delimiterIdx = base.lastIndexOf(baseDelimiter);
if (delimiterIdx > 0) {
base = base.substring(0, delimiterIdx);
}
} while (delimiterIdx >= 0);
// come here if URL is not found in content store
throw new UrlTransformationException("Unable to map the short url '" + shortUrl + "' to a long url");
}
Aggregations