Skip to content

Commit

Permalink
"file" notification transport
Browse files Browse the repository at this point in the history
  • Loading branch information
mederly committed Jul 28, 2017
1 parent 8113059 commit b1a2310
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 11 deletions.
Expand Up @@ -186,6 +186,10 @@
</xsd:restriction>
</xsd:simpleType>

<xsd:complexType name="NamedConfigurationType">
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="SmsConfigurationType">
<xsd:annotation>
<xsd:documentation>
Expand Down Expand Up @@ -222,6 +226,30 @@
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="FileConfigurationType">
<xsd:annotation>
<xsd:documentation>
Where and how to store "file" notifications.
</xsd:documentation>
<!--<xsd:appinfo>-->
<!--<a:container/>-->
<!--</xsd:appinfo>-->
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="tns:NamedConfigurationType">
<xsd:sequence>
<xsd:element name="file" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
File to store notifications into.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="CustomTransportConfigurationType">
<xsd:annotation>
<xsd:documentation>
Expand Down Expand Up @@ -310,6 +338,7 @@
<xsd:element name="handler" type="tns:EventHandlerType" minOccurs="0" maxOccurs="unbounded"/> <!-- implicit fork -->
<xsd:element name="mail" type="tns:MailConfigurationType" minOccurs="0"/>
<xsd:element name="sms" type="tns:SmsConfigurationType" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="file" type="tns:FileConfigurationType" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="customTransport" type="tns:CustomTransportConfigurationType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
Expand Down
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2010-2017 Evolveum
*
* 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 com.evolveum.midpoint.notifications.impl.api.transports;

import com.evolveum.midpoint.notifications.api.NotificationManager;
import com.evolveum.midpoint.notifications.api.events.Event;
import com.evolveum.midpoint.notifications.api.transports.Message;
import com.evolveum.midpoint.notifications.api.transports.Transport;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.repo.api.RepositoryService;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.FileConfigurationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

import static com.evolveum.midpoint.notifications.impl.api.transports.TransportUtil.formatToFileNew;

/**
* @author mederly
*/
@Component
public class FileTransport implements Transport {

private static final Trace LOGGER = TraceManager.getTrace(FileTransport.class);

private static final String NAME = "file";

private static final String DOT_CLASS = FileTransport.class.getName() + ".";
private static final String DEFAULT_FILE_NAME = "notifications.txt";

@Autowired @Qualifier("cacheRepositoryService") private transient RepositoryService cacheRepositoryService;
@Autowired private NotificationManager notificationManager;

@PostConstruct
public void init() {
notificationManager.registerTransport(NAME, this);
}

@Override
public void send(Message message, String transportName, Event event, Task task, OperationResult parentResult) {
OperationResult result = parentResult.createMinorSubresult(DOT_CLASS + "send");
FileConfigurationType fileConfig = TransportUtil.getTransportConfiguration(transportName, NAME, (c) -> c.getFile(), cacheRepositoryService, result);
String fileName;
if (fileConfig != null && fileConfig.getFile() != null) {
fileName = fileConfig.getFile();
} else {
LOGGER.info("Notification transport configuration for '{}' was not found or has no file name configured: using default of '{}'",
transportName, DEFAULT_FILE_NAME);
fileName = DEFAULT_FILE_NAME;
}
TransportUtil.appendToFile(fileName, formatToFileNew(message, transportName), LOGGER, result);
}

@Override
public String getDefaultRecipientAddress(UserType recipient) {
return PolyString.getOrig(recipient.getName()) + " <" + recipient.getEmailAddress() + ">";
}

@Override
public String getName() {
return NAME;
}
}
Expand Up @@ -51,6 +51,8 @@
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
import com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType;

import static com.evolveum.midpoint.notifications.impl.api.transports.TransportUtil.formatToFileOld;

