forked from community/device-mgt-core
parent
ac9acbef82
commit
535c74a962
@ -1,110 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you 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 org.wso2.carbon.device.mgt.core.config.email;
|
||||
|
||||
import javax.xml.bind.annotation.XmlAttribute;
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
@XmlRootElement(name = "NotificationMessage")
|
||||
public class NotificationMessages {
|
||||
|
||||
private String header;
|
||||
private String body;
|
||||
private String footerLine1;
|
||||
private String footerLine2;
|
||||
private String footerLine3;
|
||||
private String subject;
|
||||
private String url;
|
||||
|
||||
@XmlAttribute(name = "type")
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
private String type;
|
||||
|
||||
@XmlElement(name = "Header", required = true)
|
||||
public String getHeader() {
|
||||
return header;
|
||||
}
|
||||
|
||||
public void setHeader(String header) {
|
||||
this.header = header;
|
||||
}
|
||||
|
||||
@XmlElement(name = "Body", required = true)
|
||||
public String getBody() {
|
||||
return body;
|
||||
}
|
||||
|
||||
public void setBody(String body) {
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
@XmlElement(name = "Subject", required = true)
|
||||
public String getSubject() {
|
||||
return subject;
|
||||
}
|
||||
|
||||
public void setSubject(String subject) {
|
||||
this.subject = subject;
|
||||
}
|
||||
|
||||
@XmlElement(name = "Url", required = true)
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
@XmlElement(name = "Footer1")
|
||||
public String getFooterLine1() {
|
||||
return footerLine1;
|
||||
}
|
||||
|
||||
public void setFooterLine1(String footerLine1) {
|
||||
this.footerLine1 = footerLine1;
|
||||
}
|
||||
|
||||
@XmlElement(name = "Footer2")
|
||||
public String getFooterLine2() {
|
||||
return footerLine2;
|
||||
}
|
||||
|
||||
public void setFooterLine2(String footerLine2) {
|
||||
this.footerLine2 = footerLine2;
|
||||
}
|
||||
|
||||
@XmlElement(name = "Footer3")
|
||||
public String getFooterLine3() {
|
||||
return footerLine3;
|
||||
}
|
||||
|
||||
public void setFooterLine3(String footerLine3) {
|
||||
this.footerLine3 = footerLine3;
|
||||
}
|
||||
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you 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 org.wso2.carbon.device.mgt.core.config.email;
|
||||
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import java.util.List;
|
||||
|
||||
@XmlRootElement(name = "Notifications")
|
||||
public class NotificationMessagesConfig {
|
||||
|
||||
private List<NotificationMessages> notificationMessagesList;
|
||||
|
||||
@XmlElement(name = "NotificationMessage")
|
||||
public List<NotificationMessages> getNotificationMessagesList() {
|
||||
return notificationMessagesList;
|
||||
}
|
||||
|
||||
public void setNotificationMessagesList(List<NotificationMessages> notificationMessagesList) {
|
||||
this.notificationMessagesList = notificationMessagesList;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you 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 org.wso2.carbon.device.mgt.core.email;
|
||||
|
||||
public class ContentProcessingInterruptedException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = -3151279311929070298L;
|
||||
|
||||
public ContentProcessingInterruptedException(String msg, Exception nestedEx) {
|
||||
super(msg, nestedEx);
|
||||
}
|
||||
|
||||
public ContentProcessingInterruptedException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public ContentProcessingInterruptedException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
public ContentProcessingInterruptedException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public ContentProcessingInterruptedException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you 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 org.wso2.carbon.device.mgt.core.email;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface EmailContentProvider {
|
||||
|
||||
EmailData getContent(String path,
|
||||
Map<String, TypedValue<Class<?>, Object>> params) throws ContentProcessingInterruptedException;
|
||||
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you 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 org.wso2.carbon.device.mgt.core.email;
|
||||
|
||||
public class EmailContentProviderFactory {
|
||||
|
||||
public static EmailContentProvider getContentProvider() {
|
||||
return new VelocityBasedEmailContentProvider();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you 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 org.wso2.carbon.device.mgt.core.email;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class EmailContext {
|
||||
|
||||
private Set<String> recipients;
|
||||
private Properties properties;
|
||||
|
||||
public EmailContext(final Set<String> recipients, final Properties properties) {
|
||||
if (recipients == null) {
|
||||
throw new IllegalArgumentException("Recipient list cannot be null");
|
||||
}
|
||||
if (recipients.size() == 0) {
|
||||
throw new IllegalArgumentException("No recipient is configured. Recipient list should carry at " +
|
||||
"least one recipient");
|
||||
}
|
||||
this.recipients = recipients;
|
||||
if (properties == null) {
|
||||
throw new IllegalArgumentException("Email Context property bag cannot be null");
|
||||
}
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
public EmailContext(final String recipient, final Properties properties) {
|
||||
if (recipient == null || recipient.isEmpty()) {
|
||||
throw new IllegalArgumentException("Recipient can't be null or empty. Please specify a valid " +
|
||||
"recipient email address");
|
||||
}
|
||||
this.recipients = new HashSet<String>() {{
|
||||
add(recipient);
|
||||
}};
|
||||
if (properties == null) {
|
||||
throw new IllegalArgumentException("Email Context property bag cannot be null");
|
||||
}
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
public EmailContext(final Set<String> recipients) {
|
||||
this(recipients, new Properties());
|
||||
}
|
||||
|
||||
public Set<String> getRecipients() {
|
||||
return recipients;
|
||||
}
|
||||
|
||||
public Properties getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
public String getProperty(String name) {
|
||||
return (String) properties.get(name);
|
||||
}
|
||||
|
||||
public void addProperty(String name, String value) {
|
||||
properties.put(name, value);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you 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 org.wso2.carbon.device.mgt.core.email;
|
||||
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
@XmlRootElement(name = "EmailConfig")
|
||||
public class EmailData {
|
||||
|
||||
private String subject;
|
||||
private String body;
|
||||
|
||||
@XmlElement(name = "Subject", required = true)
|
||||
public String getSubject() {
|
||||
return subject;
|
||||
}
|
||||
|
||||
public void setSubject(String subject) {
|
||||
this.subject = subject;
|
||||
}
|
||||
|
||||
@XmlElement(name = "Body", required = true)
|
||||
public String getBody() {
|
||||
return body;
|
||||
}
|
||||
|
||||
public void setBody(String body) {
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you 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 org.wso2.carbon.device.mgt.core.email;
|
||||
|
||||
public class EmailSendingFailedException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = -3151279311929070294L;
|
||||
|
||||
public EmailSendingFailedException(String msg, Exception nestedEx) {
|
||||
super(msg, nestedEx);
|
||||
}
|
||||
|
||||
public EmailSendingFailedException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public EmailSendingFailedException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
public EmailSendingFailedException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public EmailSendingFailedException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you 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 org.wso2.carbon.device.mgt.core.email;
|
||||
|
||||
import org.apache.commons.collections.ExtendedProperties;
|
||||
import org.apache.velocity.exception.ResourceNotFoundException;
|
||||
import org.apache.velocity.runtime.resource.Resource;
|
||||
import org.apache.velocity.runtime.resource.loader.ResourceLoader;
|
||||
import org.wso2.carbon.context.CarbonContext;
|
||||
import org.wso2.carbon.context.RegistryType;
|
||||
import org.wso2.carbon.registry.api.Registry;
|
||||
import org.wso2.carbon.registry.api.RegistryException;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class RegistryBasedResourceLoader extends ResourceLoader {
|
||||
|
||||
private static final String EMAIL_CONFIG_BASE_LOCATION = "email-templates";
|
||||
|
||||
@Override
|
||||
public void init(ExtendedProperties extendedProperties) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getResourceStream(String name) throws ResourceNotFoundException {
|
||||
try {
|
||||
Registry registry =
|
||||
CarbonContext.getThreadLocalCarbonContext().getRegistry(RegistryType.SYSTEM_CONFIGURATION);
|
||||
if (registry == null) {
|
||||
throw new IllegalStateException("No valid registry instance is attached to the current carbon context");
|
||||
}
|
||||
if (!registry.resourceExists(EMAIL_CONFIG_BASE_LOCATION + "/" + name + ".vm")) {
|
||||
throw new ResourceNotFoundException("Resource '" + name + "' does not exist");
|
||||
}
|
||||
org.wso2.carbon.registry.api.Resource resource =
|
||||
registry.get(EMAIL_CONFIG_BASE_LOCATION + "/" + name + ".vm");
|
||||
|
||||
return resource.getContentStream();
|
||||
} catch (RegistryException e) {
|
||||
throw new ResourceNotFoundException("Error occurred while retrieving resource", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSourceModified(Resource resource) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLastModified(Resource resource) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you 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 org.wso2.carbon.device.mgt.core.email;
|
||||
|
||||
public class TypedValue<T, V> {
|
||||
|
||||
private final T type;
|
||||
private final V value;
|
||||
|
||||
public TypedValue(T type, V value) {
|
||||
this.type = type;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public T getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public V getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return (type.hashCode() ^ value.hashCode());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof TypedValue && (this.type == ((TypedValue) o).getType() &&
|
||||
this.value == ((TypedValue) o).getValue());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you 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 org.wso2.carbon.device.mgt.core.email;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.velocity.Template;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
import org.apache.velocity.app.VelocityEngine;
|
||||
import org.apache.velocity.runtime.resource.loader.ResourceLoader;
|
||||
import org.wso2.carbon.context.CarbonContext;
|
||||
import org.wso2.carbon.context.RegistryType;
|
||||
import org.wso2.carbon.registry.api.Registry;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Map;
|
||||
|
||||
public class VelocityBasedEmailContentProvider implements EmailContentProvider {
|
||||
|
||||
private VelocityEngine engine;
|
||||
private static final Log log = LogFactory.getLog(VelocityBasedEmailContentProvider.class);
|
||||
|
||||
public VelocityBasedEmailContentProvider() {
|
||||
engine = new VelocityEngine();
|
||||
engine.setProperty("resource.loader", "registry");
|
||||
engine.setProperty("velocimacro.library", "");
|
||||
engine.setProperty("registry.resource.loader.class",
|
||||
"org.wso2.carbon.device.mgt.core.email.RegistryBasedResourceLoader");
|
||||
engine.init();
|
||||
}
|
||||
|
||||
@Override
|
||||
public EmailData getContent(String name, Map<String,
|
||||
TypedValue<Class<?>, Object>> params) throws ContentProcessingInterruptedException {
|
||||
VelocityContext ctx = new VelocityContext();
|
||||
for (Map.Entry<String, TypedValue<Class<?>, Object>> param : params.entrySet()) {
|
||||
ctx.put(param.getKey(), param.getValue().getValue());
|
||||
}
|
||||
Template template = engine.getTemplate(name);
|
||||
|
||||
StringWriter content = new StringWriter();
|
||||
template.merge(ctx, content);
|
||||
|
||||
InputStream is = null;
|
||||
try {
|
||||
JAXBContext jaxbCtx = JAXBContext.newInstance(EmailData.class);
|
||||
Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
|
||||
|
||||
is = new ByteArrayInputStream(content.toString().getBytes());
|
||||
return (EmailData) unmarshaller.unmarshal(is);
|
||||
} catch (JAXBException e) {
|
||||
throw new ContentProcessingInterruptedException("Error occurred while parsing email data", e);
|
||||
} finally {
|
||||
if (is != null) {
|
||||
try {
|
||||
is.close();
|
||||
} catch (IOException e) {
|
||||
log.warn("Error occurred while closing input stream used to convert email configuration " +
|
||||
"to an internal object model", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you 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 org.wso2.carbon.device.mgt.core.email.sender;
|
||||
|
||||
import org.apache.axiom.om.OMAbstractFactory;
|
||||
import org.apache.axiom.om.OMElement;
|
||||
import org.apache.axis2.AxisFault;
|
||||
import org.apache.axis2.Constants;
|
||||
import org.apache.axis2.addressing.EndpointReference;
|
||||
import org.apache.axis2.client.Options;
|
||||
import org.apache.axis2.client.ServiceClient;
|
||||
import org.apache.axis2.context.MessageContext;
|
||||
import org.apache.axis2.transport.base.BaseConstants;
|
||||
import org.apache.axis2.transport.mail.MailConstants;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
|
||||
import org.wso2.carbon.device.mgt.core.config.email.EmailConfigurations;
|
||||
import org.wso2.carbon.device.mgt.core.email.EmailData;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class EmailSender {
|
||||
|
||||
private static ThreadPoolExecutor threadPoolExecutor;
|
||||
|
||||
static {
|
||||
EmailConfigurations emailConfig =
|
||||
DeviceConfigurationManager.getInstance().getDeviceManagementConfig().
|
||||
getDeviceManagementConfigRepository().getEmailConfigurations();
|
||||
|
||||
threadPoolExecutor = new ThreadPoolExecutor(emailConfig.getMinNumOfThread(),
|
||||
emailConfig.getMaxNumOfThread(), emailConfig.getKeepAliveTime(), TimeUnit.SECONDS,
|
||||
new LinkedBlockingQueue<Runnable>(emailConfig.getThreadQueueCapacity()));
|
||||
}
|
||||
|
||||
private static final String EMAIL_URI_SCHEME = "mailto:";
|
||||
private static Log log = LogFactory.getLog(EmailSender.class);
|
||||
|
||||
public void sendEmail(EmailData emailData, String... recipients) throws DeviceManagementException {
|
||||
for (String recipient : recipients) {
|
||||
threadPoolExecutor
|
||||
.submit(new EmailSenderTask(recipient, emailData));
|
||||
}
|
||||
}
|
||||
|
||||
public static class EmailSenderTask implements Runnable {
|
||||
|
||||
private String to;
|
||||
private EmailData data;
|
||||
|
||||
EmailSenderTask(String to, EmailData data) {
|
||||
this.to = to;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
Map<String, String> headerMap = new HashMap<>();
|
||||
headerMap.put(MailConstants.MAIL_HEADER_SUBJECT, data.getSubject());
|
||||
OMElement payload = OMAbstractFactory.getOMFactory().createOMElement(
|
||||
BaseConstants.DEFAULT_TEXT_WRAPPER, null);
|
||||
payload.setText(data.getBody());
|
||||
try {
|
||||
ServiceClient serviceClient = new ServiceClient();
|
||||
Options options = new Options();
|
||||
options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE);
|
||||
options.setProperty(MessageContext.TRANSPORT_HEADERS, headerMap);
|
||||
options.setProperty(MailConstants.TRANSPORT_MAIL_FORMAT,
|
||||
MailConstants.TRANSPORT_FORMAT_TEXT);
|
||||
options.setTo(new EndpointReference(EMAIL_URI_SCHEME + to));
|
||||
serviceClient.setOptions(options);
|
||||
serviceClient.fireAndForget(payload);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Email has been successfully sent to '" + to + "'");
|
||||
}
|
||||
} catch (AxisFault e) {
|
||||
log.error("Error occurred while delivering the message, subject: '" + data.getSubject() +
|
||||
"', to: '" + to + "'", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue