| .. | .. |
|---|
| 1 | +package net.curisit.securis.ioc; |
|---|
| 2 | + |
|---|
| 3 | +import java.io.File; |
|---|
| 4 | +import java.net.URI; |
|---|
| 5 | +import java.text.MessageFormat; |
|---|
| 6 | +import java.util.Arrays; |
|---|
| 7 | +import java.util.List; |
|---|
| 8 | +import java.util.Properties; |
|---|
| 9 | + |
|---|
| 10 | +import javafx.application.Application; |
|---|
| 11 | + |
|---|
| 12 | +import javax.inject.Named; |
|---|
| 13 | +import javax.inject.Singleton; |
|---|
| 14 | +import javax.ws.rs.core.UriBuilder; |
|---|
| 15 | +import javax.ws.rs.core.UriBuilderException; |
|---|
| 16 | + |
|---|
| 17 | +import org.slf4j.Logger; |
|---|
| 18 | +import org.slf4j.LoggerFactory; |
|---|
| 19 | + |
|---|
| 20 | +import com.google.inject.AbstractModule; |
|---|
| 21 | +import com.google.inject.Provides; |
|---|
| 22 | + |
|---|
| 23 | +public class SecurisModule extends AbstractModule { |
|---|
| 24 | + |
|---|
| 25 | + Application app = null; |
|---|
| 26 | + |
|---|
| 27 | + private static final int DEFAULT_PORT = 9997; |
|---|
| 28 | + private static final String PROPERTIES_FILE_NAME = "/server.properties"; |
|---|
| 29 | + |
|---|
| 30 | + private static final Logger log = LoggerFactory.getLogger(SecurisModule.class); |
|---|
| 31 | + |
|---|
| 32 | + public SecurisModule(Application app) { |
|---|
| 33 | + this.app = app; |
|---|
| 34 | + } |
|---|
| 35 | + |
|---|
| 36 | + public SecurisModule() { |
|---|
| 37 | + this.app = null; |
|---|
| 38 | + } |
|---|
| 39 | + |
|---|
| 40 | + @Override |
|---|
| 41 | + protected void configure() { |
|---|
| 42 | + |
|---|
| 43 | + } |
|---|
| 44 | + |
|---|
| 45 | + public String getPassword() { |
|---|
| 46 | + return getFilePassword() + " " + "cur1s1nt3grity"; |
|---|
| 47 | + } |
|---|
| 48 | + |
|---|
| 49 | + public String getFilePassword() { |
|---|
| 50 | + return "cur151T"; |
|---|
| 51 | + } |
|---|
| 52 | + |
|---|
| 53 | + public String getUrl(File appDir) { |
|---|
| 54 | + return String.format("jdbc:h2:%s/db/curisintegrity_cs;CIPHER=AES", appDir.getAbsolutePath()); |
|---|
| 55 | + } |
|---|
| 56 | + |
|---|
| 57 | + @Named("base-uri") |
|---|
| 58 | + @Provides |
|---|
| 59 | + @Singleton |
|---|
| 60 | + public URI getBaseURI() { |
|---|
| 61 | + // TODO Read from configuration, where? |
|---|
| 62 | + try { |
|---|
| 63 | + // String url = MessageFormat.format("http://{0}/", InetAddress.getLocalHost().getHostAddress()); |
|---|
| 64 | + String url = MessageFormat.format("http://{0}/", "0.0.0.0"); |
|---|
| 65 | + log.debug("Server url{}", url); |
|---|
| 66 | + return UriBuilder.fromUri(url).port(getPort()).build(); |
|---|
| 67 | + } catch (IllegalArgumentException | UriBuilderException e) { |
|---|
| 68 | + return UriBuilder.fromUri("http://localhost/").port(getPort()).build(); |
|---|
| 69 | + } |
|---|
| 70 | + } |
|---|
| 71 | + |
|---|
| 72 | + private int getPort() { |
|---|
| 73 | + Integer port; |
|---|
| 74 | + Properties prop = new Properties(); |
|---|
| 75 | + try { |
|---|
| 76 | + prop.load(getClass().getResourceAsStream(PROPERTIES_FILE_NAME)); |
|---|
| 77 | + port = Integer.valueOf(prop.getProperty("port")); |
|---|
| 78 | + if (port == null) |
|---|
| 79 | + return DEFAULT_PORT; |
|---|
| 80 | + else |
|---|
| 81 | + return port; |
|---|
| 82 | + } catch (Exception ex) { |
|---|
| 83 | + return DEFAULT_PORT; |
|---|
| 84 | + } |
|---|
| 85 | + } |
|---|
| 86 | + |
|---|
| 87 | + protected List<String> getAppDbFiles() { |
|---|
| 88 | + |
|---|
| 89 | + return Arrays.asList("/db/schema.sql"); |
|---|
| 90 | + } |
|---|
| 91 | + |
|---|
| 92 | + protected Application getApp() { |
|---|
| 93 | + return this.app; |
|---|
| 94 | + } |
|---|
| 95 | + |
|---|
| 96 | +} |
|---|