Skip to content

Commit

Permalink
adding attachments to mail notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
skublik committed Jan 31, 2019
1 parent 95eba43 commit 5e80a87
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Expand Up @@ -48,6 +48,7 @@
import org.springframework.stereotype.Component;
import org.springframework.util.MimeTypeUtils;

import com.evolveum.midpoint.model.api.util.ModelUtils;
import com.evolveum.midpoint.notifications.api.NotificationManager;
import com.evolveum.midpoint.notifications.api.transports.Message;
import com.evolveum.midpoint.notifications.api.transports.Transport;
Expand All @@ -58,6 +59,7 @@
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.exception.SchemaException;
import com.evolveum.midpoint.util.logging.LoggingUtils;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
Expand All @@ -68,6 +70,7 @@
import com.evolveum.midpoint.xml.ns._public.common.common_3.SystemConfigurationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
import com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType;
import com.evolveum.prism.xml.ns._public.types_3.RawType;

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

Expand Down Expand Up @@ -234,7 +237,22 @@ public void send(Message mailMessage, String transportName, Event event, Task ta
String fileName = null;
BodyPart attachmentBody = new MimeBodyPart();
if(attachment.getContent() != null) {
attachmentBody.setContent(attachment.getContent(), attachment.getContentType());
Object content = null;
if(attachment.getContent() instanceof RawType) {
try {
content = TransportUtil.getStringOrByteArrayFromRawType((RawType)attachment.getContent());
if(content == null) {
LOGGER.warn("RawType " + attachment.getContent() + " isn't possible to parse.");
return;
}
} catch (SchemaException e) {
LOGGER.warn("RawType " + attachment.getContent() + " isn't possible to parse.");
return;
}
} else {
content = attachment.getContent();
}
attachmentBody.setContent(content, attachment.getContentType());
if(StringUtils.isBlank(attachment.getFileName())) {
fileName = "attachment";
} else {
Expand Down
Expand Up @@ -18,20 +18,28 @@

import com.evolveum.midpoint.notifications.api.transports.Message;
import com.evolveum.midpoint.notifications.impl.NotificationFunctionsImpl;
import com.evolveum.midpoint.prism.xnode.PrimitiveXNode;
import com.evolveum.midpoint.prism.xnode.XNode;
import com.evolveum.midpoint.repo.api.RepositoryService;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.util.DOMUtil;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.util.exception.SchemaException;
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 com.evolveum.prism.xml.ns._public.types_3.RawType;

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

import javax.xml.namespace.QName;

/**
* @author mederly
*/
Expand Down Expand Up @@ -86,5 +94,19 @@ public static String formatToFileOld(Message message) {
public static String formatToFileNew(Message message, String transport) {
return "================ " + new Date() + " ======= [" + transport + "]\n" + message.debugDump() + "\n\n";
}

public static Object getStringOrByteArrayFromRawType(RawType raw) throws SchemaException {
Object object = null;
XNode xnode = raw.getXnode();
if(!(xnode instanceof PrimitiveXNode)) {
return null;
}
object = ((PrimitiveXNode<?>) xnode).getValue();
System.out.println("XX object: " + object);
if(!(object instanceof String) && !(object instanceof byte[])) {
return null;
}
return object;
}

}

0 comments on commit 5e80a87

Please sign in to comment.