package com.perforce.workspace.tjuricek.p4oauth.config; import com.esotericsoftware.yamlbeans.YamlException; import com.esotericsoftware.yamlbeans.YamlReader; import org.apache.commons.io.IOUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; /** * Top level configuration object. The main application configuration file maps * to this object. * <p/> * Configuration objects must be loaded via the static load method. * * @see P4OAuthConfig#load(String) */ public class P4OAuthConfig { private static final Logger logger = LogManager.getLogger(P4OAuthConfig.class); private String contextRoot = ""; private String hostname = "localhost"; // Very likely illegal with your SSL config private int httpPort = 8080; private long httpIdleTimeout = 5000; private int httpsPort = 8443; private long httpsIdleTimeout = 15000; private String keyStorePath; private String keyStorePassword; private String keyManagerPassword; private String rootDirectory; private boolean cacheTemplates = true; private String templateDirectory; private PerforceConfig perforce; /** * Disable default construction: this must be loaded via the static load * method. */ private P4OAuthConfig() { } /** * Double checks that the configuration is actually set, and if there's any * issues at all, to log them. */ public boolean validate() { boolean valid = true; if (keyStorePath == null) { logger.error("keyStorePath missing in configuration"); valid = false; } if (keyStorePassword == null) { logger.error("keyStorePassword missing in configuration"); valid = false; } if (keyManagerPassword == null) { logger.error("keyManagerPassword missing in configuration"); valid = false; } return valid; } public String getHostname() { return hostname; } public void setHostname(String hostname) { this.hostname = hostname; } /** * Defaults to 8080, the standard (non-SSL) HTTP port. This may be used by * some informative usages, say, for basic help pages. */ public int getHttpPort() { return httpPort; } public void setHttpPort(int httpPort) { this.httpPort = httpPort; } /** * Defaults to 8443, the HTTPS port where almost all real features take * place. */ public int getHttpsPort() { return httpsPort; } public void setHttpsPort(int httpsPort) { this.httpsPort = httpsPort; } /** * By default, empty, can be set to a root context path. */ public String getContextRoot() { return contextRoot; } public void setContextRoot(String contextRoot) { this.contextRoot = contextRoot; } public long getHttpIdleTimeout() { return httpIdleTimeout; } public void setHttpIdleTimeout(long httpIdleTimeout) { this.httpIdleTimeout = httpIdleTimeout; } public String getKeyStorePath() { return keyStorePath; } public void setKeyStorePath(String keyStorePath) { this.keyStorePath = keyStorePath; } public String getKeyStorePassword() { return keyStorePassword; } public void setKeyStorePassword(String keyStorePassword) { this.keyStorePassword = keyStorePassword; } public String getKeyManagerPassword() { return keyManagerPassword; } public void setKeyManagerPassword(String keyManagerPassword) { this.keyManagerPassword = keyManagerPassword; } public long getHttpsIdleTimeout() { return httpsIdleTimeout; } public void setHttpsIdleTimeout(long httpsIdleTimeout) { this.httpsIdleTimeout = httpsIdleTimeout; } public String getRootDirectory() { return rootDirectory; } public void setRootDirectory(String rootDirectory) { this.rootDirectory = rootDirectory; } public boolean isCacheTemplates() { return cacheTemplates; } public void setCacheTemplates(boolean cacheTemplates) { this.cacheTemplates = cacheTemplates; } /** * If you've set cacheTemplates to false, you'll likely need this set to * the resource folder, because otherwise it's likely that the classpath * loaders will cache the data of your templates anyway. */ public String getTemplateDirectory() { return templateDirectory; } public void setTemplateDirectory(String templateDirectory) { this.templateDirectory = templateDirectory; } public PerforceConfig getPerforce() { return perforce; } public void setPerforce(PerforceConfig perforce) { this.perforce = perforce; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; P4OAuthConfig that = (P4OAuthConfig) o; if (cacheTemplates != that.cacheTemplates) return false; if (httpIdleTimeout != that.httpIdleTimeout) return false; if (httpPort != that.httpPort) return false; if (httpsIdleTimeout != that.httpsIdleTimeout) return false; if (httpsPort != that.httpsPort) return false; if (contextRoot != null ? !contextRoot.equals(that.contextRoot) : that.contextRoot != null) return false; if (hostname != null ? !hostname.equals(that.hostname) : that.hostname != null) return false; if (keyManagerPassword != null ? !keyManagerPassword.equals(that.keyManagerPassword) : that.keyManagerPassword != null) return false; if (keyStorePassword != null ? !keyStorePassword.equals(that.keyStorePassword) : that.keyStorePassword != null) return false; if (keyStorePath != null ? !keyStorePath.equals(that.keyStorePath) : that.keyStorePath != null) return false; if (perforce != null ? !perforce.equals(that.perforce) : that.perforce != null) return false; if (rootDirectory != null ? !rootDirectory.equals(that.rootDirectory) : that.rootDirectory != null) return false; if (templateDirectory != null ? !templateDirectory.equals(that.templateDirectory) : that.templateDirectory != null) return false; return true; } @Override public int hashCode() { int result = contextRoot != null ? contextRoot.hashCode() : 0; result = 31 * result + (hostname != null ? hostname.hashCode() : 0); result = 31 * result + httpPort; result = 31 * result + (int) (httpIdleTimeout ^ (httpIdleTimeout >>> 32)); result = 31 * result + httpsPort; result = 31 * result + (int) (httpsIdleTimeout ^ (httpsIdleTimeout >>> 32)); result = 31 * result + (keyStorePath != null ? keyStorePath.hashCode() : 0); result = 31 * result + (keyStorePassword != null ? keyStorePassword.hashCode() : 0); result = 31 * result + (keyManagerPassword != null ? keyManagerPassword.hashCode() : 0); result = 31 * result + (rootDirectory != null ? rootDirectory.hashCode() : 0); result = 31 * result + (cacheTemplates ? 1 : 0); result = 31 * result + (templateDirectory != null ? templateDirectory.hashCode() : 0); result = 31 * result + (perforce != null ? perforce.hashCode() : 0); return result; } @Override public String toString() { return "P4OAuthConfig{" + "contextRoot='" + contextRoot + '\'' + ", hostname='" + hostname + '\'' + ", httpPort=" + httpPort + ", httpIdleTimeout=" + httpIdleTimeout + ", httpsPort=" + httpsPort + ", httpsIdleTimeout=" + httpsIdleTimeout + ", keyStorePath='" + keyStorePath + '\'' + ", rootDirectory='" + rootDirectory + '\'' + ", cacheTemplates=" + cacheTemplates + ", templateDirectory='" + templateDirectory + '\'' + ", perforce=" + perforce + '}'; } /** * Search rules are: * <p/> * <ol> * <li>Direct reference (probably a program argument)</li> * <li>An xml file indicated by the <code>P4OAuthConfig</code> system property</li> * <li>A file named P4OAuthConfig.yml in the current working directory</li> * </ol> * <p/> * Note that you <em>must</em> configure the SSL settings at a minimum. * * @param path Path to a YAML configuration file. Can be null if you expect * the file to be configured * @return null in the case of failures, otherwise, the config object. * See the log file for the error warning. */ public static P4OAuthConfig load(String path) { P4OAuthConfig config = new P4OAuthConfig(); if (path == null) { path = System.getProperty("P4OAuthConfig"); } if (path == null) { File local = new File("P4OAuthConfig.yml"); if (local.exists()) { path = local.getAbsolutePath(); } } if (path != null) { FileReader reader = null; try { reader = new FileReader(path); YamlReader yamlReader = new YamlReader(reader); config = yamlReader.read(P4OAuthConfig.class); } catch (FileNotFoundException e) { logger.error("Problem reading path " + path, e); config = null; } catch (YamlException e) { logger.error("Yaml parsing failed for " + path, e); config = null; } finally { IOUtils.closeQuietly(reader); } } // Any errors found by validate will just be logged. if (config != null && !config.validate()) { config = null; } return config; } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 9095 | tjuricek | Added some basic test data and renamed "workspace" to "workshop" in package name | ||
#1 | 9089 | tjuricek |
Moving some code that worked via some manual validation to the workshop. This just implements a basic code authorization grant scheme. Automated tests are forthcoming, awating some gradle plugin work that should sit outside of this project. |