use of org.apache.directory.api.ldap.model.entry.ModificationOperation in project directory-ldap-api by apache.
the class ModifyRequestDsml method toDsml.
/**
* {@inheritDoc}
*/
@Override
public Element toDsml(Element root) {
Element element = super.toDsml(root);
ModifyRequest request = getDecorated();
// Dn
if (request.getName() != null) {
element.addAttribute("dn", request.getName().getName());
}
// Modifications
Collection<Modification> modifications = request.getModifications();
for (Modification modification : modifications) {
Element modElement = element.addElement("modification");
if (modification.getAttribute() != null) {
modElement.addAttribute("name", modification.getAttribute().getId());
for (Value value : modification.getAttribute()) {
if (value.getValue() != null) {
if (ParserUtils.needsBase64Encoding(value.getValue())) {
Namespace xsdNamespace = new Namespace("xsd", ParserUtils.XML_SCHEMA_URI);
Namespace xsiNamespace = new Namespace("xsi", ParserUtils.XML_SCHEMA_INSTANCE_URI);
element.getDocument().getRootElement().add(xsdNamespace);
element.getDocument().getRootElement().add(xsiNamespace);
Element valueElement = modElement.addElement("value").addText(ParserUtils.base64Encode(value.getValue()));
valueElement.addAttribute(new QName("type", xsiNamespace), "xsd:" + ParserUtils.BASE64BINARY);
} else {
modElement.addElement("value").setText(value.getValue());
}
}
}
}
ModificationOperation operation = modification.getOperation();
if (operation == ModificationOperation.ADD_ATTRIBUTE) {
modElement.addAttribute("operation", "add");
} else if (operation == ModificationOperation.REPLACE_ATTRIBUTE) {
modElement.addAttribute("operation", "replace");
} else if (operation == ModificationOperation.REMOVE_ATTRIBUTE) {
modElement.addAttribute("operation", "delete");
}
}
return element;
}
use of org.apache.directory.api.ldap.model.entry.ModificationOperation in project directory-ldap-api by apache.
the class QuirkySchemaTest method createFakeConnection.
private LdapConnection createFakeConnection(final String schemaFileName) {
return new LdapConnection() {
@Override
public void unBind() throws LdapException {
}
@Override
public void setTimeOut(long timeOut) {
}
@Override
public void setSchemaManager(SchemaManager schemaManager) {
}
@Override
public void setBinaryAttributeDetector(BinaryAttributeDetector binaryAttributeDetecter) {
}
@Override
public SearchCursor search(SearchRequest searchRequest) throws LdapException {
return null;
}
@Override
public EntryCursor search(String baseDn, String filter, SearchScope scope, String... attributes) throws LdapException {
return null;
}
@Override
public EntryCursor search(Dn baseDn, String filter, SearchScope scope, String... attributes) throws LdapException {
return null;
}
@Override
public void rename(Dn entryDn, Rdn newRdn, boolean deleteOldRdn) throws LdapException {
}
@Override
public void rename(String entryDn, String newRdn, boolean deleteOldRdn) throws LdapException {
}
@Override
public void rename(Dn entryDn, Rdn newRdn) throws LdapException {
}
@Override
public void rename(String entryDn, String newRdn) throws LdapException {
}
@Override
public void moveAndRename(String entryDn, String newDn, boolean deleteOldRdn) throws LdapException {
}
@Override
public void moveAndRename(Dn entryDn, Dn newDn, boolean deleteOldRdn) throws LdapException {
}
@Override
public void moveAndRename(String entryDn, String newDn) throws LdapException {
}
@Override
public void moveAndRename(Dn entryDn, Dn newDn) throws LdapException {
}
@Override
public void move(Dn entryDn, Dn newSuperiorDn) throws LdapException {
}
@Override
public void move(String entryDn, String newSuperiorDn) throws LdapException {
}
@Override
public ModifyDnResponse modifyDn(ModifyDnRequest modDnRequest) throws LdapException {
return null;
}
@Override
public ModifyResponse modify(ModifyRequest modRequest) throws LdapException {
return null;
}
@Override
public void modify(Entry entry, ModificationOperation modOp) throws LdapException {
}
@Override
public void modify(String dn, Modification... modifications) throws LdapException {
}
@Override
public void modify(Dn dn, Modification... modifications) throws LdapException {
}
@Override
public Entry lookup(String dn, Control[] controls, String... attributes) throws LdapException {
return lookup(new Dn(dn));
}
@Override
public Entry lookup(String dn, String... attributes) throws LdapException {
return lookup(new Dn(dn));
}
@Override
public Entry lookup(Dn dn, Control[] controls, String... attributes) throws LdapException {
return lookup(dn);
}
@Override
public Entry lookup(Dn dn, String... attributes) throws LdapException {
return lookup(dn);
}
@Override
public Entry lookup(String dn) throws LdapException {
return lookup(new Dn(dn));
}
@Override
public Entry lookup(Dn dn) throws LdapException {
if (dn.isRootDse()) {
Entry entry = new DefaultEntry(dn);
entry.add(SchemaConstants.SUBSCHEMA_SUBENTRY_AT, SCHEMA_DN);
return entry;
} else if (dn.toString().equals(SCHEMA_DN)) {
Entry entry = loadSchemaEntry(schemaFileName);
return entry;
} else {
throw new UnsupportedOperationException("Unexpected DN " + dn);
}
}
@Override
public void loadSchemaRelaxed() throws LdapException {
}
@Override
public void loadSchema() throws LdapException {
}
@Override
public boolean isRequestCompleted(int messageId) {
return true;
}
@Override
public boolean isControlSupported(String controlOID) throws LdapException {
return true;
}
@Override
public boolean isConnected() {
return true;
}
@Override
public boolean isAuthenticated() {
return false;
}
@Override
public List<String> getSupportedControls() throws LdapException {
return null;
}
@Override
public SchemaManager getSchemaManager() {
return null;
}
@Override
public Entry getRootDse(String... attributes) throws LdapException {
return lookup(Dn.ROOT_DSE);
}
@Override
public Entry getRootDse() throws LdapException {
return lookup(Dn.ROOT_DSE);
}
@Override
public LdapApiService getCodecService() {
return null;
}
@Override
public BinaryAttributeDetector getBinaryAttributeDetector() {
return null;
}
@Override
public ExtendedResponse extended(ExtendedRequest extendedRequest) throws LdapException {
return null;
}
@Override
public ExtendedResponse extended(Oid oid, byte[] value) throws LdapException {
return null;
}
@Override
public ExtendedResponse extended(Oid oid) throws LdapException {
return null;
}
@Override
public ExtendedResponse extended(String oid, byte[] value) throws LdapException {
return null;
}
@Override
public ExtendedResponse extended(String oid) throws LdapException {
return null;
}
@Override
public boolean exists(Dn dn) throws LdapException {
return false;
}
@Override
public boolean exists(String dn) throws LdapException {
return false;
}
@Override
public boolean doesFutureExistFor(int messageId) {
return false;
}
@Override
public DeleteResponse delete(DeleteRequest deleteRequest) throws LdapException {
return null;
}
@Override
public void delete(Dn dn) throws LdapException {
}
@Override
public void delete(String dn) throws LdapException {
}
@Override
public boolean connect() throws LdapException {
return true;
}
@Override
public CompareResponse compare(CompareRequest compareRequest) throws LdapException {
return null;
}
@Override
public boolean compare(Dn dn, String attributeName, byte[] value) throws LdapException {
return false;
}
@Override
public boolean compare(Dn dn, String attributeName, String value) throws LdapException {
return false;
}
@Override
public boolean compare(String dn, String attributeName, byte[] value) throws LdapException {
return false;
}
@Override
public boolean compare(String dn, String attributeName, String value) throws LdapException {
return false;
}
@Override
public void close() throws IOException {
}
@Override
public BindResponse bind(BindRequest bindRequest) throws LdapException {
return null;
}
@Override
public void bind(Dn name, String credentials) throws LdapException {
}
@Override
public void bind(Dn name) throws LdapException {
}
@Override
public void bind(String name, String credentials) throws LdapException {
}
@Override
public void bind(String name) throws LdapException {
}
@Override
public void bind() throws LdapException {
}
@Override
public void anonymousBind() throws LdapException {
}
@Override
public AddResponse add(AddRequest addRequest) throws LdapException {
return null;
}
@Override
public void add(Entry entry) throws LdapException {
}
@Override
public void abandon(AbandonRequest abandonRequest) {
}
@Override
public void abandon(int messageId) {
}
@Override
public boolean compare(String dn, String attributeName, Value value) throws LdapException {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean compare(Dn dn, String attributeName, Value value) throws LdapException {
// TODO Auto-generated method stub
return false;
}
@Override
public BindResponse bind(SaslRequest saslRequest) throws LdapException {
// TODO Auto-generated method stub
return null;
}
};
}
use of org.apache.directory.api.ldap.model.entry.ModificationOperation in project directory-ldap-api by apache.
the class LdifReader method parseModify.
/**
* Parse a modify change type.
*
* The grammar is :
* <pre>
* <changerecord> ::= "changetype:" FILL "modify" SEP <mod-spec> <mod-specs-e>
* <mod-spec> ::= "add:" <mod-val> | "delete:" <mod-val-del> | "replace:" <mod-val>
* <mod-specs-e> ::= <mod-spec>
* <mod-specs-e> | e
* <mod-val> ::= FILL ATTRIBUTE-DESCRIPTION SEP ATTRVAL-SPEC <attrval-specs-e> "-" SEP
* <mod-val-del> ::= FILL ATTRIBUTE-DESCRIPTION SEP <attrval-specs-e> "-" SEP
* <attrval-specs-e> ::= ATTRVAL-SPEC <attrval-specs> | e
* </pre>
*
* @param entry The entry to feed
* @param iter The lines
* @exception LdapLdifException If the modify operation is invalid
*/
private void parseModify(LdifEntry entry, Iterator<String> iter) throws LdapLdifException {
int state = MOD_SPEC;
String modified = null;
ModificationOperation modificationType = ModificationOperation.ADD_ATTRIBUTE;
Attribute attribute = null;
// The following flag is used to deal with empty modifications
boolean isEmptyValue = true;
while (iter.hasNext()) {
String line = iter.next();
String lowerLine = Strings.toLowerCaseAscii(line);
if (lowerLine.startsWith("-")) {
if ((state != ATTRVAL_SPEC_OR_SEP) && (state != ATTRVAL_SPEC)) {
String msg = I18n.err(I18n.ERR_13413_BAD_MODIFY_SEPARATOR, lineNumber);
LOG.error(msg);
throw new LdapLdifException(msg);
} else {
if (isEmptyValue) {
if (state == ATTRVAL_SPEC_OR_SEP) {
entry.addModification(modificationType, modified);
} else {
// Update the entry with a null value
entry.addModification(modificationType, modified, null);
}
} else {
// Update the entry with the attribute
entry.addModification(modificationType, attribute);
}
state = MOD_SPEC;
isEmptyValue = true;
}
} else if (lowerLine.startsWith("add:")) {
if ((state != MOD_SPEC) && (state != ATTRVAL_SPEC)) {
String msg = I18n.err(I18n.ERR_13414_BAD_MODIFY_SEPARATOR_2, lineNumber);
LOG.error(msg);
throw new LdapLdifException(msg);
}
modified = Strings.trim(line.substring("add:".length()));
modificationType = ModificationOperation.ADD_ATTRIBUTE;
attribute = new DefaultAttribute(modified);
state = ATTRVAL_SPEC;
} else if (lowerLine.startsWith("delete:")) {
if ((state != MOD_SPEC) && (state != ATTRVAL_SPEC)) {
String msg = I18n.err(I18n.ERR_13414_BAD_MODIFY_SEPARATOR_2, lineNumber);
LOG.error(msg);
throw new LdapLdifException(msg);
}
modified = Strings.trim(line.substring("delete:".length()));
modificationType = ModificationOperation.REMOVE_ATTRIBUTE;
attribute = new DefaultAttribute(modified);
isEmptyValue = false;
state = ATTRVAL_SPEC_OR_SEP;
} else if (lowerLine.startsWith("replace:")) {
if ((state != MOD_SPEC) && (state != ATTRVAL_SPEC)) {
String msg = I18n.err(I18n.ERR_13414_BAD_MODIFY_SEPARATOR_2, lineNumber);
LOG.error(msg);
throw new LdapLdifException(msg);
}
modified = Strings.trim(line.substring("replace:".length()));
modificationType = ModificationOperation.REPLACE_ATTRIBUTE;
if (schemaManager != null) {
AttributeType attributeType = schemaManager.getAttributeType(modified);
attribute = new DefaultAttribute(modified, attributeType);
} else {
attribute = new DefaultAttribute(modified);
}
state = ATTRVAL_SPEC_OR_SEP;
} else {
if ((state != ATTRVAL_SPEC) && (state != ATTRVAL_SPEC_OR_SEP)) {
String msg = I18n.err(I18n.ERR_13413_BAD_MODIFY_SEPARATOR, lineNumber);
LOG.error(msg);
throw new LdapLdifException(msg);
}
// A standard AttributeType/AttributeValue pair
int colonIndex = line.indexOf(':');
String attributeType = line.substring(0, colonIndex);
if (!attributeType.equalsIgnoreCase(modified)) {
LOG.error(I18n.err(I18n.ERR_13415_MOD_ATTR_AND_VALUE_SPEC_NOT_EQUAL, lineNumber));
throw new LdapLdifException(I18n.err(I18n.ERR_13454_BAD_MODIFY_ATTRIBUTE));
}
// We should *not* have a Dn twice
if ("dn".equalsIgnoreCase(attributeType)) {
LOG.error(I18n.err(I18n.ERR_13400_ENTRY_WITH_TWO_DNS, lineNumber));
throw new LdapLdifException(I18n.err(I18n.ERR_13439_LDIF_ENTRY_WITH_TWO_DNS));
}
Object attributeValue = parseValue(attributeType, line, colonIndex);
try {
if (attributeValue instanceof String) {
attribute.add((String) attributeValue);
} else {
attribute.add((byte[]) attributeValue);
}
} catch (LdapInvalidAttributeValueException liave) {
throw new LdapLdifException(liave.getMessage(), liave);
}
isEmptyValue = false;
state = ATTRVAL_SPEC_OR_SEP;
}
}
if (state != MOD_SPEC) {
String msg = I18n.err(I18n.ERR_13414_BAD_MODIFY_SEPARATOR_2, lineNumber);
LOG.error(msg);
throw new LdapLdifException(msg);
}
}
use of org.apache.directory.api.ldap.model.entry.ModificationOperation in project directory-ldap-api by apache.
the class ModifyRequestImplTest method testEqualsDiffImpl.
/**
* Tests for equality even when another BindRequest implementation is used.
*/
@Test
public void testEqualsDiffImpl() throws LdapException {
ModifyRequest req0 = new ModifyRequest() {
public Collection<Modification> getModifications() {
List<Modification> list = new ArrayList<Modification>();
try {
Attribute attr = new DefaultAttribute("attr0");
attr.add("val0");
attr.add("val1");
attr.add("val2");
Modification item = new DefaultModification(ModificationOperation.ADD_ATTRIBUTE, attr);
list.add(item);
attr = new DefaultAttribute("attr1");
attr.add("val3");
item = new DefaultModification(ModificationOperation.REMOVE_ATTRIBUTE, attr);
list.add(item);
attr = new DefaultAttribute("attr2");
attr.add("val4");
attr.add("val5");
item = new DefaultModification(ModificationOperation.REPLACE_ATTRIBUTE, attr);
list.add(item);
} catch (LdapInvalidAttributeValueException liave) {
// Can't happen
}
return list;
}
public ModifyRequest addModification(Modification mod) {
return this;
}
public ModifyRequest removeModification(Modification mod) {
return this;
}
public Dn getName() {
try {
return new Dn("cn=admin,dc=apache,dc=org");
} catch (Exception e) {
// do nothing
return null;
}
}
public ModifyRequest setName(Dn name) {
return this;
}
public MessageTypeEnum getResponseType() {
return MessageTypeEnum.MODIFY_RESPONSE;
}
public boolean hasResponse() {
return true;
}
public MessageTypeEnum getType() {
return MessageTypeEnum.MODIFY_REQUEST;
}
public Map<String, Control> getControls() {
return EMPTY_CONTROL_MAP;
}
public ModifyRequest addControl(Control a_control) {
return this;
}
public ModifyRequest removeControl(Control a_control) {
return this;
}
public int getMessageId() {
return 45;
}
public Object get(Object a_key) {
return null;
}
public Object put(Object a_key, Object a_value) {
return null;
}
public void abandon() {
}
public boolean isAbandoned() {
return false;
}
public ModifyRequest addAbandonListener(AbandonListener listener) {
return this;
}
public ModifyResponse getResultResponse() {
return null;
}
public ModifyRequest addAllControls(Control[] controls) {
return this;
}
public boolean hasControl(String oid) {
return false;
}
public Control getControl(String oid) {
return null;
}
public ModifyRequest setMessageId(int messageId) {
return this;
}
public ModifyRequest addModification(Attribute attr, ModificationOperation modOp) {
return this;
}
public ModifyRequest replace(String attributeName) {
return this;
}
public ModifyRequest replace(String attributeName, String... attributeValue) {
return this;
}
public ModifyRequest replace(String attributeName, byte[]... attributeValue) {
return this;
}
public ModifyRequest replace(Attribute attr) {
return this;
}
public ModifyRequest add(String attributeName, String... attributeValue) {
return this;
}
public ModifyRequest add(String attributeName, byte[]... attributeValue) {
return this;
}
public ModifyRequest add(Attribute attr) {
return this;
}
public ModifyRequest remove(String attributeName, String... attributeValue) {
return this;
}
public ModifyRequest remove(String attributeName, byte[]... attributeValue) {
return this;
}
public ModifyRequest remove(Attribute attr) {
return this;
}
public ModifyRequest remove(String attributerName) {
return this;
}
};
ModifyRequestImpl req1 = getRequest();
assertTrue(req1.equals(req0));
}
Aggregations