From 3ca64d9cca6792078816d38fa45064382a3983a7 Mon Sep 17 00:00:00 2001 From: pabloFuente Date: Wed, 19 Jun 2019 11:04:37 +0200 Subject: [PATCH] openvidu-server: server CDR files under /cdr/FILE.log. REST GET /cdr --- .../openvidu/server/cdr/CDRHttpHandler.java | 40 ++++++++++ .../server/config/SecurityConfig.java | 7 +- .../server/rest/CDRRestController.java | 79 +++++++++++++++++++ 3 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 openvidu-server/src/main/java/io/openvidu/server/cdr/CDRHttpHandler.java create mode 100644 openvidu-server/src/main/java/io/openvidu/server/rest/CDRRestController.java diff --git a/openvidu-server/src/main/java/io/openvidu/server/cdr/CDRHttpHandler.java b/openvidu-server/src/main/java/io/openvidu/server/cdr/CDRHttpHandler.java new file mode 100644 index 00000000..f5791c28 --- /dev/null +++ b/openvidu-server/src/main/java/io/openvidu/server/cdr/CDRHttpHandler.java @@ -0,0 +1,40 @@ +/* + * (C) Copyright 2017-2019 OpenVidu (https://openvidu.io/) + * + * Licensed 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 io.openvidu.server.cdr; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import io.openvidu.server.config.OpenviduConfig; + +@Configuration +public class CDRHttpHandler implements WebMvcConfigurer { + + @Autowired + OpenviduConfig openviduConfig; + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + String cdrPath = openviduConfig.getOpenviduCdrPath(); + cdrPath = cdrPath.endsWith("/") ? cdrPath : cdrPath + "/"; + registry.addResourceHandler("/cdr/**.log").addResourceLocations("file:" + cdrPath); + } + +} diff --git a/openvidu-server/src/main/java/io/openvidu/server/config/SecurityConfig.java b/openvidu-server/src/main/java/io/openvidu/server/config/SecurityConfig.java index 521f720f..1ad06ecb 100644 --- a/openvidu-server/src/main/java/io/openvidu/server/config/SecurityConfig.java +++ b/openvidu-server/src/main/java/io/openvidu/server/config/SecurityConfig.java @@ -50,9 +50,11 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { .antMatchers(HttpMethod.POST, "/api/recordings/start").authenticated() .antMatchers(HttpMethod.POST, "/api/recordings/stop").authenticated() .antMatchers(HttpMethod.DELETE, "/api/recordings/**").authenticated() - // /api/config + // /config .antMatchers(HttpMethod.GET, "/config/openvidu-publicurl").permitAll() .antMatchers(HttpMethod.GET, "/config/**").authenticated() + // /cdr + .antMatchers(HttpMethod.GET, "/cdr/**").authenticated() // Dashboard .antMatchers("/").authenticated(); @@ -71,7 +73,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { - auth.inMemoryAuthentication().withUser("OPENVIDUAPP").password("{noop}" + openviduConf.getOpenViduSecret()).roles("ADMIN"); + auth.inMemoryAuthentication().withUser("OPENVIDUAPP").password("{noop}" + openviduConf.getOpenViduSecret()) + .roles("ADMIN"); } } \ No newline at end of file diff --git a/openvidu-server/src/main/java/io/openvidu/server/rest/CDRRestController.java b/openvidu-server/src/main/java/io/openvidu/server/rest/CDRRestController.java new file mode 100644 index 00000000..df9e62cc --- /dev/null +++ b/openvidu-server/src/main/java/io/openvidu/server/rest/CDRRestController.java @@ -0,0 +1,79 @@ +/* + * (C) Copyright 2017-2019 OpenVidu (https://openvidu.io/) + * + * Licensed 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 io.openvidu.server.rest; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import com.google.gson.JsonArray; + +import io.openvidu.server.config.OpenviduConfig; + +/** + * + * @author Pablo Fuente (pablofuenteperez@gmail.com) + */ +@RestController +@CrossOrigin +@RequestMapping("/cdr") +public class CDRRestController { + + private static final Logger log = LoggerFactory.getLogger(CDRRestController.class); + + @Autowired + protected OpenviduConfig openviduConfig; + + @RequestMapping(method = RequestMethod.GET) + public ResponseEntity listCdrFiles() { + + log.info("REST API: GET /cdr"); + + String cdrPath = openviduConfig.getOpenviduCdrPath(); + JsonArray cdrFiles = new JsonArray(); + + try (Stream walk = Files.walk(Paths.get(cdrPath))) { + List result = walk.filter(Files::isRegularFile).map(x -> x.getFileName().toString()) + .collect(Collectors.toList()); + result.forEach(fileName -> cdrFiles.add(fileName)); + } catch (IOException e) { + log.error("Error listing CDR files in path {}: {}", cdrPath, e.getMessage()); + } + + HttpHeaders responseHeaders = new HttpHeaders(); + responseHeaders.setContentType(MediaType.APPLICATION_JSON); + return new ResponseEntity<>(cdrFiles.toString(), responseHeaders, HttpStatus.OK); + } + +}