Start of web application, getSchedule rest method works

This commit is contained in:
Travis Burtrum 2017-02-28 23:31:28 -05:00
parent b293263462
commit c454eb713f
6 changed files with 112 additions and 9 deletions

10
pom.xml
View File

@ -121,6 +121,7 @@
<artifactId>logback-classic</artifactId>
<!-- version here should depend closely to version of slf4j-api above -->
<version>1.1.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.codehaus.janino</groupId>
@ -129,19 +130,22 @@
</dependency>
<!-- for webservice
<!-- for webservice -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
<version>${jersey.version}</version>
</dependency>
-->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
</dependencies>
<developers>

View File

@ -20,6 +20,8 @@
<rcrdit>
<!-- jdbc database path, sqlite/postgresql/oracle/anything jdbc should work too if proper libraries are available -->
<databaseUrl>jdbc:mysql://localhost:3306/rcrdit?user=rcrdit&amp;password=rcrdit</databaseUrl>
<!-- server uri (context path and port) for web application -->
<serverUri>http://localhost:8080/rcrdit/</serverUri>
<!-- path to xmltv.xml file normally generated by xmltv scripts from schedulesdirect or http://zap2xml.awardspace.info/ -->
<xmltvPath>/etc/xmltv.xml</xmltvPath>
<!-- all channels you recieve, one of the names from each channel from xmltv.xml above -->

View File

@ -34,14 +34,26 @@ import net.fortuna.ical4j.model.TimeZoneRegistryFactory;
import net.fortuna.ical4j.model.component.VEvent;
import net.fortuna.ical4j.model.component.VTimeZone;
import net.fortuna.ical4j.model.property.*;
import org.glassfish.grizzly.http.server.CLStaticHttpHandler;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.http.server.StaticHttpHandler;
import org.glassfish.grizzly.http.server.StaticHttpHandlerBase;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import org.moparscape.xml.impl.AbstractXmlElement;
import org.moparscape.xml.impl.XmlElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.io.File;
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.net.URI;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
@ -58,7 +70,9 @@ import java.util.stream.Collectors;
/**
* Created by mopar on 2/16/17.
*/
public class RcrdIt implements AutoCloseable {
@ApplicationPath("rest")
@Path("")
public class RcrdIt extends ResourceConfig implements AutoCloseable {
private static final Logger log = LoggerFactory.getLogger(RcrdIt.class);
@ -74,10 +88,20 @@ public class RcrdIt implements AutoCloseable {
private final List<AutoRec> autoRecs = new ArrayList<>();
private Tv schedule;
private final String serverUri;
public RcrdIt(final File cfg) throws Exception {
register(this);
final XmlElement rcrdit = AbstractXmlElement.getFactory().readFromFile(cfg);
this.databaseUrl = rcrdit.getChild("databaseUrl").getValue();
// this needs to end in /
String serverUri = rcrdit.getChild("serverUri").getValue();
if (!serverUri.endsWith("/"))
serverUri += "/";
this.serverUri = serverUri;
this.xmltvPath = rcrdit.getChild("xmltvPath").getValue();
this.allChannels = Arrays.stream(rcrdit.getChild("channels").getChildren("channel")).map(XmlElement::getValue).collect(Collectors.toSet());
@ -177,8 +201,10 @@ public class RcrdIt implements AutoCloseable {
startTimers.clear();
timer.purge();
if (schedule.getPrograms().isEmpty() || autoRecs.isEmpty())
if (schedule.getPrograms().isEmpty() || autoRecs.isEmpty()) {
log.debug("nothing to import.");
return;
}
final MessageDigest md;
try {
@ -390,6 +416,20 @@ public class RcrdIt implements AutoCloseable {
'}';
}
@GET
@Path("ping")
@Produces(MediaType.TEXT_PLAIN)
public String ping() {
return "pong";
}
@GET
@Path("getSchedule")
@Produces(MediaType.APPLICATION_JSON)
public Tv getSchedule() {
return schedule;
}
public static void main(String[] args) throws Exception {
final File cfg;
if (args.length < 1 || !((cfg = new File(args[0])).exists())) {
@ -398,13 +438,38 @@ public class RcrdIt implements AutoCloseable {
return;
}
log.debug("rcrdit starting");
final RcrdIt rcrdIt = new RcrdIt(cfg);
log.debug("rcrdit started");
final ApplicationPath ap = RcrdIt.class.getAnnotation(ApplicationPath.class);
final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(URI.create(ap == null ? rcrdIt.serverUri : rcrdIt.serverUri + ap.value() + "/"), rcrdIt, false);
final File webapp = new File("./src/main/webapp/");
final StaticHttpHandlerBase staticHttpHandler;
if (new File(webapp, "index.html").canRead()) {
//staticHttpHandler = new CLStaticHttpHandler(new URLClassLoader(new URL[]{webapp.toURI().toURL()}));
staticHttpHandler = new StaticHttpHandler(webapp.getAbsolutePath());
staticHttpHandler.setFileCacheEnabled(false); // don't cache files, because we are in development?
System.out.println("File Caching disabled!");
} else {
staticHttpHandler = new CLStaticHttpHandler(RcrdIt.class.getClassLoader()); // jar class loader, leave cache enabled
}
server.getServerConfiguration().addHttpHandler(staticHttpHandler,
rcrdIt.serverUri.replaceFirst("^[^/]+//[^/]+", "")
);
server.start();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
log.debug("Shutdown requested");
server.shutdownNow();
rcrdIt.close();
log.debug("shutdown complete, exiting...");
System.exit(0);
}));
log.debug("rcrdit started");
}
}

View File

@ -78,7 +78,7 @@ public class RecordThread extends Thread {
// new io from https://thomaswabner.wordpress.com/2007/10/09/fast-stream-copy-using-javanio-channels/
final ByteBuffer buffer = ByteBuffer.allocateDirect(bufferSize);
while(running) {
while (running) {
try (InputStream is = getInputStream.apply(recording);
ReadableByteChannel ic = Channels.newChannel(is);
) {

View File

@ -36,7 +36,9 @@
<else>
<if condition='isNull("logDir")'>
<then><property name="logDir" value="${logFile}" /></then>
<then>
<property name="logDir" value="${logFile}"/>
</then>
</if>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
@ -44,7 +46,9 @@
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>/var/log/${logDir}/archive/%d{yyyy,aux}/%d{MM,aux}/%d{dd,aux}/${logFile}-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<fileNamePattern>
/var/log/${logDir}/archive/%d{yyyy,aux}/%d{MM,aux}/%d{dd,aux}/${logFile}-%d{yyyy-MM-dd}.%i.log.gz
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 50MB -->

View File

@ -0,0 +1,28 @@
<!DOCTYPE html>
<!--
~ rcrdit records TV programs from TV tuners
~ Copyright (C) 2017 Travis Burtrum
~
~ This program is free software: you can redistribute it and/or modify
~ it under the terms of the GNU Affero General Public License as
~ published by the Free Software Foundation, either version 3 of the
~ License, or (at your option) any later version.
~
~ This program is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU Affero General Public License for more details.
~
~ You should have received a copy of the GNU Affero General Public License
~ along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Rcrdit</title>
</head>
<body>
hi
</body>
</html>