diff --git a/src/main/java/io/kamax/mxisd/backend/firebase/GoogleFirebaseBackend.java b/src/main/java/io/kamax/mxisd/backend/firebase/GoogleFirebaseBackend.java index 3d50c05..bb68386 100644 --- a/src/main/java/io/kamax/mxisd/backend/firebase/GoogleFirebaseBackend.java +++ b/src/main/java/io/kamax/mxisd/backend/firebase/GoogleFirebaseBackend.java @@ -60,7 +60,9 @@ public class GoogleFirebaseBackend { private FirebaseCredential getCreds(String credsPath) throws IOException { if (StringUtils.isNotBlank(credsPath)) { - return FirebaseCredentials.fromCertificate(new FileInputStream(credsPath)); + try (FileInputStream is = new FileInputStream(credsPath)) { + return FirebaseCredentials.fromCertificate(is); + } } else { return FirebaseCredentials.applicationDefault(); } diff --git a/src/main/java/io/kamax/mxisd/config/YamlConfigLoader.java b/src/main/java/io/kamax/mxisd/config/YamlConfigLoader.java index 650613a..dea628d 100644 --- a/src/main/java/io/kamax/mxisd/config/YamlConfigLoader.java +++ b/src/main/java/io/kamax/mxisd/config/YamlConfigLoader.java @@ -37,8 +37,10 @@ public class YamlConfigLoader { rep.getPropertyUtils().setAllowReadOnlyProperties(true); rep.getPropertyUtils().setSkipMissingProperties(true); Yaml yaml = new Yaml(new Constructor(MxisdConfig.class), rep); - Object o = yaml.load(new FileInputStream(path)); - return GsonUtil.get().fromJson(GsonUtil.get().toJson(o), MxisdConfig.class); + try (FileInputStream is = new FileInputStream(path)) { + Object o = yaml.load(is); + return GsonUtil.get().fromJson(GsonUtil.get().toJson(o), MxisdConfig.class); + } } public static Optional tryLoadFromFile(String path) { diff --git a/src/main/java/io/kamax/mxisd/http/undertow/handler/identity/v1/RemoteSessionCheckHandler.java b/src/main/java/io/kamax/mxisd/http/undertow/handler/identity/v1/RemoteSessionCheckHandler.java index ab338c0..33800f1 100644 --- a/src/main/java/io/kamax/mxisd/http/undertow/handler/identity/v1/RemoteSessionCheckHandler.java +++ b/src/main/java/io/kamax/mxisd/http/undertow/handler/identity/v1/RemoteSessionCheckHandler.java @@ -24,11 +24,8 @@ import io.kamax.mxisd.config.ViewConfig; import io.kamax.mxisd.exception.SessionNotValidatedException; import io.kamax.mxisd.http.undertow.handler.BasicHttpHandler; import io.kamax.mxisd.session.SessionMananger; +import io.kamax.mxisd.util.FileUtil; import io.undertow.server.HttpServerExchange; -import org.apache.commons.io.IOUtils; - -import java.io.FileInputStream; -import java.nio.charset.StandardCharsets; public class RemoteSessionCheckHandler extends BasicHttpHandler { @@ -45,18 +42,15 @@ public class RemoteSessionCheckHandler extends BasicHttpHandler { String sid = getQueryParameter(exchange, "sid"); String secret = getQueryParameter(exchange, "client_secret"); + String viewData; try { - FileInputStream f = new FileInputStream(viewCfg.getSession().getRemote().getOnCheck().getSuccess()); - String viewData = IOUtils.toString(f, StandardCharsets.UTF_8); - mgr.validateRemote(sid, secret); - - writeBodyAsUtf8(exchange, viewData); + viewData = FileUtil.load(viewCfg.getSession().getRemote().getOnCheck().getSuccess()); } catch (SessionNotValidatedException e) { - FileInputStream f = new FileInputStream(viewCfg.getSession().getRemote().getOnCheck().getFailure()); - String viewData = IOUtils.toString(f, StandardCharsets.UTF_8); - writeBodyAsUtf8(exchange, viewData); + viewData = FileUtil.load(viewCfg.getSession().getRemote().getOnCheck().getFailure()); } + + writeBodyAsUtf8(exchange, viewData); } } diff --git a/src/main/java/io/kamax/mxisd/http/undertow/handler/identity/v1/RemoteSessionStartHandler.java b/src/main/java/io/kamax/mxisd/http/undertow/handler/identity/v1/RemoteSessionStartHandler.java index e04eb9c..9e50fe3 100644 --- a/src/main/java/io/kamax/mxisd/http/undertow/handler/identity/v1/RemoteSessionStartHandler.java +++ b/src/main/java/io/kamax/mxisd/http/undertow/handler/identity/v1/RemoteSessionStartHandler.java @@ -24,11 +24,8 @@ import io.kamax.mxisd.config.ViewConfig; import io.kamax.mxisd.http.undertow.handler.BasicHttpHandler; import io.kamax.mxisd.session.SessionMananger; import io.kamax.mxisd.threepid.session.IThreePidSession; +import io.kamax.mxisd.util.FileUtil; import io.undertow.server.HttpServerExchange; -import org.apache.commons.io.IOUtils; - -import java.io.FileInputStream; -import java.nio.charset.StandardCharsets; public class RemoteSessionStartHandler extends BasicHttpHandler { @@ -46,8 +43,7 @@ public class RemoteSessionStartHandler extends BasicHttpHandler { String secret = getQueryParameter(exchange, "client_secret"); IThreePidSession session = mgr.createRemote(sid, secret); - FileInputStream f = new FileInputStream(viewCfg.getSession().getRemote().getOnRequest().getSuccess()); - String rawData = IOUtils.toString(f, StandardCharsets.UTF_8); + String rawData = FileUtil.load(viewCfg.getSession().getRemote().getOnRequest().getSuccess()); String data = rawData.replace("${checkLink}", RemoteIdentityAPIv1.getSessionCheck(session.getId(), session.getSecret())); writeBodyAsUtf8(exchange, data); } diff --git a/src/main/java/io/kamax/mxisd/http/undertow/handler/identity/v1/SessionValidateHandler.java b/src/main/java/io/kamax/mxisd/http/undertow/handler/identity/v1/SessionValidateHandler.java index d0f6c08..f981f21 100644 --- a/src/main/java/io/kamax/mxisd/http/undertow/handler/identity/v1/SessionValidateHandler.java +++ b/src/main/java/io/kamax/mxisd/http/undertow/handler/identity/v1/SessionValidateHandler.java @@ -27,18 +27,16 @@ import io.kamax.mxisd.http.io.identity.SuccessStatusJson; import io.kamax.mxisd.http.undertow.handler.BasicHttpHandler; import io.kamax.mxisd.session.SessionMananger; import io.kamax.mxisd.session.ValidationResult; +import io.kamax.mxisd.util.FileUtil; import io.undertow.server.HttpServerExchange; import io.undertow.util.HttpString; -import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.FileInputStream; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; -import java.nio.charset.StandardCharsets; public class SessionValidateHandler extends BasicHttpHandler { @@ -95,16 +93,13 @@ public class SessionValidateHandler extends BasicHttpHandler { exchange.getResponseHeaders().add(HttpString.tryFromString("Location"), url); } else { try { + String rawData = FileUtil.load(viewCfg.getSession().getLocalRemote().getOnTokenSubmit().getSuccess()); if (r.isCanRemote()) { - FileInputStream f = new FileInputStream(viewCfg.getSession().getLocalRemote().getOnTokenSubmit().getSuccess()); String url = srvCfg.getPublicUrl() + RemoteIdentityAPIv1.getRequestToken(r.getSession().getId(), r.getSession().getSecret()); - String rawData = IOUtils.toString(f, StandardCharsets.UTF_8); String data = rawData.replace("${remoteSessionLink}", url); writeBodyAsUtf8(exchange, data); } else { - FileInputStream f = new FileInputStream(viewCfg.getSession().getLocalRemote().getOnTokenSubmit().getSuccess()); - String data = IOUtils.toString(f, StandardCharsets.UTF_8); - writeBodyAsUtf8(exchange, data); + writeBodyAsUtf8(exchange, rawData); } } catch (IOException e) { throw new RuntimeException(e); diff --git a/src/main/java/io/kamax/mxisd/threepid/generator/GenericTemplateNotificationGenerator.java b/src/main/java/io/kamax/mxisd/threepid/generator/GenericTemplateNotificationGenerator.java index bf12fc6..e34ef63 100644 --- a/src/main/java/io/kamax/mxisd/threepid/generator/GenericTemplateNotificationGenerator.java +++ b/src/main/java/io/kamax/mxisd/threepid/generator/GenericTemplateNotificationGenerator.java @@ -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()); } diff --git a/src/main/java/io/kamax/mxisd/threepid/notification/email/EmailSendGridNotificationHandler.java b/src/main/java/io/kamax/mxisd/threepid/notification/email/EmailSendGridNotificationHandler.java index 945e612..6447433 100644 --- a/src/main/java/io/kamax/mxisd/threepid/notification/email/EmailSendGridNotificationHandler.java +++ b/src/main/java/io/kamax/mxisd/threepid/notification/email/EmailSendGridNotificationHandler.java @@ -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); } diff --git a/src/main/java/io/kamax/mxisd/util/FileUtil.java b/src/main/java/io/kamax/mxisd/util/FileUtil.java new file mode 100644 index 0000000..aff740f --- /dev/null +++ b/src/main/java/io/kamax/mxisd/util/FileUtil.java @@ -0,0 +1,57 @@ +/* + * mxisd - Matrix Identity Server Daemon + * Copyright (C) 2019 Kamax Sàrl + * + * https://www.kamax.io/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package io.kamax.mxisd.util; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.util.Objects; + +public class FileUtil { + + public static String load(String loc) throws IOException { + URI uri = URI.create(loc); + + InputStream is; + if (StringUtils.equals("classpath", uri.getScheme())) { + String resource = uri.getSchemeSpecificPart(); + is = FileUtil.class.getResourceAsStream(resource); + if (Objects.isNull(is)) { + throw new FileNotFoundException("No classpath resource: " + resource); + } + } else { + is = new FileInputStream(loc); + } + + try { + return IOUtils.toString(is, StandardCharsets.UTF_8); + } finally { + is.close(); + } + } + +}