1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.media.frontend.jersey;
20
21 import java.io.IOException;
22 import java.net.URISyntaxException;
23 import java.nio.file.NoSuchFileException;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.Optional;
27
28 import org.apache.logging.log4j.LogManager;
29 import org.glassfish.jersey.server.JSONP;
30 import org.mycore.datamodel.metadata.MCRObjectID;
31 import org.mycore.frontend.jersey.MCRJerseyUtil;
32 import org.mycore.media.video.MCRMediaSource;
33 import org.mycore.media.video.MCRMediaSourceProvider;
34
35 import com.google.gson.Gson;
36
37 import jakarta.servlet.http.HttpServletRequest;
38 import jakarta.ws.rs.GET;
39 import jakarta.ws.rs.Path;
40 import jakarta.ws.rs.PathParam;
41 import jakarta.ws.rs.Produces;
42 import jakarta.ws.rs.WebApplicationException;
43 import jakarta.ws.rs.core.Context;
44 import jakarta.ws.rs.core.Response.Status;
45
46
47
48
49 @Path("jwplayer")
50 public class MCRJWPlayerResource {
51 @Context
52 private HttpServletRequest request;
53
54 @GET
55 @Path("{derivateId}/{path: .+}/sources.js")
56 @Produces({ "application/javascript" })
57 @JSONP(callback = "callback", queryParam = "callback")
58 public String getSourcesAsJSONP(@PathParam("derivateId") String derivateId, @PathParam("path") String path)
59 throws URISyntaxException, IOException {
60
61 return getSources(derivateId, path);
62 }
63
64 @GET
65 @Path("{derivateId}/{path: .+}/sources.json")
66 @Produces({ "application/javascript" })
67 public String getSources(@PathParam("derivateId") String derivateId, @PathParam("path") String path)
68 throws URISyntaxException, IOException {
69 MCRObjectID derivate = MCRJerseyUtil.getID(derivateId);
70 MCRJerseyUtil.checkDerivateReadPermission(derivate);
71 try {
72 MCRMediaSourceProvider formatter = new MCRMediaSourceProvider(derivateId, path,
73 Optional.ofNullable(request.getHeader("User-Agent")),
74 () -> Arrays.stream(Optional.ofNullable(request.getQueryString()).orElse("").split("&"))
75 .filter(p -> !p.startsWith("callback="))
76 .toArray(String[]::new));
77 return toJson(formatter.getSources());
78 } catch (NoSuchFileException e) {
79 LogManager.getLogger().warn("Could not find video file.", e);
80 throw new WebApplicationException(Status.NOT_FOUND);
81 }
82 }
83
84 private String toJson(List<MCRMediaSource> sources) {
85 return new Gson().toJson(sources.stream().map(s -> new Source(s.getUri(), s.getType().getSimpleType())).toArray(
86 Source[]::new));
87 }
88
89
90 private static class Source {
91 @SuppressWarnings("unused")
92 private String file;
93
94 @SuppressWarnings("unused")
95 private String type;
96
97 Source(String file, String type) {
98 LogManager.getLogger().info("file : {}", file);
99 this.file = file;
100 this.type = type;
101 }
102 }
103
104 }