/**
* @author mederly
*/
Expand Down Expand Up @@ -117,13 +119,7 @@ public void send(Message mailMessage, String transportName, Event event, Task ta
// }
String redirectToFile = mailConfigurationType.getRedirectToFile();
if (redirectToFile != null) {
try {
TransportUtil.appendToFile(redirectToFile, formatToFile(mailMessage));
result.recordSuccess();
} catch (IOException e) {
LoggingUtils.logException(LOGGER, "Couldn't write to mail redirect file {}", e, redirectToFile);
result.recordPartialError("Couldn't write to mail redirect file " + redirectToFile, e);
}
TransportUtil.appendToFile(redirectToFile, formatToFileOld(mailMessage), LOGGER, result);
return;
}

Expand Down
Expand Up @@ -16,8 +16,21 @@

package com.evolveum.midpoint.notifications.impl.api.transports;

import com.evolveum.midpoint.notifications.api.transports.Message;
import com.evolveum.midpoint.notifications.impl.NotificationFunctionsImpl;
import com.evolveum.midpoint.repo.api.RepositoryService;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.util.logging.LoggingUtils;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.xml.ns._public.common.common_3.NamedConfigurationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.NotificationConfigurationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.SystemConfigurationType;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.function.Function;

/**
* @author mederly
Expand All @@ -30,4 +43,40 @@ static void appendToFile(String filename, String text) throws IOException {
fw.close();
}

public static <T extends NamedConfigurationType> T getTransportConfiguration(String transportName, String baseTransportName,
Function<NotificationConfigurationType, List<T>> getter, RepositoryService cacheRepositoryService,
OperationResult result) {

SystemConfigurationType systemConfiguration = NotificationFunctionsImpl.getSystemConfiguration(cacheRepositoryService, result);
if (systemConfiguration == null || systemConfiguration.getNotificationConfiguration() == null) {
return null;
}

String transportConfigName = transportName.length() > baseTransportName.length() ? transportName.substring(baseTransportName.length() + 1) : null; // after e.g. "sms:" or "file:"
for (T namedConfiguration: getter.apply(systemConfiguration.getNotificationConfiguration())) {
if ((transportConfigName == null && namedConfiguration.getName() == null) || (transportConfigName != null && transportConfigName.equals(namedConfiguration.getName()))) {
return namedConfiguration;
}
}
return null;
}

public static void appendToFile(String fileName, String messageText, Trace logger, OperationResult result) {
try {
TransportUtil.appendToFile(fileName, messageText);
result.recordSuccess();
} catch (Throwable t) {
LoggingUtils.logException(logger, "Couldn't write the notification to a file {}", t, fileName);
result.recordPartialError("Couldn't write the notification to a file " + fileName, t);
}
}

public static String formatToFileOld(Message message) {
return "============================================ " + "\n" +new Date() + "\n" + message.toString() + "\n\n";
}

public static String formatToFileNew(Message message, String transport) {
return "================ " + new Date() + " ======= [" + transport + "]\n" + message.debugDump() + "\n\n";
}

}
Expand Up @@ -108,14 +108,13 @@ public boolean processEvent(Event event, EventHandlerType eventHandlerType, Noti
ExpressionVariables variables = getDefaultVariables(event, result);

if (event instanceof ModelEvent) {
((ModelEvent) event).getModelContext().reportProgress(
new ProgressInformation(NOTIFICATIONS, ENTERING));
((ModelEvent) event).getModelContext().reportProgress(new ProgressInformation(NOTIFICATIONS, ENTERING));
}

try {
for (String transportName : generalNotifierType.getTransport()) {

variables.addVariableDefinition(SchemaConstants.C_TRANSPORT_NAME, transportName);
variables.replaceVariableDefinition(SchemaConstants.C_TRANSPORT_NAME, transportName);
Transport transport = notificationManager.getTransport(transportName);

List<String> recipientsAddresses = getRecipientsAddresses(event, generalNotifierType, variables,
Expand Down
Expand Up @@ -132,9 +132,13 @@ public void addVariableDefinition(QName name, Object value) {
LOGGER.warn("Duplicate definition of variable {}", name);
return;
}
variables.put(name, value);
replaceVariableDefinition(name, value);
}

public void replaceVariableDefinition(QName name, Object value) {
variables.put(name, value);
}

public boolean hasVariableDefinition(QName name) {
return variables.containsKey(name);
}
Expand Down

0 comments on commit b1a2310

Please sign in to comment.