rsanchez
2014-11-14 5beef4e86aa5355342e4e128752f40c51a493765
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package net.curisit.securis;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class SeCurisServer {
    private static final Logger LOG = LogManager.getLogger(SeCurisServer.class);
    private static final Logger CONSOLE = LogManager.getLogger("console");
    //
    // private static final String PID_FILE = System.getProperty("user.home") +
    // "/.SeCuris/securis-server.pid";
    //
    // private static Server server;
    // private static Injector injector = null;
    //
    // @Inject
    // @Named("base-uri")
    // private URI uri;
    //
    // private static void showHelp() {
    // CONSOLE.info("Execute SeCuris server using:");
    // CONSOLE.info("    $ ./securis-server.sh {start|stop}");
    // }
    //
    // public static void main(String[] args) throws Exception {
    // String command;
    // if (args.length > 0) {
    // command = args[0].toLowerCase();
    // } else {
    // command = "start";
    // }
    //
    // switch (command) {
    // case "start":
    // startServer();
    // break;
    // case "stop":
    // stopServer();
    // break;
    //
    // default:
    // showHelp();
    // System.exit(-1);
    // }
    // }
    //
    // private static void stopServer() {
    // if (!new File(PID_FILE).exists()) {
    // CONSOLE.error("SeCuris server is NOT running or PID file is missing");
    // System.exit(-3);
    // }
    // try {
    // int pid = Integer.parseInt(FileUtils.readFileToString(new
    // File(PID_FILE)));
    // Runtime.getRuntime().exec("kill -SIGINT " + pid);
    // new File(PID_FILE).delete();
    // CONSOLE.info("SeCuris server process stopped sucessfully (PID: {})",
    // pid);
    // } catch (NumberFormatException | IOException e) {
    // LOG.error("Error getting SeCuris server process PID from file: {}",
    // PID_FILE);
    // }
    // }
    //
    // private static void startServer() {
    //
    // if (new File(PID_FILE).exists()) {
    // try {
    // CONSOLE.error("SeCuris server is already running with PID: {}",
    // FileUtils.readFileToString(new File(PID_FILE)));
    // } catch (IOException e) {
    // LOG.error("Unexpected error", e);
    // }
    // System.exit(-2);
    // }
    //
    // SecurisModule securisModule = new SecurisModule();
    // JpaPersistModule jpaPersistModule = new JpaPersistModule("localdb");
    // Properties props = new Properties();
    // props.put("javax.persistence.jdbc.password",
    // securisModule.getPassword());
    // props.put("javax.persistence.jdbc.url",
    // securisModule.getUrl(securisModule.getAppDir()));
    // // LOG.info("BD Url: {} {}",
    // // securisModule.getUrl(securisModule.getAppDir()),
    // // securisModule.getPassword());
    // jpaPersistModule.properties(props);
    //
    // injector = Guice.createInjector(securisModule, new RequestsModule(),
    // jpaPersistModule);
    //
    // try {
    // startServer(injector.getInstance(Key.get(URI.class,
    // Names.named("base-uri"))));
    //
    // } catch (SeCurisException e) {
    // CONSOLE.error("Error launching the SeCuris server, {}", e);
    // }
    // }
    //
    // private static void savePID() throws SeCurisException {
    // String runtimeName = ManagementFactory.getRuntimeMXBean().getName();
    // // runtimeName contains something like: "12345@localhost"
    // String pid = runtimeName.substring(0, runtimeName.indexOf('@'));
    // try {
    // FileUtils.writeStringToFile(new File(PID_FILE), pid);
    // CONSOLE.info("SeCuris server process started sucessfully (PID: {})",
    // pid);
    // } catch (IOException e) {
    // LOG.error("Error saving pid file", e);
    // throw new SeCurisException("Error saving pid file");
    // }
    // }
    //
    // private static void startServer(URI uri) throws SeCurisException {
    // System.out.println("Starting jetty...");
    //
    // QueuedThreadPool threadPool = new QueuedThreadPool();
    // threadPool.setMaxThreads(50);
    //
    // server = new Server();
    //
    // ServerConnector httpConnector = new ServerConnector(server);
    // httpConnector.setPort(Config.getInt(Config.KEYS.SERVER_PORT, 9080));
    // httpConnector.setHost(Config.get(Config.KEYS.SERVER_HOSTNAME,
    // "0.0.0.0"));
    // server.addConnector(httpConnector);
    //
    // ServletContextHandler context = new
    // ServletContextHandler(ServletContextHandler.SESSIONS);
    // context.setContextPath("/");
    // context.addEventListener(injector.getInstance(GuiceResteasyBootstrapServletContextListener.class));
    //
    // context.setInitParameter("resteasy.role.based.security", "true");
    // context.setInitParameter("resteasy.providers",
    // DefaultExceptionHandler.class.getName());
    // context.addFilter(new
    // FilterHolder(injector.getInstance(PersistFilter.class)), "/*", null);
    // ServletHolder sh = new ServletHolder(HttpServletDispatcher.class);
    // sh.setName("resteasy");
    // context.addServlet(sh, "/*");
    //
    // ResourceHandler staticResources = new ResourceHandler();
    // try {
    // staticResources.setBaseResource(Resource.newResource(SeCurisServer.class.getResource("/static").toURI()));
    // } catch (IOException | URISyntaxException e) {
    // LOG.error("Error configuring static resources", e);
    // throw new SeCurisException("Error configuring static resources");
    // }
    // staticResources.setWelcomeFiles(new String[] {
    // "/main.html"
    // });
    // context.setHandler(staticResources);
    //
    // ErrorPageErrorHandler errorHandler = new ErrorPageErrorHandler();
    // context.setErrorHandler(errorHandler);
    // ContextHandlerCollection contexts = new ContextHandlerCollection();
    //
    // contexts.setHandlers(new Handler[] {
    // staticResources, context
    // });
    //
    // HttpConfiguration http_config = new HttpConfiguration();
    // http_config.setSecureScheme("https");
    // http_config.setSecurePort(Config.getInt(Config.KEYS.SERVER_SSL_PORT,
    // 9443));
    // http_config.setOutputBufferSize(32768);
    // http_config.setSendServerVersion(true);
    // http_config.setSendDateHeader(false);
    //
    // HttpConfiguration https_config = new HttpConfiguration(http_config);
    // https_config.addCustomizer(new SecureRequestCustomizer());
    //
    // SslContextFactory sslContextFactory = new SslContextFactory();
    // sslContextFactory.setKeyStorePath(Config.get(Config.KEYS.KEYSTORE_PATH));
    // sslContextFactory.setKeyStoreType(Config.get(Config.KEYS.KEYSTORE_TYPE,
    // "JKS"));
    // sslContextFactory.setKeyStorePassword(Config.get(Config.KEYS.KEYSTORE_PASSWORD,
    // ""));
    // // sslContextFactory.setCertAlias("1");
    // // sslContextFactory.setKeyManagerPassword("curist3c");
    // // sslContextFactory.setTrustStorePath("/Users/rob/.ssh/keys/keystore");
    // // sslContextFactory.setTrustStorePassword("curist3c");
    // sslContextFactory.checkKeyStore();
    // sslContextFactory.setNeedClientAuth(false);
    //
    // ServerConnector sslConnector = new ServerConnector(server, new
    // SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
    // new HttpConnectionFactory(https_config));
    // sslConnector.setPort(Config.getInt(Config.KEYS.SERVER_SSL_PORT, 9443));
    // sslConnector.setHost(Config.get(Config.KEYS.SERVER_HOSTNAME, "0.0.0.0"));
    // server.addConnector(sslConnector);
    //
    // server.setHandler(context);
    // server.setStopAtShutdown(true);
    // server.addLifeCycleListener(new ServerStoppedListener());
    // try {
    // server.start();
    // savePID();
    // CONSOLE.info("Server running in: {}", String.format("http://%s:%d",
    // httpConnector.getHost(), httpConnector.getPort()));
    // server.join();
    // } catch (Exception e) {
    // LOG.error("Error starting SeCurisServer", e);
    // throw new SeCurisException("Error starting SeCurisServer");
    // }
    //
    // }
    //
    // static class ServerStoppedListener extends AbstractLifeCycleListener {
    // @Override
    // public void lifeCycleStopped(LifeCycle event) {
    // if (new File(PID_FILE).exists())
    // new File(PID_FILE).delete();
    // }
    // }
}