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
010 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012 * See the License for the specific language governing permissions
013 * and limitations under the License.
014 *
015 * When distributing Covered Code, include this CDDL HEADER in each
016 * file and include the License file at
017 * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
018 * add the following below this CDDL HEADER, with the fields enclosed
019 * by brackets "[]" replaced with your own identifying information:
020 * Portions Copyright [yyyy] [name of copyright owner]
021 *
022 * CDDL HEADER END
023 *
024 *
025 * Copyright 2006-2008 Sun Microsystems, Inc.
026 */
027 package org.opends.dsml.protocol;
028
029
030
031 import java.io.IOException;
032 import java.util.ArrayList;
033 import java.util.List;
034
035 import org.opends.messages.Message;
036 import org.opends.server.protocols.asn1.ASN1Exception;
037 import org.opends.server.protocols.asn1.ASN1OctetString;
038 import org.opends.server.protocols.ldap.LDAPAttribute;
039 import org.opends.server.protocols.ldap.LDAPMessage;
040 import org.opends.server.protocols.ldap.LDAPModification;
041 import org.opends.server.protocols.ldap.ModifyRequestProtocolOp;
042 import org.opends.server.protocols.ldap.ModifyResponseProtocolOp;
043 import org.opends.server.protocols.ldap.ProtocolOp;
044 import org.opends.server.tools.LDAPConnection;
045 import org.opends.server.types.LDAPException;
046 import org.opends.server.types.ModificationType;
047 import 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 */
055 public 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 *
076 * @return The result of the add operation.
077 *
078 * @throws IOException If an I/O problem occurs.
079 *
080 * @throws LDAPException If an error occurs while interacting with an LDAP
081 * element.
082 *
083 * @throws ASN1Exception If an error occurs while interacting with an ASN.1
084 * element.
085 */
086 public LDAPResult doOperation(ObjectFactory objFactory,
087 ModifyRequest modifyRequest)
088 throws IOException, LDAPException, ASN1Exception
089 {
090 LDAPResult modResponse = objFactory.createLDAPResult();
091 modResponse.setRequestID(modifyRequest.getRequestID());
092
093 ArrayList<RawModification> modifications =
094 new ArrayList<RawModification> ();
095
096 // Read the modification type from the DSML request.
097 List<DsmlModification> mods = modifyRequest.getModification();
098 for(DsmlModification attr : mods)
099 {
100 String operation = attr.getOperation();
101 ModificationType type = ModificationType.ADD;
102 if(operation.equals("delete"))
103 {
104 type = ModificationType.DELETE;
105 } else if(operation.equals("replace"))
106 {
107 type = ModificationType.REPLACE;
108 }
109
110 // Read the attribute name and values.
111 String attrType = attr.getName();
112 ArrayList<ASN1OctetString> values = new ArrayList<ASN1OctetString> ();
113
114 List<String> vals = attr.getValue();
115 for(String val : vals)
116 {
117 values.add(new ASN1OctetString(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 ASN1OctetString dnStr = new ASN1OctetString(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 connection.getLDAPWriter().writeMessage(msg);
132
133 // Read and parse the LDAP response from the server.
134 LDAPMessage responseMessage = connection.getLDAPReader().readMessage();
135
136 ModifyResponseProtocolOp modOp =
137 responseMessage.getModifyResponseProtocolOp();
138 int resultCode = modOp.getResultCode();
139 Message errorMessage = modOp.getErrorMessage();
140
141 // Set the result code and error message for the DSML response.
142 modResponse.setErrorMessage(
143 errorMessage != null ? errorMessage.toString() : null);
144 ResultCode code = objFactory.createResultCode();
145 code.setCode(resultCode);
146 modResponse.setResultCode(code);
147
148 return modResponse;
149 }
150
151 }
152