001/* 002 * (C) Copyright 2006-2016 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 * Alexandre Russel 018 * 019 */ 020package org.nuxeo.ecm.platform.annotations.repository; 021 022import java.net.MalformedURLException; 023import java.net.URI; 024import java.net.URISyntaxException; 025import java.util.Optional; 026 027import org.nuxeo.ecm.core.api.CoreInstance; 028import org.nuxeo.ecm.core.api.CoreSession; 029import org.nuxeo.ecm.core.api.DocumentLocation; 030import org.nuxeo.ecm.core.api.DocumentModel; 031import org.nuxeo.ecm.core.api.DocumentRef; 032import org.nuxeo.ecm.core.api.NuxeoException; 033import org.nuxeo.ecm.platform.annotations.api.UriResolver; 034import org.nuxeo.ecm.platform.url.api.DocumentView; 035import org.nuxeo.ecm.platform.url.api.DocumentViewCodecManager; 036import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper; 037import org.nuxeo.runtime.api.Framework; 038 039/** 040 * @author Alexandre Russel 041 */ 042public class DefaultNuxeoUriResolver implements UriResolver { 043 044 private static final String NUXEO = VirtualHostHelper.getContextPathProperty() + "/"; 045 046 private final URNDocumentViewTranslator translator = new URNDocumentViewTranslator(); 047 048 private DocumentViewCodecManager viewCodecManager; 049 050 public DefaultNuxeoUriResolver() { 051 viewCodecManager = Framework.getService(DocumentViewCodecManager.class); 052 } 053 054 public URI getSearchURI(URI uri) { 055 DocumentView view = translator.getDocumentViewFromUri(uri); 056 return getGraphURIFromDocumentView(view); 057 } 058 059 public URI translateFromGraphURI(URI uri, String baseUrl) { 060 DocumentView view = translator.getDocumentViewFromUri(uri); 061 if (view == null || baseUrl == null) { // not a nuxeo document or 062 // already a urn 063 return uri; 064 } 065 String url = viewCodecManager.getUrlFromDocumentView(view, true, baseUrl); 066 URI u; 067 try { 068 u = new URI(url); 069 } catch (URISyntaxException e) { 070 throw new NuxeoException(e); 071 } 072 return u; 073 } 074 075 public URI translateToGraphURI(URI uri) { 076 if (uri.toString().startsWith("urn")) { 077 return uri; 078 } 079 DocumentView view = viewCodecManager.getDocumentViewFromUrl(uri.toString(), true, getBaseUrl(uri)); 080 if (view == null) {// not a nuxeo uri 081 return uri; 082 } 083 return getGraphURIFromDocumentView(view); 084 } 085 086 protected URI getGraphURIFromDocumentView(DocumentView view) { 087 DocumentLocation docLoc = view.getDocumentLocation(); 088 try (CoreSession session = CoreInstance.openCoreSession(docLoc.getServerName())) { 089 DocumentRef idRef = docLoc.getIdRef(); 090 if (idRef == null) { 091 DocumentModel docModel = session.getDocument(docLoc.getDocRef()); 092 idRef = docModel.getRef(); 093 } 094 return translator.getUriFromDocumentView(docLoc.getServerName(), idRef); 095 } 096 } 097 098 public String getBaseUrl(URI uri) { 099 String url; 100 try { 101 url = uri.toURL().toString(); 102 } catch (MalformedURLException e) { 103 throw new NuxeoException(e); 104 } 105 return url.substring(0, url.lastIndexOf(NUXEO) + NUXEO.length()); 106 } 107 108 public DocumentRef getDocumentRef(URI uri) { 109 return retrieveDocumentLocation(uri).map(DocumentLocation::getDocRef).orElse(null); 110 } 111 112 public DocumentLocation getDocumentLocation(URI uri) { 113 return retrieveDocumentLocation(uri).orElse(null); 114 } 115 116 private Optional<DocumentLocation> retrieveDocumentLocation(URI uri) { 117 Optional<DocumentView> view; 118 if (translator.isNuxeoUrn(uri)) { 119 view = Optional.of(translator.getDocumentViewFromUri(uri)); 120 } else { 121 view = Optional.ofNullable(viewCodecManager.getDocumentViewFromUrl(uri.toString(), true, getBaseUrl(uri))); 122 } 123 return view.map(DocumentView::getDocumentLocation); 124 } 125 126 public URI getUri(DocumentView view, String baseUrl) throws URISyntaxException { 127 return new URI(viewCodecManager.getUrlFromDocumentView(view, true, baseUrl)); 128 } 129 130}