/**
 * Copyright 2014 DuraSpace, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fcrepo.http.api;

import com.google.common.annotations.VisibleForTesting;
import com.hp.hpl.jena.rdf.model.Resource;
import org.fcrepo.http.commons.AbstractResource;
import org.fcrepo.http.commons.api.rdf.UriAwareIdentifierConverter;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.identifiers.IdentifierConverter;
import org.fcrepo.kernel.impl.DatastreamImpl;
import org.fcrepo.kernel.impl.FedoraBinaryImpl;
import org.fcrepo.kernel.impl.FedoraObjectImpl;
import org.slf4j.Logger;

import javax.jcr.Node;
import javax.jcr.Session;
import javax.jcr.observation.ObservationManager;
import javax.ws.rs.core.UriInfo;

import java.net.URI;

import static org.fcrepo.jcr.FedoraJcrTypes.FCR_METADATA;
import static org.slf4j.LoggerFactory.getLogger;

/**
 * @author cabeer
 * @since 10/5/14
 */
abstract public class FedoraBaseResource extends AbstractResource {

    private static final Logger LOGGER = getLogger(FedoraBaseResource.class);

    protected IdentifierConverter<Resource,Node> identifierTranslator;

    protected abstract Session session();

    protected IdentifierConverter<Resource,Node> translator() {
        if (identifierTranslator == null) {
            identifierTranslator = new UriAwareIdentifierConverter(session(),
                    uriInfo.getBaseUriBuilder().clone().path(FedoraLdp.class));
        }

        return identifierTranslator;
    }

    /**
     * Get the FedoraResource for the resource at the external path
     * @param externalPath
     * @return
     */
    @VisibleForTesting
    public FedoraResource getResourceFromPath(final String externalPath) {
        final FedoraResource resource;
        final boolean metadata = externalPath != null
                && externalPath.endsWith(FCR_METADATA);

        final Node node = translator().convert(translator().toDomain(externalPath));

        if (DatastreamImpl.hasMixin(node)) {
            final DatastreamImpl datastream = new DatastreamImpl(node);

            if (metadata) {
                resource = datastream;
            } else {
                resource = datastream.getBinary();
            }
        } else if (FedoraBinaryImpl.hasMixin(node)) {
            resource = new FedoraBinaryImpl(node);
        } else {
            resource = new FedoraObjectImpl(node);
        }
        return resource;
    }



    private static boolean baseURLSet = false;
    /**
     * Set the baseURL for JMS events.
     **/
    protected void setUpJMSBaseURIs(final UriInfo uriInfo) {
        if ( !baseURLSet ) {
            // set to true the first time this is run.  if there is an exception the first time, there
            // will likely be an exception every time.  since this is run on each repository update,
            // we should fail fast rather than retrying over and over.
            baseURLSet = true;
            try {
                final URI baseURL = uriInfo.getBaseUri();
                final Class<? extends FedoraBaseResource> klass = this.getClass();
                LOGGER.debug(klass + ".init(): baseURL = " + baseURL.toString());
                final ObservationManager obs = session().getWorkspace().getObservationManager();
                final String json = "{\"baseURL\":\"" + baseURL.toString() + "\"}";
                obs.setUserData(json);
                LOGGER.trace(klass + ".init(): done");
            } catch ( Exception ex ) {
                LOGGER.warn("Error setting baseURL", ex);
            }
        }
    }

}

@@ -2947,56 +2947,8 @@
 %7D%0A%0A%0A
-%0A    private static boolean baseURLSet = false;%0A
     
@@ -3064,347 +3064,8 @@
 ) %7B%0A
-        if ( !baseURLSet ) %7B%0A            // set to true the first time this is run.  if there is an exception the first time, there%0A            // will likely be an exception every time.  since this is run on each repository update,%0A            // we should fail fast rather than retrying over and over.%0A            baseURLSet = true;%0A    
     
@@ -3074,20 +3074,16 @@
   try %7B%0A
-    
         
@@ -3144,125 +3144,29 @@
     
-    final Class%3C? extends FedoraBaseResource%3E klass = this.getClass();%0A                LOGGER.debug(klass + %22.init():
+LOGGER.debug(%22setting
  bas
@@ -3193,28 +3193,24 @@
 oString());%0A
-    
             
@@ -3298,28 +3298,24 @@
             
-  
-  
 final String
@@ -3382,20 +3382,16 @@
         
-    
 obs.setU
@@ -3409,67 +3409,8 @@
 n);%0A
-                LOGGER.trace(klass + %22.init(): done%22);%0A    
     
@@ -3448,28 +3448,24 @@
             
-  
-  
 LOGGER.warn(
@@ -3494,30 +3494,16 @@
 %22, ex);%0A
-            %7D%0A
         

Update setUpJMSBaseURIs to run every request