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-2014 ForgeRock AS. 026 */ 027package org.opends.dsml.protocol; 028 029 030 031import java.io.IOException; 032import java.util.List; 033 034import org.forgerock.i18n.LocalizableMessage; 035import org.forgerock.opendj.ldap.DecodeException; 036import org.opends.server.protocols.ldap.CompareRequestProtocolOp; 037import org.opends.server.protocols.ldap.CompareResponseProtocolOp; 038import org.opends.server.protocols.ldap.LDAPMessage; 039import org.opends.server.protocols.ldap.ProtocolOp; 040import org.opends.server.tools.LDAPConnection; 041import org.forgerock.opendj.ldap.ByteString; 042import org.opends.server.types.LDAPException; 043 044 045 046/** 047 * This class provides the functionality for the performing an 048 * LDAP COMPARE operation based on the specified DSML request. 049 */ 050public class DSMLCompareOperation 051{ 052 private LDAPConnection connection; 053 054 /** 055 * Create an instance with the specified LDAP connection. 056 * 057 * @param connection The LDAP connection to send the request on. 058 */ 059 public DSMLCompareOperation(LDAPConnection connection) 060 { 061 this.connection = connection; 062 } 063 064 /** 065 * Perform the LDAP COMPARE operation and send the result back to the 066 * client. 067 * 068 * @param objFactory The object factory for this operation. 069 * @param compareRequest The compare request for this operation. 070 * @param controls Any required controls (e.g. for proxy authz). 071 * 072 * @return The result of the compare 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 DecodeException If an error occurs while interacting with an ASN.1 080 * element. 081 */ 082 public LDAPResult doOperation(ObjectFactory objFactory, 083 CompareRequest compareRequest, 084 List<org.opends.server.types.Control> controls) 085 throws IOException, LDAPException, DecodeException 086 { 087 LDAPResult compareResponse = objFactory.createLDAPResult(); 088 compareResponse.setRequestID(compareRequest.getRequestID()); 089 090 // Read the attribute name and value for the compare request. 091 AttributeValueAssertion attrValAssertion = compareRequest.getAssertion(); 092 String attrName = attrValAssertion.getName(); 093 Object assertion = attrValAssertion.getValue(); 094 ByteString attrValue = ByteStringUtility.convertValue(assertion); 095 ByteString dnStr = ByteString.valueOf(compareRequest.getDn()); 096 097 // Create and send the LDAP compare request to the server. 098 ProtocolOp op = new CompareRequestProtocolOp(dnStr, attrName, attrValue); 099 LDAPMessage msg = new LDAPMessage(DSMLServlet.nextMessageID(), op, 100 controls); 101 connection.getLDAPWriter().writeMessage(msg); 102 103 // Read and decode the LDAP response from the server. 104 LDAPMessage responseMessage = connection.getLDAPReader().readMessage(); 105 106 CompareResponseProtocolOp compareOp = 107 responseMessage.getCompareResponseProtocolOp(); 108 int resultCode = compareOp.getResultCode(); 109 LocalizableMessage errorMessage = compareOp.getErrorMessage(); 110 111 // Set the response code and error message for the DSML response. 112 compareResponse.setErrorMessage( 113 errorMessage != null ? errorMessage.toString() : null); 114 ResultCode code = ResultCodeFactory.create(objFactory, resultCode); 115 compareResponse.setResultCode(code); 116 117 if(compareOp.getMatchedDN() != null) 118 { 119 compareResponse.setMatchedDN(compareOp.getMatchedDN().toString()); 120 } 121 122 return compareResponse; 123 } 124 125} 126