Better handle of File reading / Input Streams
This commit is contained in:
@@ -27,16 +27,12 @@ import io.kamax.mxisd.config.threepid.medium.GenericTemplateConfig;
|
||||
import io.kamax.mxisd.exception.InternalServerError;
|
||||
import io.kamax.mxisd.invitation.IThreePidInviteReply;
|
||||
import io.kamax.mxisd.threepid.session.IThreePidSession;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import io.kamax.mxisd.util.FileUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public abstract class GenericTemplateNotificationGenerator extends PlaceholderNotificationGenerator implements NotificationGenerator {
|
||||
|
||||
@@ -51,14 +47,7 @@ public abstract class GenericTemplateNotificationGenerator extends PlaceholderNo
|
||||
|
||||
private String getTemplateContent(String location) {
|
||||
try {
|
||||
URI loc = URI.create(location);
|
||||
InputStream is;
|
||||
if (StringUtils.equals("classpath", loc.getScheme())) {
|
||||
is = getClass().getResourceAsStream(loc.getSchemeSpecificPart());
|
||||
} else {
|
||||
is = new FileInputStream(location);
|
||||
}
|
||||
return IOUtils.toString(is, StandardCharsets.UTF_8);
|
||||
return FileUtil.load(location);
|
||||
} catch (IOException e) {
|
||||
throw new InternalServerError("Unable to read template content at " + location + ": " + e.getMessage());
|
||||
}
|
||||
|
||||
@@ -31,14 +31,12 @@ import io.kamax.mxisd.invitation.IThreePidInviteReply;
|
||||
import io.kamax.mxisd.notification.NotificationHandler;
|
||||
import io.kamax.mxisd.threepid.generator.PlaceholderNotificationGenerator;
|
||||
import io.kamax.mxisd.threepid.session.IThreePidSession;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import io.kamax.mxisd.util.FileUtil;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static com.sendgrid.SendGrid.Email;
|
||||
import static com.sendgrid.SendGrid.Response;
|
||||
@@ -78,7 +76,7 @@ public class EmailSendGridNotificationHandler extends PlaceholderNotificationGen
|
||||
|
||||
private String getFromFile(String path) {
|
||||
try {
|
||||
return IOUtils.toString(new FileInputStream(path), StandardCharsets.UTF_8);
|
||||
return FileUtil.load(path);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Couldn't create notification content using file " + path, e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user