001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2006-2008 Sun Microsystems, Inc. 015 * Portions Copyright 2012-2015 ForgeRock AS. 016 */ 017package org.opends.dsml.protocol; 018 019 020 021import java.io.IOException; 022import java.util.ArrayList; 023import java.util.List; 024 025import org.forgerock.i18n.LocalizableMessage; 026import org.forgerock.opendj.ldap.DecodeException; 027import org.opends.server.protocols.ldap.AddRequestProtocolOp; 028import org.opends.server.protocols.ldap.AddResponseProtocolOp; 029import org.opends.server.protocols.ldap.LDAPAttribute; 030import org.opends.server.protocols.ldap.LDAPMessage; 031import org.opends.server.protocols.ldap.ProtocolOp; 032import org.opends.server.tools.LDAPConnection; 033import org.forgerock.opendj.ldap.ByteString; 034import org.opends.server.types.LDAPException; 035import org.opends.server.types.RawAttribute; 036 037 038 039/** 040 * This class provides the functionality for the performing an 041 * LDAP ADD operation based on the specified DSML request. 042 */ 043public class DSMLAddOperation 044{ 045 046 private LDAPConnection connection; 047 048 /** 049 * Create the instance with the specified LDAP connection. 050 * 051 * @param connection The LDAP connection to the server. 052 */ 053 public DSMLAddOperation(LDAPConnection connection) 054 { 055 this.connection = connection; 056 } 057 058 /** 059 * Perform the LDAP ADD operation and return the result to the client. 060 * 061 * @param objFactory The object factory for this operation. 062 * @param addRequest The add request for this operation. 063 * @param controls Any required controls (e.g. for proxy authz). 064 * 065 * @return The result of the add operation. 066 * 067 * @throws IOException If an I/O problem occurs. 068 * 069 * @throws LDAPException If an error occurs while interacting with an LDAP 070 * element. 071 * 072 * @throws DecodeException If an error occurs while interacting with an ASN.1 073 * element. 074 */ 075 public LDAPResult doOperation(ObjectFactory objFactory, 076 AddRequest addRequest, 077 List<org.opends.server.types.Control> controls) 078 throws IOException, LDAPException, DecodeException 079 { 080 LDAPResult addResponse = objFactory.createLDAPResult(); 081 addResponse.setRequestID(addRequest.getRequestID()); 082 083 ByteString dnStr = ByteString.valueOfUtf8(addRequest.getDn()); 084 ArrayList<RawAttribute> attributes = new ArrayList<>(); 085 086 List<DsmlAttr> addList = addRequest.getAttr(); 087 for(DsmlAttr attr : addList) 088 { 089 ArrayList<ByteString> values = new ArrayList<>(); 090 List<Object> vals = attr.getValue(); 091 for(Object val : vals) 092 { 093 values.add(ByteStringUtility.convertValue(val)); 094 } 095 LDAPAttribute ldapAttribute = new LDAPAttribute(attr.getName(), values); 096 attributes.add(ldapAttribute); 097 } 098 099 // Create and send the LDAP request to the server. 100 ProtocolOp op = new AddRequestProtocolOp(dnStr, attributes); 101 LDAPMessage msg = new LDAPMessage(DSMLServlet.nextMessageID(), op, 102 controls); 103 connection.getLDAPWriter().writeMessage(msg); 104 105 // Read and decode the LDAP response from the server. 106 LDAPMessage responseMessage = connection.getLDAPReader().readMessage(); 107 108 AddResponseProtocolOp addOp = responseMessage.getAddResponseProtocolOp(); 109 int resultCode = addOp.getResultCode(); 110 LocalizableMessage errorMessage = addOp.getErrorMessage(); 111 112 // Set the result code and error message for the DSML response. 113 addResponse.setErrorMessage( 114 errorMessage != null ? errorMessage.toString() : null); 115 ResultCode code = ResultCodeFactory.create(objFactory, resultCode); 116 addResponse.setResultCode(code); 117 118 return addResponse; 119 } 120 121} 122