001/**
002 * Copyright 2014 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                this.baseURL = json.get("baseURL").getAsString();
078                log.debug("MessageFactory baseURL: {}", baseURL);
079
080            } else {
081                log.warn("MessageFactory baseURL is empty!");
082            }
083
084        } catch ( Exception ex ) {
085            log.warn("Error setting baseURL", ex);
086        }
087    }
088
089    @Override
090    public Message getMessage(final FedoraEvent jcrEvent,
091        final javax.jms.Session jmsSession) throws RepositoryException,
092        JMSException {
093
094        if ( baseURL == null ) {
095            setBaseURL(jcrEvent);
096        }
097
098        final Message message = jmsSession.createMessage();
099        message.setLongProperty(TIMESTAMP_HEADER_NAME, jcrEvent.getDate());
100        String path = jcrEvent.getPath();
101        if ( path.endsWith("/" + JCR_CONTENT) ) {
102            path = path.replaceAll("/" + JCR_CONTENT,"");
103        }
104        message.setStringProperty(IDENTIFIER_HEADER_NAME, path);
105        message.setStringProperty(EVENT_TYPE_HEADER_NAME, getEventURIs( jcrEvent
106                .getTypes()));
107        message.setStringProperty(BASE_URL_HEADER_NAME, baseURL);
108        message.setStringProperty(PROPERTIES_HEADER_NAME, Joiner.on(',').join(jcrEvent.getProperties()));
109
110        log.trace("getMessage() returning: {}", message);
111        return message;
112    }
113
114    private static String getEventURIs(final Set<Integer> types) {
115        final String uris = Joiner.on(',').join(Iterables.transform(types, new Function<Integer, String>() {
116
117            @Override
118            public String apply(final Integer type) {
119                return REPOSITORY_NAMESPACE + EventType.valueOf(type);
120            }
121        }));
122        log.debug("Constructed event type URIs: {}", uris);
123        return uris;
124    }
125
126    private static final Logger log = getLogger(DefaultMessageFactory.class);
127
128}