From: <ap...@vh...> - 2006-05-02 15:19:32
|
Author: apevec Date: 2006-05-02 17:16:38 +0200 (Tue, 02 May 2006) New Revision: 1131 Modified: trunk/ccm-cms/src/com/arsdigita/cms/dispatcher/ItemURLFinder.java Log: fixes for content item redirection: 404 for public users when try to access switch to draft version when context=draft Modified: trunk/ccm-cms/src/com/arsdigita/cms/dispatcher/ItemURLFinder.java =================================================================== --- trunk/ccm-cms/src/com/arsdigita/cms/dispatcher/ItemURLFinder.java 2006-05-02 14:18:22 UTC (rev 1130) +++ trunk/ccm-cms/src/com/arsdigita/cms/dispatcher/ItemURLFinder.java 2006-05-02 15:16:38 UTC (rev 1131) @@ -40,11 +40,12 @@ import com.arsdigita.web.URL; import com.arsdigita.web.ParameterMap; +import com.arsdigita.web.Web; import org.apache.log4j.Logger; public class ItemURLFinder implements URLFinder { - + private static final Logger s_log = Logger.getLogger(ItemURLFinder.class); public String find(OID oid,String context) throws NoValidURLException { @@ -58,11 +59,11 @@ .newInstance(oid); } catch (DataObjectNotFoundException ex) { throw new NoValidURLException( - "cannot instantiate item " + oid + + "cannot instantiate item " + oid + " message: " + ex.getMessage() ); } - + if (ContentItem.LIVE.equals(context)) { if (item.isLive()) { if (!item.isLiveVersion()) { @@ -78,27 +79,26 @@ ); } } - + return find(item, context); } - - public String find(OID oid) - throws NoValidURLException { + + public String find(OID oid) throws NoValidURLException { if (s_log.isDebugEnabled()) { s_log.debug("Locating " + oid); } - + ContentItem item; try { item = (ContentItem)DomainObjectFactory .newInstance(oid); } catch (DataObjectNotFoundException ex) { throw new NoValidURLException( - "cannot instantiate item " + oid + + "cannot instantiate item " + oid + " message: " + ex.getMessage() ); } - + if (s_log.isDebugEnabled()) { s_log.debug("Item version is " + item.getVersion()); } @@ -107,8 +107,8 @@ // links with no context defaulted to the live version (if one existed). Changing // that behavior broke a lot of links in static content that couldn't easily be updated. // This change restores the old behavior. We don't get a regression of bz 116226 - // (which bz 41315 was intended to fix) because the CMS search.xsl has been updated to - // append "&context=draft" to the search results. The CMS DHTML editor has also been + // (which bz 41315 was intended to fix) because the CMS search.xsl has been updated to + // append "&context=draft" to the search results. The CMS DHTML editor has also been // updated to append generated links with "&context=live". If at some point in the future // all unqualified links have been removed, then this fix could be removed as well. if (item.isLive() && !item.isLiveVersion()) { @@ -122,76 +122,89 @@ } private String find(ContentItem item, - String context) { + String context) throws NoValidURLException { ContentSection section = item.getContentSection(); ItemResolver resolver = section.getItemResolver(); // If the ContentItem is an index object for a Category, redirect // to the URL for that Category instead of the item. - // in fact don't do that if we have content='draft', in which case - // always send to the admin screen (that's for results of the admin search) + // in fact don't do that if we have content='draft', in which case + // always send to the admin screen (that's for results of the admin + // search) if (!ContentItem.DRAFT.equals(context)) { - ACSObject parent = item.getParent(); - ContentBundle bundle = null; - if (parent instanceof ContentBundle) { - bundle = (ContentBundle) ((ContentBundle) parent).getDraftVersion(); - } - if (bundle != null) { - DataAssociationCursor categories = - ((DataAssociation) DomainServiceInterfaceExposer. - get(bundle, Category.CATEGORIES)).cursor(); - categories.addEqualsFilter("link." + Category.IS_INDEX, Boolean.TRUE); - - if (categories.next()) { - Category indexCat = (Category) DomainObjectFactory. - newInstance(categories.getDataObject()); - categories.close(); - try { - if (s_log.isDebugEnabled()) { - s_log.debug(item + " is a Category index item. Resolving URL for " + - indexCat); - } - return URLService.locate(indexCat.getOID(), context); - } catch (URLFinderNotFoundException ufnfe) { - if (s_log.isDebugEnabled()) { - s_log.debug("Could not find URLFinder for " + indexCat + - ", continuing with URL resolution for " + item, - ufnfe); - } - } catch (NoValidURLException nvue) { - if (s_log.isDebugEnabled()) { - s_log.debug("Could not find valid URL for " + indexCat + - ", continuing with URL resolution for " + item, - nvue); - } - } - } - } - } + ACSObject parent = item.getParent(); + ContentBundle bundle = null; + if (parent instanceof ContentBundle) { + bundle = (ContentBundle) ((ContentBundle) parent).getDraftVersion(); + } + if (bundle != null) { + DataAssociationCursor categories = + ((DataAssociation) DomainServiceInterfaceExposer. + get(bundle, Category.CATEGORIES)).cursor(); + categories.addEqualsFilter("link." + Category.IS_INDEX, Boolean.TRUE); + if (categories.next()) { + Category indexCat = (Category) DomainObjectFactory. + newInstance(categories.getDataObject()); + categories.close(); + try { + if (s_log.isDebugEnabled()) { + s_log.debug(item + " is a Category index item. Resolving URL for " + indexCat); + } + return URLService.locate(indexCat.getOID(), context); + } catch (URLFinderNotFoundException ufnfe) { + if (s_log.isDebugEnabled()) { + s_log.debug("Could not find URLFinder for " + indexCat + + ", continuing with URL resolution for " + item, + ufnfe); + } + } catch (NoValidURLException nvue) { + if (s_log.isDebugEnabled()) { + s_log.debug("Could not find valid URL for " + indexCat + + ", continuing with URL resolution for " + item, + nvue); + } + } + } + } + } else { // DRAFT context + // public users should get 404 when item gets unpublished + if (!Web.getUserContext().isLoggedIn()) { + throw new NoValidURLException("user must be logged-in to get draft"); + } else { + // force the switch to draft version at this point + // otherwise resolver below breaks with: + // java.lang.IllegalStateException: Generating draft url: item must be draft version + if (!item.isDraftVersion()) { + item = item.getDraftVersion(); + s_log.debug("switching to draft version"); + } + } + } String url = resolver.generateItemURL(null, item, section, context); - + if (s_log.isDebugEnabled()) { s_log.debug("Resolved " + item + " in " + context + " to " + url); } - + final int sep = url.indexOf('?'); URL destination = null; - + if (sep == -1) { destination = URL.there(url, null); } else { final ParameterMap params = ParameterMap.fromString (url.substring(sep + 1)); - + destination = URL.there(url.substring(0, sep), params); } - + if (s_log.isDebugEnabled()) { s_log.debug("After munging, destination is " + destination); - } - + } + return destination.toString(); } + } |