AutoRefactor'ed use try-with-resources
| | |
| | | * information: "Portions Copyright [year] [name of copyright owner]". |
| | | * |
| | | * Copyright 2008 Sun Microsystems, Inc. |
| | | * Portions copyright 2014-2015 ForgeRock AS. |
| | | * Portions copyright 2014-2016 ForgeRock AS. |
| | | */ |
| | | |
| | | package org.forgerock.opendj.config; |
| | | |
| | | import static org.forgerock.util.Utils.closeSilently; |
| | | |
| | | import java.io.BufferedInputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | |
| | | throw new MissingResourceException("Can't find resource " + path, baseName, ""); |
| | | } |
| | | |
| | | final InputStream is = new BufferedInputStream(stream); |
| | | p = new Properties(); |
| | | try { |
| | | try (InputStream is = new BufferedInputStream(stream)) { |
| | | p.load(is); |
| | | } catch (IOException e) { |
| | | throw new MissingResourceException("Can't load resource " + path |
| | | + " due to IO exception: " + e.getMessage(), baseName, ""); |
| | | } finally { |
| | | closeSilently(is); |
| | | } |
| | | |
| | | // Cache the resource. |
| | |
| | | |
| | | import static com.forgerock.opendj.ldap.config.ConfigMessages.*; |
| | | import static com.forgerock.opendj.util.StaticUtils.stackTraceToSingleLineString; |
| | | import static org.forgerock.util.Utils.closeSilently; |
| | | |
| | | import java.io.BufferedReader; |
| | | import java.io.File; |
| | |
| | | public static BuildVersion instanceVersion() throws ConfigException { |
| | | final String buildInfo = ConfigurationFramework.getInstance().getInstancePath() + File.separator + "config" |
| | | + File.separator + "buildinfo"; |
| | | BufferedReader reader = null; |
| | | try { |
| | | reader = new BufferedReader(new FileReader(buildInfo)); |
| | | try (BufferedReader reader = new BufferedReader(new FileReader(buildInfo))) { |
| | | final String s = reader.readLine(); |
| | | if (s != null) { |
| | | return valueOf(s); |
| | |
| | | throw new ConfigException(ERR_BUILDVERSION_NOT_FOUND.get(buildInfo)); |
| | | } catch (final IllegalArgumentException e) { |
| | | throw new ConfigException(ERR_BUILDVERSION_MALFORMED.get(buildInfo)); |
| | | } finally { |
| | | closeSilently(reader); |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | import static org.forgerock.opendj.config.PropertyOption.*; |
| | | import static org.forgerock.opendj.config.dsconfig.ArgumentExceptionFactory.*; |
| | | import static org.forgerock.util.Utils.*; |
| | | |
| | | import java.io.BufferedReader; |
| | | import java.io.BufferedWriter; |
| | | import java.io.File; |
| | | import java.io.FileNotFoundException; |
| | | import java.io.FileReader; |
| | | import java.io.FileWriter; |
| | | import java.io.IOException; |
| | |
| | | } |
| | | if (equivalentCommandFileArgument.isPresent()) { |
| | | String file = equivalentCommandFileArgument.getValue(); |
| | | BufferedWriter writer = null; |
| | | try { |
| | | writer = new BufferedWriter(new FileWriter(file, true)); |
| | | |
| | | try (BufferedWriter writer = new BufferedWriter(new FileWriter(file, true))) { |
| | | if (!sessionStartTimePrinted) { |
| | | writer.write(SHELL_COMMENT_SEPARATOR + getSessionStartTimeMessage()); |
| | | writer.newLine(); |
| | |
| | | writer.flush(); |
| | | } catch (IOException ioe) { |
| | | errPrintln(ERR_DSCFG_ERROR_WRITING_EQUIVALENT_COMMAND_LINE.get(file, ioe)); |
| | | } finally { |
| | | closeSilently(writer); |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | |
| | | private void handleBatch(String[] args) { |
| | | BufferedReader bReader = null; |
| | | try { |
| | | if (batchArgument.isPresent()) { |
| | | bReader = new BufferedReader(new InputStreamReader(System.in)); |
| | | } else if (batchFileArgument.isPresent()) { |
| | | final String batchFilePath = batchFileArgument.getValue().trim(); |
| | | bReader = new BufferedReader(new FileReader(batchFilePath)); |
| | | } else { |
| | | throw new IllegalArgumentException("Either --" + OPTION_LONG_BATCH |
| | | + " or --" + OPTION_LONG_BATCH_FILE_PATH + " argument should have been set"); |
| | | } |
| | | |
| | | try (BufferedReader bReader = batchCommandsReader()) { |
| | | List<String> initialArgs = removeBatchArgs(args); |
| | | |
| | | // Split the CLI string into arguments array |
| | |
| | | } |
| | | } catch (IOException ex) { |
| | | errPrintln(ERR_DSCFG_ERROR_READING_BATCH_FILE.get(ex)); |
| | | } finally { |
| | | closeSilently(bReader); |
| | | } |
| | | } |
| | | |
| | | private BufferedReader batchCommandsReader() throws FileNotFoundException { |
| | | if (batchArgument.isPresent()) { |
| | | return new BufferedReader(new InputStreamReader(System.in)); |
| | | } else if (batchFileArgument.isPresent()) { |
| | | final String batchFilePath = batchFileArgument.getValue().trim(); |
| | | return new BufferedReader(new FileReader(batchFilePath)); |
| | | } else { |
| | | throw new IllegalArgumentException("Either --" + OPTION_LONG_BATCH |
| | | + " or --" + OPTION_LONG_BATCH_FILE_PATH + " argument should have been set"); |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | @Test |
| | | public final void testHasNextWhenError() throws Exception { |
| | | final ConnectionEntryReader reader = newReader(ERROR); |
| | | try { |
| | | try (ConnectionEntryReader reader = newReader(ERROR)) { |
| | | reader.hasNext(); |
| | | fail(); |
| | | } catch (final LdapException e) { |
| | | assertThat(e.getResult()).isSameAs(ERROR); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | | @Test |
| | | public final void testReadEntry() throws Exception { |
| | | final ConnectionEntryReader reader = newReader(ENTRY1, SUCCESS); |
| | | try { |
| | | try (ConnectionEntryReader reader = newReader(ENTRY1, SUCCESS)) { |
| | | assertThat(reader.hasNext()).isTrue(); |
| | | assertThat(reader.isEntry()).isTrue(); |
| | | assertThat(reader.isReference()).isFalse(); |
| | | assertThat(reader.readEntry()).isSameAs(ENTRY1); |
| | | assertThat(reader.hasNext()).isFalse(); |
| | | assertThat(reader.readResult()).isSameAs(SUCCESS); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | | @Test |
| | | public final void testReadEntryWhenError() throws Exception { |
| | | final ConnectionEntryReader reader = newReader(ERROR); |
| | | try { |
| | | try (ConnectionEntryReader reader = newReader(ERROR)) { |
| | | reader.readEntry(); |
| | | fail(); |
| | | } catch (final LdapException e) { |
| | | assertThat(e.getResult()).isSameAs(ERROR); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | | @Test(expectedExceptions = NoSuchElementException.class) |
| | | public final void testReadEntryWhenNoMore() throws Exception { |
| | | final ConnectionEntryReader reader = newReader(SUCCESS); |
| | | try { |
| | | try (ConnectionEntryReader reader = newReader(SUCCESS)) { |
| | | assertThat(reader.hasNext()).isFalse(); |
| | | reader.readEntry(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | | @Test |
| | | public final void testReadEntryWhenReference() throws Exception { |
| | | final ConnectionEntryReader reader = newReader(REF, SUCCESS); |
| | | try { |
| | | try (ConnectionEntryReader reader = newReader(REF, SUCCESS)) { |
| | | assertThat(reader.hasNext()).isTrue(); |
| | | try { |
| | | reader.readEntry(); |
| | |
| | | } |
| | | assertThat(reader.hasNext()).isFalse(); |
| | | assertThat(reader.readResult()).isSameAs(SUCCESS); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | | @Test |
| | | public final void testReadMultipleResults() throws Exception { |
| | | final ConnectionEntryReader reader = newReader(ENTRY1, ENTRY2, REF, ENTRY3, SUCCESS); |
| | | try { |
| | | try (ConnectionEntryReader reader = newReader(ENTRY1, ENTRY2, REF, ENTRY3, SUCCESS)) { |
| | | assertThat(reader.hasNext()).isTrue(); |
| | | assertThat(reader.readEntry()).isSameAs(ENTRY1); |
| | | assertThat(reader.hasNext()).isTrue(); |
| | |
| | | assertThat(reader.readEntry()).isSameAs(ENTRY3); |
| | | assertThat(reader.hasNext()).isFalse(); |
| | | assertThat(reader.readResult()).isSameAs(SUCCESS); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | | @Test |
| | | public final void testReadReference() throws Exception { |
| | | final ConnectionEntryReader reader = newReader(REF, SUCCESS); |
| | | try { |
| | | try (ConnectionEntryReader reader = newReader(REF, SUCCESS)) { |
| | | assertThat(reader.hasNext()).isTrue(); |
| | | assertThat(reader.isEntry()).isFalse(); |
| | | assertThat(reader.isReference()).isTrue(); |
| | | assertThat(reader.readReference()).isSameAs(REF); |
| | | assertThat(reader.hasNext()).isFalse(); |
| | | assertThat(reader.readResult()).isSameAs(SUCCESS); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | | @Test |
| | | public final void testReadReferenceWhenEntry() throws Exception { |
| | | final ConnectionEntryReader reader = newReader(ENTRY1, SUCCESS); |
| | | try { |
| | | try (ConnectionEntryReader reader = newReader(ENTRY1, SUCCESS)) { |
| | | assertThat(reader.hasNext()).isTrue(); |
| | | assertThat(reader.readReference()).isNull(); |
| | | assertThat(reader.readEntry()).isSameAs(ENTRY1); |
| | | assertThat(reader.hasNext()).isFalse(); |
| | | assertThat(reader.readResult()).isSameAs(SUCCESS); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | | @Test |
| | | public final void testReadReferenceWhenError() throws Exception { |
| | | final ConnectionEntryReader reader = newReader(ERROR); |
| | | try { |
| | | try (ConnectionEntryReader reader = newReader(ERROR)) { |
| | | reader.readReference(); |
| | | fail(); |
| | | } catch (final LdapException e) { |
| | | assertThat(e.getResult()).isSameAs(ERROR); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | | @Test(expectedExceptions = NoSuchElementException.class) |
| | | public final void testReadReferenceWhenNoMore() throws Exception { |
| | | final ConnectionEntryReader reader = newReader(SUCCESS); |
| | | try { |
| | | try (ConnectionEntryReader reader = newReader(SUCCESS)) { |
| | | assertThat(reader.hasNext()).isFalse(); |
| | | reader.readReference(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | | @Test |
| | | public final void testReadResult() throws Exception { |
| | | final ConnectionEntryReader reader = newReader(SUCCESS); |
| | | try { |
| | | try (ConnectionEntryReader reader = newReader(SUCCESS)) { |
| | | assertThat(reader.readResult()).isSameAs(SUCCESS); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | | @Test |
| | | public final void testReadResultWhenError() throws Exception { |
| | | final ConnectionEntryReader reader = newReader(ERROR); |
| | | try { |
| | | try (ConnectionEntryReader reader = newReader(ERROR)) { |
| | | reader.readResult(); |
| | | fail(); |
| | | } catch (final LdapException e) { |
| | | assertThat(e.getResult()).isSameAs(ERROR); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | | @Test(expectedExceptions = IllegalStateException.class) |
| | | public final void testReadResultWhenEntry() throws Exception { |
| | | final ConnectionEntryReader reader = newReader(ENTRY1, SUCCESS); |
| | | try { |
| | | try (ConnectionEntryReader reader = newReader(ENTRY1, SUCCESS)) { |
| | | reader.readResult(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | @Test(expectedExceptions = DecodeException.class) |
| | | public void testReadAddRecordWithEmptyPairKeyChangeType() throws Exception { |
| | | try (LDIFChangeRecordReader reader = new LDIFChangeRecordReader( |
| | | // @formatter:off |
| | | LDIFChangeRecordReader reader = new LDIFChangeRecordReader( |
| | | "dn: dc=example,dc=com", |
| | | ":add" // if empty spaces, ko. |
| | | ); |
| | | // @formatter:on |
| | | |
| | | try { |
| | | )) { |
| | | reader.readChangeRecord(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | @Test(expectedExceptions = DecodeException.class) |
| | | public void testReadAddRecordWithWrongChangeType() throws Exception { |
| | | try (LDIFChangeRecordReader reader = new LDIFChangeRecordReader( |
| | | // @formatter:off |
| | | LDIFChangeRecordReader reader = new LDIFChangeRecordReader( |
| | | "dn: dc=example,dc=com", |
| | | "changetype: oops", // wrong |
| | | "objectClass: top", |
| | | "objectClass: domainComponent", |
| | | "dc: example" |
| | | ); |
| | | // @formatter:on |
| | | try { |
| | | )) { |
| | | reader.readChangeRecord(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | final String url = file.toURI().toURL().toString(); |
| | | file.delete(); |
| | | |
| | | try (LDIFChangeRecordReader reader = new LDIFChangeRecordReader( |
| | | // @formatter:off |
| | | final LDIFChangeRecordReader reader = new LDIFChangeRecordReader( |
| | | "version: 1", |
| | | "# Add a new entry", |
| | | "dn: cn=Fiona Jensen, ou=Marketing, dc=airius, dc=com", |
| | |
| | | "uid: fiona", |
| | | "telephonenumber: +1 408 555 1212", |
| | | "jpegphoto:< " + url |
| | | ); |
| | | // @formatter:on |
| | | |
| | | try { |
| | | )) { |
| | | reader.readChangeRecord(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | @Test(expectedExceptions = DecodeException.class) |
| | | public void testParseChangeRecordEntryRejectedWhenControlIsEmpty() throws Exception { |
| | | |
| | | try (LDIFChangeRecordReader reader = new LDIFChangeRecordReader( |
| | | // @formatter:off |
| | | final LDIFChangeRecordReader reader = new LDIFChangeRecordReader( |
| | | "dn: ou=Product Development, dc=airius, dc=com", |
| | | "control:", |
| | | "changetype: add", |
| | | "objectClass: top", |
| | | "objectClass: organization", |
| | | "o: testing" |
| | | ); |
| | | // @formatter:on |
| | | |
| | | try { |
| | | )) { |
| | | reader.readChangeRecord(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | @Test(expectedExceptions = DecodeException.class) |
| | | public void testParseModifyDNChangeRecordEntryMalformedMissedNewRDN() throws Exception { |
| | | // @formatter:off |
| | | final LDIFChangeRecordReader reader = new LDIFChangeRecordReader( |
| | | try (LDIFChangeRecordReader reader = new LDIFChangeRecordReader( |
| | | "version: 1", |
| | | "dn: cn=scarter,dc=example,dc=com", |
| | | "changetype: modrdn" |
| | | ); |
| | | // @formatter:on |
| | | |
| | | try { |
| | | )) { |
| | | reader.readChangeRecord(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | @Test(expectedExceptions = DecodeException.class) |
| | | public void testParseModifyDNChangeRecordEntryKeyMalformedEmptyNewRDN() throws Exception { |
| | | |
| | | try (LDIFChangeRecordReader reader = new LDIFChangeRecordReader( |
| | | // @formatter:off |
| | | final LDIFChangeRecordReader reader = new LDIFChangeRecordReader( |
| | | "version: 1", |
| | | "dn: cn=scarter,dc=example,dc=com", |
| | | "changetype: modrdn", |
| | | "newrdn:", |
| | | "deleteoldrdn: 1" |
| | | ); |
| | | // @formatter:on |
| | | try { |
| | | )) { |
| | | reader.readChangeRecord(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | @Test(expectedExceptions = DecodeException.class) |
| | | public void testParseModifyDNChangeRecordEntryKeyValueMalformedRDN() throws Exception { |
| | | try (LDIFChangeRecordReader reader = new LDIFChangeRecordReader( |
| | | // @formatter:off |
| | | final LDIFChangeRecordReader reader = new LDIFChangeRecordReader( |
| | | "version: 1", |
| | | "dn: cn=scarter,dc=example,dc=com", |
| | | "changetype: modrdn", |
| | | "newrdn:oops", // wrong |
| | | "deleteoldrdn: 1" |
| | | ); |
| | | // @formatter:on |
| | | try { |
| | | )) { |
| | | reader.readChangeRecord(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | @Test(expectedExceptions = DecodeException.class) |
| | | public void testParseModifyDNChangeRecordEntryKeyValueMalformedDeleteOldRDN() throws Exception { |
| | | |
| | | try (LDIFChangeRecordReader reader = new LDIFChangeRecordReader( |
| | | // @formatter:off |
| | | final LDIFChangeRecordReader reader = new LDIFChangeRecordReader( |
| | | "version: 1", |
| | | "dn: cn=scarter,dc=example,dc=com", |
| | | "changetype: modrdn", |
| | | "newrdn:cn=Susan Jacobs", |
| | | "deleteoldrdn: cn=scarter,dc=example,dc=com" // wrong |
| | | ); |
| | | // @formatter:on |
| | | try { |
| | | )) { |
| | | reader.readChangeRecord(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | @Test(expectedExceptions = DecodeException.class) |
| | | public void testParseModifyDNChangeRecordEntryKeyValueMalformedDeleteOldRDN2() throws Exception { |
| | | try (LDIFChangeRecordReader reader = new LDIFChangeRecordReader( |
| | | // @formatter:off |
| | | final LDIFChangeRecordReader reader = new LDIFChangeRecordReader( |
| | | "version: 1", |
| | | "dn: cn=scarter,dc=example,dc=com", |
| | | "changetype: modrdn", |
| | | "newrdn:cn=Susan Jacobs", |
| | | "deleteold: 1" // wrong |
| | | ); |
| | | // @formatter:on |
| | | try { |
| | | )) { |
| | | reader.readChangeRecord(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | @Test(expectedExceptions = DecodeException.class) |
| | | public void testParseModifyDNChangeRecordEntryKeyValueMalformedDeleteOldRDN3() throws Exception { |
| | | |
| | | try (LDIFChangeRecordReader reader = new LDIFChangeRecordReader( |
| | | // @formatter:off |
| | | final LDIFChangeRecordReader reader = new LDIFChangeRecordReader( |
| | | "version: 1", |
| | | "dn: cn=scarter,dc=example,dc=com", |
| | | "changetype: modrdn", |
| | | "newrdn:cn=Susan Jacobs" |
| | | // missing deleteoldrn: 1/0||true/false||yes/no |
| | | ); |
| | | // @formatter:on |
| | | try { |
| | | )) { |
| | | reader.readChangeRecord(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | @Test(expectedExceptions = DecodeException.class) |
| | | public void testParseModifyRecordEntryNewSuperiorMalformed() throws Exception { |
| | | try (LDIFChangeRecordReader reader = new LDIFChangeRecordReader( |
| | | // @formatter:off |
| | | LDIFChangeRecordReader reader = new LDIFChangeRecordReader( |
| | | "dn: cn=scarter,ou=People,dc=example,dc=com", |
| | | "changeType: modrdn", |
| | | "newrdn: cn=Susan Jacobs", |
| | | "deleteOldRdn: 1", |
| | | "newSuperior:" // wrong |
| | | ); |
| | | // @formatter:on |
| | | try { |
| | | )) { |
| | | reader.readChangeRecord(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | @Test(expectedExceptions = DecodeException.class) |
| | | public void testParseModifyRecordEntryNewSuperiorMalformed2() throws Exception { |
| | | |
| | | try (LDIFChangeRecordReader reader = new LDIFChangeRecordReader( |
| | | // @formatter:off |
| | | LDIFChangeRecordReader reader = new LDIFChangeRecordReader( |
| | | "dn: cn=scarter,ou=People,dc=example,dc=com", |
| | | "changeType: modrdn", |
| | | "newrdn: cn=Susan Jacobs", |
| | | "deleteOldRdn: 1", |
| | | "newSuperior: Susan Jacobs" // wrong |
| | | ); |
| | | // @formatter:on |
| | | try { |
| | | )) { |
| | | reader.readChangeRecord(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | @Test(expectedExceptions = NoSuchElementException.class) |
| | | public void testChangeRecordReaderDoesntAllowNull() throws Exception { |
| | | List<String> cr = Collections.emptyList(); |
| | | LDIFChangeRecordReader reader = new LDIFChangeRecordReader(cr); |
| | | try { |
| | | try (LDIFChangeRecordReader reader = new LDIFChangeRecordReader(cr)) { |
| | | reader.readChangeRecord(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | @Test(expectedExceptions = NoSuchElementException.class) |
| | | public void testChangeRecordReaderLDIFLineDoesntAllowNull() throws Exception { |
| | | LDIFChangeRecordReader reader = new LDIFChangeRecordReader(new String()); |
| | | try { |
| | | try (LDIFChangeRecordReader reader = new LDIFChangeRecordReader(new String())) { |
| | | reader.readChangeRecord(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | @Test(expectedExceptions = NullPointerException.class) |
| | | public void testSetExcludeAttributeDoesntAllowNull() throws Exception { |
| | | final List<String> actual = new ArrayList<>(); |
| | | final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual); |
| | | |
| | | try { |
| | | try (LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual)) { |
| | | writer.setExcludeAttribute(null); |
| | | } finally { |
| | | writer.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | @Test(expectedExceptions = NullPointerException.class) |
| | | public void testSetExcludeBranchDoesntAllowNull() throws Exception { |
| | | final List<String> actual = new ArrayList<>(); |
| | | final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual); |
| | | |
| | | try { |
| | | try (LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual)) { |
| | | writer.setExcludeBranch(null); |
| | | } finally { |
| | | writer.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | @Test(expectedExceptions = NullPointerException.class) |
| | | public void testSetIncludeAttributeDoesntAllowNull() throws Exception { |
| | | final List<String> actual = new ArrayList<>(); |
| | | final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual); |
| | | try { |
| | | try (LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual)) { |
| | | writer.setIncludeAttribute(null); |
| | | } finally { |
| | | writer.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | @Test(expectedExceptions = NullPointerException.class) |
| | | public void testSetIncludeBranchDoesntAllowNull() throws Exception { |
| | | final List<String> actual = new ArrayList<>(); |
| | | final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual); |
| | | try { |
| | | try (LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual)) { |
| | | writer.setIncludeBranch(null); |
| | | } finally { |
| | | writer.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | @Test |
| | | public void testWriteChangeRecordFlushClose() throws Exception { |
| | | final OutputStream mockOutput = mock(OutputStream.class); |
| | | final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(mockOutput); |
| | | try { |
| | | try (LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(mockOutput)) { |
| | | writer.writeComment("TLDIFChangeRecordWriter, this is a comment."); |
| | | writer.flush(); |
| | | writer.flush(); |
| | | verify(mockOutput, times(2)).flush(); |
| | | } finally { |
| | | writer.close(); |
| | | verify(mockOutput, times(1)).close(); |
| | | } |
| | | } |
| | |
| | | * Copyright 2009-2010 Sun Microsystems, Inc. |
| | | * Portions copyright 2012-2016 ForgeRock AS. |
| | | */ |
| | | |
| | | package org.forgerock.opendj.ldif; |
| | | |
| | | import static org.testng.Assert.assertNotNull; |
| | |
| | | import static org.mockito.Matchers.eq; |
| | | import static org.mockito.Mockito.*; |
| | | |
| | | /** |
| | | * This class tests the LDIFEntryReader functionality. |
| | | */ |
| | | /** This class tests the LDIFEntryReader functionality. */ |
| | | @SuppressWarnings("javadoc") |
| | | public final class LDIFEntryReaderTestCase extends AbstractLDIFTestCase { |
| | | /** |
| | |
| | | // @formatter:on |
| | | } |
| | | |
| | | /** |
| | | * Number of attributes of the standard entry. |
| | | */ |
| | | /** Number of attributes of the standard entry. */ |
| | | public final int nbStandardEntryAttributes = new LinkedHashMapEntry(getStandardEntry()) |
| | | .getAttributeCount(); |
| | | |
| | |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * Test to read an entry containing spaces before the attribute. |
| | | */ |
| | | /** Test to read an entry containing spaces before the attribute. */ |
| | | @Test |
| | | public void testReadEntryWithAttributesSpacesAtStart() throws Exception { |
| | | // @formatter:off |
| | |
| | | public void testReadEntry() throws Exception { |
| | | final String path = TestCaseUtils.createTempFile(getStandardEntry()); |
| | | final FileInputStream in = new FileInputStream(path); |
| | | final LDIFEntryReader reader = new LDIFEntryReader(in); |
| | | try { |
| | | try (LDIFEntryReader reader = new LDIFEntryReader(in)) { |
| | | Assert.assertTrue(reader.hasNext()); |
| | | final Entry entry = reader.readEntry(); |
| | | assertNotNull(entry); |
| | |
| | | .valueOf("uid=user.0,ou=People,dc=example,dc=com")); |
| | | Assert.assertFalse(reader.hasNext()); |
| | | reader.readEntry(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | // @formatter:on |
| | | final String path = TestCaseUtils.createTempFile(strEntry); |
| | | final FileInputStream in = new FileInputStream(path); |
| | | final LDIFEntryReader reader = new LDIFEntryReader(in); |
| | | try { |
| | | try (LDIFEntryReader reader = new LDIFEntryReader(in)) { |
| | | assertThat(reader.hasNext()); |
| | | final Entry entry = reader.readEntry(); |
| | | assertThat(entry.getName().toString()).isEqualTo( |
| | |
| | | assertThat(entry.getAttribute("ds-cfg-character-set")).isNotEmpty(); |
| | | assertThat(entry.getAttribute("ds-cfg-character-set").toArray().length).isEqualTo(4); |
| | | assertThat(reader.hasNext()).isFalse(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | }; |
| | | // @formatter:on |
| | | |
| | | final LDIFEntryReader reader = new LDIFEntryReader(strEntry); |
| | | try { |
| | | try (LDIFEntryReader reader = new LDIFEntryReader(strEntry)) { |
| | | reader.readEntry(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Test(expectedExceptions = DecodeException.class) |
| | | public void testValueOfLDIFEntryReadEntryContainingMalformedURL() throws Exception { |
| | | |
| | | try (LDIFEntryReader reader = new LDIFEntryReader( |
| | | // @formatter:off |
| | | final LDIFEntryReader reader = new LDIFEntryReader( |
| | | "version: 1", |
| | | "dn:: b3U95Za25qWt6YOoLG89QWlyaXVz", |
| | | "# dn:: ou=<JapaneseOU>,o=Airius", |
| | |
| | | "telephonenumber: +1 408 555 1212", |
| | | "jpegphoto:< invalidProtocol", |
| | | " ", |
| | | " "); |
| | | " " |
| | | // @formatter:on |
| | | |
| | | try { |
| | | )) { |
| | | reader.readEntry(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | // @formatter:on |
| | | |
| | | final FileInputStream in = new FileInputStream(path); |
| | | final LDIFEntryReader reader = new LDIFEntryReader(in); |
| | | try { |
| | | try (LDIFEntryReader reader = new LDIFEntryReader(in)) { |
| | | reader.readEntry(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | @Test(expectedExceptions = DecodeException.class) |
| | | public void testReadEntryBase64EncodedMalformedBase64Attribute() throws Exception { |
| | | |
| | | try (LDIFEntryReader reader = new LDIFEntryReader(Arrays.asList( |
| | | // @formatter:off |
| | | final LDIFEntryReader reader = new LDIFEntryReader(Arrays.asList( |
| | | "version: 1", |
| | | "dn: cn=Gern Jensen, ou=Product Testing, dc=airius, dc=com", |
| | | "objectclass: top", |
| | |
| | | + "IGlzIGJhc2UtNjQtZW5aaaaaaaaaaajb2RlZCBiZWNhdXNlIGl0IGhhcyBhIGNvbnRyb2wgY2hhcmFjdG" |
| | | + "VyIGluIGl0IChhIENSKS4NICBCeSB0aGUgd2F5LCB5b3Ugc2hvdWxkIHJlYWxseSBnZXQg" |
| | | + "b3V0IG1vcmUu" |
| | | )); |
| | | // @formatter:on |
| | | try { |
| | | // @formatter:off |
| | | ))) { |
| | | reader.readEntry(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | * information: "Portions Copyright [year] [name of copyright owner]". |
| | | * |
| | | * Copyright 2009-2010 Sun Microsystems, Inc. |
| | | * Portions copyright 2012-2015 ForgeRock AS. |
| | | * Portions copyright 2012-2016 ForgeRock AS. |
| | | */ |
| | | |
| | | package org.forgerock.opendj.ldif; |
| | |
| | | doThrow(new IOException()).when(mockOutput).write(any(byte[].class)); |
| | | doThrow(new IOException()).when(mockOutput).write(any(byte[].class), anyInt(), anyInt()); |
| | | |
| | | LDIFEntryWriter writer = new LDIFEntryWriter(mockOutput); |
| | | try { |
| | | try (LDIFEntryWriter writer = new LDIFEntryWriter(mockOutput)) { |
| | | CharSequence comment = "This is a new comment"; |
| | | writer.writeComment(comment); |
| | | } finally { |
| | | writer.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | @Test |
| | | public void testWriteEntryUsingMockOutputForFlushAndClose() throws Exception { |
| | | OutputStream mockOutput = mock(OutputStream.class); |
| | | LDIFEntryWriter writer = new LDIFEntryWriter(mockOutput); |
| | | try { |
| | | try (LDIFEntryWriter writer = new LDIFEntryWriter(mockOutput)) { |
| | | writer.flush(); |
| | | writer.flush(); |
| | | verify(mockOutput, times(2)).flush(); |
| | | } finally { |
| | | writer.close(); |
| | | verify(mockOutput, times(1)).close(); |
| | | } |
| | | } |
| | |
| | | @Test(expectedExceptions = NullPointerException.class) |
| | | public final void testLdifSearchDoesntAllowNullSearchRequest() throws Exception { |
| | | |
| | | final EntryReader reader = new LDIFEntryReader(getStandardEntry()); |
| | | try { |
| | | try (EntryReader reader = new LDIFEntryReader(getStandardEntry())) { |
| | | LDIF.search(reader, null); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | "changetype: delete" |
| | | ); |
| | | // @formatter:on |
| | | final java.util.List<String> actual = new ArrayList<>(); |
| | | final List<String> actual = new ArrayList<>(); |
| | | final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual); |
| | | |
| | | try { |
| | |
| | | */ |
| | | @Test(expectedExceptions = NullPointerException.class) |
| | | public final void testLdifCopyToChangeRecordDoesntAllowNullReader() throws Exception { |
| | | final java.util.List<String> actual = new ArrayList<>(); |
| | | final List<String> actual = new ArrayList<>(); |
| | | final LDIFChangeRecordWriter writer = new LDIFChangeRecordWriter(actual); |
| | | |
| | | LDIF.copyTo(null, writer); |
| | |
| | | "manager: uid=joneill,ou=People,dc=example,dc=com" |
| | | ); |
| | | // @formatter:on |
| | | EntryReader reader = new LDIFEntryReader(); |
| | | try { |
| | | reader = LDIF.patch(input, patch); |
| | | try (EntryReader reader = LDIF.patch(input, patch)) { |
| | | reader.readEntry(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | "work-phone: 650/506-7000" |
| | | ); |
| | | // @formatter:on |
| | | EntryReader reader = new LDIFEntryReader(); |
| | | try { |
| | | reader = LDIF.patch(input, patch, listener); |
| | | } finally { |
| | | reader.close(); |
| | | try (EntryReader reader = LDIF.patch(input, patch, listener)) { |
| | | // should throw |
| | | } |
| | | } |
| | | |
| | |
| | | "work-phone: 650/506-7000" |
| | | ); |
| | | // @formatter:on |
| | | EntryReader reader = new LDIFEntryReader(); |
| | | try { |
| | | reader = LDIF.patch(input, patch, listener); |
| | | try (EntryReader reader = LDIF.patch(input, patch, listener)) { |
| | | Entry entry = reader.readEntry(); |
| | | assertThat(entry.getName().toString()).isEqualTo("uid=scarter,ou=People,dc=example,dc=com"); |
| | | assertThat(entry.getAttributeCount()).isEqualTo(3); |
| | | assertThat(entry.getAttribute("objectClass").firstValueAsString()).isEqualTo( |
| | | "person"); |
| | | assertThat(entry.getAttribute("objectClass").firstValueAsString()).isEqualTo("person"); |
| | | assertThat(entry.getAttribute("sn").firstValueAsString()).isEqualTo( |
| | | "new user"); |
| | | assertThat(entry.getAttribute("mail").firstValueAsString()).isEqualTo( |
| | | "mail@mailme.org"); |
| | | assertThat(reader.hasNext()).isFalse(); |
| | | assertThat(entry.getAttribute("work-phone")).isNull(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | "deleteoldrdn: 1" |
| | | ); |
| | | // @formatter:on |
| | | EntryReader reader = new LDIFEntryReader(); |
| | | try { |
| | | reader = LDIF.patch(input, patch, listener); |
| | | } finally { |
| | | reader.close(); |
| | | try (EntryReader reader = LDIF.patch(input, patch, listener)) { |
| | | // should throw |
| | | } |
| | | } |
| | | |
| | |
| | | "deleteoldrdn: 1" |
| | | ); |
| | | // @formatter:on |
| | | EntryReader reader = new LDIFEntryReader(); |
| | | try { |
| | | reader = LDIF.patch(input, patch, listener); |
| | | try (EntryReader reader = LDIF.patch(input, patch, listener)) { |
| | | Entry entry = reader.readEntry(); |
| | | assertThat(entry.getName().toString()).isEqualTo("uid=scarter,ou=People,dc=example,dc=com"); |
| | | assertThat(entry.getName().toString()).isNotEqualTo("uid=scarter,ou=Human Resources,dc=example,dc=com"); |
| | |
| | | assertThat(entry.getAttribute("mail").firstValueAsString()).isEqualTo( |
| | | "mail@mailme.org"); |
| | | assertThat(reader.hasNext()).isFalse(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | "deleteoldrdn: 0" |
| | | ); |
| | | // @formatter:on |
| | | EntryReader reader = new LDIFEntryReader(); |
| | | try { |
| | | reader = LDIF.patch(input, patch, listener); |
| | | } finally { |
| | | reader.close(); |
| | | try (EntryReader reader = LDIF.patch(input, patch, listener)) { |
| | | // should throw |
| | | } |
| | | } |
| | | |
| | |
| | | "deleteoldrdn: 0" |
| | | ); |
| | | // @formatter:on |
| | | EntryReader reader = new LDIFEntryReader(); |
| | | try { |
| | | reader = LDIF.patch(input, patch, listener); |
| | | try (EntryReader reader = LDIF.patch(input, patch, listener)) { |
| | | Entry entry = reader.readEntry(); |
| | | assertThat(entry.getName().toString()).isEqualTo("uid=user.2,ou=People,dc=example,dc=com"); |
| | | assertThat(entry.getAttributeCount()).isEqualTo(4); |
| | | assertThat(entry.getAttribute("objectClass").firstValueAsString()).isEqualTo( |
| | | "person"); |
| | | assertThat(entry.getAttribute("sn").firstValueAsString()).isEqualTo( |
| | | "new user"); |
| | | assertThat(entry.getAttribute("mail").firstValueAsString()).isEqualTo( |
| | | "mail@mailme.org"); |
| | | assertThat(entry.getAttribute("objectClass").firstValueAsString()).isEqualTo("person"); |
| | | assertThat(entry.getAttribute("sn").firstValueAsString()).isEqualTo("new user"); |
| | | assertThat(entry.getAttribute("mail").firstValueAsString()).isEqualTo("mail@mailme.org"); |
| | | assertThat(reader.hasNext()).isFalse(); |
| | | } finally { |
| | | reader.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | SocketAddress socketAddress = transport.get().bind(loopbackWithDynamicPort()).getLocalAddress(); |
| | | |
| | | // Establish a socket connection to see if the transport factory works. |
| | | final Socket socket = new Socket(); |
| | | try { |
| | | try (Socket socket = new Socket()) { |
| | | socket.connect(socketAddress); |
| | | |
| | | // Successfully connected if there is no exception. |
| | | assertTrue(socket.isConnected()); |
| | | // Don't stop the transport because it is shared with the ldap server. |
| | | } finally { |
| | | socket.close(); |
| | | transport.release(); |
| | | } |
| | | } |
| | |
| | | |
| | | @Test(description = "OPENDJ-1197") |
| | | public void testClientSideConnectTimeout() throws Exception { |
| | | // Use an non-local unreachable network address. |
| | | final ConnectionFactory factory = new LDAPConnectionFactory("10.20.30.40", 1389, |
| | | Options.defaultOptions().set(CONNECT_TIMEOUT, duration("1 ms"))); |
| | | try { |
| | | // Use a non-local unreachable network address. |
| | | try (ConnectionFactory factory = new LDAPConnectionFactory("10.20.30.40", 1389, |
| | | Options.defaultOptions().set(CONNECT_TIMEOUT, duration("1 ms")))) { |
| | | for (int i = 0; i < ITERATIONS; i++) { |
| | | final PromiseImpl<LdapException, NeverThrowsException> promise = PromiseImpl.create(); |
| | | final Promise<? extends Connection, LdapException> connectionPromise = factory.getConnectionAsync(); |
| | |
| | | assertThat(ce.getResult().getResultCode()).isEqualTo(ResultCode.CLIENT_SIDE_CONNECT_ERROR); |
| | | } |
| | | } |
| | | } finally { |
| | | factory.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | registerBindEvent(); |
| | | registerCloseEvent(); |
| | | |
| | | final Connection connection = factory.getConnection(); |
| | | try { |
| | | try (Connection connection = factory.getConnection()) { |
| | | waitForConnect(); |
| | | final MockConnectionEventListener listener = new MockConnectionEventListener(); |
| | | connection.addConnectionEventListener(listener); |
| | |
| | | connection.close(); |
| | | waitForClose(); |
| | | verifyNoAbandonSent(); |
| | | } finally { |
| | | connection.close(); |
| | | } |
| | | } |
| | | |
| | |
| | | registerCloseEvent(); |
| | | |
| | | for (int i = 0; i < ITERATIONS; i++) { |
| | | final Connection connection = pool.getConnection(); |
| | | try { |
| | | try (Connection connection = pool.getConnection()) { |
| | | waitForConnect(); |
| | | final MockConnectionEventListener listener = new MockConnectionEventListener(); |
| | | connection.addConnectionEventListener(listener); |
| | |
| | | connection.close(); |
| | | waitForClose(); |
| | | verifyNoAbandonSent(); |
| | | } finally { |
| | | connection.close(); |
| | | } |
| | | } |
| | | } |
| | |
| | | registerAbandonEvent(); |
| | | |
| | | for (int i = 0; i < ITERATIONS; i++) { |
| | | final Connection connection = factory.getConnection(); |
| | | try { |
| | | try (Connection connection = factory.getConnection()) { |
| | | waitForConnect(); |
| | | final ConnectionEventListener listener = mock(ConnectionEventListener.class); |
| | | connection.addConnectionEventListener(listener); |
| | |
| | | * LDAPConnection for explanation). |
| | | */ |
| | | // waitForAbandon(); |
| | | } finally { |
| | | connection.close(); |
| | | } |
| | | } |
| | | } |
| | |
| | | resetState(); |
| | | |
| | | for (int i = 0; i < ITERATIONS; i++) { |
| | | final Connection connection = factory.getConnection(); |
| | | try { |
| | | try (Connection connection = factory.getConnection()) { |
| | | waitForConnect(); |
| | | final MockConnectionEventListener listener = new MockConnectionEventListener(); |
| | | connection.addConnectionEventListener(listener); |
| | |
| | | |
| | | // Wait for the error notification to reach the client. |
| | | listener.awaitError(TEST_TIMEOUT, TimeUnit.SECONDS); |
| | | } finally { |
| | | connection.close(); |
| | | } |
| | | } |
| | | } |
| | |
| | | address.getPort(), |
| | | Options.defaultOptions() |
| | | .set(REQUEST_TIMEOUT, duration("100 ms"))); |
| | | GrizzlyLDAPConnection connection = (GrizzlyLDAPConnection) factory.getConnectionAsync().getOrThrow(); |
| | | try { |
| | | try (GrizzlyLDAPConnection connection = (GrizzlyLDAPConnection) factory.getConnectionAsync().getOrThrow()) { |
| | | SearchRequest request = |
| | | Requests.newSearchRequest("dc=test", SearchScope.BASE_OBJECT, "(objectClass=*)"); |
| | | if (isPersistentSearch) { |
| | |
| | | ResultCode.CLIENT_SIDE_TIMEOUT); |
| | | } |
| | | } finally { |
| | | connection.close(); |
| | | listener.close(); |
| | | factory.close(); |
| | | } |
| | |
| | | import java.io.PrintStream; |
| | | import java.util.List; |
| | | |
| | | import com.forgerock.opendj.cli.IntegerArgument; |
| | | import org.forgerock.i18n.LocalizableException; |
| | | import org.forgerock.i18n.LocalizableMessage; |
| | | import org.forgerock.opendj.ldap.DecodeException; |
| | |
| | | import org.forgerock.opendj.ldif.LDIFEntryReader; |
| | | import org.forgerock.opendj.ldif.LDIFEntryWriter; |
| | | import org.forgerock.opendj.ldif.RejectedChangeRecordListener; |
| | | import org.forgerock.util.annotations.VisibleForTesting; |
| | | |
| | | import com.forgerock.opendj.cli.ArgumentException; |
| | | import com.forgerock.opendj.cli.ArgumentParser; |
| | | import com.forgerock.opendj.cli.BooleanArgument; |
| | | import com.forgerock.opendj.cli.IntegerArgument; |
| | | import com.forgerock.opendj.cli.StringArgument; |
| | | import org.forgerock.util.annotations.VisibleForTesting; |
| | | |
| | | /** |
| | | * A tool that can be used to issue update (Add/Delete/Modify/ModifyDN) requests |
| | |
| | | return ResultCode.SUCCESS.intValue(); |
| | | } |
| | | |
| | | InputStream sourceInputStream = null; |
| | | OutputStream outputStream = null; |
| | | final List<String> trailingArguments = argParser.getTrailingArguments(); |
| | | LDIFEntryReader sourceReader = null; |
| | | LDIFChangeRecordReader changesReader = null; |
| | | LDIFEntryWriter outputWriter = null; |
| | | |
| | | try { |
| | | final List<String> trailingArguments = argParser.getTrailingArguments(); |
| | | sourceInputStream = getLDIFToolInputStream(this, trailingArguments.get(0)); |
| | | outputStream = getLDIFToolOutputStream(this, outputFilename); |
| | | |
| | | try (InputStream sourceInputStream = getLDIFToolInputStream(this, trailingArguments.get(0)); |
| | | OutputStream outputStream = getLDIFToolOutputStream(this, outputFilename)) { |
| | | final int nbTrailingArgs = trailingArguments.size(); |
| | | final boolean readChangesFromStdin = nbTrailingArgs == 1 |
| | | || (nbTrailingArgs == 2 && USE_SYSTEM_STREAM_TOKEN.equals(trailingArguments.get(1))); |
| | |
| | | } catch (final ArgumentException ae) { |
| | | throw newToolParamException(ae, ae.getMessageObject()); |
| | | } finally { |
| | | closeSilently(sourceReader, changesReader, outputWriter, |
| | | sourceInputStream, outputStream); |
| | | closeSilently(sourceReader, changesReader, outputWriter); |
| | | } |
| | | |
| | | return ResultCode.SUCCESS.intValue(); |
| | |
| | | } |
| | | |
| | | /** |
| | | * Converts from OpenDJ LDAP SDK {@link org.forgerock.opendj.ldap.responses.SearchResultEntry} to OpenDJ |
| | | * Converts from OpenDJ LDAP SDK {@link SearchResultEntry} to OpenDJ |
| | | * server {@link org.opends.server.types.SearchResultEntry}. |
| | | * |
| | | * @param value |
| | |
| | | * @return the converted value |
| | | */ |
| | | public static org.opends.server.types.SearchResultEntry to( |
| | | final org.forgerock.opendj.ldap.responses.SearchResultEntry value) { |
| | | final SearchResultEntry value) { |
| | | if (value != null) { |
| | | org.opends.server.types.Entry entry = |
| | | new org.opends.server.types.Entry(value.getName(), null, null, null); |
| | |
| | | |
| | | /** |
| | | * Converts from OpenDJ LDAP SDK {@link Control} to OpenDJ server |
| | | * {@link org.opends.server.protocols.ldap.LDAPControl}. |
| | | * {@link LDAPControl}. |
| | | * |
| | | * @param control |
| | | * value to convert |
| | | * @return the converted value |
| | | */ |
| | | public static org.opends.server.protocols.ldap.LDAPControl to(final Control control) { |
| | | public static LDAPControl to(final Control control) { |
| | | return new LDAPControl(control.getOID(), control.isCritical(), control.getValue()); |
| | | } |
| | | |
| | | /** |
| | | * Converts from a <code>List</code> of OpenDJ LDAP SDK |
| | | * {@link org.forgerock.opendj.ldap.controls.Control} to a <code>List</code> |
| | | * {@link Control} to a <code>List</code> |
| | | * of OpenDJ server {@link org.opends.server.types.Control}. |
| | | * |
| | | * @param listOfControl |
| | |
| | | * @return the converted value |
| | | */ |
| | | public static List<org.opends.server.types.Control> to( |
| | | final List<org.forgerock.opendj.ldap.controls.Control> listOfControl) { |
| | | final List<Control> listOfControl) { |
| | | List<org.opends.server.types.Control> toListOfControl = new ArrayList<>(listOfControl.size()); |
| | | for (org.forgerock.opendj.ldap.controls.Control c : listOfControl) { |
| | | for (Control c : listOfControl) { |
| | | toListOfControl.add(to(c)); |
| | | } |
| | | return toListOfControl; |
| | | } |
| | | |
| | | /** |
| | | * Converts from OpenDJ LDAP SDK {@link org.forgerock.opendj.ldap.Attribute} |
| | | * Converts from OpenDJ LDAP SDK {@link Attribute} |
| | | * to OpenDJ server {@link org.opends.server.types.RawAttribute}. |
| | | * |
| | | * @param attribute |
| | |
| | | * @return the converted value |
| | | */ |
| | | public static org.opends.server.types.RawAttribute to( |
| | | final org.forgerock.opendj.ldap.Attribute attribute) { |
| | | final Attribute attribute) { |
| | | ArrayList<ByteString> listAttributeValues = newArrayList(attribute.toArray()); |
| | | return new LDAPAttribute(attribute.getAttributeDescriptionAsString(), listAttributeValues); |
| | | } |
| | | |
| | | /** |
| | | * Converts from an <code>Iterable</code> of OpenDJ LDAP SDK |
| | | * {@link org.forgerock.opendj.ldap.Attribute} to a <code>List</code> of |
| | | * {@link Attribute} to a <code>List</code> of |
| | | * OpenDJ server {@link org.opends.server.types.RawAttribute}. |
| | | * |
| | | * @param listOfAttributes |
| | |
| | | * @return the converted value |
| | | */ |
| | | public static List<org.opends.server.types.RawAttribute> to( |
| | | final Iterable<org.forgerock.opendj.ldap.Attribute> listOfAttributes) { |
| | | final Iterable<Attribute> listOfAttributes) { |
| | | List<org.opends.server.types.RawAttribute> toListOfAttributes = |
| | | new ArrayList<>(((Collection<?>) listOfAttributes).size()); |
| | | for (org.forgerock.opendj.ldap.Attribute a : listOfAttributes) { |
| | | for (Attribute a : listOfAttributes) { |
| | | toListOfAttributes.add(to(a)); |
| | | } |
| | | return toListOfAttributes; |
| | |
| | | } |
| | | |
| | | /** |
| | | * Converts from OpenDJ LDAP SDK {@link org.forgerock.opendj.ldap.Attribute} |
| | | * Converts from OpenDJ LDAP SDK {@link Attribute} |
| | | * to OpenDJ server {@link org.opends.server.types.Attribute}. |
| | | * |
| | | * @param attribute |
| | |
| | | * @return the converted value |
| | | */ |
| | | public static org.opends.server.types.Attribute toAttribute( |
| | | final org.forgerock.opendj.ldap.Attribute attribute) { |
| | | final Attribute attribute) { |
| | | final AttributeBuilder attrBuilder = |
| | | new AttributeBuilder(attribute.getAttributeDescriptionAsString()); |
| | | for (ByteString b : attribute.toArray()) { |
| | |
| | | |
| | | /** |
| | | * Converts from an <code>Iterable</code> of OpenDJ LDAP SDK |
| | | * {@link org.forgerock.opendj.ldap.Attribute} to a <code>List</code> of |
| | | * {@link Attribute} to a <code>List</code> of |
| | | * OpenDJ server {@link org.opends.server.types.RawAttribute}. |
| | | * |
| | | * @param listOfAttributes |
| | |
| | | * @return the converted value |
| | | */ |
| | | public static List<org.opends.server.types.Attribute> toAttributes( |
| | | final Iterable<org.forgerock.opendj.ldap.Attribute> listOfAttributes) { |
| | | final Iterable<Attribute> listOfAttributes) { |
| | | List<org.opends.server.types.Attribute> toListOfAttributes = new ArrayList<>(); |
| | | Iterator<Attribute> it = listOfAttributes.iterator(); |
| | | while (it.hasNext()) |
| | |
| | | |
| | | /** |
| | | * Converts from OpenDJ server |
| | | * {@link org.opends.server.protocols.ldap.LDAPControl} to OpenDJ LDAP SDK |
| | | * {@link LDAPControl} to OpenDJ LDAP SDK |
| | | * {@link Control}. |
| | | * |
| | | * @param ldapControl |
| | | * value to convert |
| | | * @return the converted value |
| | | */ |
| | | public static Control from(final org.opends.server.protocols.ldap.LDAPControl ldapControl) { |
| | | public static Control from(final LDAPControl ldapControl) { |
| | | return GenericControl.newControl(ldapControl.getOID(), ldapControl.isCritical(), |
| | | ldapControl.getValue()); |
| | | } |
| | |
| | | } |
| | | |
| | | final ByteString sdkByteString = builder.toByteString(); |
| | | final org.forgerock.opendj.io.ASN1Reader sdkReaderASN1 = |
| | | org.forgerock.opendj.io.ASN1.getReader(sdkByteString.toByteArray()); |
| | | final ASN1Reader sdkReaderASN1 = |
| | | ASN1.getReader(sdkByteString.toByteArray()); |
| | | |
| | | // Reads the ASN1 SDK byte string. |
| | | try { |
| | |
| | | /** |
| | | * Converts from a <code>List</code> of OpenDJ server |
| | | * {@link org.opends.server.types.Control} to a <code>List</code> of OpenDJ |
| | | * LDAP SDK {@link org.forgerock.opendj.ldap.controls.Control}. |
| | | * LDAP SDK {@link Control}. |
| | | * |
| | | * @param listOfControl |
| | | * value to convert |
| | | * @return the converted value |
| | | */ |
| | | public static List<org.forgerock.opendj.ldap.controls.Control> from( |
| | | public static List<Control> from( |
| | | final List<org.opends.server.types.Control> listOfControl) { |
| | | List<org.forgerock.opendj.ldap.controls.Control> fromListofControl = new ArrayList<>(listOfControl.size()); |
| | | List<Control> fromListofControl = new ArrayList<>(listOfControl.size()); |
| | | for (org.opends.server.types.Control c : listOfControl) { |
| | | fromListofControl.add(from(c)); |
| | | } |
| | |
| | | |
| | | /** |
| | | * Converts from OpenDJ server {@link org.opends.server.types.Attribute} to |
| | | * OpenDJ LDAP SDK {@link org.forgerock.opendj.ldap.Attribute}. |
| | | * OpenDJ LDAP SDK {@link Attribute}. |
| | | * |
| | | * @param attribute |
| | | * value to convert |
| | | * @return the converted value |
| | | */ |
| | | public static org.forgerock.opendj.ldap.Attribute from( |
| | | public static Attribute from( |
| | | final org.opends.server.types.Attribute attribute) { |
| | | Attribute sdkAttribute = new LinkedAttribute(attribute.getAttributeDescription()); |
| | | for (ByteString value : attribute) { |
| | |
| | | |
| | | /** |
| | | * Converts from OpenDJ server {@link org.opends.server.types.Attribute} to OpenDJ LDAP SDK |
| | | * {@link org.forgerock.opendj.ldap.Attribute}. This method is an optimization of the equivalent from() method. It's |
| | | * {@link Attribute}. This method is an optimization of the equivalent from() method. It's |
| | | * only used in encoding/decoding and as such only wrap the required methods: the returned object partially wrap the |
| | | * incoming one. |
| | | * |
| | |
| | | * value to convert |
| | | * @return the converted value |
| | | */ |
| | | public static org.forgerock.opendj.ldap.Attribute partiallyWrap(final org.opends.server.types.Attribute attribute) { |
| | | public static Attribute partiallyWrap(final org.opends.server.types.Attribute attribute) { |
| | | return new Attribute() { |
| | | |
| | | @Override |
| | |
| | | /** |
| | | * Converts from an <code>Iterable</code> of OpenDJ server |
| | | * {@link org.opends.server.types.Attribute} to a <code>List</code> of OpenDJ |
| | | * LDAP SDK {@link org.forgerock.opendj.ldap.Attribute}. |
| | | * LDAP SDK {@link Attribute}. |
| | | * |
| | | * @param listOfAttributes |
| | | * value to convert |
| | | * @return the converted value |
| | | */ |
| | | public static List<org.forgerock.opendj.ldap.Attribute> from( |
| | | public static List<Attribute> from( |
| | | final Iterable<org.opends.server.types.Attribute> listOfAttributes) { |
| | | List<org.forgerock.opendj.ldap.Attribute> fromListofAttributes = |
| | | List<Attribute> fromListofAttributes = |
| | | new ArrayList<>(((Collection<?>) listOfAttributes).size()); |
| | | for (org.opends.server.types.Attribute a : listOfAttributes) { |
| | | fromListofAttributes.add(from(a)); |
| | |
| | | /** |
| | | * Converts from OpenDJ server |
| | | * {@link org.opends.server.types.SearchResultEntry} to OpenDJ LDAP SDK |
| | | * {@link org.forgerock.opendj.ldap.responses.SearchResultEntry}. |
| | | * {@link SearchResultEntry}. |
| | | * |
| | | * @param srvResultEntry |
| | | * value to convert |
| | | * @return the converted value |
| | | */ |
| | | public static org.forgerock.opendj.ldap.responses.SearchResultEntry from( |
| | | public static SearchResultEntry from( |
| | | final org.opends.server.types.SearchResultEntry srvResultEntry) { |
| | | |
| | | final SearchResultEntry searchResultEntry = |
| | |
| | | |
| | | /** |
| | | * Converts from OpenDJ server {@link org.opends.server.types.SearchResultEntry} to OpenDJ LDAP SDK |
| | | * {@link org.forgerock.opendj.ldap.responses.SearchResultEntry}. This method is an optimization of the equivalent |
| | | * {@link SearchResultEntry}. This method is an optimization of the equivalent |
| | | * from() method. It's only used in encoding/decoding and as such only wrap the required methods: the returned |
| | | * object partially wrap the incoming one. |
| | | * |
| | |
| | | * Version of the ldap protocol |
| | | * @return the converted value |
| | | */ |
| | | public static org.forgerock.opendj.ldap.responses.SearchResultEntry partiallyWrap( |
| | | public static SearchResultEntry partiallyWrap( |
| | | final org.opends.server.types.SearchResultEntry srvResultEntry, final int ldapVersion) { |
| | | |
| | | final ArrayList<Control> controls = new ArrayList<>(srvResultEntry.getControls().size()); |
| | | for (org.opends.server.types.Control control : srvResultEntry.getControls()) { |
| | | controls.add(Converters.from(control)); |
| | | controls.add(from(control)); |
| | | } |
| | | |
| | | return new SearchResultEntry() { |
| | |
| | | |
| | | /** |
| | | * Converts from OpenDJ server |
| | | * {@link org.forgerock.opendj.server.config.meta.VirtualAttributeCfgDefn.Scope} |
| | | * to OpenDJ LDAP SDK {@link org.forgerock.opendj.ldap.SearchScope}. |
| | | * {@link VirtualAttributeCfgDefn.Scope} |
| | | * to OpenDJ LDAP SDK {@link SearchScope}. |
| | | * |
| | | * @param srvScope |
| | | * The server scope value. |
| | |
| | | reader.readStartSequence(); |
| | | return reader.readOctetString(TYPE_PASSWORD_MODIFY_GENERATED_PASSWORD); |
| | | } catch (IOException e) { |
| | | throw LdapException.newLdapException(ResultCode.PROTOCOL_ERROR, |
| | | throw newLdapException(ResultCode.PROTOCOL_ERROR, |
| | | ERR_EXTOP_PASSMOD_CANNOT_DECODE_REQUEST.get(getExceptionMessage(e)), e); |
| | | } |
| | | } |
| | |
| | | File tempDir = getFileForPath("config"); |
| | | File tempFile = File.createTempFile(configuration.getBackendId(), |
| | | certAlias, tempDir); |
| | | try |
| | | { |
| | | try (FileOutputStream outputStream = new FileOutputStream(tempFile.getPath(), false)) |
| | | { |
| | | certBytes.copyTo(outputStream); |
| | | } |
| | | |
| | | certificateManager.addCertificate(certAlias, tempFile); |
| | | } |
| | | finally |
| | |
| | | return newUndefinedSet(); |
| | | } |
| | | |
| | | try |
| | | try (Cursor<ByteString, EntryIDSet> cursor = index.openCursor(txn)) |
| | | { |
| | | // Total number of IDs found so far. |
| | | int totalIDCount = 0; |
| | | ArrayList<EntryIDSet> sets = new ArrayList<>(); |
| | | Cursor<ByteString, EntryIDSet> cursor = index.openCursor(txn); |
| | | try |
| | | { |
| | | boolean success; |
| | | // Set the lower bound if necessary. |
| | | if (lower.length() > 0) |
| | |
| | | return EntryIDSet.newDefinedSet(); |
| | | } |
| | | |
| | | ArrayList<EntryIDSet> sets = new ArrayList<>(); |
| | | // Step through the keys until we hit the upper bound or the last key. |
| | | while (success) |
| | | { |
| | |
| | | |
| | | return EntryIDSet.newSetFromUnion(sets); |
| | | } |
| | | finally |
| | | { |
| | | cursor.close(); |
| | | } |
| | | } |
| | | catch (StorageRuntimeException e) |
| | | { |
| | | logger.traceException(e); |
| | |
| | | */ |
| | | package org.opends.server.backends.pluggable; |
| | | |
| | | import static org.opends.messages.BackendMessages.*; |
| | | |
| | | import java.io.IOException; |
| | | import java.util.Collection; |
| | | import java.util.LinkedList; |
| | |
| | | import org.opends.server.types.DirectoryException; |
| | | import org.opends.server.types.InitializationException; |
| | | |
| | | import static org.opends.messages.BackendMessages.*; |
| | | |
| | | /** |
| | | * This class provides a compressed schema implementation whose definitions are |
| | | * persisted in a tree. |
| | |
| | | // Cursor through the object class database and load the object class set |
| | | // definitions. At the same time, figure out the highest token value and |
| | | // initialize the object class counter to one greater than that. |
| | | final Cursor<ByteString, ByteString> ocCursor = txn.openCursor(ocTreeName); |
| | | try |
| | | try (Cursor<ByteString, ByteString> ocCursor = txn.openCursor(ocTreeName)) |
| | | { |
| | | while (ocCursor.next()) |
| | | { |
| | |
| | | logger.traceException(e); |
| | | throw new InitializationException(ERR_COMPSCHEMA_CANNOT_DECODE_OC_TOKEN.get(e.getMessage()), e); |
| | | } |
| | | finally |
| | | { |
| | | ocCursor.close(); |
| | | } |
| | | |
| | | // Cursor through the attribute description database and load the attribute |
| | | // set definitions. |
| | | final Cursor<ByteString, ByteString> adCursor = txn.openCursor(adTreeName); |
| | | try |
| | | // Cursor through the attribute description database and load the attribute set definitions. |
| | | try (Cursor<ByteString, ByteString> adCursor = txn.openCursor(adTreeName)) |
| | | { |
| | | while (adCursor.next()) |
| | | { |
| | |
| | | logger.traceException(e); |
| | | throw new InitializationException(ERR_COMPSCHEMA_CANNOT_DECODE_AD_TOKEN.get(e.getMessage()), e); |
| | | } |
| | | finally |
| | | { |
| | | adCursor.close(); |
| | | } |
| | | } |
| | | |
| | | private boolean store(final TreeName treeName, final byte[] key, final ByteStringBuilder value) |
| | |
| | | final TimeService time = mock(TimeService.class); |
| | | when(time.now()).thenReturn(100L, 200L); |
| | | ReplicationEnvironment environment = new ReplicationEnvironment(rootPath.getAbsolutePath(), null, time); |
| | | Log<Long,ChangeNumberIndexRecord> cnIndexDB = environment.getOrCreateCNIndexDB(); |
| | | |
| | | try { |
| | | try (Log<Long,ChangeNumberIndexRecord> cnIndexDB = environment.getOrCreateCNIndexDB()) { |
| | | environment.notifyLogFileRotation(cnIndexDB); |
| | | |
| | | // check runtime change of last rotation time is effective |
| | |
| | | } |
| | | finally |
| | | { |
| | | cnIndexDB.close(); |
| | | environment.shutdown(); |
| | | } |
| | | |
| | |
| | | * Header, with the fields enclosed by brackets [] replaced by your own identifying |
| | | * information: "Portions Copyright [year] [name of copyright owner]". |
| | | * |
| | | * Copyright 2014 ForgeRock AS. |
| | | * Copyright 2014-2016 ForgeRock AS. |
| | | */ |
| | | |
| | | package org.forgerock.opendj.server.core; |
| | | |
| | | import static org.forgerock.util.Utils.closeSilently; |
| | | |
| | | import java.io.BufferedInputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | |
| | | } |
| | | |
| | | properties = new Properties(); |
| | | final InputStream is = new BufferedInputStream(stream); |
| | | try { |
| | | try (InputStream is = new BufferedInputStream(stream)) { |
| | | properties.load(is); |
| | | } catch (final IOException e) { |
| | | throw new MissingResourceException("Can't load product information " + resourceName |
| | | + " due to IO exception: " + e.getMessage(), productName, ""); |
| | | } finally { |
| | | closeSilently(is); |
| | | } |
| | | |
| | | versionFull = |