+/*
+ * 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.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;
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<Resource> ldifs = new ArrayList<Resource>();
+ private List<String> ignoredLdifAttributes = new ArrayList<String>();
+ /** 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 {
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();
}
}
+ /**
+ * 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(
this.deleteWorkingDirOnExit = deleteWorkingDirOnExit;
}
+ public void setIgnoredLdifAttributes(List<String> ignoredLdifAttributes) {
+ this.ignoredLdifAttributes = ignoredLdifAttributes;
+ }
+
+ public void setLdifPassword(String ldifPassword) {
+ this.ldifPassword = ldifPassword;
+ }
+
+ public void setLdifPasswordAttribute(String ldifPasswordAttribute) {
+ this.ldifPasswordAttribute = ldifPasswordAttribute;
+ }
+
}