001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.jndi.provider.ldap;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.HashMap;
023: import java.util.List;
024: import java.util.Map;
025:
026: import javax.naming.NamingException;
027: import javax.naming.directory.Attributes;
028: import javax.naming.directory.BasicAttributes;
029:
030: import org.apache.harmony.jndi.provider.ldap.asn1.LdapASN1Constant;
031: import org.apache.harmony.jndi.provider.ldap.asn1.Utils;
032: import org.apache.harmony.jndi.provider.ldap.asn1.ASN1ChoiceWrap.ChosenValue;
033:
034: public class LdapSearchResult {
035:
036: /**
037: * all search result entries
038: */
039: private Map<String, Attributes> entries = new HashMap<String, Attributes>();
040:
041: /**
042: * SearchResultReference from server
043: */
044: private List<String> refURLs;
045:
046: private LdapResult result;
047:
048: private NamingException ex;
049:
050: private String address;
051:
052: public String getAddress() {
053: return address;
054: }
055:
056: public void setAddress(String address) {
057: this .address = address;
058: }
059:
060: public void decodeSearchResponse(Object[] values) {
061: ChosenValue chosen = (ChosenValue) values[0];
062: switch (chosen.getIndex()) {
063: case LdapASN1Constant.OP_SEARCH_RESULT_ENTRY:
064: decodeEntry(chosen.getValue());
065: break;
066: case LdapASN1Constant.OP_SEARCH_RESULT_REF:
067: decodeRef(chosen.getValue());
068: break;
069: case LdapASN1Constant.OP_SEARCH_RESULT_DONE:
070: decodeDone(chosen.getValue());
071: break;
072: }
073: }
074:
075: protected void decodeDone(Object value) {
076: result = new LdapResult();
077: result.decodeValues((Object[]) value);
078: }
079:
080: protected void decodeRef(Object value) {
081: if (refURLs == null) {
082: refURLs = new ArrayList<String>();
083: }
084:
085: Collection<byte[]> list = (Collection<byte[]>) value;
086: for (byte[] bs : list) {
087: refURLs.add(Utils.getString(bs));
088: }
089: }
090:
091: protected void decodeEntry(Object value) {
092: Object[] values = (Object[]) value;
093: String name = Utils.getString((byte[]) values[0]);
094:
095: if (address != null) {
096: name = address + name;
097: }
098:
099: Attributes attrs = null;
100:
101: if (entries.containsKey(name)) {
102: attrs = entries.get(name);
103: } else {
104: attrs = new BasicAttributes();
105: entries.put(name, attrs);
106: }
107:
108: Collection<Object[]> list = (Collection<Object[]>) values[1];
109: for (Object[] objects : list) {
110: LdapAttribute attr = new LdapAttribute();
111: attr.decodeValues(objects);
112: attrs.put(attr);
113: }
114: }
115:
116: public Map<String, Attributes> getEntries() {
117: return entries;
118: }
119:
120: public List<String> getRefURLs() {
121: return refURLs;
122: }
123:
124: public LdapResult getResult() {
125: return result;
126: }
127:
128: public NamingException getException() {
129: return ex;
130: }
131:
132: public void setException(NamingException ex) {
133: this .ex = ex;
134: }
135:
136: public boolean isEmpty() {
137: return entries.size() == 0 && refURLs.size() == 0;
138: }
139:
140: public void setRefURLs(List<String> refURLs) {
141: this.refURLs = refURLs;
142: }
143: }
|