View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
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   * @author Thomas Scheffler (yagee)
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          // TODO: FIX THIS: https://jersey.java.net/documentation/latest/user-guide.html#d0e8837
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      /* simple pojo for json output */
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 }