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 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.CompareRequestProtocolOp; 037 import org.opends.server.protocols.ldap.CompareResponseProtocolOp; 038 import org.opends.server.protocols.ldap.LDAPMessage; 039 import org.opends.server.protocols.ldap.ProtocolOp; 040 import org.opends.server.tools.LDAPConnection; 041 import org.opends.server.types.LDAPException; 042 043 044 045 /** 046 * This class provides the functionality for the performing an 047 * LDAP COMPARE operation based on the specified DSML request. 048 */ 049 public class DSMLCompareOperation 050 { 051 private LDAPConnection connection; 052 053 /** 054 * Create an instance with the specified LDAP connection. 055 * 056 * @param connection The LDAP connection to send the request on. 057 */ 058 public DSMLCompareOperation(LDAPConnection connection) 059 { 060 this.connection = connection; 061 } 062 063 /** 064 * Perform the LDAP COMPARE operation and send the result back to the 065 * client. 066 * 067 * @param objFactory The object factory for this operation. 068 * @param compareRequest The compare request for this operation. 069 * 070 * @return The result of the compare operation. 071 * 072 * @throws IOException If an I/O problem occurs. 073 * 074 * @throws LDAPException If an error occurs while interacting with an LDAP 075 * element. 076 * 077 * @throws ASN1Exception If an error occurs while interacting with an ASN.1 078 * element. 079 */ 080 public LDAPResult doOperation(ObjectFactory objFactory, 081 CompareRequest compareRequest) 082 throws IOException, LDAPException, ASN1Exception 083 { 084 LDAPResult compareResponse = objFactory.createLDAPResult(); 085 compareResponse.setRequestID(compareRequest.getRequestID()); 086 087 // Read the attribute name and value for the compare request. 088 AttributeValueAssertion attrValAssertion = compareRequest.getAssertion(); 089 String attrName = attrValAssertion.getName(); 090 ASN1OctetString attrValue = 091 new ASN1OctetString(attrValAssertion.getValue()); 092 093 ASN1OctetString dnStr = new ASN1OctetString(compareRequest.getDn()); 094 095 // Create and send the LDAP compare request to the server. 096 ProtocolOp op = new CompareRequestProtocolOp(dnStr, attrName, attrValue); 097 LDAPMessage msg = new LDAPMessage(DSMLServlet.nextMessageID(), op); 098 connection.getLDAPWriter().writeMessage(msg); 099 100 // Read and decode the LDAP response from the server. 101 LDAPMessage responseMessage = connection.getLDAPReader().readMessage(); 102 103 CompareResponseProtocolOp compareOp = 104 responseMessage.getCompareResponseProtocolOp(); 105 int resultCode = compareOp.getResultCode(); 106 Message errorMessage = compareOp.getErrorMessage(); 107 108 // Set the response code and error message for the DSML response. 109 compareResponse.setErrorMessage( 110 errorMessage != null ? errorMessage.toString() : null); 111 ResultCode code = objFactory.createResultCode(); 112 code.setCode(resultCode); 113 compareResponse.setResultCode(code); 114 115 if(compareOp.getMatchedDN() != null) 116 { 117 compareResponse.setMatchedDN(compareOp.getMatchedDN().toString()); 118 } 119 120 return compareResponse; 121 } 122 123 } 124