View Javadoc
1   /*
2    * Copyright 2018 the original author or authors.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package org.openehealth.ipf.commons.audit;
18  
19  import io.vertx.core.AbstractVerticle;
20  import io.vertx.core.http.ClientAuth;
21  import io.vertx.core.net.JksOptions;
22  import io.vertx.core.net.NetServer;
23  import io.vertx.core.net.NetServerOptions;
24  import io.vertx.ext.unit.Async;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  /**
29   *
30   */
31  public class TCPSyslogServer extends AbstractVerticle {
32  
33      private static final Logger LOG = LoggerFactory.getLogger(TCPSyslogServer.class);
34  
35      private final int port;
36  
37      private final NetServerOptions nsOptions;
38  
39      private final Async async;
40  
41      public TCPSyslogServer(int port, Async async){
42          this.port = port;
43          this.async = async;
44          nsOptions = new NetServerOptions()
45                  .setReuseAddress(true)
46                  .setHost("localhost")
47                  .setSsl(false);
48      }
49  
50      public TCPSyslogServer(int port, String clientAuth,
51                             String trustStorePath, String trustStorePassword,
52                             String keyStorePath, String keyStorePassword,
53                             Async async){
54          this.port = port;
55          this.async = async;
56          nsOptions = new NetServerOptions()
57                  .setReuseAddress(true)
58                  .setHost("localhost")
59                  .setClientAuth(ClientAuth.valueOf(clientAuth))
60                  .setTrustStoreOptions(trustStorePath != null? new JksOptions().
61                          setPath(trustStorePath).
62                          setPassword(trustStorePassword): null)
63                  .setKeyStoreOptions(keyStorePath != null? new JksOptions().
64                          setPath(keyStorePath).
65                          setPassword(keyStorePassword): null)
66                  .setSsl(true);
67      }
68  
69      @Override
70      public void start() {
71          NetServer netServer = vertx.createNetServer(nsOptions);
72          netServer.connectHandler(netSocket -> netSocket.handler(buffer -> {
73              LOG.debug("Received content on port {} ({}) : {}", port, async.count(), buffer.toString());
74              async.countDown();
75          })).listen(port);
76      }
77  }