1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * See LICENSE.txt included in this distribution for the specific 9 * language governing permissions and limitations under the License. 10 * 11 * When distributing Covered Code, include this CDDL HEADER in each 12 * file and include the License file at LICENSE.txt. 13 * If applicable, add the following below this CDDL HEADER, with the 14 * fields enclosed by brackets "[]" replaced with your own identifying 15 * information: Portions Copyright [yyyy] [name of copyright owner] 16 * 17 * CDDL HEADER END 18 */ 19 20 /* 21 * Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved. 22 */ 23 package opengrok.auth.plugin.configuration; 24 25 import java.beans.XMLDecoder; 26 import java.beans.XMLEncoder; 27 import java.io.BufferedInputStream; 28 import java.io.BufferedOutputStream; 29 import java.io.ByteArrayInputStream; 30 import java.io.ByteArrayOutputStream; 31 import java.io.File; 32 import java.io.FileInputStream; 33 import java.io.IOException; 34 import java.io.InputStream; 35 import java.io.OutputStream; 36 import java.io.Serializable; 37 import java.util.ArrayList; 38 import java.util.List; 39 import opengrok.auth.plugin.ldap.LdapServer; 40 import opengrok.auth.plugin.util.WebHooks; 41 42 /** 43 * Encapsulates configuration for LDAP plugins. 44 */ 45 public class Configuration implements Serializable { 46 47 private static final long serialVersionUID = -1; 48 49 private List<LdapServer> servers = new ArrayList<>(); 50 private int interval; 51 private String searchBase; 52 private WebHooks webHooks; 53 private int searchTimeout; 54 private int connectTimeout; 55 private int readTimeout; 56 private int countLimit; 57 setServers(List<LdapServer> servers)58 public void setServers(List<LdapServer> servers) { 59 this.servers = servers; 60 } 61 getServers()62 public List<LdapServer> getServers() { 63 return servers; 64 } 65 setWebHooks(WebHooks webHooks)66 public void setWebHooks(WebHooks webHooks) { 67 this.webHooks = webHooks; 68 } 69 getWebHooks()70 public WebHooks getWebHooks() { 71 return webHooks; 72 } 73 getInterval()74 public int getInterval() { 75 return interval; 76 } 77 setInterval(int interval)78 public void setInterval(int interval) { 79 this.interval = interval; 80 } 81 getSearchTimeout()82 public int getSearchTimeout() { 83 return this.searchTimeout; 84 } 85 setSearchTimeout(int timeout)86 public void setSearchTimeout(int timeout) { 87 this.searchTimeout = timeout; 88 } 89 getConnectTimeout()90 public int getConnectTimeout() { 91 return this.connectTimeout; 92 } 93 setConnectTimeout(int timeout)94 public void setConnectTimeout(int timeout) { 95 this.connectTimeout = timeout; 96 } 97 getReadTimeout()98 public int getReadTimeout() { 99 return this.readTimeout; 100 } 101 setReadTimeout(int timeout)102 public void setReadTimeout(int timeout) { 103 this.readTimeout = timeout; 104 } 105 getCountLimit()106 public int getCountLimit() { 107 return this.countLimit; 108 } 109 setCountLimit(int limit)110 public void setCountLimit(int limit) { 111 this.countLimit = limit; 112 } 113 getSearchBase()114 public String getSearchBase() { 115 return searchBase; 116 } 117 setSearchBase(String base)118 public void setSearchBase(String base) { 119 this.searchBase = base; 120 } 121 getXMLRepresentationAsString()122 public String getXMLRepresentationAsString() { 123 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 124 this.encodeObject(bos); 125 return bos.toString(); 126 } 127 encodeObject(OutputStream out)128 private void encodeObject(OutputStream out) { 129 try (XMLEncoder e = new XMLEncoder(new BufferedOutputStream(out))) { 130 e.writeObject(this); 131 } 132 } 133 134 /** 135 * Read a configuration from a file in XML format. 136 * 137 * @param file input file 138 * @return the new configuration object 139 * @throws IOException if any error occurs 140 */ read(File file)141 public static Configuration read(File file) throws IOException { 142 try (FileInputStream in = new FileInputStream(file)) { 143 return decodeObject(in); 144 } 145 } 146 147 /** 148 * Read a configuration from a string in xml format. 149 * 150 * @param xmlconfig input string 151 * @return the new configuration object 152 * @throws IOException if any error occurs 153 */ makeXMLStringAsConfiguration(String xmlconfig)154 public static Configuration makeXMLStringAsConfiguration(String xmlconfig) throws IOException { 155 final Configuration ret; 156 final ByteArrayInputStream in = new ByteArrayInputStream(xmlconfig.getBytes()); 157 ret = decodeObject(in); 158 return ret; 159 } 160 decodeObject(InputStream in)161 private static Configuration decodeObject(InputStream in) throws IOException { 162 final Object ret; 163 164 try (XMLDecoder d = new XMLDecoder(new BufferedInputStream(in), null, null, 165 new PluginConfigurationClassLoader())) { 166 ret = d.readObject(); 167 } 168 169 if (!(ret instanceof Configuration)) { 170 throw new IOException("Not a valid configuration file"); 171 } 172 173 return (Configuration) ret; 174 } 175 } 176