Better handle of File reading / Input Streams
This commit is contained in:
@@ -60,7 +60,9 @@ public class GoogleFirebaseBackend {
|
|||||||
|
|
||||||
private FirebaseCredential getCreds(String credsPath) throws IOException {
|
private FirebaseCredential getCreds(String credsPath) throws IOException {
|
||||||
if (StringUtils.isNotBlank(credsPath)) {
|
if (StringUtils.isNotBlank(credsPath)) {
|
||||||
return FirebaseCredentials.fromCertificate(new FileInputStream(credsPath));
|
try (FileInputStream is = new FileInputStream(credsPath)) {
|
||||||
|
return FirebaseCredentials.fromCertificate(is);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return FirebaseCredentials.applicationDefault();
|
return FirebaseCredentials.applicationDefault();
|
||||||
}
|
}
|
||||||
|
@@ -37,8 +37,10 @@ public class YamlConfigLoader {
|
|||||||
rep.getPropertyUtils().setAllowReadOnlyProperties(true);
|
rep.getPropertyUtils().setAllowReadOnlyProperties(true);
|
||||||
rep.getPropertyUtils().setSkipMissingProperties(true);
|
rep.getPropertyUtils().setSkipMissingProperties(true);
|
||||||
Yaml yaml = new Yaml(new Constructor(MxisdConfig.class), rep);
|
Yaml yaml = new Yaml(new Constructor(MxisdConfig.class), rep);
|
||||||
Object o = yaml.load(new FileInputStream(path));
|
try (FileInputStream is = new FileInputStream(path)) {
|
||||||
return GsonUtil.get().fromJson(GsonUtil.get().toJson(o), MxisdConfig.class);
|
Object o = yaml.load(is);
|
||||||
|
return GsonUtil.get().fromJson(GsonUtil.get().toJson(o), MxisdConfig.class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Optional<MxisdConfig> tryLoadFromFile(String path) {
|
public static Optional<MxisdConfig> tryLoadFromFile(String path) {
|
||||||
|
@@ -24,11 +24,8 @@ import io.kamax.mxisd.config.ViewConfig;
|
|||||||
import io.kamax.mxisd.exception.SessionNotValidatedException;
|
import io.kamax.mxisd.exception.SessionNotValidatedException;
|
||||||
import io.kamax.mxisd.http.undertow.handler.BasicHttpHandler;
|
import io.kamax.mxisd.http.undertow.handler.BasicHttpHandler;
|
||||||
import io.kamax.mxisd.session.SessionMananger;
|
import io.kamax.mxisd.session.SessionMananger;
|
||||||
|
import io.kamax.mxisd.util.FileUtil;
|
||||||
import io.undertow.server.HttpServerExchange;
|
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 {
|
public class RemoteSessionCheckHandler extends BasicHttpHandler {
|
||||||
|
|
||||||
@@ -45,18 +42,15 @@ public class RemoteSessionCheckHandler extends BasicHttpHandler {
|
|||||||
String sid = getQueryParameter(exchange, "sid");
|
String sid = getQueryParameter(exchange, "sid");
|
||||||
String secret = getQueryParameter(exchange, "client_secret");
|
String secret = getQueryParameter(exchange, "client_secret");
|
||||||
|
|
||||||
|
String viewData;
|
||||||
try {
|
try {
|
||||||
FileInputStream f = new FileInputStream(viewCfg.getSession().getRemote().getOnCheck().getSuccess());
|
|
||||||
String viewData = IOUtils.toString(f, StandardCharsets.UTF_8);
|
|
||||||
|
|
||||||
mgr.validateRemote(sid, secret);
|
mgr.validateRemote(sid, secret);
|
||||||
|
viewData = FileUtil.load(viewCfg.getSession().getRemote().getOnCheck().getSuccess());
|
||||||
writeBodyAsUtf8(exchange, viewData);
|
|
||||||
} catch (SessionNotValidatedException e) {
|
} catch (SessionNotValidatedException e) {
|
||||||
FileInputStream f = new FileInputStream(viewCfg.getSession().getRemote().getOnCheck().getFailure());
|
viewData = FileUtil.load(viewCfg.getSession().getRemote().getOnCheck().getFailure());
|
||||||
String viewData = IOUtils.toString(f, StandardCharsets.UTF_8);
|
|
||||||
writeBodyAsUtf8(exchange, viewData);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
writeBodyAsUtf8(exchange, viewData);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -24,11 +24,8 @@ import io.kamax.mxisd.config.ViewConfig;
|
|||||||
import io.kamax.mxisd.http.undertow.handler.BasicHttpHandler;
|
import io.kamax.mxisd.http.undertow.handler.BasicHttpHandler;
|
||||||
import io.kamax.mxisd.session.SessionMananger;
|
import io.kamax.mxisd.session.SessionMananger;
|
||||||
import io.kamax.mxisd.threepid.session.IThreePidSession;
|
import io.kamax.mxisd.threepid.session.IThreePidSession;
|
||||||
|
import io.kamax.mxisd.util.FileUtil;
|
||||||
import io.undertow.server.HttpServerExchange;
|
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 {
|
public class RemoteSessionStartHandler extends BasicHttpHandler {
|
||||||
|
|
||||||
@@ -46,8 +43,7 @@ public class RemoteSessionStartHandler extends BasicHttpHandler {
|
|||||||
String secret = getQueryParameter(exchange, "client_secret");
|
String secret = getQueryParameter(exchange, "client_secret");
|
||||||
IThreePidSession session = mgr.createRemote(sid, secret);
|
IThreePidSession session = mgr.createRemote(sid, secret);
|
||||||
|
|
||||||
FileInputStream f = new FileInputStream(viewCfg.getSession().getRemote().getOnRequest().getSuccess());
|
String rawData = FileUtil.load(viewCfg.getSession().getRemote().getOnRequest().getSuccess());
|
||||||
String rawData = IOUtils.toString(f, StandardCharsets.UTF_8);
|
|
||||||
String data = rawData.replace("${checkLink}", RemoteIdentityAPIv1.getSessionCheck(session.getId(), session.getSecret()));
|
String data = rawData.replace("${checkLink}", RemoteIdentityAPIv1.getSessionCheck(session.getId(), session.getSecret()));
|
||||||
writeBodyAsUtf8(exchange, data);
|
writeBodyAsUtf8(exchange, data);
|
||||||
}
|
}
|
||||||
|
@@ -27,18 +27,16 @@ import io.kamax.mxisd.http.io.identity.SuccessStatusJson;
|
|||||||
import io.kamax.mxisd.http.undertow.handler.BasicHttpHandler;
|
import io.kamax.mxisd.http.undertow.handler.BasicHttpHandler;
|
||||||
import io.kamax.mxisd.session.SessionMananger;
|
import io.kamax.mxisd.session.SessionMananger;
|
||||||
import io.kamax.mxisd.session.ValidationResult;
|
import io.kamax.mxisd.session.ValidationResult;
|
||||||
|
import io.kamax.mxisd.util.FileUtil;
|
||||||
import io.undertow.server.HttpServerExchange;
|
import io.undertow.server.HttpServerExchange;
|
||||||
import io.undertow.util.HttpString;
|
import io.undertow.util.HttpString;
|
||||||
import org.apache.commons.io.IOUtils;
|
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
|
|
||||||
public class SessionValidateHandler extends BasicHttpHandler {
|
public class SessionValidateHandler extends BasicHttpHandler {
|
||||||
|
|
||||||
@@ -95,16 +93,13 @@ public class SessionValidateHandler extends BasicHttpHandler {
|
|||||||
exchange.getResponseHeaders().add(HttpString.tryFromString("Location"), url);
|
exchange.getResponseHeaders().add(HttpString.tryFromString("Location"), url);
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
|
String rawData = FileUtil.load(viewCfg.getSession().getLocalRemote().getOnTokenSubmit().getSuccess());
|
||||||
if (r.isCanRemote()) {
|
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 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);
|
String data = rawData.replace("${remoteSessionLink}", url);
|
||||||
writeBodyAsUtf8(exchange, data);
|
writeBodyAsUtf8(exchange, data);
|
||||||
} else {
|
} else {
|
||||||
FileInputStream f = new FileInputStream(viewCfg.getSession().getLocalRemote().getOnTokenSubmit().getSuccess());
|
writeBodyAsUtf8(exchange, rawData);
|
||||||
String data = IOUtils.toString(f, StandardCharsets.UTF_8);
|
|
||||||
writeBodyAsUtf8(exchange, data);
|
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
@@ -27,16 +27,12 @@ import io.kamax.mxisd.config.threepid.medium.GenericTemplateConfig;
|
|||||||
import io.kamax.mxisd.exception.InternalServerError;
|
import io.kamax.mxisd.exception.InternalServerError;
|
||||||
import io.kamax.mxisd.invitation.IThreePidInviteReply;
|
import io.kamax.mxisd.invitation.IThreePidInviteReply;
|
||||||
import io.kamax.mxisd.threepid.session.IThreePidSession;
|
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.apache.commons.lang3.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
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 {
|
public abstract class GenericTemplateNotificationGenerator extends PlaceholderNotificationGenerator implements NotificationGenerator {
|
||||||
|
|
||||||
@@ -51,14 +47,7 @@ public abstract class GenericTemplateNotificationGenerator extends PlaceholderNo
|
|||||||
|
|
||||||
private String getTemplateContent(String location) {
|
private String getTemplateContent(String location) {
|
||||||
try {
|
try {
|
||||||
URI loc = URI.create(location);
|
return FileUtil.load(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);
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new InternalServerError("Unable to read template content at " + location + ": " + e.getMessage());
|
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.notification.NotificationHandler;
|
||||||
import io.kamax.mxisd.threepid.generator.PlaceholderNotificationGenerator;
|
import io.kamax.mxisd.threepid.generator.PlaceholderNotificationGenerator;
|
||||||
import io.kamax.mxisd.threepid.session.IThreePidSession;
|
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.apache.commons.lang.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
|
|
||||||
import static com.sendgrid.SendGrid.Email;
|
import static com.sendgrid.SendGrid.Email;
|
||||||
import static com.sendgrid.SendGrid.Response;
|
import static com.sendgrid.SendGrid.Response;
|
||||||
@@ -78,7 +76,7 @@ public class EmailSendGridNotificationHandler extends PlaceholderNotificationGen
|
|||||||
|
|
||||||
private String getFromFile(String path) {
|
private String getFromFile(String path) {
|
||||||
try {
|
try {
|
||||||
return IOUtils.toString(new FileInputStream(path), StandardCharsets.UTF_8);
|
return FileUtil.load(path);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException("Couldn't create notification content using file " + path, e);
|
throw new RuntimeException("Couldn't create notification content using file " + path, e);
|
||||||
}
|
}
|
||||||
|
57
src/main/java/io/kamax/mxisd/util/FileUtil.java
Normal file
57
src/main/java/io/kamax/mxisd/util/FileUtil.java
Normal file
@@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user