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.
432e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly * @hide
449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly */
453998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejunpublic final class ServerSession extends ObexSession implements Runnable {
469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
473998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private static final String TAG = "Obex ServerSession";
489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
493998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private ObexTransport mTransport;
509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
513998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private InputStream mInput;
529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
533998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private OutputStream mOutput;
549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
553998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private ServerRequestHandler mListener;
569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
573998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private Thread mProcessThread;
589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
593998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private int mMaxPacketLength;
609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
613998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    private boolean mClosed;
629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
639439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Creates new ServerSession.
6505ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @param trans the connection to the client
6605ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @param handler the event listener that will process requests
6705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @param auth the authenticator to use with this connection
6805ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @throws IOException if an error occurred while opening the input and
6905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     *         output streams
709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
713998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    public ServerSession(ObexTransport trans, ServerRequestHandler handler, Authenticator auth)
729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            throws IOException {
733998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mAuthenticator = auth;
743998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mTransport = trans;
753998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mInput = mTransport.openInputStream();
763998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mOutput = mTransport.openOutputStream();
773998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mListener = handler;
783998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mMaxPacketLength = 256;
793998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun
803998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mClosed = false;
813998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mProcessThread = new Thread(this);
823998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mProcessThread.start();
839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Processes requests made to the server and forwards them to the
879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * appropriate event listener.
889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public void run() {
909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        try {
919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            boolean done = false;
933998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            while (!done && !mClosed) {
943998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                int requestType = mInput.read();
959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                switch (requestType) {
963998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    case ObexHelper.OBEX_OPCODE_CONNECT:
979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        handleConnectRequest();
989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        break;
999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1003998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    case ObexHelper.OBEX_OPCODE_DISCONNECT:
1019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        handleDisconnectRequest();
1029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        done = true;
1039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        break;
1049439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1053998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    case ObexHelper.OBEX_OPCODE_GET:
1063998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    case ObexHelper.OBEX_OPCODE_GET_FINAL:
1079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        handleGetRequest(requestType);
1089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        break;
1099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1103998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    case ObexHelper.OBEX_OPCODE_PUT:
1113998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    case ObexHelper.OBEX_OPCODE_PUT_FINAL:
1129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        handlePutRequest(requestType);
1139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        break;
1149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1153998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    case ObexHelper.OBEX_OPCODE_SETPATH:
1169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        handleSetPathRequest();
1179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        break;
1188258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue                    case ObexHelper.OBEX_OPCODE_ABORT:
1198258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue                        handleAbortRequest();
1208258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue                        break;
1219439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    case -1:
1239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        done = true;
1249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        break;
1259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    default:
1279439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        /*
1299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                         * Received a request type that is not recognized so I am
1309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                         * just going to read the packet and send a not implemented
1319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                         * to the client
1329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                         */
1333998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        int length = mInput.read();
1343998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        length = (length << 8) + mInput.read();
1359439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        for (int i = 3; i < length; i++) {
1363998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                            mInput.read();
1379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        }
1389439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        sendResponse(ResponseCodes.OBEX_HTTP_NOT_IMPLEMENTED, null);
1399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
1409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
1419439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } catch (NullPointerException e) {
1433998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            Log.d(TAG, e.toString());
1449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } catch (Exception e) {
1453998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            Log.d(TAG, e.toString());
1469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
1479439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        close();
1489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
1499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
1518258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue     * Handles a ABORT request from a client. This method will read the rest of
1528258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue     * the request from the client. Assuming the request is valid, it will
1538258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue     * create a <code>HeaderSet</code> object to pass to the
1548258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue     * <code>ServerRequestHandler</code> object. After the handler processes the
1558258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue     * request, this method will create a reply message to send to the server.
1568258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue     *
1578258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue     * @throws IOException if an error occurred at the transport layer
1588258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue     */
1598258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue    private void handleAbortRequest() throws IOException {
1608258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue        int code = ResponseCodes.OBEX_HTTP_OK;
1618258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue        HeaderSet request = new HeaderSet();
1628258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue        HeaderSet reply = new HeaderSet();
1638258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue
1648258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue        int length = mInput.read();
1658258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue        length = (length << 8) + mInput.read();
1668258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue        if (length > ObexHelper.MAX_PACKET_SIZE_INT) {
1678258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue            code = ResponseCodes.OBEX_HTTP_REQ_TOO_LARGE;
1688258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue        } else {
1698258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue            for (int i = 3; i < length; i++) {
1708258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue                mInput.read();
1718258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue            }
1728258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue            code = mListener.onAbort(request, reply);
1738258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue            Log.v(TAG, "onAbort request handler return value- " + code);
1748258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue            code = validateResponseCode(code);
1758258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue        }
1768258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue        sendResponse(code, null);
1778258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue    }
1788258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue
1798258ebdf128ef70a96fcaaa294167e39f1b5e94eLixin Yue    /**
1809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Handles a PUT request from a client. This method will provide a
1819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <code>ServerOperation</code> object to the request handler. The
1829439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <code>ServerOperation</code> object will handle the rest of the request.
1839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * It will also send replies and receive requests until the final reply
1849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * should be sent. When the final reply should be sent, this method will get
1859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * the response code to use and send the reply. The
1869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <code>ServerOperation</code> object will always reply with a
1879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * OBEX_HTTP_CONTINUE reply. It will only reply if further information is
1889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * needed.
18905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @param type the type of request received; either 0x02 or 0x82
19005ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @throws IOException if an error occurred at the transport layer
1919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
1929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    private void handlePutRequest(int type) throws IOException {
1933998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        ServerOperation op = new ServerOperation(this, mInput, type, mMaxPacketLength, mListener);
1949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        try {
1959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            int response = -1;
1969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
1973998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if ((op.finalBitSet) && !op.isValidBody()) {
1983998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                response = validateResponseCode(mListener
1993998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        .onDelete(op.requestHeader, op.replyHeader));
2009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            } else {
2013998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                response = validateResponseCode(mListener.onPut(op));
2029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
2030b309d6ee433d55b7499d7bbd447494366ef1316Jackson Fan            if (response != ResponseCodes.OBEX_HTTP_OK && !op.isAborted) {
2043998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                op.sendReply(response);
2053998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            } else if (!op.isAborted) {
2069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // wait for the final bit
2073998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                while (!op.finalBitSet) {
2083998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    op.sendReply(ResponseCodes.OBEX_HTTP_CONTINUE);
2099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
2103998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                op.sendReply(response);
2119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
2129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } catch (Exception e) {
21305ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun            /*To fix bugs in aborted cases,
21405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun             *(client abort file transfer prior to the last packet which has the end of body header,
21505ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun             *internal error should not be sent because server has already replied with
21605ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun             *OK response in "sendReply")
21705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun             */
21805ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun            if (!op.isAborted) {
21905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun                sendResponse(ResponseCodes.OBEX_HTTP_INTERNAL_ERROR, null);
22005ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun            }
2219439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
2229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
2239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
2259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Handles a GET request from a client. This method will provide a
2269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <code>ServerOperation</code> object to the request handler. The
2279439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <code>ServerOperation</code> object will handle the rest of the request.
2289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * It will also send replies and receive requests until the final reply
2299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * should be sent. When the final reply should be sent, this method will get
2309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * the response code to use and send the reply. The
2319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <code>ServerOperation</code> object will always reply with a
2329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * OBEX_HTTP_CONTINUE reply. It will only reply if further information is
2339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * needed.
23405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @param type the type of request received; either 0x03 or 0x83
23505ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @throws IOException if an error occurred at the transport layer
2369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
2379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    private void handleGetRequest(int type) throws IOException {
2383998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        ServerOperation op = new ServerOperation(this, mInput, type, mMaxPacketLength, mListener);
2399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        try {
2403998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            int response = validateResponseCode(mListener.onGet(op));
2419439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2423998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (!op.isAborted) {
2433998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                op.sendReply(response);
2449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
2459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } catch (Exception e) {
2469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            sendResponse(ResponseCodes.OBEX_HTTP_INTERNAL_ERROR, null);
2479439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
2489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
2499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
2519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Send standard response.
25205ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @param code the response code to send
25305ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @param header the headers to include in the response
25405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @throws IOException if an IO error occurs
2559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
2563998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun    public void sendResponse(int code, byte[] header) throws IOException {
2579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int totalLength = 3;
2589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        byte[] data = null;
2599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if (header != null) {
2619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            totalLength += header.length;
2629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            data = new byte[totalLength];
2639439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            data[0] = (byte)code;
2649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            data[1] = (byte)(totalLength >> 8);
2659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            data[2] = (byte)totalLength;
2669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            System.arraycopy(header, 0, data, 3, header.length);
2679439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } else {
2689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            data = new byte[totalLength];
2699439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            data[0] = (byte)code;
2709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            data[1] = (byte)0x00;
2719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            data[2] = (byte)totalLength;
2729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
2733998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mOutput.write(data);
2743998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mOutput.flush();
2759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
2769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
2789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Handles a SETPATH request from a client. This method will read the rest
2799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * of the request from the client. Assuming the request is valid, it will
2809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * create a <code>HeaderSet</code> object to pass to the
2819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <code>ServerRequestHandler</code> object. After the handler processes the
2829439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * request, this method will create a reply message to send to the server
2839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * with the response code provided.
28405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @throws IOException if an error occurred at the transport layer
2859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
2869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    private void handleSetPathRequest() throws IOException {
2879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int length;
2889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int flags;
2893998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        @SuppressWarnings("unused")
2909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int constants;
2919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int totalLength = 3;
2929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        byte[] head = null;
2939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int code = -1;
2949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int bytesReceived;
2959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        HeaderSet request = new HeaderSet();
2969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        HeaderSet reply = new HeaderSet();
2979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
2983998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        length = mInput.read();
2993998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        length = (length << 8) + mInput.read();
3003998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        flags = mInput.read();
3013998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        constants = mInput.read();
3029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3032e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly        if (length > ObexHelper.MAX_PACKET_SIZE_INT) {
3049439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            code = ResponseCodes.OBEX_HTTP_REQ_TOO_LARGE;
3059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            totalLength = 3;
3069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } else {
3079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            if (length > 5) {
3089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                byte[] headers = new byte[length - 5];
3093998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                bytesReceived = mInput.read(headers);
3109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                while (bytesReceived != headers.length) {
3123998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    bytesReceived += mInput.read(headers, bytesReceived, headers.length
3139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                            - bytesReceived);
3149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3162e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly                ObexHelper.updateHeaderSet(request, headers);
3179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
318e80534ff59b2e62a0ddf4359147b81f5ba10de86Tao Liejun                if (mListener.getConnectionId() != -1 && request.mConnectionID != null) {
3193998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mListener.setConnectionId(ObexHelper.convertToLong(request.mConnectionID));
3209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                } else {
321e80534ff59b2e62a0ddf4359147b81f5ba10de86Tao Liejun                    mListener.setConnectionId(1);
3229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3233998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                // the Auth chan is initiated by the server, client sent back the authResp .
3243998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (request.mAuthResp != null) {
3253998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    if (!handleAuthResp(request.mAuthResp)) {
3269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        code = ResponseCodes.OBEX_HTTP_UNAUTHORIZED;
3273998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        mListener.onAuthenticationFailure(ObexHelper.getTagValue((byte)0x01,
3283998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                                request.mAuthResp));
3299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
3303998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    request.mAuthResp = null;
3319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
3339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3349439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            if (code != ResponseCodes.OBEX_HTTP_UNAUTHORIZED) {
3353998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                // the Auth challenge is initiated by the client
3369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                // the server will send back the authResp to the client
3373998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (request.mAuthChall != null) {
3389439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    handleAuthChall(request);
3393998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    reply.mAuthResp = new byte[request.mAuthResp.length];
3403998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    System.arraycopy(request.mAuthResp, 0, reply.mAuthResp, 0,
3413998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                            reply.mAuthResp.length);
3423998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    request.mAuthChall = null;
3433998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    request.mAuthResp = null;
3449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                boolean backup = false;
3469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                boolean create = true;
3479439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if (!((flags & 1) == 0)) {
3489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    backup = true;
3499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
35069f72c00a12933f0fde049e8465257745f373d21Lixin Yue                if (!((flags & 2) == 0)) {
3519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    create = false;
3529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                try {
3553998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    code = mListener.onSetPath(request, reply, backup, create);
3569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                } catch (Exception e) {
3579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    sendResponse(ResponseCodes.OBEX_HTTP_INTERNAL_ERROR, null);
3589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    return;
3599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                code = validateResponseCode(code);
3629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3639439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if (reply.nonce != null) {
3643998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mChallengeDigest = new byte[16];
3653998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    System.arraycopy(reply.nonce, 0, mChallengeDigest, 0, 16);
3669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                } else {
3673998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mChallengeDigest = null;
3689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3699439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3703998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                long id = mListener.getConnectionId();
3719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if (id == -1) {
3723998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    reply.mConnectionID = null;
3739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                } else {
3743998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    reply.mConnectionID = ObexHelper.convertToByteArray(id);
3759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3772e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly                head = ObexHelper.createHeader(reply, false);
3789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                totalLength += head.length;
3799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3803998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (totalLength > mMaxPacketLength) {
3819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    totalLength = 3;
3829439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    head = null;
3839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    code = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
3849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
3859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
3869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
3879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
3889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        // Compute Length of OBEX SETPATH packet
3899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        byte[] replyData = new byte[totalLength];
3909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        replyData[0] = (byte)code;
3919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        replyData[1] = (byte)(totalLength >> 8);
3929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        replyData[2] = (byte)totalLength;
3939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if (head != null) {
3949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            System.arraycopy(head, 0, replyData, 3, head.length);
3959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
3969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        /*
3979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * Write the OBEX SETPATH packet to the server. Byte 0: response code
3989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * Byte 1&2: Connect Packet Length Byte 3 to n: headers
3999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         */
4003998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mOutput.write(replyData);
4013998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mOutput.flush();
4029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
4039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4049439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
4059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Handles a disconnect request from a client. This method will read the
4069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * rest of the request from the client. Assuming the request is valid, it
4079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * will create a <code>HeaderSet</code> object to pass to the
4089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <code>ServerRequestHandler</code> object. After the handler processes the
4099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * request, this method will create a reply message to send to the server.
41005ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @throws IOException if an error occurred at the transport layer
4119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
4129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    private void handleDisconnectRequest() throws IOException {
4139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int length;
4149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int code = ResponseCodes.OBEX_HTTP_OK;
4159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int totalLength = 3;
4169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        byte[] head = null;
4179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int bytesReceived;
4189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        HeaderSet request = new HeaderSet();
4199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        HeaderSet reply = new HeaderSet();
4209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4213998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        length = mInput.read();
4223998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        length = (length << 8) + mInput.read();
4239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4242e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly        if (length > ObexHelper.MAX_PACKET_SIZE_INT) {
4259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            code = ResponseCodes.OBEX_HTTP_REQ_TOO_LARGE;
4269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            totalLength = 3;
4279439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } else {
4289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            if (length > 3) {
4299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                byte[] headers = new byte[length - 3];
4303998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                bytesReceived = mInput.read(headers);
4319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                while (bytesReceived != headers.length) {
4333998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    bytesReceived += mInput.read(headers, bytesReceived, headers.length
4349439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                            - bytesReceived);
4359439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4372e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly                ObexHelper.updateHeaderSet(request, headers);
4389439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
4399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
440e80534ff59b2e62a0ddf4359147b81f5ba10de86Tao Liejun            if (mListener.getConnectionId() != -1 && request.mConnectionID != null) {
4413998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mListener.setConnectionId(ObexHelper.convertToLong(request.mConnectionID));
4429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            } else {
4433998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mListener.setConnectionId(1);
4449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
4459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4463998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (request.mAuthResp != null) {
4473998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (!handleAuthResp(request.mAuthResp)) {
4489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    code = ResponseCodes.OBEX_HTTP_UNAUTHORIZED;
4493998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mListener.onAuthenticationFailure(ObexHelper.getTagValue((byte)0x01,
4503998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                            request.mAuthResp));
4519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4523998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                request.mAuthResp = null;
4539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
4549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            if (code != ResponseCodes.OBEX_HTTP_UNAUTHORIZED) {
4569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4573998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (request.mAuthChall != null) {
4589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    handleAuthChall(request);
4593998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    request.mAuthChall = null;
4609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                try {
4633998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mListener.onDisconnect(request, reply);
4649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                } catch (Exception e) {
4659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    sendResponse(ResponseCodes.OBEX_HTTP_INTERNAL_ERROR, null);
4669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    return;
4679439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4693998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                long id = mListener.getConnectionId();
4709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                if (id == -1) {
4713998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    reply.mConnectionID = null;
4729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                } else {
4733998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    reply.mConnectionID = ObexHelper.convertToByteArray(id);
4749439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4762e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly                head = ObexHelper.createHeader(reply, false);
4779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                totalLength += head.length;
4789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4793998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (totalLength > mMaxPacketLength) {
4809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    totalLength = 3;
4819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    head = null;
4829439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    code = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
4839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
4849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
4859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
4869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
4879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        // Compute Length of OBEX CONNECT packet
4889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        byte[] replyData;
4899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if (head != null) {
4909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            replyData = new byte[3 + head.length];
4919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } else {
4929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            replyData = new byte[3];
4939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
4949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        replyData[0] = (byte)code;
4959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        replyData[1] = (byte)(totalLength >> 8);
4969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        replyData[2] = (byte)totalLength;
4979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if (head != null) {
4989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            System.arraycopy(head, 0, replyData, 3, head.length);
4999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        /*
5019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * Write the OBEX DISCONNECT packet to the server. Byte 0: response code
5029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * Byte 1&2: Connect Packet Length Byte 3 to n: headers
5039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         */
5043998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mOutput.write(replyData);
5053998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mOutput.flush();
5069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
5079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
5099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Handles a connect request from a client. This method will read the rest
5109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * of the request from the client. Assuming the request is valid, it will
5119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * create a <code>HeaderSet</code> object to pass to the
5129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * <code>ServerRequestHandler</code> object. After the handler processes the
5139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * request, this method will create a reply message to send to the server
5149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * with the response code provided.
51505ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @throws IOException if an error occurred at the transport layer
5169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
5179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    private void handleConnectRequest() throws IOException {
5189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int packetLength;
5193998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        @SuppressWarnings("unused")
5209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int version;
5213998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        @SuppressWarnings("unused")
5229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int flags;
5239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int totalLength = 7;
5249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        byte[] head = null;
5259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int code = -1;
5269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        HeaderSet request = new HeaderSet();
5279439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        HeaderSet reply = new HeaderSet();
5289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        int bytesReceived;
5299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        /*
5319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * Read in the length of the OBEX packet, OBEX version, flags, and max
5329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * packet length
5339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         */
5343998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        packetLength = mInput.read();
5353998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        packetLength = (packetLength << 8) + mInput.read();
5363998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        version = mInput.read();
5373998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        flags = mInput.read();
5383998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mMaxPacketLength = mInput.read();
5393998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mMaxPacketLength = (mMaxPacketLength << 8) + mInput.read();
5409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5419439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        // should we check it?
5423998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mMaxPacketLength > ObexHelper.MAX_PACKET_SIZE_INT) {
5433998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            mMaxPacketLength = ObexHelper.MAX_PACKET_SIZE_INT;
5449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
5459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5462e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly        if (packetLength > ObexHelper.MAX_PACKET_SIZE_INT) {
5479439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            code = ResponseCodes.OBEX_HTTP_REQ_TOO_LARGE;
5489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            totalLength = 7;
5499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } else {
5509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            if (packetLength > 7) {
5519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                byte[] headers = new byte[packetLength - 7];
5523998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                bytesReceived = mInput.read(headers);
5539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                while (bytesReceived != headers.length) {
5553998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    bytesReceived += mInput.read(headers, bytesReceived, headers.length
5569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                            - bytesReceived);
5579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
5589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5592e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly                ObexHelper.updateHeaderSet(request, headers);
5609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
5619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
562e80534ff59b2e62a0ddf4359147b81f5ba10de86Tao Liejun            if (mListener.getConnectionId() != -1 && request.mConnectionID != null) {
5633998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mListener.setConnectionId(ObexHelper.convertToLong(request.mConnectionID));
5649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            } else {
5653998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                mListener.setConnectionId(1);
5669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
5679439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5683998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            if (request.mAuthResp != null) {
5693998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (!handleAuthResp(request.mAuthResp)) {
5709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    code = ResponseCodes.OBEX_HTTP_UNAUTHORIZED;
5713998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    mListener.onAuthenticationFailure(ObexHelper.getTagValue((byte)0x01,
5723998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                            request.mAuthResp));
5739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
5743998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                request.mAuthResp = null;
5759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
5769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            if (code != ResponseCodes.OBEX_HTTP_UNAUTHORIZED) {
5783998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                if (request.mAuthChall != null) {
5799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    handleAuthChall(request);
5803998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    reply.mAuthResp = new byte[request.mAuthResp.length];
5813998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    System.arraycopy(request.mAuthResp, 0, reply.mAuthResp, 0,
5823998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                            reply.mAuthResp.length);
5833998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    request.mAuthChall = null;
5843998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    request.mAuthResp = null;
5859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
5869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                try {
5883998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    code = mListener.onConnect(request, reply);
5899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    code = validateResponseCode(code);
5909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
5919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (reply.nonce != null) {
5923998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        mChallengeDigest = new byte[16];
5933998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        System.arraycopy(reply.nonce, 0, mChallengeDigest, 0, 16);
5949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    } else {
5953998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        mChallengeDigest = null;
5969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
5973998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    long id = mListener.getConnectionId();
5989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    if (id == -1) {
5993998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        reply.mConnectionID = null;
6009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    } else {
6013998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                        reply.mConnectionID = ObexHelper.convertToByteArray(id);
6029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
6039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6042e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly                    head = ObexHelper.createHeader(reply, false);
6059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    totalLength += head.length;
6069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6073998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun                    if (totalLength > mMaxPacketLength) {
6089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        totalLength = 7;
6099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        head = null;
6109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                        code = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
6119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    }
6129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                } catch (Exception e) {
6139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    e.printStackTrace();
6149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    totalLength = 7;
6159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    head = null;
6169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                    code = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
6179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                }
6189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            }
6209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6219439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        // Compute Length of OBEX CONNECT packet
6232e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly        byte[] length = ObexHelper.convertToByteArray(totalLength);
6249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        /*
6269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * Write the OBEX CONNECT packet to the server. Byte 0: response code
6279439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * Byte 1&2: Connect Packet Length Byte 3: OBEX Version Number
6289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * (Presently, 0x10) Byte 4: Flags (For TCP 0x00) Byte 5&6: Max OBEX
6299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         * Packet Length (Defined in MAX_PACKET_SIZE) Byte 7 to n: headers
6309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly         */
6319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        byte[] sendData = new byte[totalLength];
6329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        sendData[0] = (byte)code;
6339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        sendData[1] = length[2];
6349439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        sendData[2] = length[3];
6359439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        sendData[3] = (byte)0x10;
6369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        sendData[4] = (byte)0x00;
6372e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly        sendData[5] = (byte)(ObexHelper.MAX_PACKET_SIZE_INT >> 8);
6382e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly        sendData[6] = (byte)(ObexHelper.MAX_PACKET_SIZE_INT & 0xFF);
6399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if (head != null) {
6419439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            System.arraycopy(head, 0, sendData, 7, head.length);
6429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6439439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6443998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mOutput.write(sendData);
6453998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mOutput.flush();
6469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
6479439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
6499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Closes the server session - in detail close I/O streams and the
6509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * underlying transport layer. Internal flag is also set so that later
6519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * attempt to read/write will throw an exception.
6529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
6539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    public synchronized void close() {
6543998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        if (mListener != null) {
6553998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            mListener.onClose();
6569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        try {
6583998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            mInput.close();
6593998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            mOutput.close();
6603998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            mTransport.close();
6613998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun            mClosed = true;
6629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        } catch (Exception e) {
6639439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6643998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mTransport = null;
6653998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mInput = null;
6663998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mOutput = null;
6673998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun        mListener = null;
6689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
6699439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    /**
6719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * Verifies that the response code is valid. If it is not valid, it will
6729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * return the <code>OBEX_HTTP_INTERNAL_ERROR</code> response code.
67305ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun     * @param code the response code to check
6749439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     * @return the valid response code or <code>OBEX_HTTP_INTERNAL_ERROR</code>
6759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     *         if <code>code</code> is not valid
6769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly     */
6779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    private int validateResponseCode(int code) {
6789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
6799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if ((code >= ResponseCodes.OBEX_HTTP_OK) && (code <= ResponseCodes.OBEX_HTTP_PARTIAL)) {
6809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            return code;
6819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6829439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if ((code >= ResponseCodes.OBEX_HTTP_MULT_CHOICE)
6839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                && (code <= ResponseCodes.OBEX_HTTP_USE_PROXY)) {
6849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            return code;
6859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if ((code >= ResponseCodes.OBEX_HTTP_BAD_REQUEST)
6879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                && (code <= ResponseCodes.OBEX_HTTP_UNSUPPORTED_TYPE)) {
6889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            return code;
6899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if ((code >= ResponseCodes.OBEX_HTTP_INTERNAL_ERROR)
6919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                && (code <= ResponseCodes.OBEX_HTTP_VERSION)) {
6929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            return code;
6939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        if ((code >= ResponseCodes.OBEX_DATABASE_FULL)
6959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly                && (code <= ResponseCodes.OBEX_DATABASE_LOCKED)) {
6969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly            return code;
6979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        }
6989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly        return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
6999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly    }
7009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly
7019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly}
702