All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.amherst.acdc.event.EventMapper Maven / Gradle / Ivy

The newest version!
/*
 * Copyright Amherst College
 *
 * 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 edu.amherst.acdc.event;

import static java.lang.String.join;
import static java.util.stream.Collectors.joining;
import static org.fcrepo.kernel.api.observer.OptionalValues.BASE_URL;
import static org.fcrepo.kernel.api.observer.OptionalValues.USER_AGENT;
import static org.slf4j.LoggerFactory.getLogger;

import java.util.Set;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import edu.amherst.acdc.orchard.AppleTreeConverter;
import org.fcrepo.jms.JMSEventMessageFactory;
import org.fcrepo.kernel.api.identifiers.InternalIdentifierConverter;
import org.fcrepo.kernel.api.observer.EventType;
import org.fcrepo.kernel.api.observer.FedoraEvent;
import org.fcrepo.event.serialization.EventSerializer;
import org.fcrepo.event.serialization.JsonLDSerializer;

import org.slf4j.Logger;

/**
 * @author acoburn
 */
public class EventMapper implements JMSEventMessageFactory {

    public static final String JMS_NAMESPACE = "org.fcrepo.jms.";

    public static final String TIMESTAMP_HEADER_NAME = JMS_NAMESPACE
            + "timestamp";

    public static final String IDENTIFIER_HEADER_NAME = JMS_NAMESPACE
            + "identifier";

    public static final String EVENT_TYPE_HEADER_NAME = JMS_NAMESPACE
            + "eventType";

    public static final String BASE_URL_HEADER_NAME = JMS_NAMESPACE
            + "baseURL";

    public static final String RESOURCE_TYPE_HEADER_NAME = JMS_NAMESPACE + "resourceType";

    public static final String USER_HEADER_NAME = JMS_NAMESPACE + "user";
    public static final String USER_AGENT_HEADER_NAME = JMS_NAMESPACE + "userAgent";
    public static final String EVENT_ID_HEADER_NAME = JMS_NAMESPACE + "eventID";

    private final int length;
    private final int segments;

    /**
     * A default EventMapper
     */
    public EventMapper() {
        this(2, 4);
    }

    /**
     * An event mapper
     * @param length the length
     * @param segments the number of segments
     */
    public EventMapper(final int length, final int segments) {
        this.length = length;
        this.segments = segments;
    }

    @Override
    public Message getMessage(final FedoraEvent event, final Session jmsSession)
            throws JMSException {

        final EventSerializer serializer = new JsonLDSerializer();
        final InternalIdentifierConverter converter = new AppleTreeConverter(length, segments);

        final String path = converter.reverse().convert(event.getPath());
        final FedoraEvent evt = new Event(event.getEventID(), path, event.getUserID(),
                    event.getDate(), event.getTypes(), event.getResourceTypes(), event.getInfo());

        final String body = serializer.serialize(evt);
        final Message message = jmsSession.createTextMessage(body);

        message.setLongProperty(TIMESTAMP_HEADER_NAME, evt.getDate().toEpochMilli());

        if (evt.getInfo().containsKey(BASE_URL)) {
            message.setStringProperty(BASE_URL_HEADER_NAME, evt.getInfo().get(BASE_URL));
        } else {
            LOGGER.warn("Could not extract baseUrl from FedoraEvent!");
        }
        if (evt.getInfo().containsKey(USER_AGENT)) {
            message.setStringProperty(USER_AGENT_HEADER_NAME, evt.getInfo().get(USER_AGENT));
        }

        message.setStringProperty(IDENTIFIER_HEADER_NAME, evt.getPath());
        message.setStringProperty(EVENT_TYPE_HEADER_NAME, getEventURIs(evt.getTypes()));
        message.setStringProperty(USER_HEADER_NAME, evt.getUserID());
        message.setStringProperty(RESOURCE_TYPE_HEADER_NAME, join(",", evt.getResourceTypes()));
        message.setStringProperty(EVENT_ID_HEADER_NAME, evt.getEventID());

        LOGGER.trace("getMessage() returning: {}", message);
        return message;
    }

    private static String getEventURIs(final Set types) {
        final String uris = types.stream()
                                 .map(EventType::getType)
                                 .collect(joining(","));

        LOGGER.debug("Constructed event type URIs: {}", uris);
        return uris;
    }

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

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy