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