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