001/* 002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Arnaud Kervern 018 */ 019package org.nuxeo.ecm.mobile.webengine; 020 021import org.apache.commons.lang.StringUtils; 022import org.apache.commons.logging.Log; 023import org.apache.commons.logging.LogFactory; 024import org.nuxeo.ecm.core.api.DocumentModel; 025import org.nuxeo.ecm.core.api.DocumentRef; 026import org.nuxeo.ecm.core.api.PathRef; 027import org.nuxeo.ecm.platform.url.DocumentViewImpl; 028import org.nuxeo.ecm.platform.url.api.DocumentView; 029import org.nuxeo.ecm.platform.url.api.DocumentViewCodecManager; 030import org.nuxeo.runtime.api.Framework; 031 032/** 033 * Utility class to handle redirection from a given targetURL 034 * 035 * @author <a href="mailto:ak@nuxeo.com">Arnaud Kervern</a> 036 * @since 5.7 037 */ 038public class RedirectHelper { 039 040 private static final Log log = LogFactory.getLog(RedirectHelper.class); 041 042 public static String getJSFDocumentPath(DocumentModel doc, String baseUrl) { 043 DocumentView view = new DocumentViewImpl(doc); 044 return getCodecManager().getUrlFromDocumentView(view, true, baseUrl); 045 } 046 047 public static DocumentRef findDocumentRef(String targetURL) { 048 if (StringUtils.isBlank(targetURL)) { 049 log.debug("TargetURL is empty"); 050 return null; 051 } 052 053 DocumentView docView = getCodecManager().getDocumentViewFromUrl(targetURL, true, ""); 054 if (docView == null) { 055 log.info("Unable to resolve docView for targetURL: " + targetURL); 056 return null; 057 } 058 059 // Handle root pathRef, to redirect to workspace selection not on detail 060 // view. 061 PathRef docPathRef = docView.getDocumentLocation().getPathRef(); 062 if (docPathRef != null && docPathRef.equals(new PathRef("/"))) { 063 log.debug("Trying to access root document using targetUrl"); 064 return null; 065 } 066 067 return docView.getDocumentLocation().getDocRef(); 068 } 069 070 protected static DocumentViewCodecManager getCodecManager() { 071 return Framework.getLocalService(DocumentViewCodecManager.class); 072 } 073}