001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Alexandre Russel
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.annotations.repository;
021
022import java.net.MalformedURLException;
023import java.net.URI;
024import java.net.URISyntaxException;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.ecm.core.api.CoreInstance;
029import org.nuxeo.ecm.core.api.CoreSession;
030import org.nuxeo.ecm.core.api.DocumentLocation;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.DocumentRef;
033import org.nuxeo.ecm.core.api.NuxeoException;
034import org.nuxeo.ecm.platform.annotations.api.UriResolver;
035import org.nuxeo.ecm.platform.url.api.DocumentView;
036import org.nuxeo.ecm.platform.url.api.DocumentViewCodecManager;
037import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper;
038import org.nuxeo.runtime.api.Framework;
039
040/**
041 * @author Alexandre Russel
042 */
043public class DefaultNuxeoUriResolver implements UriResolver {
044
045    private static final String NUXEO = VirtualHostHelper.getContextPathProperty() + "/";
046
047    private static final Log log = LogFactory.getLog(DefaultNuxeoUriResolver.class);
048
049    private final URNDocumentViewTranslator translator = new URNDocumentViewTranslator();
050
051    private DocumentViewCodecManager viewCodecManager;
052
053    public DefaultNuxeoUriResolver() {
054        viewCodecManager = Framework.getService(DocumentViewCodecManager.class);
055    }
056
057    public URI getSearchURI(URI uri) {
058        DocumentView view = translator.getDocumentViewFromUri(uri);
059        URI translatedUri = null;
060        try (CoreSession session = CoreInstance.openCoreSession(null)) {
061            DocumentRef idRef = view.getDocumentLocation().getIdRef();
062            if (idRef == null) {
063                DocumentModel docModel = session.getDocument(view.getDocumentLocation().getDocRef());
064                idRef = docModel.getRef();
065            }
066            translatedUri = translator.getUriFromDocumentView(view.getDocumentLocation().getServerName(), idRef);
067        }
068        return translatedUri;
069    }
070
071    public URI translateFromGraphURI(URI uri, String baseUrl) {
072        DocumentView view = translator.getDocumentViewFromUri(uri);
073        if (view == null || baseUrl == null) { // not a nuxeo document or
074            // already a urn
075            return uri;
076        }
077        String url = viewCodecManager.getUrlFromDocumentView(view, true, baseUrl);
078        URI u = null;
079        try {
080            u = new URI(url);
081        } catch (URISyntaxException e) {
082            throw new NuxeoException(e);
083        }
084        return u;
085    }
086
087    public URI translateToGraphURI(URI uri) {
088        if (uri.toString().startsWith("urn")) {
089            return uri;
090        }
091        DocumentView view = viewCodecManager.getDocumentViewFromUrl(uri.toString(), true, getBaseUrl(uri));
092        if (view == null) {// not a nuxeo uri
093            return uri;
094        }
095        URI result;
096        try (CoreSession session = CoreInstance.openCoreSession(null)) {
097            DocumentRef idRef = view.getDocumentLocation().getIdRef();
098            if (idRef == null) {
099                DocumentModel docModel = session.getDocument(view.getDocumentLocation().getDocRef());
100                idRef = docModel.getRef();
101            }
102            result = translator.getUriFromDocumentView(view.getDocumentLocation().getServerName(), idRef);
103        }
104        return result;
105    }
106
107    public String getBaseUrl(URI uri) {
108        String url;
109        try {
110            url = uri.toURL().toString();
111        } catch (MalformedURLException e) {
112            throw new NuxeoException(e);
113        }
114        return url.substring(0, url.lastIndexOf(NUXEO) + NUXEO.length());
115    }
116
117    public DocumentRef getDocumentRef(URI uri) {
118        DocumentView view = null;
119        if (translator.isNuxeoUrn(uri)) {
120            view = translator.getDocumentViewFromUri(uri);
121        } else {
122            view = viewCodecManager.getDocumentViewFromUrl(uri.toString(), true, getBaseUrl(uri));
123            if (view == null) {
124                return null;
125            }
126        }
127        DocumentLocation location = view.getDocumentLocation();
128        return location.getDocRef();
129    }
130
131    public DocumentLocation getDocumentLocation(URI uri) {
132        DocumentView view;
133        if (translator.isNuxeoUrn(uri)) {
134            view = translator.getDocumentViewFromUri(uri);
135        } else {
136            view = viewCodecManager.getDocumentViewFromUrl(uri.toString(), true, getBaseUrl(uri));
137            if (view == null) {
138                return null;
139            }
140        }
141        return view.getDocumentLocation();
142    }
143
144    public URI getUri(DocumentView view, String baseUrl) throws URISyntaxException {
145        return new URI(viewCodecManager.getUrlFromDocumentView(view, true, baseUrl));
146    }
147
148}