001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020 021 package org.apache.directory.shared.dsmlv2.reponse; 022 023 024 import java.util.List; 025 026 import org.apache.directory.shared.dsmlv2.DsmlDecorator; 027 import org.apache.directory.shared.dsmlv2.ParserUtils; 028 import org.apache.directory.shared.ldap.codec.LdapMessageCodec; 029 import org.apache.directory.shared.ldap.codec.LdapResultCodec; 030 import org.apache.directory.shared.ldap.util.LdapURL; 031 import org.apache.directory.shared.ldap.message.ResultCodeEnum; 032 import org.apache.directory.shared.ldap.name.DN; 033 import org.dom4j.Element; 034 035 036 /** 037 * DSML Decorator for the LdapResult class. 038 * 039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 040 * @version $Rev$, $Date$ 041 */ 042 public class LdapResultDsml implements DsmlDecorator 043 { 044 /** The LDAP Result to decorate */ 045 private LdapResultCodec result; 046 047 /** The associated LDAP Message */ 048 private LdapMessageCodec message; 049 050 051 /** 052 * Creates a new instance of LdapResultDsml. 053 * 054 * @param result 055 * the LdapResult to decorate 056 * @param message 057 * the associated message 058 */ 059 public LdapResultDsml( LdapResultCodec result, LdapMessageCodec message ) 060 { 061 this.result = result; 062 this.message = message; 063 } 064 065 066 /* (non-Javadoc) 067 * @see org.apache.directory.shared.dsmlv2.reponse.DsmlDecorator#toDsml(org.dom4j.Element) 068 */ 069 public Element toDsml( Element root ) 070 { 071 072 // RequestID 073 int requestID = message.getMessageId(); 074 if ( requestID != 0 ) 075 { 076 root.addAttribute( "requestID", "" + requestID ); 077 } 078 079 // Matched DN 080 String matchedDN = result.getMatchedDN(); 081 if ( !matchedDN.equals( "" ) ) 082 { 083 root.addAttribute( "matchedDN", matchedDN ); 084 } 085 086 // Controls 087 ParserUtils.addControls( root, message.getControls() ); 088 089 // ResultCode 090 Element resultCodeElement = root.addElement( "resultCode" ); 091 resultCodeElement.addAttribute( "code", "" + result.getResultCode().getResultCode() ); 092 resultCodeElement.addAttribute( "descr", LdapResultEnum.getResultCodeDescr( result.getResultCode() ) ); 093 094 // ErrorMessage 095 String errorMessage = ( result.getErrorMessage() ); 096 if ( ( errorMessage != null ) && ( !errorMessage.equals( "" ) ) ) 097 { 098 Element errorMessageElement = root.addElement( "errorMessage" ); 099 errorMessageElement.addText( errorMessage ); 100 } 101 102 // Referals 103 List<LdapURL> referals = result.getReferrals(); 104 if ( referals != null ) 105 { 106 for ( int i = 0; i < referals.size(); i++ ) 107 { 108 Element referalElement = root.addElement( "referal" ); 109 referalElement.addText( referals.get( i ).toString() ); 110 } 111 } 112 113 return root; 114 } 115 116 117 /** 118 * Initialize the referrals list 119 */ 120 public void initReferrals() 121 { 122 result.initReferrals(); 123 } 124 125 126 /** 127 * Get the error message 128 * 129 * @return Returns the errorMessage. 130 */ 131 public String getErrorMessage() 132 { 133 return result.getErrorMessage(); 134 } 135 136 137 /** 138 * Set the error message 139 * 140 * @param errorMessage The errorMessage to set. 141 */ 142 public void setErrorMessage( String errorMessage ) 143 { 144 result.setErrorMessage( errorMessage ); 145 } 146 147 148 /** 149 * Get the matched DN 150 * 151 * @return Returns the matchedDN. 152 */ 153 public String getMatchedDN() 154 { 155 return result.getMatchedDN(); 156 } 157 158 159 /** 160 * Set the Matched DN 161 * 162 * @param matchedDN The matchedDN to set. 163 */ 164 public void setMatchedDN( DN matchedDN ) 165 { 166 result.setMatchedDN( matchedDN ); 167 } 168 169 170 /** 171 * Get the referrals 172 * 173 * @return Returns the referrals. 174 */ 175 public List<LdapURL> getReferrals() 176 { 177 return result.getReferrals(); 178 } 179 180 181 /** 182 * Add a referral 183 * 184 * @param referral The referral to add. 185 */ 186 public void addReferral( LdapURL referral ) 187 { 188 result.addReferral( referral ); 189 } 190 191 192 /** 193 * Get the result code 194 * 195 * @return Returns the resultCode. 196 */ 197 public ResultCodeEnum getResultCode() 198 { 199 return result.getResultCode(); 200 } 201 202 203 /** 204 * Set the result code 205 * 206 * @param resultCode The resultCode to set. 207 */ 208 public void setResultCode( ResultCodeEnum resultCode ) 209 { 210 result.setResultCode( resultCode ); 211 } 212 }