Fix adding models in the webinterface
This commit is contained in:
parent
4c408501bb
commit
fe7b263d2e
|
@ -1,4 +1,11 @@
|
|||
package ctbrec.servlet;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
@ -7,60 +14,62 @@ import java.net.URLConnection;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
@Slf4j
|
||||
public class StaticFileServlet extends HttpServlet {
|
||||
|
||||
private String classPathRoot;
|
||||
private Map<String, String> mimetypes = new HashMap<>();
|
||||
private boolean contextAware = true;
|
||||
private final String classPathRoot;
|
||||
private final Map<String, String> mimetypes = new HashMap<>();
|
||||
private final boolean contextAware;
|
||||
|
||||
public StaticFileServlet(String classPathRoot) {
|
||||
this(classPathRoot, true);
|
||||
}
|
||||
|
||||
public StaticFileServlet(String classPathRoot, boolean contextAware) {
|
||||
this.classPathRoot = classPathRoot;
|
||||
this.contextAware = contextAware;
|
||||
mimetypes.put("css", "text/css");
|
||||
mimetypes.put("js", "application/javascript");
|
||||
}
|
||||
|
||||
public StaticFileServlet(String classPathRoot, boolean contextAware) {
|
||||
this(classPathRoot);
|
||||
this.contextAware = contextAware;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
String request = req.getRequestURI();
|
||||
if (contextAware) {
|
||||
String contextPath = getServletContext().getContextPath();
|
||||
request = request.substring(contextPath.length());
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
|
||||
try {
|
||||
String request = req.getRequestURI();
|
||||
if (contextAware) {
|
||||
String contextPath = getServletContext().getContextPath();
|
||||
request = request.substring(contextPath.length());
|
||||
}
|
||||
serveFile(request, resp);
|
||||
} catch (FileNotFoundException e) {
|
||||
req.getRequestDispatcher("/404").forward(req, resp);
|
||||
}
|
||||
serveFile(request, resp);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
|
||||
doGet(req, resp);
|
||||
}
|
||||
|
||||
private void serveFile(String resource, HttpServletResponse resp) throws IOException {
|
||||
InputStream resourceAsStream = getClass().getResourceAsStream(classPathRoot + resource);
|
||||
if (resourceAsStream == null) {
|
||||
throw new FileNotFoundException();
|
||||
}
|
||||
try (InputStream resourceAsStream = getClass().getResourceAsStream(classPathRoot + resource)) {
|
||||
if (resourceAsStream == null) {
|
||||
resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
||||
throw new FileNotFoundException();
|
||||
}
|
||||
|
||||
String mimetype = URLConnection.guessContentTypeFromName(resource);
|
||||
if (mimetype == null) {
|
||||
mimetype = guessMimeType(resource);
|
||||
}
|
||||
resp.setContentType(mimetype);
|
||||
resp.setStatus(HttpServletResponse.SC_OK);
|
||||
OutputStream out = resp.getOutputStream();
|
||||
int length = 0;
|
||||
byte[] buffer = new byte[1024];
|
||||
while ((length = resourceAsStream.read(buffer)) >= 0) {
|
||||
out.write(buffer, 0, length);
|
||||
String mimetype = URLConnection.guessContentTypeFromName(resource);
|
||||
if (mimetype == null) {
|
||||
mimetype = guessMimeType(resource);
|
||||
}
|
||||
resp.setContentType(mimetype);
|
||||
resp.setStatus(HttpServletResponse.SC_OK);
|
||||
OutputStream out = resp.getOutputStream();
|
||||
int length;
|
||||
byte[] buffer = new byte[1024];
|
||||
while ((length = resourceAsStream.read(buffer)) >= 0) {
|
||||
out.write(buffer, 0, length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -68,7 +77,9 @@ public class StaticFileServlet extends HttpServlet {
|
|||
try {
|
||||
String extension = resource.substring(resource.lastIndexOf('.') + 1);
|
||||
return mimetypes.get(extension);
|
||||
} catch(Exception e) {}
|
||||
} catch (Exception e) {
|
||||
// fail silently
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -326,7 +326,7 @@ public class HttpServer {
|
|||
|
||||
@Override
|
||||
protected void handleErrorPage(HttpServletRequest request, Writer writer, int code, String message) throws IOException {
|
||||
if (code == 404) {
|
||||
if (code == SC_NOT_FOUND) {
|
||||
writer.write("<html><head><title>404</title><style>* {font-family: sans-serif}</style></head><body><h1>404</h1><p>Looking for <a href=\"" + contextPath + "/static/index.html\">CTB Recorder</a>?</p></body>");
|
||||
} else {
|
||||
super.handleErrorPage(request, writer, code, message);
|
||||
|
|
|
@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||
import ctbrec.*;
|
||||
import ctbrec.io.BandwidthMeter;
|
||||
import ctbrec.io.json.ObjectMapperFactory;
|
||||
import ctbrec.io.json.dto.ModelDto;
|
||||
import ctbrec.io.json.mapper.ModelMapper;
|
||||
import ctbrec.io.json.mapper.RecordingMapper;
|
||||
import ctbrec.recorder.Recorder;
|
||||
|
@ -59,15 +60,29 @@ public class RecorderServlet extends AbstractCtbrecServlet {
|
|||
}
|
||||
|
||||
log.debug("Request: {}", json);
|
||||
RequestDto requestDto = mapper.readValue(json, RequestDto.class);
|
||||
ModelDto modelDto = requestDto.getModel();
|
||||
if (requestDto.getAction() == null) {
|
||||
sendError(resp, SC_BAD_REQUEST, "{\"status\": \"error\", \"msg\": \"action is missing\"}");
|
||||
return;
|
||||
}
|
||||
if (requestDto.getAction().startsWith("startBy")) {
|
||||
log.debug("Starting recording for model {}", modelDto.getUrl());
|
||||
if (requestDto.getAction().equals("startByUrl")) {
|
||||
startByUrl(requestDto);
|
||||
} else {
|
||||
startByName(requestDto);
|
||||
}
|
||||
var response = "{\"status\": \"success\", \"msg\": \"Recording started\"}";
|
||||
responseWriter.write(response);
|
||||
return;
|
||||
}
|
||||
|
||||
Request request = Mappers.getMapper(RequestMapper.class).toRequest(mapper.readValue(json, RequestDto.class));
|
||||
Model model = request.getModel();
|
||||
Optional.ofNullable(model).ifPresent(m -> SiteUtil.getSiteForModel(sites, m).ifPresent(m::setSite));
|
||||
Recording recording = request.getRecording();
|
||||
if (request.getAction() == null) {
|
||||
sendError(resp, SC_BAD_REQUEST, "{\"status\": \"error\", \"msg\": \"action is missing\"}");
|
||||
return;
|
||||
}
|
||||
switch (request.getAction()) {
|
||||
switch (requestDto.getAction()) {
|
||||
case "start":
|
||||
log.debug("Starting recording for model {} - {}", model.getName(), model.getUrl());
|
||||
log.trace("Model marked: {}", model.isMarkedForLaterRecording());
|
||||
|
@ -78,18 +93,6 @@ public class RecorderServlet extends AbstractCtbrecServlet {
|
|||
String response = "{\"status\": \"success\", \"msg\": \"Recording started\"}";
|
||||
responseWriter.write(response);
|
||||
break;
|
||||
case "startByUrl":
|
||||
log.debug("Starting recording for model {}", model.getUrl());
|
||||
startByUrl(request);
|
||||
response = "{\"status\": \"success\", \"msg\": \"Recording started\"}";
|
||||
responseWriter.write(response);
|
||||
break;
|
||||
case "startByName":
|
||||
log.debug("Starting recording for model {}", model.getUrl());
|
||||
startByName(request);
|
||||
response = "{\"status\": \"success\", \"msg\": \"Recording started\"}";
|
||||
responseWriter.write(response);
|
||||
break;
|
||||
case "stop":
|
||||
GlobalThreadPool.submit(() -> {
|
||||
try {
|
||||
|
@ -276,8 +279,8 @@ public class RecorderServlet extends AbstractCtbrecServlet {
|
|||
resp.getWriter().write(jsonResponse.toString());
|
||||
}
|
||||
|
||||
private void startByUrl(Request request) throws InvalidKeyException, NoSuchAlgorithmException, IOException {
|
||||
String url = request.getModel().getUrl();
|
||||
private void startByUrl(RequestDto request) throws InvalidKeyException, NoSuchAlgorithmException, IOException {
|
||||
String url = request.getModel().getUrl().toString();
|
||||
for (Site site : sites) {
|
||||
Model model = site.createModelFromUrl(url);
|
||||
if (model != null) {
|
||||
|
@ -288,8 +291,8 @@ public class RecorderServlet extends AbstractCtbrecServlet {
|
|||
throw new IllegalArgumentException("No site found to record " + url);
|
||||
}
|
||||
|
||||
private void startByName(Request request) throws InvalidKeyException, NoSuchAlgorithmException, IOException {
|
||||
String[] input = request.getModel().getUrl().split(":");
|
||||
private void startByName(RequestDto request) throws InvalidKeyException, NoSuchAlgorithmException, IOException {
|
||||
String[] input = request.getModel().getUrl().toString().split(":");
|
||||
if (input.length != 2) {
|
||||
throw new IllegalArgumentException("Invalid input. Should be site:model");
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue