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