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=0753e14ebd5cf46bd4c582ee424d20f1236b7f23;hpb=490d9907457c43acfa965e7979ce5974bc1ba6ca;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 0753e14eb..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,5 +1,5 @@ /* - * Copyright (C) 2010 Mathieu Baudier + * 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. @@ -13,12 +13,14 @@ * 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.FileOutputStream; -import java.io.OutputStream; +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; @@ -41,18 +43,25 @@ 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 { @@ -73,26 +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"); + } - // Deals with provided LDIF files + // Process provided LDIF files if (ldifs.size() > 0) configuration.getLdifDirectory().mkdirs(); for (Resource ldif : ldifs) { File targetFile = new File(configuration.getLdifDirectory() .getAbsolutePath() - + File.separator + ldif.getFilename().replace(':', '_')); - OutputStream output = null; - try { - output = new FileOutputStream(targetFile); - IOUtils.copy(ldif.getInputStream(), output); - if (log.isDebugEnabled()) - log.debug("Copied " + ldif + " to LDIF directory " - + configuration.getLdifDirectory()); - } finally { - IOUtils.closeQuietly(output); - } + + File.separator + + ldif.getFilename().replace(':', '_')); + processLdif(ldif, targetFile); } Properties env = new Properties(); @@ -110,6 +120,60 @@ public class AdsContainer implements InitializingBean, DisposableBean { } } + /** + * 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); + } + } + @SuppressWarnings("unchecked") public void destroy() throws Exception { ShutdownConfiguration shutdown = new ShutdownConfiguration( @@ -164,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; + } + }