X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=server%2Fruntime%2Forg.argeo.server.ads%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fserver%2Fads%2FAdsContainer.java;h=ce6e137c39c8fc40e8f42fdb0c61267718a08999;hb=1d5afdce3e91054f07ddd3c98309c363b4cf1d46;hp=4a36f721db3c749c133fede75eb40e5c5dafac5d;hpb=c6c3aa132f6559ac1e2fa015cf2efba92802eba0;p=lgpl%2Fargeo-commons.git diff --git a/server/runtime/org.argeo.server.ads/src/main/java/org/argeo/server/ads/AdsContainer.java b/server/runtime/org.argeo.server.ads/src/main/java/org/argeo/server/ads/AdsContainer.java index 4a36f721d..ce6e137c3 100644 --- a/server/runtime/org.argeo.server.ads/src/main/java/org/argeo/server/ads/AdsContainer.java +++ b/server/runtime/org.argeo.server.ads/src/main/java/org/argeo/server/ads/AdsContainer.java @@ -1,6 +1,26 @@ +/* + * Copyright (C) 2007-2012 Mathieu Baudier + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.argeo.server.ads; +import java.io.BufferedReader; import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Writer; import java.util.ArrayList; import java.util.List; import java.util.Properties; @@ -11,27 +31,37 @@ import javax.naming.NamingException; import javax.naming.directory.InitialDirContext; import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.directory.server.configuration.MutableServerStartupConfiguration; import org.apache.directory.server.core.configuration.ShutdownConfiguration; import org.apache.directory.server.jndi.ServerContextFactory; +import org.argeo.ArgeoException; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.core.io.Resource; import org.springframework.util.Assert; +/** Wraps an Apache Directory Server instance. */ +@SuppressWarnings("restriction") public class AdsContainer implements InitializingBean, DisposableBean { private final static Log log = LogFactory.getLog(AdsContainer.class); private MutableServerStartupConfiguration configuration; private Properties environment = null; - private File workingDirectory = new File(System - .getProperty("java.io.tmpdir") - + File.separator + "argeo-apacheDirectoryServer"); + private File workingDirectory = new File( + System.getProperty("java.io.tmpdir") + File.separator + + "argeo-apacheDirectoryServer"); + private Boolean deleteWorkingDirOnExit = false; + + // LDIF private List ldifs = new ArrayList(); + private List ignoredLdifAttributes = new ArrayList(); + /** default is 'demo' */ + private String ldifPassword = "e1NIQX1pZVNWNTVRYytlUU9hWURSU2hhL0Fqek5USkU9"; + private String ldifPasswordAttribute = "userPassword"; private File ldifDirectory; - private Boolean deleteWorkingDirOnExit = false; @SuppressWarnings("unchecked") public void afterPropertiesSet() throws Exception { @@ -52,18 +82,27 @@ public class AdsContainer implements InitializingBean, DisposableBean { configuration.setLdifDirectory(ldifDirectory); else configuration.setLdifDirectory(new File(workingDirectory - .getAbsolutePath() - + File.separator + "ldif")); + .getAbsolutePath() + File.separator + "ldif")); + + if (ignoredLdifAttributes.size() == 0) { + ignoredLdifAttributes.add("entryUUID"); + ignoredLdifAttributes.add("structuralObjectClass"); + ignoredLdifAttributes.add("creatorsName"); + ignoredLdifAttributes.add("createTimestamp"); + ignoredLdifAttributes.add("entryCSN"); + ignoredLdifAttributes.add("modifiersName"); + ignoredLdifAttributes.add("modifyTimestamp"); + } + // Process provided LDIF files if (ldifs.size() > 0) configuration.getLdifDirectory().mkdirs(); for (Resource ldif : ldifs) { - FileUtils.copyURLToFile(ldif.getURL(), new File(configuration - .getLdifDirectory().getAbsolutePath() - + File.separator + ldif.getFilename().replace(':', '_'))); - if (log.isDebugEnabled()) - log.debug("Copied " + ldif + " to LDIF directory " - + configuration.getLdifDirectory()); + File targetFile = new File(configuration.getLdifDirectory() + .getAbsolutePath() + + File.separator + + ldif.getFilename().replace(':', '_')); + processLdif(ldif, targetFile); } Properties env = new Properties(); @@ -76,8 +115,62 @@ public class AdsContainer implements InitializingBean, DisposableBean { try { new InitialDirContext(env); } catch (NamingException e) { - throw new RuntimeException( - "Failed to start Apache Directory server", e); + throw new ArgeoException("Failed to start Apache Directory server", + e); + } + } + + /** + * Processes an LDIF resource, filtering out attributes that cannot be + * imported in ADS and forcing a password. + */ + protected void processLdif(Resource ldif, File targetFile) { + BufferedReader reader = null; + Writer writer = null; + try { + reader = new BufferedReader(new InputStreamReader( + ldif.getInputStream())); + writer = new FileWriter(targetFile); + String line = null; + lines: while ((line = reader.readLine()) != null) { + // comment and empty lines + if (line.trim().equals("") || line.startsWith("#")) { + writer.write(line); + writer.write('\n'); + continue lines; + } + + String[] tokens = line.split(":"); + String attribute = null; + if (tokens != null && tokens.length > 1) { + attribute = tokens[0].trim(); + if (ignoredLdifAttributes.contains(attribute)) + continue lines;// ignore + + if (attribute.equals("bdb_db_open")) { + log.warn("Ignored OpenLDAP output\n" + line); + continue lines; + } + + if (ldifPassword != null + && attribute.equals(ldifPasswordAttribute)) { + line = ldifPasswordAttribute + ":: " + ldifPassword; + } + + writer.write(line); + writer.write('\n'); + } else { + log.warn("Ignored LDIF line\n" + line); + } + } + if (log.isDebugEnabled()) + log.debug("Processed " + ldif + " to LDIF directory " + + configuration.getLdifDirectory()); + } catch (IOException e) { + throw new ArgeoException("Cannot process LDIF " + ldif, e); + } finally { + IOUtils.closeQuietly(reader); + IOUtils.closeQuietly(writer); } } @@ -99,8 +192,8 @@ public class AdsContainer implements InitializingBean, DisposableBean { try { new InitialContext(env); } catch (NamingException e) { - throw new RuntimeException( - "Failed to stop Apache Directory server", e); + throw new ArgeoException("Failed to stop Apache Directory server", + e); } if (workingDirectory.exists() && deleteWorkingDirOnExit) { @@ -135,4 +228,16 @@ public class AdsContainer implements InitializingBean, DisposableBean { this.deleteWorkingDirOnExit = deleteWorkingDirOnExit; } + public void setIgnoredLdifAttributes(List ignoredLdifAttributes) { + this.ignoredLdifAttributes = ignoredLdifAttributes; + } + + public void setLdifPassword(String ldifPassword) { + this.ldifPassword = ldifPassword; + } + + public void setLdifPasswordAttribute(String ldifPasswordAttribute) { + this.ldifPasswordAttribute = ldifPasswordAttribute; + } + }