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.AddRequestProtocolOp; 038import org.opends.server.protocols.ldap.AddResponseProtocolOp; 039import org.opends.server.protocols.ldap.LDAPAttribute; 040import org.opends.server.protocols.ldap.LDAPMessage; 041import org.opends.server.protocols.ldap.ProtocolOp; 042import org.opends.server.tools.LDAPConnection; 043import org.forgerock.opendj.ldap.ByteString; 044import org.opends.server.types.LDAPException; 045import org.opends.server.types.RawAttribute; 046 047 048 049/** 050 * This class provides the functionality for the performing an 051 * LDAP ADD operation based on the specified DSML request. 052 */ 053public class DSMLAddOperation 054{ 055 056 private LDAPConnection connection; 057 058 /** 059 * Create the instance with the specified LDAP connection. 060 * 061 * @param connection The LDAP connection to the server. 062 */ 063 public DSMLAddOperation(LDAPConnection connection) 064 { 065 this.connection = connection; 066 } 067 068 /** 069 * Perform the LDAP ADD operation and return the result to the client. 070 * 071 * @param objFactory The object factory for this operation. 072 * @param addRequest The add request for this operation. 073 * @param controls Any required controls (e.g. for proxy authz). 074 * 075 * @return The result of the add operation. 076 * 077 * @throws IOException If an I/O problem occurs. 078 * 079 * @throws LDAPException If an error occurs while interacting with an LDAP 080 * element. 081 * 082 * @throws DecodeException If an error occurs while interacting with an ASN.1 083 * element. 084 */ 085 public LDAPResult doOperation(ObjectFactory objFactory, 086 AddRequest addRequest, 087 List<org.opends.server.types.Control> controls) 088 throws IOException, LDAPException, DecodeException 089 { 090 LDAPResult addResponse = objFactory.createLDAPResult(); 091 addResponse.setRequestID(addRequest.getRequestID()); 092 093 ByteString dnStr = ByteString.valueOf(addRequest.getDn()); 094 ArrayList<RawAttribute> attributes = new ArrayList<>(); 095 096 List<DsmlAttr> addList = addRequest.getAttr(); 097 for(DsmlAttr attr : addList) 098 { 099 ArrayList<ByteString> values = new ArrayList<>(); 100 List<Object> vals = attr.getValue(); 101 for(Object val : vals) 102 { 103 values.add(ByteStringUtility.convertValue(val)); 104 } 105 LDAPAttribute ldapAttribute = new LDAPAttribute(attr.getName(), values); 106 attributes.add(ldapAttribute); 107 } 108 109 // Create and send the LDAP request to the server. 110 ProtocolOp op = new AddRequestProtocolOp(dnStr, attributes); 111 LDAPMessage msg = new LDAPMessage(DSMLServlet.nextMessageID(), op, 112 controls); 113 connection.getLDAPWriter().writeMessage(msg); 114 115 // Read and decode the LDAP response from the server. 116 LDAPMessage responseMessage = connection.getLDAPReader().readMessage(); 117 118 AddResponseProtocolOp addOp = responseMessage.getAddResponseProtocolOp(); 119 int resultCode = addOp.getResultCode(); 120 LocalizableMessage errorMessage = addOp.getErrorMessage(); 121 122 // Set the result code and error message for the DSML response. 123 addResponse.setErrorMessage( 124 errorMessage != null ? errorMessage.toString() : null); 125 ResultCode code = ResultCodeFactory.create(objFactory, resultCode); 126 addResponse.setResultCode(code); 127 128 return addResponse; 129 } 130 131} 132