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 javax.naming.ldap;
019:
020: import java.io.IOException;
021: import java.io.UnsupportedEncodingException;
022: import java.util.ArrayList;
023:
024: import org.apache.harmony.security.asn1.ASN1Boolean;
025: import org.apache.harmony.security.asn1.ASN1Implicit;
026: import org.apache.harmony.security.asn1.ASN1OctetString;
027: import org.apache.harmony.security.asn1.ASN1Sequence;
028: import org.apache.harmony.security.asn1.ASN1SequenceOf;
029: import org.apache.harmony.security.asn1.ASN1StringType;
030: import org.apache.harmony.security.asn1.ASN1Type;
031:
032: /**
033: * TODO: JavaDoc
034: */
035: public final class SortControl extends BasicControl {
036:
037: /**
038: * Represents the following ASN1 Syntax:
039: *
040: * SortKey ::= SEQUENCE { attributeType AttributeDescription, orderingRule
041: * [0] MatchingRuleId OPTIONAL, reverseOrder [1] BOOLEAN DEFAULT FALSE }
042: *
043: */
044: static ASN1Sequence ASN1_SORTKEY = new ASN1Sequence(new ASN1Type[] {
045: ASN1OctetString.getInstance(),
046: new ASN1Implicit(0, ASN1StringType.UTF8STRING),
047: new ASN1Implicit(1, ASN1Boolean.getInstance()),
048:
049: }) {
050:
051: {
052: setOptional(1);
053: setDefault(Boolean.FALSE, 2);
054: }
055:
056: /**
057: * This method encode a <code>SortKey<code> Object
058: *
059: * @param object - a <code>SortKey<code> Object to encode
060: * @param values - a Object array to return the encoded values
061: */
062: public void getValues(Object object, Object values[]) {
063: SortKey sk = (SortKey) object;
064:
065: try {
066: values[0] = sk.getAttributeID().getBytes("utf-8");
067: } catch (UnsupportedEncodingException e) {
068: //FIXME: is this right thing to do?
069: values[0] = sk.getAttributeID().getBytes();
070: }
071: values[1] = sk.getMatchingRuleID();
072: values[2] = Boolean.valueOf(!sk.isAscending());
073: }
074: };
075:
076: /**
077: * Represents the following ASN1 Syntax:
078: *
079: * SortKeyList ::= SEQUENCE OF SortKey;
080: */
081: static ASN1SequenceOf ASN1_SORTKEYLIST = new ASN1SequenceOf(
082: ASN1_SORTKEY);
083:
084: private static final long serialVersionUID = -1965961680233330744L;
085:
086: public static final String OID = "1.2.840.113556.1.4.473"; //$NON-NLS-1$
087:
088: public SortControl(String sortBy, boolean criticality)
089: throws IOException {
090: super (OID, criticality, null);
091: ArrayList<SortKey> list = new ArrayList<SortKey>();
092: if (sortBy != null) {
093: list.add(new SortKey(sortBy, true, null));
094: } else {
095: list.add(new SortKey("", true, null)); //$NON-NLS-1$
096: }
097: value = ASN1_SORTKEYLIST.encode(list);
098: }
099:
100: public SortControl(String[] sortBy, boolean criticality)
101: throws IOException {
102: super (OID, criticality, null);
103: ArrayList<SortKey> list = new ArrayList<SortKey>();
104: for (int i = 0; i < sortBy.length; i++) {
105: if (sortBy[i] != null) {
106: list.add(new SortKey(sortBy[i], true, null));
107: } else {
108: list.add(new SortKey("", true, null));
109: }
110: }
111: value = ASN1_SORTKEYLIST.encode(list);
112: }
113:
114: public SortControl(SortKey[] sortBy, boolean criticality)
115: throws IOException {
116: super (OID, criticality, null);
117:
118: ArrayList<SortKey> list = new ArrayList<SortKey>();
119: for (int i = 0; i < sortBy.length; i++) {
120: list.add(sortBy[i]);
121: }
122: this.value = ASN1_SORTKEYLIST.encode(list);
123: }
124:
125: }
|