Search in sources :

Example 1 with AddRequest

use of org.forgerock.opendj.ldap.requests.AddRequest in project OpenAM by OpenRock.

the class DataLayer method addEntry.

/**
     * Adds entry to the server.
     * 
     * @param principal Authenticated Principal.
     * @param guid Distinguished name.
     * @param attrSet attribute set containing name/value pairs.
     * @exception AccessRightsException if insufficient access>
     * @exception EntryAlreadyExistsException if the entry already exists.
     * @exception UMSException if fail to add entry.
     *
     * @supported.api
     */
public void addEntry(java.security.Principal principal, Guid guid, AttrSet attrSet) throws UMSException {
    String id = guid.getDn();
    ResultCode errorCode;
    try {
        AddRequest request = LDAPRequests.newAddRequest(id);
        for (Attribute attribute : attrSet.toLDAPAttributeSet()) {
            request.addAttribute(attribute);
        }
        int retry = 0;
        while (retry <= connNumRetry) {
            if (debug.messageEnabled()) {
                debug.message("DataLayer.addEntry retry: " + retry);
            }
            try (Connection conn = getConnection(principal)) {
                conn.add(request);
                return;
            } catch (LdapException e) {
                errorCode = e.getResult().getResultCode();
                if (!retryErrorCodes.contains(errorCode) || retry == connNumRetry) {
                    throw e;
                }
                retry++;
                try {
                    Thread.sleep(connRetryInterval);
                } catch (InterruptedException ex) {
                }
            }
        }
    } catch (LdapException e) {
        if (debug.warningEnabled()) {
            debug.warning("Exception in DataLayer.addEntry for DN: " + id, e);
        }
        errorCode = e.getResult().getResultCode();
        String[] args = { id };
        if (ResultCode.ENTRY_ALREADY_EXISTS.equals(errorCode)) {
            throw new EntryAlreadyExistsException(i18n.getString(IUMSConstants.ENTRY_ALREADY_EXISTS, args), e);
        } else if (ResultCode.INSUFFICIENT_ACCESS_RIGHTS.equals(errorCode)) {
            throw new AccessRightsException(i18n.getString(IUMSConstants.INSUFFICIENT_ACCESS_ADD, args), e);
        } else {
            throw new UMSException(i18n.getString(IUMSConstants.UNABLE_TO_ADD_ENTRY, args), e);
        }
    }
}
Also used : AddRequest(org.forgerock.opendj.ldap.requests.AddRequest) Attribute(org.forgerock.opendj.ldap.Attribute) Connection(org.forgerock.opendj.ldap.Connection) ByteString(org.forgerock.opendj.ldap.ByteString) LdapException(org.forgerock.opendj.ldap.LdapException) ResultCode(org.forgerock.opendj.ldap.ResultCode)

Example 2 with AddRequest

use of org.forgerock.opendj.ldap.requests.AddRequest in project OpenAM by OpenRock.

the class LdifUtils method createSchemaFromLDIF.

/**
     * Creates LDAP schema from LDIF file.
     *
     * @param ldif LDIF object.
     * @param ld LDAP Connection.
     * @throws IOException If an error occurs when reading the LDIF file.
     */
