001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2012-2015 ForgeRock AS.
026 */
027package org.opends.dsml.protocol;
028
029
030
031import java.io.IOException;
032import java.util.ArrayList;
033import java.util.List;
034
035import org.forgerock.i18n.LocalizableMessage;
036import org.forgerock.opendj.ldap.DecodeException;
037import org.opends.server.protocols.ldap.LDAPAttribute;
038import org.opends.server.protocols.ldap.LDAPMessage;
039import org.opends.server.protocols.ldap.LDAPModification;
040import org.opends.server.protocols.ldap.ModifyRequestProtocolOp;
041import org.opends.server.protocols.ldap.ModifyResponseProtocolOp;
042import org.opends.server.protocols.ldap.ProtocolOp;
043import org.opends.server.tools.LDAPConnection;
044import org.forgerock.opendj.ldap.ByteString;
045import org.opends.server.types.LDAPException;
046import org.forgerock.opendj.ldap.ModificationType;
047import org.opends.server.types.RawModification;
048
049
050
051/**
052 * This class provides the functionality for the performing an
053 * LDAP MODIFY operation based on the specified DSML request.
054 */
055public class DSMLModifyOperation
056{
057  private LDAPConnection connection;
058
059  /**
060   * Create the instance with the specified LDAP connection.
061   *
062   * @param connection    The LDAP connection to send the request.
063   */
064  public DSMLModifyOperation(LDAPConnection connection)
065  {
066    this.connection = connection;
067  }
068
069
070  /**
071   * Perform the LDAP Modify operation and send the result back to the client.
072   *
073   * @param  objFactory     The object factory for this operation.
074   * @param  modifyRequest  The modify request for this operation.
075   * @param  controls       Any required controls (e.g. for proxy authz).
076   *
077   * @return  The result of the modify operation.
078   *
079   * @throws  IOException  If an I/O problem occurs.
080   *
081   * @throws  LDAPException  If an error occurs while interacting with an LDAP
082   *                         element.
083   *
084   * @throws  DecodeException  If an error occurs while interacting with an ASN.1
085   *                         element.
086   */
087  public LDAPResult doOperation(ObjectFactory objFactory,
088        ModifyRequest modifyRequest,
089        List<org.opends.server.types.Control> controls)
090        throws IOException, LDAPException, DecodeException
091  {
092    LDAPResult modResponse = objFactory.createLDAPResult();
093    modResponse.setRequestID(modifyRequest.getRequestID());
094
095    ArrayList<RawModification> modifications = new ArrayList<>();
096
097    // Read the modification type from the DSML request.
098    List<DsmlModification> mods = modifyRequest.getModification();
099    for(DsmlModification attr : mods)
100    {
101      String operation = attr.getOperation();
102      ModificationType type = ModificationType.ADD;
103      if(operation.equals("delete"))
104      {
105        type = ModificationType.DELETE;
106      } else if(operation.equals("replace"))
107      {
108        type = ModificationType.REPLACE;
109      }
110
111      // Read the attribute name and values.
112      String attrType = attr.getName();
113      ArrayList<ByteString> values = new ArrayList<>();
114
115      for (Object val : attr.getValue())
116      {
117        values.add(ByteStringUtility.convertValue(val));
118      }
119      LDAPAttribute ldapAttr = new LDAPAttribute(attrType, values);
120
121      LDAPModification ldapMod = new LDAPModification(type, ldapAttr);
122      modifications.add(ldapMod);
123
124    }
125
126    ByteString dnStr = ByteString.valueOf(modifyRequest.getDn());
127
128    // Create and send the LDAP request to the server.
129    ProtocolOp op = new ModifyRequestProtocolOp(dnStr, modifications);
130    LDAPMessage msg = new LDAPMessage(DSMLServlet.nextMessageID(), op,
131        controls);
132    connection.getLDAPWriter().writeMessage(msg);
133
134    // Read and parse the LDAP response from the server.
135    LDAPMessage responseMessage = connection.getLDAPReader().readMessage();
136
137    ModifyResponseProtocolOp modOp =
138         responseMessage.getModifyResponseProtocolOp();
139    int resultCode = modOp.getResultCode();
140    LocalizableMessage errorMessage = modOp.getErrorMessage();
141
142    // Set the result code and error message for the DSML response.
143    modResponse.setErrorMessage(
144            errorMessage != null ? errorMessage.toString() : null);
145    ResultCode code = ResultCodeFactory.create(objFactory, resultCode);
146    modResponse.setResultCode(code);
147
148    return modResponse;
149  }
150
151}
152