19439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly/*
29439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * Copyright (c) 2008-2009, Motorola, Inc.
39439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly *
49439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * All rights reserved.
59439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly *
69439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * Redistribution and use in source and binary forms, with or without
79439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * modification, are permitted provided that the following conditions are met:
89439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly *
99439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * - Redistributions of source code must retain the above copyright notice,
109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * this list of conditions and the following disclaimer.
119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly *
129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * - Redistributions in binary form must reproduce the above copyright notice,
139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * this list of conditions and the following disclaimer in the documentation
149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * and/or other materials provided with the distribution.
159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly *
169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * - Neither the name of the Motorola, Inc. nor the names of its contributors
179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * may be used to endorse or promote products derived from this software
189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * without specific prior written permission.
199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly *
209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
219439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
279439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * POSSIBILITY OF SUCH DAMAGE.
319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly */
329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pellypackage javax.obex;
349439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
352e0da96e757a977154063f980d3f4e1abd41cf09Nick Pellyimport java.io.ByteArrayOutputStream;
362e0da96e757a977154063f980d3f4e1abd41cf09Nick Pellyimport java.io.IOException;
372e0da96e757a977154063f980d3f4e1abd41cf09Nick Pellyimport java.util.Calendar;
3825f2c4cd953764008d04147a14d990c06a8154e5Oscar Montemayorimport java.security.SecureRandom;
399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly/**
419439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * This class implements the javax.obex.HeaderSet interface for OBEX over
429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * RFCOMM.
432e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly * @hide
449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly */
459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pellypublic final class HeaderSet {
469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
479439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
4805ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Represents the OBEX Count header. This allows the connection statement to
4905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * tell the server how many objects it plans to send or retrieve.
509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <P>
519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * The value of <code>COUNT</code> is 0xC0 (192).
529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public static final int COUNT = 0xC0;
549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
5605ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Represents the OBEX Name header. This specifies the name of the object.
579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <P>
589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * The value of <code>NAME</code> is 0x01 (1).
599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public static final int NAME = 0x01;
619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
6305ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Represents the OBEX Type header. This allows a request to specify the
649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * type of the object (e.g. text, html, binary, etc.).
659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <P>
669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * The value of <code>TYPE</code> is 0x42 (66).
679439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public static final int TYPE = 0x42;
699439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
7105ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Represents the OBEX Length header. This is the length of the object in
729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * bytes.
739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <P>
749439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * The value of <code>LENGTH</code> is 0xC3 (195).
759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public static final int LENGTH = 0xC3;
779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
7905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Represents the OBEX Time header using the ISO 8601 standards. This is the
8005ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * preferred time header.
819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <P>
829439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * The value of <code>TIME_ISO_8601</code> is 0x44 (68).
839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public static final int TIME_ISO_8601 = 0x44;
859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
8705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Represents the OBEX Time header using the 4 byte representation. This is
8805ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * only included for backwards compatibility. It represents the number of
8905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * seconds since January 1, 1970.
909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <P>
919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * The value of <code>TIME_4_BYTE</code> is 0xC4 (196).
929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public static final int TIME_4_BYTE = 0xC4;
949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
9605ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Represents the OBEX Description header. This is a text description of the
9705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * object.
989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <P>
999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * The value of <code>DESCRIPTION</code> is 0x05 (5).
1009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
1019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public static final int DESCRIPTION = 0x05;
1029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
10405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Represents the OBEX Target header. This is the name of the service an
1059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * operation is targeted to.
1069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <P>
1079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * The value of <code>TARGET</code> is 0x46 (70).
1089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
1099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public static final int TARGET = 0x46;
1109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
11205ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Represents the OBEX HTTP header. This allows an HTTP 1.X header to be
1139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * included in a request or reply.
1149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <P>
1159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * The value of <code>HTTP</code> is 0x47 (71).
1169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
1179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public static final int HTTP = 0x47;
1189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
1203998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * Represents the OBEX BODY header.
1213998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * <P>
1223998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * The value of <code>BODY</code> is 0x48 (72).
1233998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     */
1243998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    public static final int BODY = 0x48;
1253998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun
1263998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    /**
1273998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * Represents the OBEX End of BODY header.
1283998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * <P>
1293998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * The value of <code>BODY</code> is 0x49 (73).
1303998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     */
1313998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    public static final int END_OF_BODY = 0x49;
1323998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun
1333998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    /**
13405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Represents the OBEX Who header. Identifies the OBEX application to
1359439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * determine if the two peers are talking to each other.
1369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <P>
1379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * The value of <code>WHO</code> is 0x4A (74).
1389439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
1399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public static final int WHO = 0x4A;
1409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1419439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
1423998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * Represents the OBEX Connection ID header. Identifies used for OBEX
1433998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * connection multiplexing.
1449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <P>
1453998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * The value of <code>CONNECTION_ID</code> is 0xCB (203).
1469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
1473998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun
1483998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    public static final int CONNECTION_ID = 0xCB;
1499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
15105ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Represents the OBEX Application Parameter header. This header specifies
1529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * additional application request and response information.
1539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <P>
1549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * The value of <code>APPLICATION_PARAMETER</code> is 0x4C (76).
1559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
1569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public static final int APPLICATION_PARAMETER = 0x4C;
1579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1583998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    /**
1593998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * Represents the OBEX authentication digest-challenge.
1603998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * <P>
1613998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * The value of <code>AUTH_CHALLENGE</code> is 0x4D (77).
1623998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     */
1633998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    public static final int AUTH_CHALLENGE = 0x4D;
1643998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun
1653998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    /**
1663998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * Represents the OBEX authentication digest-response.
1673998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * <P>
1683998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * The value of <code>AUTH_RESPONSE</code> is 0x4E (78).
1693998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     */
1703998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    public static final int AUTH_RESPONSE = 0x4E;
1713998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun
1723998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    /**
17305ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Represents the OBEX Object Class header. This header specifies the OBEX
17405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * object class of the object.
1753998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * <P>
1763998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * The value of <code>OBJECT_CLASS</code> is 0x4F (79).
1773998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     */
1783998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    public static final int OBJECT_CLASS = 0x4F;
1793998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun
1803998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private Long mCount; // 4 byte unsigned integer
1819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1823998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private String mName; // null terminated Unicode text string
1839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1843998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private String mType; // null terminated ASCII text string
1859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1863998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private Long mLength; // 4 byte unsigend integer
1879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1883998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private Calendar mIsoTime; // String of the form YYYYMMDDTHHMMSSZ
1899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1903998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private Calendar mByteTime; // 4 byte unsigned integer
1919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1923998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private String mDescription; // null terminated Unicode text String
1939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1943998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private byte[] mTarget; // byte sequence
1959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1963998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private byte[] mHttpHeader; // byte sequence
1979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1983998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private byte[] mWho; // length prefixed byte sequence
1999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2003998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private byte[] mAppParam; // byte sequence of the form tag length value
2019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2023998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private byte[] mObjectClass; // byte sequence
2039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2043998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private String[] mUnicodeUserDefined; //null terminated unicode string
2059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2063998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private byte[][] mSequenceUserDefined; // byte sequence user defined
2079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2083998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private Byte[] mByteUserDefined; // 1 byte
2099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2103998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private Long[] mIntegerUserDefined; // 4 byte unsigned integer
2119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
21225f2c4cd953764008d04147a14d990c06a8154e5Oscar Montemayor    private final SecureRandom mRandom;
2139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
21405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun    /*package*/ byte[] nonce;
2159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
21605ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun    public byte[] mAuthChall; // The authentication challenge header
21705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun
21805ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun    public byte[] mAuthResp; // The authentication response header
21905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun
22005ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun    public byte[] mConnectionID; // THe connection ID
22105ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun
22205ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun    public int responseCode;
2239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
2259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Creates new <code>HeaderSet</code> object.
2269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param size the max packet size for this connection
2279439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
2289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public HeaderSet() {
2293998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mUnicodeUserDefined = new String[16];
2303998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mSequenceUserDefined = new byte[16][];
2313998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mByteUserDefined = new Byte[16];
2323998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mIntegerUserDefined = new Long[16];
2339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        responseCode = -1;
23425f2c4cd953764008d04147a14d990c06a8154e5Oscar Montemayor        mRandom = new SecureRandom();
2359439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
2369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
23805ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Sets the value of the header identifier to the value provided. The type
2399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * of object must correspond to the Java type defined in the description of
24005ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * this interface. If <code>null</code> is passed as the
2419439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <code>headerValue</code> then the header will be removed from the set of
2429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * headers to include in the next request.
2439439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param headerID the identifier to include in the message
2449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param headerValue the value of the header identifier
24505ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @throws IllegalArgumentException if the header identifier provided is not
24605ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         one defined in this interface or a user-defined header; if the
24705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         type of <code>headerValue</code> is not the correct Java type as
24805ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         defined in the description of this interface\
2499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
2509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public void setHeader(int headerID, Object headerValue) {
2519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        long temp = -1;
2529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        switch (headerID) {
2549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case COUNT:
2559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if (!(headerValue instanceof Long)) {
2569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (headerValue == null) {
2573998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        mCount = null;
2589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        break;
2599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
2609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    throw new IllegalArgumentException("Count must be a Long");
2619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
2629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                temp = ((Long)headerValue).longValue();
2639439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((temp < 0L) || (temp > 0xFFFFFFFFL)) {
2649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    throw new IllegalArgumentException("Count must be between 0 and 0xFFFFFFFF");
2659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
2663998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mCount = (Long)headerValue;
2679439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                break;
2689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case NAME:
2699439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((headerValue != null) && (!(headerValue instanceof String))) {
2709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    throw new IllegalArgumentException("Name must be a String");
2719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
2723998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mName = (String)headerValue;
2739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                break;
2749439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case TYPE:
2759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((headerValue != null) && (!(headerValue instanceof String))) {
2769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    throw new IllegalArgumentException("Type must be a String");
2779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
2783998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mType = (String)headerValue;
2799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                break;
2809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case LENGTH:
2819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if (!(headerValue instanceof Long)) {
2829439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (headerValue == null) {
2833998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        mLength = null;
2849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        break;
2859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
2869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    throw new IllegalArgumentException("Length must be a Long");
2879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
2889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                temp = ((Long)headerValue).longValue();
2899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((temp < 0L) || (temp > 0xFFFFFFFFL)) {
2909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    throw new IllegalArgumentException("Length must be between 0 and 0xFFFFFFFF");
2919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
2923998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mLength = (Long)headerValue;
2939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                break;
2949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case TIME_ISO_8601:
2959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((headerValue != null) && (!(headerValue instanceof Calendar))) {
2969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    throw new IllegalArgumentException("Time ISO 8601 must be a Calendar");
2979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
2983998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mIsoTime = (Calendar)headerValue;
2999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                break;
3009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case TIME_4_BYTE:
3019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((headerValue != null) && (!(headerValue instanceof Calendar))) {
3029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    throw new IllegalArgumentException("Time 4 Byte must be a Calendar");
3039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3043998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mByteTime = (Calendar)headerValue;
3059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                break;
3069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case DESCRIPTION:
3079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((headerValue != null) && (!(headerValue instanceof String))) {
3089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    throw new IllegalArgumentException("Description must be a String");
3099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3103998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mDescription = (String)headerValue;
3119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                break;
3129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case TARGET:
3139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if (headerValue == null) {
3143998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mTarget = null;
3159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                } else {
3169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (!(headerValue instanceof byte[])) {
3179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        throw new IllegalArgumentException("Target must be a byte array");
3189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    } else {
3193998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        mTarget = new byte[((byte[])headerValue).length];
3203998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        System.arraycopy(headerValue, 0, mTarget, 0, mTarget.length);
3219439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
3229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                break;
3249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case HTTP:
3259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if (headerValue == null) {
3263998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mHttpHeader = null;
3279439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                } else {
3289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (!(headerValue instanceof byte[])) {
3299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        throw new IllegalArgumentException("HTTP must be a byte array");
3309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    } else {
3313998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        mHttpHeader = new byte[((byte[])headerValue).length];
3323998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        System.arraycopy(headerValue, 0, mHttpHeader, 0, mHttpHeader.length);
3339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
3349439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3359439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                break;
3369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case WHO:
3379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if (headerValue == null) {
3383998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mWho = null;
3399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                } else {
3409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (!(headerValue instanceof byte[])) {
3419439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        throw new IllegalArgumentException("WHO must be a byte array");
3429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    } else {
3433998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        mWho = new byte[((byte[])headerValue).length];
3443998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        System.arraycopy(headerValue, 0, mWho, 0, mWho.length);
3459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
3469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3479439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                break;
3489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case OBJECT_CLASS:
3499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if (headerValue == null) {
3503998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mObjectClass = null;
3519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                } else {
3529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (!(headerValue instanceof byte[])) {
3539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        throw new IllegalArgumentException("Object Class must be a byte array");
3549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    } else {
3553998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        mObjectClass = new byte[((byte[])headerValue).length];
3563998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        System.arraycopy(headerValue, 0, mObjectClass, 0, mObjectClass.length);
3579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
3589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                break;
3609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case APPLICATION_PARAMETER:
3619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if (headerValue == null) {
3623998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mAppParam = null;
3639439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                } else {
3649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (!(headerValue instanceof byte[])) {
3659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        throw new IllegalArgumentException(
3669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                                "Application Parameter must be a byte array");
3679439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    } else {
3683998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        mAppParam = new byte[((byte[])headerValue).length];
3693998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        System.arraycopy(headerValue, 0, mAppParam, 0, mAppParam.length);
3709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
3719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                break;
3739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            default:
3749439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // Verify that it was not a Unicode String user Defined
3759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((headerID >= 0x30) && (headerID <= 0x3F)) {
3769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if ((headerValue != null) && (!(headerValue instanceof String))) {
3779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        throw new IllegalArgumentException(
3789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                                "Unicode String User Defined must be a String");
3799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
3803998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mUnicodeUserDefined[headerID - 0x30] = (String)headerValue;
3819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3829439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    break;
3839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // Verify that it was not a byte sequence user defined value
3859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((headerID >= 0x70) && (headerID <= 0x7F)) {
3869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (headerValue == null) {
3883998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        mSequenceUserDefined[headerID - 0x70] = null;
3899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    } else {
3909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        if (!(headerValue instanceof byte[])) {
3919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                            throw new IllegalArgumentException(
3929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                                    "Byte Sequence User Defined must be a byte array");
3939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        } else {
3943998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                            mSequenceUserDefined[headerID - 0x70] = new byte[((byte[])headerValue).length];
3953998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                            System.arraycopy(headerValue, 0, mSequenceUserDefined[headerID - 0x70],
3963998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                                    0, mSequenceUserDefined[headerID - 0x70].length);
3979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        }
3989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
3999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    break;
4009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // Verify that it was not a Byte user Defined
4029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((headerID >= 0xB0) && (headerID <= 0xBF)) {
4039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if ((headerValue != null) && (!(headerValue instanceof Byte))) {
4049439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        throw new IllegalArgumentException("ByteUser Defined must be a Byte");
4059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
4063998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mByteUserDefined[headerID - 0xB0] = (Byte)headerValue;
4079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    break;
4099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // Verify that is was not the 4 byte unsigned integer user
4119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // defined header
4129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((headerID >= 0xF0) && (headerID <= 0xFF)) {
4139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (!(headerValue instanceof Long)) {
4149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        if (headerValue == null) {
4153998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                            mIntegerUserDefined[headerID - 0xF0] = null;
4169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                            break;
4179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        }
4189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        throw new IllegalArgumentException("Integer User Defined must be a Long");
4199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
4209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    temp = ((Long)headerValue).longValue();
4219439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if ((temp < 0L) || (temp > 0xFFFFFFFFL)) {
4229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        throw new IllegalArgumentException(
4239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                                "Integer User Defined must be between 0 and 0xFFFFFFFF");
4249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
4253998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mIntegerUserDefined[headerID - 0xF0] = (Long)headerValue;
4269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    break;
4279439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                throw new IllegalArgumentException("Invalid Header Identifier");
4299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
4309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
4319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
43305ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Retrieves the value of the header identifier provided. The type of the
4349439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Object returned is defined in the description of this interface.
4359439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param headerID the header identifier whose value is to be returned
4369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return the value of the header provided or <code>null</code> if the
43705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         header identifier specified is not part of this
43805ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         <code>HeaderSet</code> object
43905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @throws IllegalArgumentException if the <code>headerID</code> is not one
44005ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         defined in this interface or any of the user-defined headers
4412e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an error occurred in the transport layer during
44205ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         the operation or if the connection has been closed
4439439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
4449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public Object getHeader(int headerID) throws IOException {
4459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        switch (headerID) {
4479439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case COUNT:
4483998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                return mCount;
4499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case NAME:
4503998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                return mName;
4519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case TYPE:
4523998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                return mType;
4539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case LENGTH:
4543998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                return mLength;
4559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case TIME_ISO_8601:
4563998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                return mIsoTime;
4579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case TIME_4_BYTE:
4583998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                return mByteTime;
4599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case DESCRIPTION:
4603998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                return mDescription;
4619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case TARGET:
4623998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                return mTarget;
4639439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case HTTP:
4643998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                return mHttpHeader;
4659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case WHO:
4663998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                return mWho;
4679439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case OBJECT_CLASS:
4683998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                return mObjectClass;
4699439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            case APPLICATION_PARAMETER:
4703998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                return mAppParam;
4719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            default:
4729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // Verify that it was not a Unicode String user Defined
4739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((headerID >= 0x30) && (headerID <= 0x3F)) {
4743998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    return mUnicodeUserDefined[headerID - 0x30];
4759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // Verify that it was not a byte sequence user defined header
4779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((headerID >= 0x70) && (headerID <= 0x7F)) {
4783998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    return mSequenceUserDefined[headerID - 0x70];
4799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // Verify that it was not a byte user defined header
4819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((headerID >= 0xB0) && (headerID <= 0xBF)) {
4823998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    return mByteUserDefined[headerID - 0xB0];
4839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4843998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                // Verify that it was not a integer user defined header
4859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((headerID >= 0xF0) && (headerID <= 0xFF)) {
4863998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    return mIntegerUserDefined[headerID - 0xF0];
4879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                throw new IllegalArgumentException("Invalid Header Identifier");
4899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
4909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
4919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
4939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Retrieves the list of headers that may be retrieved via the
49405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * <code>getHeader</code> method that will not return <code>null</code>. In
49505ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * other words, this method returns all the headers that are available in
49605ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * this object.
4979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @see #getHeader
4989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return the array of headers that are set in this object or
49905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         <code>null</code> if no headers are available
5002e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an error occurred in the transport layer during
50105ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         the operation or the connection has been closed
5029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
5039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public int[] getHeaderList() throws IOException {
5049439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        ByteArrayOutputStream out = new ByteArrayOutputStream();
5059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5063998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mCount != null) {
5079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write(COUNT);
5089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5093998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mName != null) {
5109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write(NAME);
5119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5123998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mType != null) {
5139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write(TYPE);
5149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5153998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mLength != null) {
5169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write(LENGTH);
5179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5183998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mIsoTime != null) {
5199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write(TIME_ISO_8601);
5209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5213998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mByteTime != null) {
5229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write(TIME_4_BYTE);
5239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5243998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mDescription != null) {
5259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write(DESCRIPTION);
5269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5273998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mTarget != null) {
5289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write(TARGET);
5299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5303998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mHttpHeader != null) {
5319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write(HTTP);
5329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5333998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mWho != null) {
5349439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write(WHO);
5359439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5363998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mAppParam != null) {
5379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write(APPLICATION_PARAMETER);
5389439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5393998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mObjectClass != null) {
5409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            out.write(OBJECT_CLASS);
5419439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5439439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        for (int i = 0x30; i < 0x40; i++) {
5443998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (mUnicodeUserDefined[i - 0x30] != null) {
5459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                out.write(i);
5469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
5479439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        for (int i = 0x70; i < 0x80; i++) {
5503998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (mSequenceUserDefined[i - 0x70] != null) {
5519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                out.write(i);
5529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
5539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        for (int i = 0xB0; i < 0xC0; i++) {
5563998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (mByteUserDefined[i - 0xB0] != null) {
5579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                out.write(i);
5589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
5599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        for (int i = 0xF0; i < 0x100; i++) {
5623998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (mIntegerUserDefined[i - 0xF0] != null) {
5639439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                out.write(i);
5649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
5659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5679439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        byte[] headers = out.toByteArray();
5689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        out.close();
5699439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if ((headers == null) || (headers.length == 0)) {
5719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            return null;
5729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5749439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int[] result = new int[headers.length];
5759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        for (int i = 0; i < headers.length; i++) {
5769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            // Convert the byte to a positive integer.  That is, an integer
5779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            // between 0 and 256.
5789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            result[i] = headers[i] & 0xFF;
5799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        return result;
5829439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
5839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
58505ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Sets the authentication challenge header. The <code>realm</code> will be
58605ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * encoded based upon the default encoding scheme used by the implementation
58705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * to encode strings. Therefore, the encoding scheme used to encode the
58805ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * <code>realm</code> is application dependent.
5899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param realm a short description that describes what password to use; if
59005ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *        <code>null</code> no realm will be sent in the authentication
59105ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *        challenge header
5929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param userID if <code>true</code>, a user ID is required in the reply;
59305ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *        if <code>false</code>, no user ID is required
5949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param access if <code>true</code> then full access will be granted if
59505ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *        successful; if <code>false</code> then read-only access will be
59605ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *        granted if successful
5973998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * @throws IOException
5989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
5993998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    public void createAuthenticationChallenge(String realm, boolean userID, boolean access)
6003998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            throws IOException {
6019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
60205ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun        nonce = new byte[16];
60305ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun        for (int i = 0; i < 16; i++) {
60405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun            nonce[i] = (byte)mRandom.nextInt();
6059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
60605ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun
60705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun        mAuthChall = ObexHelper.computeAuthenticationChallenge(nonce, realm, access, userID);
6089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
6099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
61105ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * Returns the response code received from the server. Response codes are
61205ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * defined in the <code>ResponseCodes</code> class.
6139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @see ResponseCodes
6149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return the response code retrieved from the server
6152e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException if an error occurred in the transport layer during
61605ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         the transaction; if this method is called on a
61705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         <code>HeaderSet</code> object created by calling
61805ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         <code>createHeaderSet()</code> in a <code>ClientSession</code>
61905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         object; if this object was created by an OBEX server
6209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
6219439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public int getResponseCode() throws IOException {
6229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if (responseCode == -1) {
6239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            throw new IOException("May not be called on a server");
6249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } else {
6259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            return responseCode;
6269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6279439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
6289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly}
629