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.List;
022:
023: import javax.naming.directory.SearchControls;
024:
025: import org.apache.harmony.jndi.provider.ldap.asn1.ASN1Decodable;
026: import org.apache.harmony.jndi.provider.ldap.asn1.ASN1Encodable;
027: import org.apache.harmony.jndi.provider.ldap.asn1.LdapASN1Constant;
028: import org.apache.harmony.jndi.provider.ldap.asn1.Utils;
029: import org.apache.harmony.security.asn1.ASN1Integer;
030:
031: public class SearchOp implements LdapOperation, ASN1Encodable,
032: ASN1Decodable {
033: private String baseObject;
034:
035: private boolean typesOnly = false;
036:
037: // default value is 'always' = 3
038: private int derefAliases = 3;
039:
040: private Filter filter;
041:
042: private SearchControls controls;
043:
044: private LdapSearchResult result;
045:
046: public LdapSearchResult getSearchResult() {
047: return result;
048: }
049:
050: public SearchOp(String baseObject, SearchControls controls,
051: Filter filter) {
052: this .baseObject = baseObject;
053: this .controls = controls;
054: this .filter = filter;
055: }
056:
057: public ASN1Encodable getRequest() {
058: return this ;
059: }
060:
061: public int getRequestId() {
062: return LdapASN1Constant.OP_SEARCH_REQUEST;
063: }
064:
065: public ASN1Decodable getResponse() {
066: return this ;
067: }
068:
069: public int getResponseId() {
070: return LdapASN1Constant.OP_SEARCH_RESULT_DONE;
071: }
072:
073: public void encodeValues(Object[] values) {
074: values[0] = Utils.getBytes(baseObject);
075: values[1] = ASN1Integer.fromIntValue(controls.getSearchScope());
076: values[2] = ASN1Integer.fromIntValue(derefAliases);
077: values[3] = ASN1Integer.fromIntValue((int) controls
078: .getCountLimit());
079: values[4] = ASN1Integer.fromIntValue(controls.getTimeLimit());
080: values[5] = Boolean.valueOf(typesOnly);
081: values[6] = filter;
082: String[] attributes = controls.getReturningAttributes();
083: // if null, return all attributes
084: if (attributes == null) {
085: attributes = new String[0];
086: }
087:
088: List<byte[]> list = new ArrayList<byte[]>(attributes.length);
089: for (String attribute : attributes) {
090: list.add(Utils.getBytes(attribute));
091: }
092: values[7] = list;
093:
094: }
095:
096: public void decodeValues(Object[] values) {
097: if (result == null) {
098: result = new LdapSearchResult();
099: }
100: result.decodeSearchResponse(values);
101: }
102:
103: public String getBaseObject() {
104: return baseObject;
105: }
106:
107: public SearchControls getControls() {
108: return controls;
109: }
110:
111: public Filter getFilter() {
112: return filter;
113: }
114:
115: public boolean isTypesOnly() {
116: return typesOnly;
117: }
118:
119: public void setSearchResult(LdapSearchResult result) {
120: this .result = result;
121: }
122:
123: public LdapResult getResult() {
124: if (result == null) {
125: return null;
126: }
127: return result.getResult();
128: }
129:
130: public int getDerefAliases() {
131: return derefAliases;
132: }
133:
134: public void setDerefAliases(int derefAliases) {
135: this .derefAliases = derefAliases;
136: }
137:
138: public void setTypesOnly(boolean typesOnly) {
139: this .typesOnly = typesOnly;
140: }
141:
142: public void setBaseObject(String baseObject) {
143: this .baseObject = baseObject;
144: }
145:
146: public void setFilter(Filter filter) {
147: this.filter = filter;
148: }
149:
150: }
|