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 */ 020package org.apache.directory.api.ldap.codec.actions.searchResultReference; 021 022 023import org.apache.directory.api.asn1.DecoderException; 024import org.apache.directory.api.asn1.ber.grammar.GrammarAction; 025import org.apache.directory.api.asn1.ber.tlv.TLV; 026import org.apache.directory.api.i18n.I18n; 027import org.apache.directory.api.ldap.codec.api.LdapMessageContainer; 028import org.apache.directory.api.ldap.codec.decorators.SearchResultReferenceDecorator; 029import org.apache.directory.api.ldap.model.exception.LdapURLEncodingException; 030import org.apache.directory.api.ldap.model.message.Referral; 031import org.apache.directory.api.ldap.model.message.ReferralImpl; 032import org.apache.directory.api.ldap.model.message.SearchResultReference; 033import org.apache.directory.api.ldap.model.url.LdapUrl; 034import org.apache.directory.api.util.Strings; 035import org.slf4j.Logger; 036import org.slf4j.LoggerFactory; 037 038 039/** 040 * The action used to store a reference into a searchResultReference 041 * <pre> 042 * LdapMessage ::= ... SearchResultReference ... 043 * SearchResultReference ::= [APPLICATION 19] SEQUENCE OF LDAPURL 044 * </pre> 045 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 046 */ 047public class StoreReference extends GrammarAction<LdapMessageContainer<SearchResultReferenceDecorator>> 048{ 049 /** The logger */ 050 private static final Logger LOG = LoggerFactory.getLogger( StoreReference.class ); 051 052 /** Speedup for logs */ 053 private static final boolean IS_DEBUG = LOG.isDebugEnabled(); 054 055 056 /** 057 * Instantiates a new store reference action. 058 */ 059 public StoreReference() 060 { 061 super( "Store a reference" ); 062 } 063 064 065 /** 066 * {@inheritDoc} 067 */ 068 public void action( LdapMessageContainer<SearchResultReferenceDecorator> container ) throws DecoderException 069 { 070 SearchResultReference searchResultReference = container.getMessage(); 071 072 // Get the Value and store it in the BindRequest 073 TLV tlv = container.getCurrentTLV(); 074 075 // Get the referral, or create it if not existing 076 Referral referral = searchResultReference.getReferral(); 077 078 if ( referral == null ) 079 { 080 referral = new ReferralImpl(); 081 searchResultReference.setReferral( referral ); 082 } 083 084 // We have to handle the special case of a 0 length list of referrals 085 LdapUrl url = LdapUrl.EMPTY_URL; 086 087 if ( tlv.getLength() == 0 ) 088 { 089 referral.addLdapUrl( "" ); 090 } 091 else 092 { 093 String urlStr = Strings.utf8ToString( tlv.getValue().getData() ); 094 095 try 096 { 097 url = new LdapUrl( urlStr ); 098 referral.addLdapUrl( urlStr ); 099 } 100 catch ( LdapURLEncodingException luee ) 101 { 102 LOG.error( I18n.err( I18n.ERR_04021, urlStr, luee.getMessage() ) ); 103 throw new DecoderException( I18n.err( I18n.ERR_04016, luee.getMessage() ) ); 104 } 105 } 106 107 if ( IS_DEBUG ) 108 { 109 LOG.debug( "Search reference URL found : {}", url ); 110 } 111 112 // We can have an END transition 113 container.setGrammarEndAllowed( true ); 114 } 115}