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.jms.headers;
017
018import static org.fcrepo.kernel.RdfLexicon.REPOSITORY_NAMESPACE;
019import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
020import static org.slf4j.LoggerFactory.getLogger;
021
022import java.util.Set;
023
024import javax.jcr.RepositoryException;
025import javax.jms.JMSException;
026import javax.jms.Message;
027
028import org.apache.commons.lang.StringUtils;
029import org.fcrepo.jms.observer.JMSEventMessageFactory;
030import org.fcrepo.kernel.observer.FedoraEvent;
031import org.fcrepo.kernel.utils.EventType;
032import org.slf4j.Logger;
033
034import com.google.common.base.Function;
035import com.google.common.base.Joiner;
036import com.google.common.collect.Iterables;
037import com.google.gson.JsonObject;
038import com.google.gson.JsonParser;
039
040/**
041 * Generates JMS {@link Message}s composed entirely of headers, based entirely
042 * on information found in the {@link FedoraEvent} that triggers publication.
043 *
044 * @author ajs6f
045 * @since Dec 2, 2013
046 */
047public class DefaultMessageFactory implements JMSEventMessageFactory {
048
049    public static final String JMS_NAMESPACE = "org.fcrepo.jms.";
050
051    public static final String TIMESTAMP_HEADER_NAME = JMS_NAMESPACE
052            + "timestamp";
053
054    public static final String IDENTIFIER_HEADER_NAME = JMS_NAMESPACE
055            + "identifier";
056
057    public static final String EVENT_TYPE_HEADER_NAME = JMS_NAMESPACE
058            + "eventType";
059
060    public static final String BASE_URL_HEADER_NAME = JMS_NAMESPACE
061            + "baseURL";
062
063    public static final String PROPERTIES_HEADER_NAME = JMS_NAMESPACE
064            + "properties";
065
066    private String baseURL;
067
068    /**
069     * Set baseURL.
070     * @param event Fedora event object containing user data with baseURL specified.
071     */
072    private void setBaseURL(final FedoraEvent event) {
073        try {
074            final String userdata = event.getUserData();
075            if (!StringUtils.isBlank(userdata)) {
076                final JsonObject json = new JsonParser().parse(userdata).getAsJsonObject();
077                String url = json.get("baseURL").getAsString();
078                while (url.endsWith("/")) {
079                    url = url.substring(0, url.length() - 1);
080                }
081                this.baseURL = url;
082                LOGGER.debug("MessageFactory baseURL: {}", baseURL);
083
084            } else {
085                LOGGER.warn("MessageFactory baseURL is empty!");
086            }
087
088        } catch ( final Exception ex ) {
089            LOGGER.warn("Error setting baseURL", ex);
090        }
091    }
092
093    @Override
094    public Message getMessage(final FedoraEvent jcrEvent,
095        final javax.jms.Session jmsSession) throws RepositoryException,
096        JMSException {
097
098        if ( baseURL == null ) {
099            setBaseURL(jcrEvent);
100        }
101
102        final Message message = jmsSession.createMessage();
103        message.setLongProperty(TIMESTAMP_HEADER_NAME, jcrEvent.getDate());
104        String path = jcrEvent.getPath();
105        if ( path.endsWith("/" + JCR_CONTENT) ) {
106            path = path.replaceAll("/" + JCR_CONTENT,"");
107        }
108        message.setStringProperty(IDENTIFIER_HEADER_NAME, path);
109        message.setStringProperty(EVENT_TYPE_HEADER_NAME, getEventURIs( jcrEvent
110                .getTypes()));
111        message.setStringProperty(BASE_URL_HEADER_NAME, baseURL);
112        message.setStringProperty(PROPERTIES_HEADER_NAME, Joiner.on(',').join(jcrEvent.getProperties()));
113
114        LOGGER.trace("getMessage() returning: {}", message);
115        return message;
116    }
117
118    private static String getEventURIs(final Set<Integer> types) {
119        final String uris = Joiner.on(',').join(Iterables.transform(types, new Function<Integer, String>() {
120
121            @Override
122            public String apply(final Integer type) {
123                return REPOSITORY_NAMESPACE + EventType.valueOf(type);
124            }
125        }));
126        LOGGER.debug("Constructed event type URIs: {}", uris);
127        return uris;
128    }
129
130    private static final Logger LOGGER = getLogger(DefaultMessageFactory.class);
131
132}