openvidu-server: JsonUtils now in charge of file to JSON

pull/331/head
pabloFuente 2019-09-27 15:42:49 +02:00
parent 28b33203c9
commit a8ffe78411
7 changed files with 48 additions and 23 deletions

View File

@ -443,7 +443,7 @@ public abstract class SessionManager {
try { try {
closeSession(sessionId, EndReason.openviduServerStopped); closeSession(sessionId, EndReason.openviduServerStopped);
} catch (Exception e) { } catch (Exception e) {
log.warn("Error closing session '{}'", sessionId, e); log.warn("Error closing session '{}': {}", sessionId, e.getMessage());
} }
} }
} }

View File

@ -19,21 +19,19 @@ package io.openvidu.server.recording;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonIOException; import com.google.gson.JsonIOException;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException; import com.google.gson.JsonSyntaxException;
import io.openvidu.client.OpenViduException; import io.openvidu.client.OpenViduException;
import io.openvidu.client.OpenViduException.Code; import io.openvidu.client.OpenViduException.Code;
import io.openvidu.server.utils.JsonUtils;
public class RecordingInfoUtils { public class RecordingInfoUtils {
private JsonParser parser;
private JsonObject json; private JsonObject json;
private JsonObject jsonFormat; private JsonObject jsonFormat;
private JsonObject videoStream; private JsonObject videoStream;
@ -44,10 +42,9 @@ public class RecordingInfoUtils {
public RecordingInfoUtils(String infoFilePath) throws FileNotFoundException, IOException, OpenViduException { public RecordingInfoUtils(String infoFilePath) throws FileNotFoundException, IOException, OpenViduException {
this.infoFilePath = infoFilePath; this.infoFilePath = infoFilePath;
this.parser = new JsonParser();
try { try {
this.json = parser.parse(new FileReader(infoFilePath)).getAsJsonObject(); this.json = new JsonUtils().fromFileToJson(infoFilePath);
} catch (JsonIOException | JsonSyntaxException e) { } catch (JsonIOException | JsonSyntaxException e) {
// Recording metadata from ffprobe is not a JSON: video file is corrupted // Recording metadata from ffprobe is not a JSON: video file is corrupted
throw new OpenViduException(Code.RECORDING_FILE_EMPTY_ERROR, "The recording file is corrupted"); throw new OpenViduException(Code.RECORDING_FILE_EMPTY_ERROR, "The recording file is corrupted");

View File

@ -34,7 +34,7 @@ public class RecordingsHttpHandler implements WebMvcConfigurer {
public void addResourceHandlers(ResourceHandlerRegistry registry) { public void addResourceHandlers(ResourceHandlerRegistry registry) {
String recordingsPath = openviduConfig.getOpenViduRecordingPath(); String recordingsPath = openviduConfig.getOpenViduRecordingPath();
recordingsPath = recordingsPath.endsWith("/") ? recordingsPath : recordingsPath + "/"; recordingsPath = recordingsPath.endsWith("/") ? recordingsPath : (recordingsPath + "/");
openviduConfig.setOpenViduRecordingPath(recordingsPath); openviduConfig.setOpenViduRecordingPath(recordingsPath);

View File

@ -327,7 +327,7 @@ public class ComposedRecordingService extends RecordingService {
} catch (IOException e) { } catch (IOException e) {
recording.setStatus(io.openvidu.java.client.Recording.Status.failed); recording.setStatus(io.openvidu.java.client.Recording.Status.failed);
throw new OpenViduException(Code.RECORDING_REPORT_ERROR_CODE, throw new OpenViduException(Code.RECORDING_REPORT_ERROR_CODE,
"There was an error generating the metadata report file for the recording"); "There was an error generating the metadata report file for the recording: " + e.getMessage());
} }
String filesPath = this.openviduConfig.getOpenViduRecordingPath() + recording.getId() + "/"; String filesPath = this.openviduConfig.getOpenViduRecordingPath() + recording.getId() + "/";

View File

@ -18,7 +18,6 @@
package io.openvidu.server.recording.service; package io.openvidu.server.recording.service;
import java.io.File; import java.io.File;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@ -48,8 +47,9 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonSyntaxException;
import io.openvidu.client.OpenViduException; import io.openvidu.client.OpenViduException;
import io.openvidu.client.OpenViduException.Code; import io.openvidu.client.OpenViduException.Code;
@ -70,6 +70,7 @@ import io.openvidu.server.recording.Recording;
import io.openvidu.server.recording.RecordingDownloader; import io.openvidu.server.recording.RecordingDownloader;
import io.openvidu.server.utils.CustomFileManager; import io.openvidu.server.utils.CustomFileManager;
import io.openvidu.server.utils.DockerManager; import io.openvidu.server.utils.DockerManager;
import io.openvidu.server.utils.JsonUtils;
public class RecordingManager { public class RecordingManager {
@ -102,6 +103,8 @@ public class RecordingManager {
protected Map<String, Recording> sessionsRecordings = new ConcurrentHashMap<>(); protected Map<String, Recording> sessionsRecordings = new ConcurrentHashMap<>();
private final Map<String, ScheduledFuture<?>> automaticRecordingStopThreads = new ConcurrentHashMap<>(); private final Map<String, ScheduledFuture<?>> automaticRecordingStopThreads = new ConcurrentHashMap<>();
private JsonUtils jsonUtils = new JsonUtils();
private ScheduledThreadPoolExecutor automaticRecordingStopExecutor = new ScheduledThreadPoolExecutor( private ScheduledThreadPoolExecutor automaticRecordingStopExecutor = new ScheduledThreadPoolExecutor(
Runtime.getRuntime().availableProcessors()); Runtime.getRuntime().availableProcessors());
@ -403,19 +406,12 @@ public class RecordingManager {
public Recording getRecordingFromEntityFile(File file) { public Recording getRecordingFromEntityFile(File file) {
if (file.isFile() && file.getName().startsWith(RecordingManager.RECORDING_ENTITY_FILE)) { if (file.isFile() && file.getName().startsWith(RecordingManager.RECORDING_ENTITY_FILE)) {
JsonObject json = null; JsonObject json;
FileReader fr = null;
try { try {
fr = new FileReader(file); json = jsonUtils.fromFileToJson(file.getAbsolutePath());
json = new JsonParser().parse(fr).getAsJsonObject(); } catch (JsonIOException | JsonSyntaxException | IOException e) {
} catch (IOException e) { log.error("Error reading recording entity file {}: {}", file.getAbsolutePath(), (e.getMessage()));
return null; return null;
} finally {
try {
fr.close();
} catch (Exception e) {
log.error("Exception while closing FileReader: {}", e.getMessage());
}
} }
Recording recording = new Recording(json); Recording recording = new Recording(json);
if (io.openvidu.java.client.Recording.Status.ready.equals(recording.getStatus()) if (io.openvidu.java.client.Recording.Status.ready.equals(recording.getStatus())

View File

@ -18,16 +18,32 @@
package io.openvidu.server.utils; package io.openvidu.server.utils;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
public class CommandExecutor { public class CommandExecutor {
public static String execCommand(String... command) throws IOException, InterruptedException { public static String execCommand(String... command) throws IOException, InterruptedException {
ProcessBuilder processBuilder = new ProcessBuilder(command); ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.redirectErrorStream(true); processBuilder.redirectErrorStream(true);
return commonExecCommand(processBuilder);
}
public static String execCommandRedirectError(File errorOutputFile, String... command)
throws IOException, InterruptedException {
ProcessBuilder processBuilder = new ProcessBuilder(command).redirectError(errorOutputFile);
return commonExecCommand(processBuilder);
}
public static String execCommandRedirectStandardOutputAndError(File standardOutputFile, File errorOutputFile,
String... command) throws IOException, InterruptedException {
ProcessBuilder processBuilder = new ProcessBuilder(command).redirectOutput(standardOutputFile)
.redirectError(errorOutputFile);
return commonExecCommand(processBuilder);
}
private static String commonExecCommand(ProcessBuilder processBuilder) throws IOException, InterruptedException {
Process process = processBuilder.start(); Process process = processBuilder.start();
StringBuilder processOutput = new StringBuilder(); StringBuilder processOutput = new StringBuilder();
@ -40,7 +56,6 @@ public class CommandExecutor {
} }
process.waitFor(); process.waitFor();
} }
return processOutput.toString().trim(); return processOutput.toString().trim();
} }

View File

@ -17,12 +17,17 @@
package io.openvidu.server.utils; package io.openvidu.server.utils;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map.Entry; import java.util.Map.Entry;
import org.kurento.jsonrpc.Props; import org.kurento.jsonrpc.Props;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
public class JsonUtils { public class JsonUtils {
@ -38,4 +43,16 @@ public class JsonUtils {
return props; return props;
} }
public JsonObject fromFileToJson(String filePath) throws JsonIOException, JsonSyntaxException, IOException {
JsonObject json;
JsonParser parser = new JsonParser();
FileReader reader = new FileReader(filePath);
try {
json = parser.parse(new FileReader(filePath)).getAsJsonObject();
} finally {
reader.close();
}
return json;
}
} }