public static void createSchemaFromLDIF(LDIFChangeRecordReader ldif, final Connection ld) throws IOException {
    while (ldif.hasNext()) {
        final ChangeRecord changeRecord = ldif.readChangeRecord();
        changeRecord.accept(new ChangeRecordVisitor<Void, Void>() {

            @Override
            public Void visitChangeRecord(Void aVoid, AddRequest change) {
                try {
                    change.addControl(TransactionIdControl.newControl(AuditRequestContext.createSubTransactionIdValue()));
                    ld.add(change);
                } catch (LdapException e) {
                    if (ResultCode.ENTRY_ALREADY_EXISTS.equals(e.getResult().getResultCode())) {
                        for (Attribute attr : change.getAllAttributes()) {
                            ModifyRequest modifyRequest = LDAPRequests.newModifyRequest(change.getName());
                            modifyRequest.addModification(new Modification(ModificationType.ADD, attr));
                            try {
                                ld.modify(modifyRequest);
                            } catch (LdapException ex) {
                                DEBUG.warning("LDAPUtils.createSchemaFromLDIF - Could not modify schema: {}", modifyRequest, ex);
                            }
                        }
                    } else {
                        DEBUG.warning("LDAPUtils.createSchemaFromLDIF - Could not add to schema: {}", change, e);
                    }
                }
                return null;
            }

            @Override
            public Void visitChangeRecord(Void aVoid, ModifyRequest change) {
                try {
                    change.addControl(TransactionIdControl.newControl(AuditRequestContext.createSubTransactionIdValue()));
                    ld.modify(change);
                } catch (LdapException e) {
                    DEBUG.warning("LDAPUtils.createSchemaFromLDIF - Could not modify schema: {}", change, e);
                }
                return null;
            }

            @Override
            public Void visitChangeRecord(Void aVoid, ModifyDNRequest change) {
                return null;
            }

            @Override
            public Void visitChangeRecord(Void aVoid, DeleteRequest change) {
                DEBUG.message("Delete request ignored: {}", changeRecord);
                return null;
            }
        }, null);
    }
}
Also used : AddRequest(org.forgerock.opendj.ldap.requests.AddRequest) ModifyDNRequest(org.forgerock.opendj.ldap.requests.ModifyDNRequest) Modification(org.forgerock.opendj.ldap.Modification) Attribute(org.forgerock.opendj.ldap.Attribute) ModifyRequest(org.forgerock.opendj.ldap.requests.ModifyRequest) ChangeRecord(org.forgerock.opendj.ldif.ChangeRecord) LdapException(org.forgerock.opendj.ldap.LdapException) DeleteRequest(org.forgerock.opendj.ldap.requests.DeleteRequest)

Example 3 with AddRequest

use of org.forgerock.opendj.ldap.requests.AddRequest in project OpenAM by OpenRock.

the class UmaLabelsStore method createLabel.

private ResourceSetLabel createLabel(String realm, String username, ResourceSetLabel label, String id, Connection connection) throws LdapException, InternalServerErrorException {
    final AddRequest addRequest = LDAPRequests.newAddRequest(getLabelDn(realm, username, id)).addAttribute("objectClass", "top", OBJECT_CLASS).addAttribute(ID_ATTR, id).addAttribute(NAME_ATTR, label.getName()).addAttribute(TYPE_ATTR, label.getType().name());
    if (CollectionUtils.isNotEmpty(label.getResourceSetIds())) {
        addRequest.addAttribute(RESOURCE_SET_ATTR, label.getResourceSetIds().toArray());
    }
    Result result = connection.add(addRequest);
    if (!result.isSuccess()) {
        throw new InternalServerErrorException("Unknown unsuccessful request");
    }
    return new ResourceSetLabel(id, label.getName(), label.getType(), label.getResourceSetIds());
}
Also used : AddRequest(org.forgerock.opendj.ldap.requests.AddRequest) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) Result(org.forgerock.opendj.ldap.responses.Result)

Aggregations

AddRequest (org.forgerock.opendj.ldap.requests.AddRequest)3 Attribute (org.forgerock.opendj.ldap.Attribute)2 LdapException (org.forgerock.opendj.ldap.LdapException)2 InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)1 ByteString (org.forgerock.opendj.ldap.ByteString)1 Connection (org.forgerock.opendj.ldap.Connection)1 Modification (org.forgerock.opendj.ldap.Modification)1 ResultCode (org.forgerock.opendj.ldap.ResultCode)1 DeleteRequest (org.forgerock.opendj.ldap.requests.DeleteRequest)1 ModifyDNRequest (org.forgerock.opendj.ldap.requests.ModifyDNRequest)1 ModifyRequest (org.forgerock.opendj.ldap.requests.ModifyRequest)1 Result (org.forgerock.opendj.ldap.responses.Result)1 ChangeRecord (org.forgerock.opendj.ldif.ChangeRecord)1