001/* 002 * Licensed to DuraSpace under one or more contributor license agreements. 003 * See the NOTICE file distributed with this work for additional information 004 * regarding copyright ownership. 005 * 006 * DuraSpace licenses this file to you under the Apache License, 007 * Version 2.0 (the "License"); you may not use this file except in 008 * compliance with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.fcrepo.jms; 019 020import static java.lang.String.join; 021import static java.util.stream.Collectors.joining; 022import static org.fcrepo.kernel.api.observer.OptionalValues.BASE_URL; 023import static org.fcrepo.kernel.api.observer.OptionalValues.USER_AGENT; 024import static org.slf4j.LoggerFactory.getLogger; 025 026import java.util.Set; 027import javax.jms.JMSException; 028import javax.jms.Message; 029import javax.jms.Session; 030 031import org.fcrepo.kernel.api.observer.EventType; 032import org.fcrepo.kernel.api.observer.FedoraEvent; 033import org.fcrepo.event.serialization.EventSerializer; 034import org.fcrepo.event.serialization.JsonLDSerializer; 035 036import org.slf4j.Logger; 037 038/** 039 * Generates JMS {@link Message}s composed entirely of headers, based entirely 040 * on information found in the {@link FedoraEvent} that triggers publication. 041 * 042 * @author ajs6f 043 * @author escowles 044 * @since Dec 2, 2013 045 */ 046public class DefaultMessageFactory implements JMSEventMessageFactory { 047 048 public static final String JMS_NAMESPACE = "org.fcrepo.jms."; 049 050 public static final String TIMESTAMP_HEADER_NAME = JMS_NAMESPACE 051 + "timestamp"; 052 053 public static final String IDENTIFIER_HEADER_NAME = JMS_NAMESPACE 054 + "identifier"; 055 056 public static final String EVENT_TYPE_HEADER_NAME = JMS_NAMESPACE 057 + "eventType"; 058 059 public static final String BASE_URL_HEADER_NAME = JMS_NAMESPACE 060 + "baseURL"; 061 062 public static final String RESOURCE_TYPE_HEADER_NAME = JMS_NAMESPACE + "resourceType"; 063 064 public static final String USER_HEADER_NAME = JMS_NAMESPACE + "user"; 065 public static final String USER_AGENT_HEADER_NAME = JMS_NAMESPACE + "userAgent"; 066 public static final String EVENT_ID_HEADER_NAME = JMS_NAMESPACE + "eventID"; 067 068 @Override 069 public Message getMessage(final FedoraEvent event, final Session jmsSession) 070 throws JMSException { 071 072 final EventSerializer serializer = new JsonLDSerializer(); 073 final String body = serializer.serialize(event); 074 final Message message = jmsSession.createTextMessage(body); 075 076 message.setLongProperty(TIMESTAMP_HEADER_NAME, event.getDate().toEpochMilli()); 077 078 if (event.getInfo().containsKey(BASE_URL)) { 079 message.setStringProperty(BASE_URL_HEADER_NAME, event.getInfo().get(BASE_URL)); 080 } else { 081 LOGGER.warn("Could not extract baseUrl from FedoraEvent!"); 082 } 083 if (event.getInfo().containsKey(USER_AGENT)) { 084 message.setStringProperty(USER_AGENT_HEADER_NAME, event.getInfo().get(USER_AGENT)); 085 } 086 087 message.setStringProperty(IDENTIFIER_HEADER_NAME, event.getPath()); 088 message.setStringProperty(EVENT_TYPE_HEADER_NAME, getEventURIs(event.getTypes())); 089 message.setStringProperty(USER_HEADER_NAME, event.getUserID()); 090 message.setStringProperty(RESOURCE_TYPE_HEADER_NAME, join(",", event.getResourceTypes())); 091 message.setStringProperty(EVENT_ID_HEADER_NAME, event.getEventID()); 092 093 LOGGER.trace("getMessage() returning: {}", message); 094 return message; 095 } 096 097 private static String getEventURIs(final Set<EventType> types) { 098 final String uris = types.stream() 099 .map(EventType::getType) 100 .collect(joining(",")); 101 102 LOGGER.debug("Constructed event type URIs: {}", uris); 103 return uris; 104 } 105 106 private static final Logger LOGGER = getLogger(DefaultMessageFactory.class); 107 108}