inUrl with the template context removed
*/
- public String stripTemplateFromURL(String inUrl) {
+ @Override
+ public String stripTemplateFromURL(final String inUrl) {
+
String sTemplateContext = getTemplateFromURL(inUrl);
if (sTemplateContext != null) {
//there is a template context, so strip it
int iTemplateLength = TEMPLATE_CONTEXT_PREFIX.length()
- + sTemplateContext.length() + 1;
+ + sTemplateContext.length() + 1;
String sStripped = inUrl.substring(iTemplateLength, inUrl.length());
return sStripped;
} else {
diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/dispatcher/MultilingualItemResolver.java.todo b/ccm-cms/src/main/java/com/arsdigita/cms/dispatcher/MultilingualItemResolver.java.todo
index 54446684d..4458594b6 100755
--- a/ccm-cms/src/main/java/com/arsdigita/cms/dispatcher/MultilingualItemResolver.java.todo
+++ b/ccm-cms/src/main/java/com/arsdigita/cms/dispatcher/MultilingualItemResolver.java.todo
@@ -20,45 +20,64 @@ package com.arsdigita.cms.dispatcher;
import com.arsdigita.bebop.PageState;
import com.arsdigita.cms.CMS;
-import com.arsdigita.cms.ContentCenter;
+import com.arsdigita.cms.ui.ContentItemPage;
+import com.arsdigita.kernel.KernelConfig;
import com.arsdigita.util.Assert;
import org.apache.logging.log4j.Logger;
-import org.libreccm.categorization.Category;
import org.librecms.contentsection.ContentItem;
import org.librecms.contentsection.ContentSection;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
-import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Objects;
import java.util.StringTokenizer;
+
import org.apache.logging.log4j.LogManager;
import org.libreccm.cdi.utils.CdiUtil;
+import org.libreccm.core.CcmObject;
+import org.libreccm.l10n.GlobalizationHelper;
import org.librecms.CmsConstants;
+import org.librecms.contentsection.ContentItemManager;
import org.librecms.contentsection.ContentItemRepository;
import org.librecms.contentsection.ContentItemVersion;
import org.librecms.contentsection.Folder;
import org.librecms.contentsection.FolderManager;
+import org.librecms.util.LanguageUtil;
+
+import java.math.BigDecimal;
+import java.util.List;
+import java.util.Optional;
+
+import static javax.naming.ldap.SortControl.*;
/**
* Resolves items to URLs and URLs to items for multiple language variants.
*
- * Created Mon Jan 20 14:30:03 2003.
+ * *
+ * AS of version 7.0.0 this class not longer part of the public API. It is left
+ * here to keep the changes to the UI classes as minimal as possible. For new
+ * code other methods, for example from the {@link ContentItemManager} or the
+ * {@link ContentItemRepository} should be used. Because this class is no longer
+ * part of the public API the will might be removed or changed in future
+ * releases without prior warning.
+ *
+ *
+ *
*
* @author Michael Hanisch
- * @version $Id: MultilingualItemResolver.java 2090 2010-04-17 08:04:14Z pboy $
+ * @author Jens Pelzetter
*/
public class MultilingualItemResolver
- extends AbstractItemResolver implements ItemResolver {
+ extends AbstractItemResolver implements ItemResolver {
private static final Logger LOGGER = LogManager.getLogger(
- MultilingualItemResolver.class);
+ MultilingualItemResolver.class);
private static MasterPage s_masterP = null;
private static final String ADMIN_PREFIX = "admin";
@@ -83,8 +102,10 @@ public class MultilingualItemResolver
* @param section The current content section
* @param itemUrl The section-relative URL
* @param context The use context, e.g. ContentItem.LIVE,
- * CMSDispatcher.PREVIEW or ContentItem.DRAFT. See {@link
+ * CMSDispatcher.PREVIEW or
+ * ContentItem.DRAFT. See {@link
* #getCurrentContext}.
+ *
* @return The content item, or null if no such item exists
*/
@Override
@@ -92,7 +113,7 @@ public class MultilingualItemResolver
final String itemUrl,
final String context) {
LOGGER.debug("Resolving the item in content section \"{}\" at URL "
- + "\"{}\" for context \"{}\"...",
+ + "\"{}\" for context \"{}\"...",
section.getLabel(),
itemUrl,
context);
@@ -119,16 +140,16 @@ public class MultilingualItemResolver
LOGGER.debug("The root folder has a live version; recursing");
final FolderManager folderManager = cdiUtil.findBean(
- FolderManager.class);
+ FolderManager.class);
final String prefix = String.join(
- "",
- section.getPrimaryUrl(),
- folderManager.getFolderPath(rootFolder));
+ "",
+ section.getPrimaryUrl(),
+ folderManager.getFolderPath(rootFolder));
if (url.startsWith(prefix)) {
LOGGER.
- debug("The starts with prefix \"{}\"; removing it...",
- prefix);
+ debug("The starts with prefix \"{}\"; removing it...",
+ prefix);
url = url.substring(prefix.length());
}
@@ -171,8 +192,8 @@ public class MultilingualItemResolver
if (url.startsWith(prefix)) {
LOGGER.debug(
- "The URL starts with prefix \"{}\"; removing it",
- prefix);
+ "The URL starts with prefix \"{}\"; removing it",
+ prefix);
url = url.substring(prefix.length());
}
@@ -186,7 +207,7 @@ public class MultilingualItemResolver
return item;
} else {
throw new IllegalArgumentException(String.format(
- "Invalid item resolver context \"%s\".", context));
+ "Invalid item resolver context \"%s\".", context));
}
}
@@ -199,11 +220,13 @@ public class MultilingualItemResolver
* Fetches the current context based on the page state.
*
* @param state the current page state
+ *
* @return the context of the current URL, such as
- * ContentItem.LIVE or ContentItem.DRAFT
+ * ContentItem.LIVE or ContentItem.DRAFT
+ *
* @see ContentItem#LIVE
* @see ContentItem#DRAFT
- *
+ *
* ToDo: Refactor to use the {@link ContentItemVersion} directly.
*/
@Override
@@ -232,8 +255,8 @@ public class MultilingualItemResolver
url = stripTemplateFromURL(url);
// Determine if we are under the admin UI.
- if (url.startsWith(ADMIN_PREFIX)
- || url.startsWith(CmsConstants.CONTENT_CENTER_URL)) {
+ if (url.startsWith(ADMIN_PREFIX)
+ || url.startsWith(CmsConstants.CONTENT_CENTER_URL)) {
return ContentItemVersion.DRAFT.toString();
} else {
return ContentItemVersion.LIVE.toString();
@@ -243,17 +266,19 @@ public class MultilingualItemResolver
/**
* Generates a URL for a content item.
*
- * @param itemId The item ID
- * @param name The name of the content page
- * @param state The page state
+ * @param itemId The item ID
+ * @param name The name of the content page
+ * @param state The page state
* @param section the content section to which the item belongs
* @param context the context of the URL, such as "live" or "admin"
+ *
* @return The URL of the item
+ *
* @see #getCurrentContext
*/
@Override
public String generateItemURL(final PageState state,
- final BigDecimal itemId,
+ final Long itemId,
final String name,
final ContentSection section,
final String context) {
@@ -263,27 +288,29 @@ public class MultilingualItemResolver
/**
* Generates a URL for a content item.
*
- * @param itemId The item ID
- * @param name The name of the content page
- * @param state The page state
- * @param section the content section to which the item belongs
- * @param context the context of the URL, such as "live" or "admin"
+ * @param itemId The item ID
+ * @param name The name of the content page
+ * @param state The page state
+ * @param section the content section to which the item belongs
+ * @param context the context of the URL, such as "live" or "admin"
* @param templateContext the context for the URL, such as "public"
+ *
* @return The URL of the item
+ *
* @see #getCurrentContext
*/
@Override
public String generateItemURL(final PageState state,
- final BigDecimal itemId,
+ final Long itemId,
final String name,
final ContentSection section,
final String context,
final String templateContext) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Generating an item URL for item id " + itemId
- + ", section " + section + ", and context '"
- + context
- + "' with name '" + name + "'");
+ + ", section " + section + ", and context '"
+ + context
+ + "' with name '" + name + "'");
}
Assert.exists(itemId, "BigDecimal itemId");
@@ -291,40 +318,35 @@ public class MultilingualItemResolver
Assert.exists(section, "ContentSection section");
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
- final ContentItemRepository itemRepo = cdiUtil.findBean(ContentItemRepository.class);
-
+ final ContentItemRepository itemRepo = cdiUtil.findBean(
+ ContentItemRepository.class);
+
if (ContentItemVersion.DRAFT.toString().equals(context)) {
// No template context here.
return generateDraftURL(section, itemId);
} else if (CMSDispatcher.PREVIEW.equals(context)) {
- ContentItem item = new ContentItem(itemId);
-
+ final ContentItem item = itemRepo.findById(itemId);
return generatePreviewURL(section, item, templateContext);
- } else if (ContentItem.LIVE.equals(context)) {
- ContentItem item = new ContentItem(itemId);
-
- if (Assert.isEnabled()) {
- Assert.exists(item, "item");
- Assert.isTrue(ContentItem.LIVE.equals(item.getVersion()),
- "Generating " + ContentItem.LIVE + " "
- + "URL; this item must be the live version");
- }
+ } else if (ContentItemVersion.LIVE.toString().equals(context)) {
+ final ContentItem item = itemRepo.findById(itemId);
return generateLiveURL(section, item, templateContext);
} else {
throw new IllegalArgumentException("Unknown context '" + context
- + "'");
+ + "'");
}
}
/**
* Generates a URL for a content item.
*
- * @param item The item
- * @param state The page state
+ * @param item The item
+ * @param state The page state
* @param section the content section to which the item belongs
* @param context the context of the URL, such as "live" or "admin"
+ *
* @return The URL of the item
+ *
* @see #getCurrentContext
*/
@Override
@@ -338,68 +360,61 @@ public class MultilingualItemResolver
/**
* Generates a URL for a content item.
*
- * @param item The item
- * @param state The page state
- * @param section the content section to which the item belongs
- * @param context the context of the URL, such as "live" or "admin"
+ * @param item The item
+ * @param state The page state
+ * @param section the content section to which the item belongs
+ * @param context the context of the URL, such as "live" or "admin"
* @param templateContext the context for the URL, such as "public"
+ *
* @return The URL of the item
+ *
* @see #getCurrentContext
*/
@Override
public String generateItemURL(final PageState state,
final ContentItem item,
- ContentSection section,
+ final ContentSection section,
final String context,
final String templateContext) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Generating an item URL for item " + item
- + ", section "
- + section + ", and context " + context);
+ + ", section "
+ + section + ", and context " + context);
}
- Assert.exists(item, "ContentItem item");
- Assert.exists(context, "String context");
-
+ final ContentSection contentSection;
if (section == null) {
- section = item.getContentSection();
+ contentSection = item.getContentType().getContentSection();
+ } else {
+ contentSection = section;
}
- if (ContentItem.DRAFT.equals(context)) {
- if (Assert.isEnabled()) {
- Assert.isTrue(ContentItem.DRAFT.equals(item.getVersion()),
- "Generating " + ContentItem.DRAFT
- + " url: item must be draft version");
- }
-
- return generateDraftURL(section, item.getID());
+ if (ContentItemVersion.DRAFT.toString().equals(context)) {
+ return generateDraftURL(section, item.getObjectId());
} else if (CMSDispatcher.PREVIEW.equals(context)) {
return generatePreviewURL(section, item, templateContext);
- } else if (ContentItem.LIVE.equals(context)) {
- if (Assert.isEnabled()) {
- Assert.isTrue(ContentItem.LIVE.equals(item.getVersion()),
- "Generating " + ContentItem.LIVE
- + " url: item must be live version");
- }
-
- return generateLiveURL(section, item, templateContext);
+ } else if (ContentItemVersion.LIVE.toString().equals(context)) {
+ return generateLiveURL(contentSection, item, templateContext);
} else {
- throw new RuntimeException("Unknown context " + context);
+ throw new IllegalArgumentException(String.format(
+ "Unknown context \"%s\".", context));
}
}
/**
* Returns a master page based on page state (and content section).
*
- * @param item The content item
+ * @param item The content item
* @param request The HTTP request
+ *
* @return The master page
+ *
* @throws javax.servlet.ServletException
*/
@Override
public CMSPage getMasterPage(final ContentItem item,
final HttpServletRequest request)
- throws ServletException {
+ throws ServletException {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Getting the master page for item " + item);
}
@@ -421,25 +436,27 @@ public class MultilingualItemResolver
* Returns content item's draft version URL
*
* @param section The content section to which the item belongs
- * @param itemId The content item's ID
+ * @param itemId The content item's ID
+ *
* @return generated URL string
*/
protected String generateDraftURL(final ContentSection section,
- final BigDecimal itemId) {
+ final Long itemId) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Generating draft URL for item ID " + itemId
- + " and section " + section);
+ + " and section " + section);
}
if (Assert.isEnabled()) {
Assert.isTrue(section != null && itemId != null,
"get draft url: neither secion nor item "
- + "can be null");
+ + "can be null");
}
- final String url = ContentItemPage.getItemURL(section.getPath() + "/",
- itemId,
- ContentItemPage.AUTHORING_TAB);
+ final String url = ContentItemPage.getItemURL(
+ String.format("%s/", section.getPrimaryUrl()),
+ itemId,
+ ContentItemPage.AUTHORING_TAB);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Generated draft URL " + url);
@@ -455,23 +472,23 @@ public class MultilingualItemResolver
* specific language instance of the item referenced here, i.e. this URL
* will be resolved to a language-specific URL internally.
*
- * @param section the ContentSection that contains this item
- * @param item ContentItem for which a URL should be
- * constructed.
+ * @param section the ContentSection that contains this
+ * item
+ * @param item ContentItem for which a URL should be
+ * constructed.
* @param templateContext template context; will be ignored if
- * null
+ * null
*
* @return a language-independent URL to the item in
- * the given section, which will be presented within the given
- * templateContext
+ * the given section, which will be presented within
+ * the given templateContext
*/
protected String generateLiveURL(final ContentSection section,
final ContentItem item,
final String templateContext) {
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("Generating live URL for item " + item + " in "
- + "section " + section);
- }
+ LOGGER.debug("Generating live URL for item {} in section {}",
+ Objects.toString(item),
+ Objects.toString(section));
/*
* URL = URL of section + templateContext + path to the ContentBundle
@@ -479,7 +496,9 @@ public class MultilingualItemResolver
*/
final StringBuffer url = new StringBuffer(400);
//url.append(section.getURL());
- url.append(section.getPath()).append("/");
+ url
+ .append(section.getPrimaryUrl())
+ .append("/");
/*
* add template context, if one is given
@@ -487,56 +506,17 @@ public class MultilingualItemResolver
// This is breaking URL's...not sure why it's here. XXX
// this should work with the appropriate logic. trying again.
if (!(templateContext == null || templateContext.length() == 0)) {
- url.append(TEMPLATE_CONTEXT_PREFIX).append(templateContext).append(
- "/");
+ url
+ .append(TEMPLATE_CONTEXT_PREFIX)
+ .append(templateContext)
+ .append("/");
}
- // Try to retrieve the bundle.
- final ContentItem bundle = (ContentItem) item.getParent();
+ final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
+ final ContentItemManager itemManager = cdiUtil.findBean(
+ ContentItemManager.class);
+ url.append(itemManager.getItemPath(item));
- /*
- * It would be nice if we had a ContentPage here, which
- * supports the getContentBundle() method, but unfortunately
- * the API sucks and there is no real distinction between mere
- * ContentItems and top-level items, so we have to use this
- * hack. TODO: add sanity check that bundle is actually a
- * ContentItem.
- */
- if (bundle != null && bundle instanceof ContentBundle) {
- LOGGER.debug("Found a bundle; building its file name");
-
- final String fname = bundle.getPath();
-
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("Appending the bundle's file name '" + fname + "'");
- }
-
- url.append(fname);
-
- } else {
- LOGGER.debug("No bundle found; using the item's path directly");
-
- url.append(item.getPath());
- }
- /*
- * This will append the language to the url, which will in turn render
- * the language negotiation inoperative
- final String language = item.getLanguage();
-
- if (language == null) {
- s_log.debug("The item has no language; omitting the " +
- "language encoding");
- } else {
- s_log.debug("Encoding the language of the item passed in, '" +
- language + "'");
-
- url.append("." + language);
- }
-
- if (s_log.isDebugEnabled()) {
- s_log.debug("Generated live URL " + url.toString());
- }
- */
return url.toString();
}
@@ -547,71 +527,44 @@ public class MultilingualItemResolver
* no language negotiation is involved when a request is made to a
* URL that has been generated by this method.
*
- * @param section The ContentSection which contains the
- * item
- * @param item The ContentItem for which a URL should be
- * generated.
+ * @param section The ContentSection which contains the
+ * item
+ * @param item The ContentItem for which a URL
+ * should be generated.
* @param templateContext the context that determines which template should
- * render the item when it is previewed; ignored if the argument given here
- * is null
+ * render the item when it is previewed; ignored if
+ * the argument given here is null
+ *
* @return a URL which can be used to preview the given item
*/
- protected String generatePreviewURL(ContentSection section,
- ContentItem item,
- String templateContext) {
+ protected String generatePreviewURL(final ContentSection section,
+ final ContentItem item,
+ final String templateContext) {
Assert.exists(section, "ContentSection section");
Assert.exists(item, "ContentItem item");
final StringBuffer url = new StringBuffer(100);
- url.append(section.getPath());
- url.append("/");
- url.append(CMSDispatcher.PREVIEW);
- url.append("/");
+ url
+ .append(section.getPrimaryUrl())
+ .append("/")
+ .append(CMSDispatcher.PREVIEW)
+ .append("/");
/*
* add template context, if one is given
*/
// This is breaking URL's...not sure why it's here. XXX
// this should work with the appropriate logic. trying again.
if (!(templateContext == null || templateContext.length() == 0)) {
- url.append(TEMPLATE_CONTEXT_PREFIX).append(templateContext).append(
- "/");
+ url
+ .append(TEMPLATE_CONTEXT_PREFIX)
+ .append(templateContext)
+ .append("/");
}
- // Try to retrieve the bundle.
- ContentItem bundle = (ContentItem) item.getParent();
-
- /* It would be nice if we had a ContentPage here, which
- * supports the getContentBundle() method, but unfortunately
- * the API sucks and there is no real distinction between mere
- * ContentItems and top-level items, so we have to use this
- * hack. TODO: add sanity check that bundle is actually a
- * ContentItem.
- */
- if (bundle != null && bundle instanceof ContentBundle) {
- LOGGER.debug("Found a bundle; using its path");
-
- url.append(bundle.getPath());
- } else {
- LOGGER.debug("No bundle found; using the item's path directly");
-
- url.append(item.getPath());
- }
-
- final String language = item.getLanguage();
-
- if (language == null) {
- LOGGER.debug("The item has no language; omitting the "
- + "language encoding");
- } else {
- LOGGER.debug("Encoding the language of the item passed in, '"
- + language + "'");
-
- url.append(".").append(language);
- }
-
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("Generated preview URL " + url.toString());
- }
+ final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
+ final ContentItemManager itemManager = cdiUtil.findBean(
+ ContentItemManager.class);
+ url.append(itemManager.getItemPath(item));
return url.toString();
}
@@ -621,9 +574,11 @@ public class MultilingualItemResolver
* according to this ID.
*
* @param url URL that indicates which item should be retrieved; must
- * contain the ITEM_ID parameter
+ * contain the ITEM_ID parameter
+ *
* @return the ContentItem the given url points
- * to, or null if no ID has been found in the url
+ * to, or null if no ID has been found in the
+ * url
*/
protected ContentItem getItemFromDraftURL(final String url) {
if (LOGGER.isDebugEnabled()) {
@@ -661,41 +616,45 @@ public class MultilingualItemResolver
LOGGER.debug("Looking up item using item ID " + item_id);
}
- OID oid = new OID(ContentItem.BASE_DATA_OBJECT_TYPE, new BigDecimal(
- item_id));
- final ContentItem item = (ContentItem) DomainObjectFactory.newInstance(
- oid);
+ final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
+ final ContentItemRepository itemRepo = cdiUtil.findBean(
+ ContentItemRepository.class);
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("Returning item " + item);
+ final OptionalThis is the default implementation of - * {@link com.arsdigita.cms.dispatcher.ItemResolver}.
+ * This is the default implementation of + * {@link com.arsdigita.cms.dispatcher.ItemResolver}. * - *The getItem method of the default implementation of - * ItemResolver, - * {@link com.arsdigita.cms.dispatcher.SimpleItemResolver} - * runs a simple query using the passed in information to retrieve the - * content item with a name that matches the URL stub, in our example - * it looks for a content item with name cheese. If no such item - * exists, or if there is such an item, but without a live version, even - * though one has been requested, getItem returns null.
+ * The {@link #getItem(java.lang.String, org.librecms.contentsection.Folder) } + * method of the default implementation of {@link ItemResolver}, + * {@link com.arsdigita.cms.dispatcher.SimpleItemResolver} runs a simple query + * using the passed in information to retrieve the content item with a name that + * matches the URL stub, in our example it looks for a content item with name + * {@code cheese}. If no such item exists, or if there is such an item, but + * without a live version, even though one has been requested, {@code getItem} + * returns {@code null}. * - *After the CMS Dispatcher received the content item from the - * ItemResolver, it also asks it for the - * {@link com.arsdigita.cms.dispatcher.MasterPage} for that item in the - * current request. With the content item and the master page in hand, - * the dispatcher calls service on the page.
+ * After the CMS Dispatcher received the content item from the + * {@link ItemResolver}, it also asks it for the + * {@link com.arsdigita.cms.dispatcher.MasterPage} for that item in the current + * request. With the content item and the master page in hand, the dispatcher + * calls {@code service} on the page. + * + * + * AS of version 7.0.0 this class not longer part of the public API. It is left + * here to keep the changes to the UI classes as minimal as possible. For new + * code other methods, for example from the {@link ContentItemManager} or + * the {@link ContentItemRepository} should be used. Because this class is no + * longer part of the public API the will might be removed or changed in future + * releases without prior warning. + * * * @author Michael Pih (pihman@arsdigita.com) - * @version $Revision: #15 $ $DateTime: 2004/08/17 23:15:09 $ - * @version $Id: SimpleItemResolver.java 2090 2010-04-17 08:04:14Z pboy $ + * @author Jens Pelzetter */ -public class SimpleItemResolver extends AbstractItemResolver implements ItemResolver { +public class SimpleItemResolver + extends AbstractItemResolver + implements ItemResolver { - private static final Logger s_log = - Logger.getLogger(SimpleItemResolver.class.getName()); + private static final Logger LOGGER = LogManager.getLogger( + SimpleItemResolver.class.getName()); private static final String ADMIN_PREFIX = "admin"; - private static final String WORKSPACE_PREFIX = ContentCenter.getURL(); + private static final String WORKSPACE_PREFIX + = CmsConstants.CONTENT_CENTER_URL; - private static MasterPage s_masterP = null; + private static MasterPage masterPage = null; - public SimpleItemResolver() {} + public SimpleItemResolver() { + } /** * Return a content item based on page state (and content section). @@ -75,17 +97,20 @@ public class SimpleItemResolver extends AbstractItemResolver implements ItemReso * @param section The current content section * @param url The section-relative URL * @param context The LIVE/DRAFT context (*not* the template use context) - * @return The content item mapped to the content section and URL, or - * null if no such item exists + * + * @return The content item mapped to the content section and URL, or null + * if no such item exists */ - public ContentItem getItem(ContentSection section, String url, - String context) { + @Override + public ContentItem getItem(final ContentSection section, + final String url, + final String context) { - if (s_log.isDebugEnabled()) { - s_log.debug("trying to get " + context + " item for url " + url); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("trying to get " + context + " item for url " + url); } - String itemUrl = stripTemplateFromURL(url); + final String itemUrl = stripTemplateFromURL(url); // getItem fails if called from a JSP template, because the request URL // gets replaced with the filesystem path to the JSP (when @@ -95,58 +120,36 @@ public class SimpleItemResolver extends AbstractItemResolver implements ItemReso ContentItem reqItem = (ContentItem) DispatcherHelper.getRequest(). getAttribute("com.arsdigita.cms.dispatcher.item"); if (reqItem != null) { - s_log.info("found item in the request, returning it"); + LOGGER.info("found item in the request, returning it"); return reqItem; } - //ContentItem item = getCachedItem(section, itemUrl, context); - //if (item != null) { - // return item; - //} - - Folder rootFolder = section.getRootFolder(); - if ( rootFolder == null ) { - s_log.info("no root folder found, returning null"); + final Folder rootFolder = section.getRootDocumentsFolder(); + if (rootFolder == null) { + LOGGER.info("no root folder found, returning null"); return null; } - if ( context.equals(ContentItem.LIVE) ) { - rootFolder = (Folder) rootFolder.getLiveVersion(); - if ( rootFolder == null ) { - s_log.info("no LIVE version of root folder found, returning null"); - return null; - } - } else if ( context.equals(ContentItem.DRAFT) ) { - // Do nothing ? - } else { - throw new RuntimeException( - "getItem: Invalid item resolver context " + context); - } - - ContentItem item = getItem(itemUrl, rootFolder); - //if (item != null) { - // cacheItem(section, itemUrl, context, item); - //} - - return item; - } + return getItem(itemUrl, rootFolder); + } /** * @param state the current page state + * * @return the context of the current URL, such as "live" or "admin" */ - public String getCurrentContext(PageState state) { + @Override + public String getCurrentContext(final PageState state) { String url = state.getRequest().getRequestURI(); - ContentSection section = - CMS.getContext().getContentSection(); + final ContentSection section = CMS.getContext().getContentSection(); // If this page is associated with a content section, transform // the URL so that it is relative to the content section site node. - if ( section != null ) { - String sectionURL = section.getURL(); - if ( url.startsWith(sectionURL) ) { + if (section != null) { + final String sectionURL = section.getPrimaryUrl(); + if (url.startsWith(sectionURL)) { url = url.substring(sectionURL.length()); } } @@ -156,176 +159,256 @@ public class SimpleItemResolver extends AbstractItemResolver implements ItemReso url = stripTemplateFromURL(url); // Determine if we are under the admin UI. - if ( url.startsWith(ADMIN_PREFIX) || - url.startsWith(WORKSPACE_PREFIX) ) { - return ContentItem.DRAFT; + if (url.startsWith(ADMIN_PREFIX) || url.startsWith(WORKSPACE_PREFIX)) { + return ContentItemVersion.DRAFT.toString(); } else { - return ContentItem.LIVE; + return ContentItemVersion.LIVE.toString(); } } /** - * Return the content item at the specified path, or null - * if no such item exists. The path is interpreted as a series - * of folders; for example, "/foo/bar/baz" will look for - * an item named "baz" in a folder named "bar" in a folder named "foo" - * under the specified root folder. + * Return the content item at the specified path, or null if no such item + * exists. The path is interpreted as a series of folders; for example, + * "/foo/bar/baz" will look for an item named "baz" in a folder named "bar" + * in a folder named "foo" under the specified root folder. * - * @param url the URL to the item + * @param url the URL to the item * @param rootFolder The root folder where the item search will start + * * @return the item on success, null if no such item exists - * @pre rootFolder != null - * @pre url != null */ - public ContentItem getItem(String url, Folder rootFolder) { + public ContentItem getItem(final String url, final Folder rootFolder) { - StringTokenizer tokenizer = new StringTokenizer(url, "/"); - String name = null; - Folder oldFolder = null; + final String[] tokens = url.split("/"); - while(rootFolder != null && tokenizer.hasMoreTokens()) { - name = tokenizer.nextToken(); - oldFolder = rootFolder; - rootFolder = (Folder)rootFolder.getItem(name, true); + Folder currentFolder = rootFolder; + int i; + for (i = 0; i < tokens.length; i++) { + final String token = tokens[i]; + final Optional:.
+ *
+ * @return An {@link Optional} containing the content item identified by the
+ * provided path or an empty {@code Optional} if there is no such
+ * item.
+ */
+ public Optional." in the path with a slash.