001package org.nuxeo.ecm.mobile.webengine;
002
003import org.apache.commons.lang.StringUtils;
004import org.apache.commons.logging.Log;
005import org.apache.commons.logging.LogFactory;
006import org.nuxeo.ecm.core.api.DocumentModel;
007import org.nuxeo.ecm.core.api.DocumentRef;
008import org.nuxeo.ecm.core.api.PathRef;
009import org.nuxeo.ecm.platform.url.DocumentViewImpl;
010import org.nuxeo.ecm.platform.url.api.DocumentView;
011import org.nuxeo.ecm.platform.url.api.DocumentViewCodecManager;
012import org.nuxeo.runtime.api.Framework;
013
014/**
015 * Utility class to handle redirection from a given targetURL
016 * 
017 * @author <a href="mailto:ak@nuxeo.com">Arnaud Kervern</a>
018 * @since 5.7
019 */
020public class RedirectHelper {
021
022    private static final Log log = LogFactory.getLog(RedirectHelper.class);
023
024    public static String getJSFDocumentPath(DocumentModel doc, String baseUrl) {
025        DocumentView view = new DocumentViewImpl(doc);
026        return getCodecManager().getUrlFromDocumentView(view, true, baseUrl);
027    }
028
029    public static DocumentRef findDocumentRef(String targetURL) {
030        if (StringUtils.isBlank(targetURL)) {
031            log.debug("TargetURL is empty");
032            return null;
033        }
034
035        DocumentView docView = getCodecManager().getDocumentViewFromUrl(targetURL, true, "");
036        if (docView == null) {
037            log.info("Unable to resolve docView for targetURL: " + targetURL);
038            return null;
039        }
040
041        // Handle root pathRef, to redirect to workspace selection not on detail
042        // view.
043        PathRef docPathRef = docView.getDocumentLocation().getPathRef();
044        if (docPathRef != null && docPathRef.equals(new PathRef("/"))) {
045            log.debug("Trying to access root document using targetUrl");
046            return null;
047        }
048
049        return docView.getDocumentLocation().getDocRef();
050    }
051
052    protected static DocumentViewCodecManager getCodecManager() {
053        return Framework.getLocalService(DocumentViewCodecManager.class);
054    }
055}