ServerSession.java revision 0d376053747615ac7c4b45ab7810329ffbdf80d1
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; 2590d376053747615ac7c4b45ab7810329ffbdf80d1Kim Schulz OutputStream op = mOutput; 2600d376053747615ac7c4b45ab7810329ffbdf80d1Kim Schulz if (op == null) { 2610d376053747615ac7c4b45ab7810329ffbdf80d1Kim Schulz return; 2620d376053747615ac7c4b45ab7810329ffbdf80d1Kim Schulz } 2639439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 2649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly if (header != null) { 2659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly totalLength += header.length; 2669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly data = new byte[totalLength]; 2679439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly data[0] = (byte)code; 2689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly data[1] = (byte)(totalLength >> 8); 2699439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly data[2] = (byte)totalLength; 2709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly System.arraycopy(header, 0, data, 3, header.length); 2719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } else { 2729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly data = new byte[totalLength]; 2739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly data[0] = (byte)code; 2749439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly data[1] = (byte)0x00; 2759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly data[2] = (byte)totalLength; 2769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 2770d376053747615ac7c4b45ab7810329ffbdf80d1Kim Schulz op.write(data); 2780d376053747615ac7c4b45ab7810329ffbdf80d1Kim Schulz op.flush(); 2799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 2809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 2819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly /** 2829439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * Handles a SETPATH request from a client. This method will read the rest 2839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * of the request from the client. Assuming the request is valid, it will 2849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * create a <code>HeaderSet</code> object to pass to the 2859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * <code>ServerRequestHandler</code> object. After the handler processes the 2869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * request, this method will create a reply message to send to the server 2879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * with the response code provided. 28805ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun * @throws IOException if an error occurred at the transport layer 2899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly */ 2909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly private void handleSetPathRequest() throws IOException { 2919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly int length; 2929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly int flags; 2933998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun @SuppressWarnings("unused") 2949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly int constants; 2959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly int totalLength = 3; 2969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly byte[] head = null; 2979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly int code = -1; 2989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly int bytesReceived; 2999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly HeaderSet request = new HeaderSet(); 3009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly HeaderSet reply = new HeaderSet(); 3019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 3023998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun length = mInput.read(); 3033998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun length = (length << 8) + mInput.read(); 3043998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun flags = mInput.read(); 3053998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun constants = mInput.read(); 3069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 3072e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly if (length > ObexHelper.MAX_PACKET_SIZE_INT) { 3089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly code = ResponseCodes.OBEX_HTTP_REQ_TOO_LARGE; 3099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly totalLength = 3; 3109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } else { 3119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly if (length > 5) { 3129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly byte[] headers = new byte[length - 5]; 3133998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun bytesReceived = mInput.read(headers); 3149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 3159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly while (bytesReceived != headers.length) { 3163998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun bytesReceived += mInput.read(headers, bytesReceived, headers.length 3179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly - bytesReceived); 3189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 3199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 3202e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly ObexHelper.updateHeaderSet(request, headers); 3219439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 322e80534ff59b2e62a0ddf4359147b81f5ba10de86Tao Liejun if (mListener.getConnectionId() != -1 && request.mConnectionID != null) { 3233998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mListener.setConnectionId(ObexHelper.convertToLong(request.mConnectionID)); 3249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } else { 325e80534ff59b2e62a0ddf4359147b81f5ba10de86Tao Liejun mListener.setConnectionId(1); 3269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 3273998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun // the Auth chan is initiated by the server, client sent back the authResp . 3283998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun if (request.mAuthResp != null) { 3293998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun if (!handleAuthResp(request.mAuthResp)) { 3309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly code = ResponseCodes.OBEX_HTTP_UNAUTHORIZED; 3313998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mListener.onAuthenticationFailure(ObexHelper.getTagValue((byte)0x01, 3323998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun request.mAuthResp)); 3339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 3343998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun request.mAuthResp = null; 3359439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 3369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 3379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 3389439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly if (code != ResponseCodes.OBEX_HTTP_UNAUTHORIZED) { 3393998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun // the Auth challenge is initiated by the client 3409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly // the server will send back the authResp to the client 3413998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun if (request.mAuthChall != null) { 3429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly handleAuthChall(request); 3433998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun reply.mAuthResp = new byte[request.mAuthResp.length]; 3443998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun System.arraycopy(request.mAuthResp, 0, reply.mAuthResp, 0, 3453998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun reply.mAuthResp.length); 3463998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun request.mAuthChall = null; 3473998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun request.mAuthResp = null; 3489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 3499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly boolean backup = false; 3509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly boolean create = true; 3519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly if (!((flags & 1) == 0)) { 3529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly backup = true; 3539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 35469f72c00a12933f0fde049e8465257745f373d21Lixin Yue if (!((flags & 2) == 0)) { 3559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly create = false; 3569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 3579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 3589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly try { 3593998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun code = mListener.onSetPath(request, reply, backup, create); 3609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } catch (Exception e) { 3619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly sendResponse(ResponseCodes.OBEX_HTTP_INTERNAL_ERROR, null); 3629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly return; 3639439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 3649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 3659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly code = validateResponseCode(code); 3669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 3679439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly if (reply.nonce != null) { 3683998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mChallengeDigest = new byte[16]; 3693998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun System.arraycopy(reply.nonce, 0, mChallengeDigest, 0, 16); 3709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } else { 3713998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mChallengeDigest = null; 3729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 3739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 3743998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun long id = mListener.getConnectionId(); 3759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly if (id == -1) { 3763998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun reply.mConnectionID = null; 3779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } else { 3783998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun reply.mConnectionID = ObexHelper.convertToByteArray(id); 3799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 3809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 3812e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly head = ObexHelper.createHeader(reply, false); 3829439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly totalLength += head.length; 3839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 3843998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun if (totalLength > mMaxPacketLength) { 3859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly totalLength = 3; 3869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly head = null; 3879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly code = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR; 3889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 3899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 3909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 3919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 3929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly // Compute Length of OBEX SETPATH packet 3939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly byte[] replyData = new byte[totalLength]; 3949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly replyData[0] = (byte)code; 3959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly replyData[1] = (byte)(totalLength >> 8); 3969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly replyData[2] = (byte)totalLength; 3979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly if (head != null) { 3989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly System.arraycopy(head, 0, replyData, 3, head.length); 3999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 4009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly /* 4019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * Write the OBEX SETPATH packet to the server. Byte 0: response code 4029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * Byte 1&2: Connect Packet Length Byte 3 to n: headers 4039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly */ 4043998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mOutput.write(replyData); 4053998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mOutput.flush(); 4069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 4079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 4089439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly /** 4099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * Handles a disconnect request from a client. This method will read the 4109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * rest of the request from the client. Assuming the request is valid, it 4119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * will create a <code>HeaderSet</code> object to pass to the 4129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * <code>ServerRequestHandler</code> object. After the handler processes the 4139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * request, this method will create a reply message to send to the server. 41405ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun * @throws IOException if an error occurred at the transport layer 4159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly */ 4169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly private void handleDisconnectRequest() throws IOException { 4179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly int length; 4189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly int code = ResponseCodes.OBEX_HTTP_OK; 4199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly int totalLength = 3; 4209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly byte[] head = null; 4219439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly int bytesReceived; 4229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly HeaderSet request = new HeaderSet(); 4239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly HeaderSet reply = new HeaderSet(); 4249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 4253998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun length = mInput.read(); 4263998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun length = (length << 8) + mInput.read(); 4279439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 4282e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly if (length > ObexHelper.MAX_PACKET_SIZE_INT) { 4299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly code = ResponseCodes.OBEX_HTTP_REQ_TOO_LARGE; 4309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly totalLength = 3; 4319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } else { 4329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly if (length > 3) { 4339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly byte[] headers = new byte[length - 3]; 4343998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun bytesReceived = mInput.read(headers); 4359439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 4369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly while (bytesReceived != headers.length) { 4373998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun bytesReceived += mInput.read(headers, bytesReceived, headers.length 4389439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly - bytesReceived); 4399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 4409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 4412e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly ObexHelper.updateHeaderSet(request, headers); 4429439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 4439439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 444e80534ff59b2e62a0ddf4359147b81f5ba10de86Tao Liejun if (mListener.getConnectionId() != -1 && request.mConnectionID != null) { 4453998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mListener.setConnectionId(ObexHelper.convertToLong(request.mConnectionID)); 4469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } else { 4473998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mListener.setConnectionId(1); 4489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 4499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 4503998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun if (request.mAuthResp != null) { 4513998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun if (!handleAuthResp(request.mAuthResp)) { 4529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly code = ResponseCodes.OBEX_HTTP_UNAUTHORIZED; 4533998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mListener.onAuthenticationFailure(ObexHelper.getTagValue((byte)0x01, 4543998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun request.mAuthResp)); 4559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 4563998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun request.mAuthResp = null; 4579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 4589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 4599439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly if (code != ResponseCodes.OBEX_HTTP_UNAUTHORIZED) { 4609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 4613998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun if (request.mAuthChall != null) { 4629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly handleAuthChall(request); 4633998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun request.mAuthChall = null; 4649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 4659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 4669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly try { 4673998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mListener.onDisconnect(request, reply); 4689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } catch (Exception e) { 4699439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly sendResponse(ResponseCodes.OBEX_HTTP_INTERNAL_ERROR, null); 4709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly return; 4719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 4729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 4733998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun long id = mListener.getConnectionId(); 4749439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly if (id == -1) { 4753998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun reply.mConnectionID = null; 4769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } else { 4773998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun reply.mConnectionID = ObexHelper.convertToByteArray(id); 4789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 4799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 4802e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly head = ObexHelper.createHeader(reply, false); 4819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly totalLength += head.length; 4829439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 4833998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun if (totalLength > mMaxPacketLength) { 4849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly totalLength = 3; 4859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly head = null; 4869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly code = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR; 4879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 4889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 4899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 4909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 4919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly // Compute Length of OBEX CONNECT packet 4929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly byte[] replyData; 4939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly if (head != null) { 4949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly replyData = new byte[3 + head.length]; 4959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } else { 4969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly replyData = new byte[3]; 4979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 4989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly replyData[0] = (byte)code; 4999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly replyData[1] = (byte)(totalLength >> 8); 5009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly replyData[2] = (byte)totalLength; 5019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly if (head != null) { 5029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly System.arraycopy(head, 0, replyData, 3, head.length); 5039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 5049439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly /* 5059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * Write the OBEX DISCONNECT packet to the server. Byte 0: response code 5069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * Byte 1&2: Connect Packet Length Byte 3 to n: headers 5079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly */ 5083998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mOutput.write(replyData); 5093998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mOutput.flush(); 5109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 5119439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 5129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly /** 5139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * Handles a connect request from a client. This method will read the rest 5149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * of the request from the client. Assuming the request is valid, it will 5159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * create a <code>HeaderSet</code> object to pass to the 5169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * <code>ServerRequestHandler</code> object. After the handler processes the 5179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * request, this method will create a reply message to send to the server 5189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * with the response code provided. 51905ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun * @throws IOException if an error occurred at the transport layer 5209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly */ 5219439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly private void handleConnectRequest() throws IOException { 5229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly int packetLength; 5233998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun @SuppressWarnings("unused") 5249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly int version; 5253998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun @SuppressWarnings("unused") 5269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly int flags; 5279439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly int totalLength = 7; 5289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly byte[] head = null; 5299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly int code = -1; 5309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly HeaderSet request = new HeaderSet(); 5319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly HeaderSet reply = new HeaderSet(); 5329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly int bytesReceived; 5339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 5349439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly /* 5359439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * Read in the length of the OBEX packet, OBEX version, flags, and max 5369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * packet length 5379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly */ 5383998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun packetLength = mInput.read(); 5393998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun packetLength = (packetLength << 8) + mInput.read(); 5403998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun version = mInput.read(); 5413998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun flags = mInput.read(); 5423998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mMaxPacketLength = mInput.read(); 5433998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mMaxPacketLength = (mMaxPacketLength << 8) + mInput.read(); 5449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 5459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly // should we check it? 5463998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun if (mMaxPacketLength > ObexHelper.MAX_PACKET_SIZE_INT) { 5473998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mMaxPacketLength = ObexHelper.MAX_PACKET_SIZE_INT; 5489439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 5499439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 5502e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly if (packetLength > ObexHelper.MAX_PACKET_SIZE_INT) { 5519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly code = ResponseCodes.OBEX_HTTP_REQ_TOO_LARGE; 5529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly totalLength = 7; 5539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } else { 5549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly if (packetLength > 7) { 5559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly byte[] headers = new byte[packetLength - 7]; 5563998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun bytesReceived = mInput.read(headers); 5579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 5589439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly while (bytesReceived != headers.length) { 5593998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun bytesReceived += mInput.read(headers, bytesReceived, headers.length 5609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly - bytesReceived); 5619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 5629439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 5632e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly ObexHelper.updateHeaderSet(request, headers); 5649439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 5659439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 566e80534ff59b2e62a0ddf4359147b81f5ba10de86Tao Liejun if (mListener.getConnectionId() != -1 && request.mConnectionID != null) { 5673998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mListener.setConnectionId(ObexHelper.convertToLong(request.mConnectionID)); 5689439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } else { 5693998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mListener.setConnectionId(1); 5709439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 5719439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 5723998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun if (request.mAuthResp != null) { 5733998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun if (!handleAuthResp(request.mAuthResp)) { 5749439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly code = ResponseCodes.OBEX_HTTP_UNAUTHORIZED; 5753998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mListener.onAuthenticationFailure(ObexHelper.getTagValue((byte)0x01, 5763998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun request.mAuthResp)); 5779439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 5783998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun request.mAuthResp = null; 5799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 5809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 5819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly if (code != ResponseCodes.OBEX_HTTP_UNAUTHORIZED) { 5823998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun if (request.mAuthChall != null) { 5839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly handleAuthChall(request); 5843998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun reply.mAuthResp = new byte[request.mAuthResp.length]; 5853998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun System.arraycopy(request.mAuthResp, 0, reply.mAuthResp, 0, 5863998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun reply.mAuthResp.length); 5873998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun request.mAuthChall = null; 5883998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun request.mAuthResp = null; 5899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 5909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 5919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly try { 5923998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun code = mListener.onConnect(request, reply); 5939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly code = validateResponseCode(code); 5949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 5959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly if (reply.nonce != null) { 5963998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mChallengeDigest = new byte[16]; 5973998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun System.arraycopy(reply.nonce, 0, mChallengeDigest, 0, 16); 5989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } else { 5993998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mChallengeDigest = null; 6009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 6013998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun long id = mListener.getConnectionId(); 6029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly if (id == -1) { 6033998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun reply.mConnectionID = null; 6049439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } else { 6053998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun reply.mConnectionID = ObexHelper.convertToByteArray(id); 6069439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 6079439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 6082e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly head = ObexHelper.createHeader(reply, false); 6099439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly totalLength += head.length; 6109439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 6113998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun if (totalLength > mMaxPacketLength) { 6129439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly totalLength = 7; 6139439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly head = null; 6149439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly code = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR; 6159439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 6169439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } catch (Exception e) { 6179439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly e.printStackTrace(); 6189439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly totalLength = 7; 6199439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly head = null; 6209439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly code = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR; 6219439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 6229439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 6239439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 6249439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 6259439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 6269439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly // Compute Length of OBEX CONNECT packet 6272e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly byte[] length = ObexHelper.convertToByteArray(totalLength); 6289439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 6299439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly /* 6309439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * Write the OBEX CONNECT packet to the server. Byte 0: response code 6319439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * Byte 1&2: Connect Packet Length Byte 3: OBEX Version Number 6329439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * (Presently, 0x10) Byte 4: Flags (For TCP 0x00) Byte 5&6: Max OBEX 6339439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * Packet Length (Defined in MAX_PACKET_SIZE) Byte 7 to n: headers 6349439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly */ 6359439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly byte[] sendData = new byte[totalLength]; 6369439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly sendData[0] = (byte)code; 6379439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly sendData[1] = length[2]; 6389439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly sendData[2] = length[3]; 6399439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly sendData[3] = (byte)0x10; 6409439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly sendData[4] = (byte)0x00; 6412e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly sendData[5] = (byte)(ObexHelper.MAX_PACKET_SIZE_INT >> 8); 6422e0da96e757a977154063f980d3f4e1abd41cf09Nick Pelly sendData[6] = (byte)(ObexHelper.MAX_PACKET_SIZE_INT & 0xFF); 6439439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 6449439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly if (head != null) { 6459439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly System.arraycopy(head, 0, sendData, 7, head.length); 6469439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 6479439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 6483998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mOutput.write(sendData); 6493998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mOutput.flush(); 6509439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 6519439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 6529439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly /** 6539439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * Closes the server session - in detail close I/O streams and the 6549439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * underlying transport layer. Internal flag is also set so that later 6559439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * attempt to read/write will throw an exception. 6569439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly */ 6579439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly public synchronized void close() { 6583998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun if (mListener != null) { 6593998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mListener.onClose(); 6609439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 6619439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly try { 6623998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mInput.close(); 6633998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mOutput.close(); 6643998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mTransport.close(); 6653998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mClosed = true; 6669439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } catch (Exception e) { 6679439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 6683998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mTransport = null; 6693998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mInput = null; 6703998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mOutput = null; 6713998bf009acaf8cde4d7f837f8b8e41ae0a65141Tao Liejun mListener = null; 6729439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 6739439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 6749439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly /** 6759439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * Verifies that the response code is valid. If it is not valid, it will 6769439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * return the <code>OBEX_HTTP_INTERNAL_ERROR</code> response code. 67705ff98bbefda39b9ff26f8bca132cfd0248745c6Tao Liejun * @param code the response code to check 6789439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * @return the valid response code or <code>OBEX_HTTP_INTERNAL_ERROR</code> 6799439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly * if <code>code</code> is not valid 6809439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly */ 6819439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly private int validateResponseCode(int code) { 6829439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 6839439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly if ((code >= ResponseCodes.OBEX_HTTP_OK) && (code <= ResponseCodes.OBEX_HTTP_PARTIAL)) { 6849439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly return code; 6859439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 6869439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly if ((code >= ResponseCodes.OBEX_HTTP_MULT_CHOICE) 6879439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly && (code <= ResponseCodes.OBEX_HTTP_USE_PROXY)) { 6889439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly return code; 6899439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 6909439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly if ((code >= ResponseCodes.OBEX_HTTP_BAD_REQUEST) 6919439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly && (code <= ResponseCodes.OBEX_HTTP_UNSUPPORTED_TYPE)) { 6929439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly return code; 6939439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 6949439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly if ((code >= ResponseCodes.OBEX_HTTP_INTERNAL_ERROR) 6959439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly && (code <= ResponseCodes.OBEX_HTTP_VERSION)) { 6969439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly return code; 6979439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 6989439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly if ((code >= ResponseCodes.OBEX_DATABASE_FULL) 6999439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly && (code <= ResponseCodes.OBEX_DATABASE_LOCKED)) { 7009439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly return code; 7019439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 7029439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR; 7039439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly } 7049439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly 7059439a7fe517b858bc5e5c654b459315e4722feb2Nick Pelly} 706