ServerSession.java revision 3998bf009acaf8cde4d7f837f8b8e41ae0a65141
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
353998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejunimport android.util.Log;
363998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun
372e0da96e757a977154063f980d3f4e1abd41cf09Nick Pellyimport java.io.InputStream;
382e0da96e757a977154063f980d3f4e1abd41cf09Nick Pellyimport java.io.IOException;
392e0da96e757a977154063f980d3f4e1abd41cf09Nick Pellyimport java.io.OutputStream;
409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
419439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly/**
423998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun * This class in an implementation of the OBEX ServerSession.
439439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly *
442e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly * @hide
459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly */
463998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejunpublic final class ServerSession extends ObexSession implements Runnable {
479439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
483998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private static final String TAG = "Obex ServerSession";
499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
503998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private ObexTransport mTransport;
519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
523998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private InputStream mInput;
539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
543998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private OutputStream mOutput;
559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
563998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private ServerRequestHandler mListener;
579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
583998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private Thread mProcessThread;
599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
603998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private int mMaxPacketLength;
619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
623998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private boolean mClosed;
639439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Creates new ServerSession.
669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *
673998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun     * @param trans
689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *            the connection to the client
699439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *
709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param handler
719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *            the event listener that will process requests
729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *
739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param auth
749439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *            the authenticator to use with this connection
759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *
762e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException
779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *                if an error occurred while opening the input and output
789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *                streams
799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
803998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    public ServerSession(ObexTransport trans, ServerRequestHandler handler, Authenticator auth)
819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            throws IOException {
823998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mAuthenticator = auth;
833998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mTransport = trans;
843998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mInput = mTransport.openInputStream();
853998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mOutput = mTransport.openOutputStream();
863998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mListener = handler;
873998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mMaxPacketLength = 256;
883998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun
893998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mClosed = false;
903998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mProcessThread = new Thread(this);
913998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mProcessThread.start();
929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Processes requests made to the server and forwards them to the
969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * appropriate event listener.
979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public void run() {
999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        try {
1009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            boolean done = false;
1023998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            while (!done && !mClosed) {
1033998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                int requestType = mInput.read();
1049439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                switch (requestType) {
1053998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    case ObexHelper.OBEX_OPCODE_CONNECT:
1069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        handleConnectRequest();
1079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        break;
1089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1093998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    case ObexHelper.OBEX_OPCODE_DISCONNECT:
1109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        handleDisconnectRequest();
1119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        done = true;
1129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        break;
1139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1143998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    case ObexHelper.OBEX_OPCODE_GET:
1153998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    case ObexHelper.OBEX_OPCODE_GET_FINAL:
1169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        handleGetRequest(requestType);
1179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        break;
1189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1193998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    case ObexHelper.OBEX_OPCODE_PUT:
1203998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    case ObexHelper.OBEX_OPCODE_PUT_FINAL:
1219439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        handlePutRequest(requestType);
1229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        break;
1239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1243998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    case ObexHelper.OBEX_OPCODE_SETPATH:
1259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        handleSetPathRequest();
1269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        break;
1279439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    case -1:
1299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        done = true;
1309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        break;
1319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    default:
1339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1349439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        /*
1359439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                         * Received a request type that is not recognized so I am
1369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                         * just going to read the packet and send a not implemented
1379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                         * to the client
1389439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                         */
1393998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        int length = mInput.read();
1403998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        length = (length << 8) + mInput.read();
1419439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        for (int i = 3; i < length; i++) {
1423998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                            mInput.read();
1439439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        }
1449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        sendResponse(ResponseCodes.OBEX_HTTP_NOT_IMPLEMENTED, null);
1459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
1469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
1479439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } catch (NullPointerException e) {
1493998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            Log.d(TAG, e.toString());
1509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } catch (Exception e) {
1513998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            Log.d(TAG, e.toString());
1529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
1539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        close();
1549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
1559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
1579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Handles a PUT request from a client. This method will provide a
1589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <code>ServerOperation</code> object to the request handler. The
1599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <code>ServerOperation</code> object will handle the rest of the request.
1609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * It will also send replies and receive requests until the final reply
1619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * should be sent. When the final reply should be sent, this method will get
1629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * the response code to use and send the reply. The
1639439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <code>ServerOperation</code> object will always reply with a
1649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * OBEX_HTTP_CONTINUE reply. It will only reply if further information is
1659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * needed.
1669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *
1679439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param type
1689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *            the type of request received; either 0x02 or 0x82
1699439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *
1702e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException
1719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *                if an error occurred at the transport layer
1729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
1739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    private void handlePutRequest(int type) throws IOException {
1743998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        ServerOperation op = new ServerOperation(this, mInput, type, mMaxPacketLength, mListener);
1759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        try {
1769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            int response = -1;
1779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1783998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if ((op.finalBitSet) && !op.isValidBody()) {
1793998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                response = validateResponseCode(mListener
1803998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        .onDelete(op.requestHeader, op.replyHeader));
1819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            } else {
1823998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                response = validateResponseCode(mListener.onPut(op));
1839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
1849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            if (response != ResponseCodes.OBEX_HTTP_OK) {
1853998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                op.sendReply(response);
1863998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            } else if (!op.isAborted) {
1879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // wait for the final bit
1883998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                while (!op.finalBitSet) {
1893998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    op.sendReply(ResponseCodes.OBEX_HTTP_CONTINUE);
1909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
1913998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                op.sendReply(response);
1929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
1939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } catch (Exception e) {
1949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            sendResponse(ResponseCodes.OBEX_HTTP_INTERNAL_ERROR, null);
1959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
1969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
1979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
1999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Handles a GET request from a client. This method will provide a
2009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <code>ServerOperation</code> object to the request handler. The
2019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <code>ServerOperation</code> object will handle the rest of the request.
2029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * It will also send replies and receive requests until the final reply
2039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * should be sent. When the final reply should be sent, this method will get
2049439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * the response code to use and send the reply. The
2059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <code>ServerOperation</code> object will always reply with a
2069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * OBEX_HTTP_CONTINUE reply. It will only reply if further information is
2079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * needed.
2089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *
2099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param type
2109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *            the type of request received; either 0x03 or 0x83
2119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *
2122e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException
2139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *                if an error occurred at the transport layer
2149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
2159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    private void handleGetRequest(int type) throws IOException {
2163998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        ServerOperation op = new ServerOperation(this, mInput, type, mMaxPacketLength, mListener);
2179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        try {
2183998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            int response = validateResponseCode(mListener.onGet(op));
2199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2203998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (!op.isAborted) {
2213998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                op.sendReply(response);
2229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
2239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } catch (Exception e) {
2249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            sendResponse(ResponseCodes.OBEX_HTTP_INTERNAL_ERROR, null);
2259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
2269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
2279439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
2299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Send standard response.
2309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *
2319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param code
2329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *            the response code to send
2339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *
2349439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param header
2359439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *            the headers to include in the response
2369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *
2372e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException
2389439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *                if an IO error occurs
2399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
2403998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    public void sendResponse(int code, byte[] header) throws IOException {
2419439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int totalLength = 3;
2429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        byte[] data = null;
2439439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if (header != null) {
2459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            totalLength += header.length;
2469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            data = new byte[totalLength];
2479439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            data[0] = (byte)code;
2489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            data[1] = (byte)(totalLength >> 8);
2499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            data[2] = (byte)totalLength;
2509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            System.arraycopy(header, 0, data, 3, header.length);
2519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } else {
2529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            data = new byte[totalLength];
2539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            data[0] = (byte)code;
2549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            data[1] = (byte)0x00;
2559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            data[2] = (byte)totalLength;
2569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
2573998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mOutput.write(data);
2583998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mOutput.flush();
2599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
2609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
2629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Handles a SETPATH request from a client. This method will read the rest
2639439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * of the request from the client. Assuming the request is valid, it will
2649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * create a <code>HeaderSet</code> object to pass to the
2659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <code>ServerRequestHandler</code> object. After the handler processes the
2669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * request, this method will create a reply message to send to the server
2679439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * with the response code provided.
2689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *
2692e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException
2709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *                if an error occurred at the transport layer
2719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
2729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    private void handleSetPathRequest() throws IOException {
2739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int length;
2749439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int flags;
2753998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        @SuppressWarnings("unused")
2769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int constants;
2779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int totalLength = 3;
2789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        byte[] head = null;
2799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int code = -1;
2809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int bytesReceived;
2819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        HeaderSet request = new HeaderSet();
2829439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        HeaderSet reply = new HeaderSet();
2839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2843998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        length = mInput.read();
2853998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        length = (length << 8) + mInput.read();
2863998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        flags = mInput.read();
2873998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        constants = mInput.read();
2889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2892e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly        if (length > ObexHelper.MAX_PACKET_SIZE_INT) {
2909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            code = ResponseCodes.OBEX_HTTP_REQ_TOO_LARGE;
2919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            totalLength = 3;
2929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } else {
2939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            if (length > 5) {
2949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                byte[] headers = new byte[length - 5];
2953998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                bytesReceived = mInput.read(headers);
2969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                while (bytesReceived != headers.length) {
2983998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    bytesReceived += mInput.read(headers, bytesReceived, headers.length
2999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                            - bytesReceived);
3009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3022e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly                ObexHelper.updateHeaderSet(request, headers);
3039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3043998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (request.mConnectionID != null) {
3053998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mListener.setConnectionId(ObexHelper.convertToLong(request.mConnectionID));
3069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                } else {
3073998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mListener.setConnectionId(-1);
3089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3093998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                // the Auth chan is initiated by the server, client sent back the authResp .
3103998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (request.mAuthResp != null) {
3113998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    if (!handleAuthResp(request.mAuthResp)) {
3129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        code = ResponseCodes.OBEX_HTTP_UNAUTHORIZED;
3133998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        mListener.onAuthenticationFailure(ObexHelper.getTagValue((byte)0x01,
3143998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                                request.mAuthResp));
3159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
3163998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    request.mAuthResp = null;
3179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
3199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            if (code != ResponseCodes.OBEX_HTTP_UNAUTHORIZED) {
3213998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                // the Auth challenge is initiated by the client
3229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // the server will send back the authResp to the client
3233998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (request.mAuthChall != null) {
3249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    handleAuthChall(request);
3253998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    reply.mAuthResp = new byte[request.mAuthResp.length];
3263998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    System.arraycopy(request.mAuthResp, 0, reply.mAuthResp, 0,
3273998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                            reply.mAuthResp.length);
3283998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    request.mAuthChall = null;
3293998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    request.mAuthResp = null;
3309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                boolean backup = false;
3329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                boolean create = true;
3339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if (!((flags & 1) == 0)) {
3349439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    backup = true;
3359439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if ((flags & 2) == 0) {
3379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    create = false;
3389439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                try {
3413998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    code = mListener.onSetPath(request, reply, backup, create);
3429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                } catch (Exception e) {
3439439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    sendResponse(ResponseCodes.OBEX_HTTP_INTERNAL_ERROR, null);
3449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    return;
3459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3479439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                code = validateResponseCode(code);
3489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if (reply.nonce != null) {
3503998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mChallengeDigest = new byte[16];
3513998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    System.arraycopy(reply.nonce, 0, mChallengeDigest, 0, 16);
3529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                } else {
3533998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mChallengeDigest = null;
3549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3563998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                long id = mListener.getConnectionId();
3579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if (id == -1) {
3583998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    reply.mConnectionID = null;
3599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                } else {
3603998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    reply.mConnectionID = ObexHelper.convertToByteArray(id);
3619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3632e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly                head = ObexHelper.createHeader(reply, false);
3649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                totalLength += head.length;
3659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3663998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (totalLength > mMaxPacketLength) {
3679439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    totalLength = 3;
3689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    head = null;
3699439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    code = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
3709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
3729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
3739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3749439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        // Compute Length of OBEX SETPATH packet
3759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        byte[] replyData = new byte[totalLength];
3769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        replyData[0] = (byte)code;
3779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        replyData[1] = (byte)(totalLength >> 8);
3789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        replyData[2] = (byte)totalLength;
3799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if (head != null) {
3809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            System.arraycopy(head, 0, replyData, 3, head.length);
3819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
3829439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        /*
3839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * Write the OBEX SETPATH packet to the server. Byte 0: response code
3849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * Byte 1&2: Connect Packet Length Byte 3 to n: headers
3859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         */
3863998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mOutput.write(replyData);
3873998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mOutput.flush();
3889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
3899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
3919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Handles a disconnect request from a client. This method will read the
3929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * rest of the request from the client. Assuming the request is valid, it
3939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * will create a <code>HeaderSet</code> object to pass to the
3949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <code>ServerRequestHandler</code> object. After the handler processes the
3959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * request, this method will create a reply message to send to the server.
3969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *
3972e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException
3989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *                if an error occurred at the transport layer
3999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
4009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    private void handleDisconnectRequest() throws IOException {
4019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int length;
4029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int code = ResponseCodes.OBEX_HTTP_OK;
4039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int totalLength = 3;
4049439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        byte[] head = null;
4059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int bytesReceived;
4069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        HeaderSet request = new HeaderSet();
4079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        HeaderSet reply = new HeaderSet();
4089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4093998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        length = mInput.read();
4103998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        length = (length << 8) + mInput.read();
4119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4122e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly        if (length > ObexHelper.MAX_PACKET_SIZE_INT) {
4139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            code = ResponseCodes.OBEX_HTTP_REQ_TOO_LARGE;
4149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            totalLength = 3;
4159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } else {
4169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            if (length > 3) {
4179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                byte[] headers = new byte[length - 3];
4183998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                bytesReceived = mInput.read(headers);
4199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                while (bytesReceived != headers.length) {
4213998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    bytesReceived += mInput.read(headers, bytesReceived, headers.length
4229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                            - bytesReceived);
4239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4252e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly                ObexHelper.updateHeaderSet(request, headers);
4269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
4279439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4283998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (request.mConnectionID != null) {
4293998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mListener.setConnectionId(ObexHelper.convertToLong(request.mConnectionID));
4309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            } else {
4313998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mListener.setConnectionId(1);
4329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
4339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4343998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (request.mAuthResp != null) {
4353998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (!handleAuthResp(request.mAuthResp)) {
4369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    code = ResponseCodes.OBEX_HTTP_UNAUTHORIZED;
4373998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mListener.onAuthenticationFailure(ObexHelper.getTagValue((byte)0x01,
4383998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                            request.mAuthResp));
4399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4403998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                request.mAuthResp = null;
4419439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
4429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4439439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            if (code != ResponseCodes.OBEX_HTTP_UNAUTHORIZED) {
4449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4453998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (request.mAuthChall != null) {
4469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    handleAuthChall(request);
4473998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    request.mAuthChall = null;
4489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                try {
4513998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mListener.onDisconnect(request, reply);
4529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                } catch (Exception e) {
4539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    sendResponse(ResponseCodes.OBEX_HTTP_INTERNAL_ERROR, null);
4549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    return;
4559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4573998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                long id = mListener.getConnectionId();
4589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if (id == -1) {
4593998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    reply.mConnectionID = null;
4609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                } else {
4613998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    reply.mConnectionID = ObexHelper.convertToByteArray(id);
4629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4639439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4642e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly                head = ObexHelper.createHeader(reply, false);
4659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                totalLength += head.length;
4669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4673998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (totalLength > mMaxPacketLength) {
4689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    totalLength = 3;
4699439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    head = null;
4709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    code = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
4719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
4739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
4749439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        // Compute Length of OBEX CONNECT packet
4769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        byte[] replyData;
4779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if (head != null) {
4789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            replyData = new byte[3 + head.length];
4799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } else {
4809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            replyData = new byte[3];
4819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
4829439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        replyData[0] = (byte)code;
4839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        replyData[1] = (byte)(totalLength >> 8);
4849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        replyData[2] = (byte)totalLength;
4859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if (head != null) {
4869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            System.arraycopy(head, 0, replyData, 3, head.length);
4879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
4889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        /*
4899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * Write the OBEX DISCONNECT packet to the server. Byte 0: response code
4909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * Byte 1&2: Connect Packet Length Byte 3 to n: headers
4919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         */
4923998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mOutput.write(replyData);
4933998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mOutput.flush();
4949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
4959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
4979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Handles a connect request from a client. This method will read the rest
4989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * of the request from the client. Assuming the request is valid, it will
4999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * create a <code>HeaderSet</code> object to pass to the
5009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <code>ServerRequestHandler</code> object. After the handler processes the
5019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * request, this method will create a reply message to send to the server
5029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * with the response code provided.
5039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *
5042e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly     * @throws IOException
5059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *                if an error occurred at the transport layer
5069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
5079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    private void handleConnectRequest() throws IOException {
5089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int packetLength;
5093998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        @SuppressWarnings("unused")
5109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int version;
5113998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        @SuppressWarnings("unused")
5129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int flags;
5139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int totalLength = 7;
5149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        byte[] head = null;
5159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int code = -1;
5169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        HeaderSet request = new HeaderSet();
5179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        HeaderSet reply = new HeaderSet();
5189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int bytesReceived;
5199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        /*
5219439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * Read in the length of the OBEX packet, OBEX version, flags, and max
5229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * packet length
5239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         */
5243998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        packetLength = mInput.read();
5253998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        packetLength = (packetLength << 8) + mInput.read();
5263998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        version = mInput.read();
5273998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        flags = mInput.read();
5283998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mMaxPacketLength = mInput.read();
5293998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mMaxPacketLength = (mMaxPacketLength << 8) + mInput.read();
5309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        // should we check it?
5323998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mMaxPacketLength > ObexHelper.MAX_PACKET_SIZE_INT) {
5333998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            mMaxPacketLength = ObexHelper.MAX_PACKET_SIZE_INT;
5349439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5359439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5362e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly        if (packetLength > ObexHelper.MAX_PACKET_SIZE_INT) {
5379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            code = ResponseCodes.OBEX_HTTP_REQ_TOO_LARGE;
5389439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            totalLength = 7;
5399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } else {
5409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            if (packetLength > 7) {
5419439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                byte[] headers = new byte[packetLength - 7];
5423998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                bytesReceived = mInput.read(headers);
5439439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                while (bytesReceived != headers.length) {
5453998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    bytesReceived += mInput.read(headers, bytesReceived, headers.length
5469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                            - bytesReceived);
5479439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
5489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5492e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly                ObexHelper.updateHeaderSet(request, headers);
5509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
5519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5523998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (request.mConnectionID != null) {
5533998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mListener.setConnectionId(ObexHelper.convertToLong(request.mConnectionID));
5549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            } else {
5553998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mListener.setConnectionId(1);
5569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
5579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5583998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (request.mAuthResp != null) {
5593998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (!handleAuthResp(request.mAuthResp)) {
5609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    code = ResponseCodes.OBEX_HTTP_UNAUTHORIZED;
5613998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mListener.onAuthenticationFailure(ObexHelper.getTagValue((byte)0x01,
5623998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                            request.mAuthResp));
5639439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
5643998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                request.mAuthResp = null;
5659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
5669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5679439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            if (code != ResponseCodes.OBEX_HTTP_UNAUTHORIZED) {
5683998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (request.mAuthChall != null) {
5699439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    handleAuthChall(request);
5703998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    reply.mAuthResp = new byte[request.mAuthResp.length];
5713998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    System.arraycopy(request.mAuthResp, 0, reply.mAuthResp, 0,
5723998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                            reply.mAuthResp.length);
5733998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    request.mAuthChall = null;
5743998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    request.mAuthResp = null;
5759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
5769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                try {
5783998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    code = mListener.onConnect(request, reply);
5799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    code = validateResponseCode(code);
5809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (reply.nonce != null) {
5823998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        mChallengeDigest = new byte[16];
5833998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        System.arraycopy(reply.nonce, 0, mChallengeDigest, 0, 16);
5849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    } else {
5853998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        mChallengeDigest = null;
5869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
5873998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    long id = mListener.getConnectionId();
5889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (id == -1) {
5893998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        reply.mConnectionID = null;
5909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    } else {
5913998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        reply.mConnectionID = ObexHelper.convertToByteArray(id);
5929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
5939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5942e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly                    head = ObexHelper.createHeader(reply, false);
5959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    totalLength += head.length;
5969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5973998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    if (totalLength > mMaxPacketLength) {
5989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        totalLength = 7;
5999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        head = null;
6009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        code = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
6019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
6029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                } catch (Exception e) {
6039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    e.printStackTrace();
6049439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    totalLength = 7;
6059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    head = null;
6069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    code = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
6079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
6089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
6109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        // Compute Length of OBEX CONNECT packet
6132e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly        byte[] length = ObexHelper.convertToByteArray(totalLength);
6149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        /*
6169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * Write the OBEX CONNECT packet to the server. Byte 0: response code
6179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * Byte 1&2: Connect Packet Length Byte 3: OBEX Version Number
6189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * (Presently, 0x10) Byte 4: Flags (For TCP 0x00) Byte 5&6: Max OBEX
6199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * Packet Length (Defined in MAX_PACKET_SIZE) Byte 7 to n: headers
6209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         */
6219439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        byte[] sendData = new byte[totalLength];
6229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        sendData[0] = (byte)code;
6239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        sendData[1] = length[2];
6249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        sendData[2] = length[3];
6259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        sendData[3] = (byte)0x10;
6269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        sendData[4] = (byte)0x00;
6272e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly        sendData[5] = (byte)(ObexHelper.MAX_PACKET_SIZE_INT >> 8);
6282e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly        sendData[6] = (byte)(ObexHelper.MAX_PACKET_SIZE_INT & 0xFF);
6299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if (head != null) {
6319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            System.arraycopy(head, 0, sendData, 7, head.length);
6329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6343998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mOutput.write(sendData);
6353998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mOutput.flush();
6369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
6379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6389439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
6399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Closes the server session - in detail close I/O streams and the
6409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * underlying transport layer. Internal flag is also set so that later
6419439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * attempt to read/write will throw an exception.
6429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
6439439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public synchronized void close() {
6443998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mListener != null) {
6453998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            mListener.onClose();
6469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6479439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        try {
6483998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            mInput.close();
6493998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            mOutput.close();
6503998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            mTransport.close();
6513998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            mClosed = true;
6529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } catch (Exception e) {
6539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6543998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mTransport = null;
6553998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mInput = null;
6563998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mOutput = null;
6573998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mListener = null;
6589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
6599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
6619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Verifies that the response code is valid. If it is not valid, it will
6629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * return the <code>OBEX_HTTP_INTERNAL_ERROR</code> response code.
6639439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *
6649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @param code
6659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *            the response code to check
6669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *
6679439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return the valid response code or <code>OBEX_HTTP_INTERNAL_ERROR</code>
6689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *         if <code>code</code> is not valid
6699439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
6709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    private int validateResponseCode(int code) {
6719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if ((code >= ResponseCodes.OBEX_HTTP_OK) && (code <= ResponseCodes.OBEX_HTTP_PARTIAL)) {
6739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            return code;
6749439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if ((code >= ResponseCodes.OBEX_HTTP_MULT_CHOICE)
6769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                && (code <= ResponseCodes.OBEX_HTTP_USE_PROXY)) {
6779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            return code;
6789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if ((code >= ResponseCodes.OBEX_HTTP_BAD_REQUEST)
6809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                && (code <= ResponseCodes.OBEX_HTTP_UNSUPPORTED_TYPE)) {
6819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            return code;
6829439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if ((code >= ResponseCodes.OBEX_HTTP_INTERNAL_ERROR)
6849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                && (code <= ResponseCodes.OBEX_HTTP_VERSION)) {
6859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            return code;
6869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if ((code >= ResponseCodes.OBEX_DATABASE_FULL)
6889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                && (code <= ResponseCodes.OBEX_DATABASE_LOCKED)) {
6899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            return code;
6909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
6929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
6939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly}
695