001/** 002 * Copyright 2015 DuraSpace, Inc. 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 */ 016package org.fcrepo.http.api; 017 018import com.google.common.annotations.VisibleForTesting; 019import com.hp.hpl.jena.rdf.model.Resource; 020import org.fcrepo.http.commons.AbstractResource; 021import org.fcrepo.http.commons.api.rdf.HttpResourceConverter; 022import org.fcrepo.kernel.models.FedoraResource; 023import org.fcrepo.kernel.models.Tombstone; 024import org.fcrepo.kernel.exception.TombstoneException; 025import org.fcrepo.kernel.identifiers.IdentifierConverter; 026import org.slf4j.Logger; 027 028import javax.jcr.Session; 029import javax.jcr.observation.ObservationManager; 030import javax.ws.rs.core.UriInfo; 031 032import java.net.URI; 033 034import static org.slf4j.LoggerFactory.getLogger; 035 036/** 037 * @author cabeer 038 * @since 10/5/14 039 */ 040abstract public class FedoraBaseResource extends AbstractResource { 041 042 private static final Logger LOGGER = getLogger(FedoraBaseResource.class); 043 044 protected IdentifierConverter<Resource, FedoraResource> idTranslator; 045 046 protected abstract Session session(); 047 048 protected IdentifierConverter<Resource, FedoraResource> translator() { 049 if (idTranslator == null) { 050 idTranslator = new HttpResourceConverter(session(), 051 uriInfo.getBaseUriBuilder().clone().path(FedoraLdp.class)); 052 } 053 054 return idTranslator; 055 } 056 057 /** 058 * Get the FedoraResource for the resource at the external path 059 * @param externalPath the external path 060 * @return the fedora resource at the external path 061 */ 062 @VisibleForTesting 063 public FedoraResource getResourceFromPath(final String externalPath) { 064 final Resource resource = translator().toDomain(externalPath); 065 final FedoraResource fedoraResource = translator().convert(resource); 066 067 if (fedoraResource instanceof Tombstone) { 068 throw new TombstoneException(fedoraResource, resource.getURI() + "/fcr:tombstone"); 069 } 070 071 return fedoraResource; 072 } 073 074 /** 075 * Set the baseURL for JMS events. 076 * @param uriInfo the uri info 077 **/ 078 protected void setUpJMSBaseURIs(final UriInfo uriInfo) { 079 try { 080 final URI baseURL = uriInfo.getBaseUri(); 081 LOGGER.debug("setting baseURL = " + baseURL.toString()); 082 final ObservationManager obs = session().getWorkspace().getObservationManager(); 083 final String json = "{\"baseURL\":\"" + baseURL.toString() + "\"}"; 084 obs.setUserData(json); 085 } catch ( Exception ex ) { 086 LOGGER.warn("Error setting baseURL", ex); 087 } 088 } 089 090}