1 package eu.fbk.dkm.pikes.tintop.server;
2
3 import eu.fbk.dkm.pikes.tintop.AnnotationPipeline;
4 import eu.fbk.utils.core.CommandLine;
5 import org.glassfish.grizzly.http.server.CLStaticHttpHandler;
6 import org.glassfish.grizzly.http.server.HttpServer;
7 import org.glassfish.grizzly.http.server.NetworkListener;
8 import org.slf4j.LoggerFactory;
9
10 import javax.annotation.Nullable;
11 import java.io.File;
12 import java.io.StringReader;
13 import java.util.Date;
14 import java.util.List;
15 import java.util.Properties;
16
17
18
19
20
21
22
23
24
25 public class PipelineServer {
26
27 private static final org.slf4j.Logger logger = LoggerFactory.getLogger(PipelineServer.class);
28
29 public static final String DEFAULT_HOST = "0.0.0.0";
30 public static final Integer DEFAULT_PORT = 8011;
31
32 public PipelineServer(String host, Integer port) {
33 this(host, port, null, null);
34 }
35
36 public PipelineServer(String host, Integer port, @Nullable File configFile) {
37 this(host, port, configFile, null);
38 }
39
40 public PipelineServer(String host, Integer port, @Nullable File configFile,
41 @Nullable Properties additionalProperties) {
42 logger.info("starting " + host + "\t" + port + " (" + new Date() + ")...");
43
44 AnnotationPipeline pipeline = null;
45 try {
46 pipeline = new AnnotationPipeline(configFile, additionalProperties);
47 pipeline.loadModels();
48 } catch (Exception e) {
49 e.printStackTrace();
50 logger.error(e.getMessage());
51 System.exit(1);
52 }
53
54 int timeoutInSeconds = -1;
55 try {
56 if (pipeline.getDefaultConfig().getProperty("timeout") != null) {
57 timeoutInSeconds = Integer.parseInt(pipeline.getDefaultConfig().getProperty("timeout"));
58 logger.info("Timeout set to: " + timeoutInSeconds);
59 }
60 } catch (Exception e) {
61 e.printStackTrace();
62 }
63
64
65 final HttpServer httpServer = new HttpServer();
66 NetworkListener nl = new NetworkListener("pikes-web", host, port);
67 httpServer.addListener(nl);
68
69 httpServer.getServerConfiguration().setSessionTimeoutSeconds(timeoutInSeconds);
70 httpServer.getServerConfiguration().setMaxPostSize(4194304);
71 httpServer.getServerConfiguration().addHttpHandler(new NafHandler(pipeline), "/naf");
72 httpServer.getServerConfiguration().addHttpHandler(new NafVisualizeHandler(pipeline), "/view");
73 httpServer.getServerConfiguration().addHttpHandler(new NafGenerateHandler(pipeline), "/text");
74 httpServer.getServerConfiguration().addHttpHandler(new EverythingHandler(pipeline), "/all");
75 httpServer.getServerConfiguration().addHttpHandler(new Text2NafHandler(pipeline), "/text2naf");
76 httpServer.getServerConfiguration().addHttpHandler(new TriplesHandler(pipeline), "/text2rdf");
77 httpServer.getServerConfiguration().addHttpHandler(new JsonHandler(pipeline), "/text2json");
78
79 httpServer.getServerConfiguration().addHttpHandler(
80 new CLStaticHttpHandler(HttpServer.class.getClassLoader(), "webdemo/"), "/");
81
82
83
84
85
86
87 try {
88 httpServer.start();
89 Thread.currentThread().join();
90 } catch (Exception e) {
91
92 e.printStackTrace();
93 }
94 }
95
96 public static void main(String[] args) {
97
98 try {
99 final CommandLine cmd = CommandLine
100 .parser()
101 .withName("./tintop-server")
102 .withHeader("Run the Tintop Server")
103 .withOption("c", "config", "Configuration file", "FILE", CommandLine.Type.FILE_EXISTING, true,
104 false, false)
105 .withOption("p", "port", String.format("Host port (default %d)", DEFAULT_PORT), "NUM",
106 CommandLine.Type.INTEGER, true, false, false)
107 .withOption("h", "host", String.format("Host address (default %s)", DEFAULT_HOST), "NUM",
108 CommandLine.Type.STRING, true, false, false)
109 .withOption(null, "properties", "Additional properties", "PROPS", CommandLine.Type.STRING, true,
110 true, false)
111 .withLogger(LoggerFactory.getLogger("eu.fbk")).parse(args);
112
113 String host = cmd.getOptionValue("host", String.class, DEFAULT_HOST);
114 Integer port = cmd.getOptionValue("port", Integer.class, DEFAULT_PORT);
115 File configFile = cmd.getOptionValue("config", File.class);
116
117 List<String> addProperties = cmd.getOptionValues("properties", String.class);
118
119 Properties additionalProps = new Properties();
120 for (String property : addProperties) {
121 try {
122 additionalProps.load(new StringReader(property));
123 } catch (Exception e) {
124 logger.warn(e.getMessage());
125 }
126 }
127
128 PipelineServer pipelineServer = new PipelineServer(host, port, configFile, additionalProps);
129
130 } catch (Exception e) {
131 CommandLine.fail(e);
132 }
133
134 }
135 }