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.kernel.impl.observer;
017
018import com.google.common.base.Function;
019import org.fcrepo.kernel.observer.FedoraEvent;
020import org.slf4j.Logger;
021
022import javax.jcr.NamespaceRegistry;
023import javax.jcr.RepositoryException;
024import javax.jcr.Session;
025
026import static com.hp.hpl.jena.rdf.model.ResourceFactory.createProperty;
027import static org.fcrepo.kernel.RdfLexicon.jcrProperties;
028import static org.fcrepo.kernel.RdfLexicon.REPOSITORY_NAMESPACE;
029import static org.fcrepo.kernel.utils.NamespaceTools.getNamespaceRegistry;
030import static org.slf4j.LoggerFactory.getLogger;
031
032/**
033 * @author Andrew Woods
034 * @since 11/22/14
035 */
036public class GetNamespacedProperties implements Function<FedoraEvent, FedoraEvent> {
037
038    private static final Logger LOGGER = getLogger(SimpleObserver.class);
039
040    private Session session;
041
042    /**
043     * Constructor
044     *
045     * @param session used to get NamespaceRegistry
046     */
047    public GetNamespacedProperties(final Session session) {
048        this.session = session;
049    }
050
051    @Override
052    public FedoraEvent apply(final FedoraEvent evt) {
053        final NamespaceRegistry namespaceRegistry = getNamespaceRegistry(session);
054
055        final FedoraEvent event = new FedoraEvent(evt);
056        for (String property : evt.getProperties()) {
057            final String[] parts = property.split(":", 2);
058            if (parts.length == 2) {
059                final String prefix = parts[0];
060                if ("jcr".equals(prefix)) {
061                    if (jcrProperties.contains(createProperty(REPOSITORY_NAMESPACE + parts[1]))) {
062                        event.addProperty(REPOSITORY_NAMESPACE + parts[1]);
063                    } else {
064                        LOGGER.debug("Swallowing jcr property: {}", property);
065                    }
066                } else {
067                    try {
068                        event.addProperty(namespaceRegistry.getURI(prefix) + parts[1]);
069                    } catch (RepositoryException ex) {
070                        LOGGER.debug("Prefix could not be dereferenced using the namespace registry: {}", property);
071                        event.addProperty(property);
072                    }
073                }
074            } else {
075                event.addProperty(property);
076            }
077        }
078
079        for (Integer type : evt.getTypes()) {
080            event.addType(type);
081        }
082        return event;
083    }
084
085}