Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008-2009, Motorola, Inc. |
| 3 | * |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are met: |
| 8 | * |
| 9 | * - Redistributions of source code must retain the above copyright notice, |
| 10 | * this list of conditions and the following disclaimer. |
| 11 | * |
| 12 | * - Redistributions in binary form must reproduce the above copyright notice, |
| 13 | * this list of conditions and the following disclaimer in the documentation |
| 14 | * and/or other materials provided with the distribution. |
| 15 | * |
| 16 | * - Neither the name of the Motorola, Inc. nor the names of its contributors |
| 17 | * may be used to endorse or promote products derived from this software |
| 18 | * without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 | * POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | package javax.obex; |
| 34 | |
| 35 | import java.io.IOException; |
| 36 | import java.io.InputStream; |
| 37 | import java.io.DataInputStream; |
| 38 | import java.io.OutputStream; |
| 39 | import java.io.DataOutputStream; |
| 40 | import java.io.ByteArrayOutputStream; |
| 41 | |
| 42 | /** |
| 43 | * This class implements the Operation interface for server side connections. |
| 44 | * <P> |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 45 | * <STRONG>Request Codes</STRONG> There are four different request codes that |
| 46 | * are in this class. 0x02 is a PUT request that signals that the request is not |
| 47 | * complete and requires an additional OBEX packet. 0x82 is a PUT request that |
| 48 | * says that request is complete. In this case, the server can begin sending the |
| 49 | * response. The 0x03 is a GET request that signals that the request is not |
| 50 | * finished. When the server receives a 0x83, the client is signaling the server |
| 51 | * that it is done with its request. TODO: Extend the ClientOperation and reuse |
| 52 | * the methods defined TODO: in that class. |
Nick Pelly | 2e0da96 | 2009-06-30 16:28:54 -0700 | [diff] [blame] | 53 | * @hide |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 54 | */ |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 55 | public final class ServerOperation implements Operation, BaseStream { |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 56 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 57 | public boolean isAborted; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 58 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 59 | public HeaderSet requestHeader; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 60 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 61 | public HeaderSet replyHeader; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 62 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 63 | public boolean finalBitSet; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 64 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 65 | private InputStream mInput; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 66 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 67 | private ServerSession mParent; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 68 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 69 | private int mMaxPacketLength; |
Nick Pelly | 2e0da96 | 2009-06-30 16:28:54 -0700 | [diff] [blame] | 70 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 71 | private int mResponseSize; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 72 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 73 | private boolean mClosed; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 74 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 75 | private boolean mGetOperation; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 76 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 77 | private PrivateInputStream mPrivateInput; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 78 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 79 | private PrivateOutputStream mPrivateOutput; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 80 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 81 | private boolean mPrivateOutputOpen; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 82 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 83 | private String mExceptionString; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 84 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 85 | private ServerRequestHandler mListener; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 86 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 87 | private boolean mRequestFinished; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 88 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 89 | private boolean mHasBody; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 90 | |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 91 | private boolean mSendBodyHeader = true; |
| 92 | |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 93 | /** |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 94 | * Creates new ServerOperation |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 95 | * @param p the parent that created this object |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 96 | * @param in the input stream to read from |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 97 | * @param out the output stream to write to |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 98 | * @param request the initial request that was received from the client |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 99 | * @param maxSize the max packet size that the client will accept |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 100 | * @param listen the listener that is responding to the request |
Nick Pelly | 2e0da96 | 2009-06-30 16:28:54 -0700 | [diff] [blame] | 101 | * @throws IOException if an IO error occurs |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 102 | */ |
| 103 | public ServerOperation(ServerSession p, InputStream in, int request, int maxSize, |
| 104 | ServerRequestHandler listen) throws IOException { |
| 105 | |
| 106 | isAborted = false; |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 107 | mParent = p; |
| 108 | mInput = in; |
| 109 | mMaxPacketLength = maxSize; |
| 110 | mClosed = false; |
| 111 | requestHeader = new HeaderSet(); |
| 112 | replyHeader = new HeaderSet(); |
| 113 | mPrivateInput = new PrivateInputStream(this); |
| 114 | mResponseSize = 3; |
| 115 | mListener = listen; |
| 116 | mRequestFinished = false; |
| 117 | mPrivateOutputOpen = false; |
| 118 | mHasBody = false; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 119 | int bytesReceived; |
| 120 | |
| 121 | /* |
| 122 | * Determine if this is a PUT request |
| 123 | */ |
| 124 | if ((request == 0x02) || (request == 0x82)) { |
| 125 | /* |
| 126 | * It is a PUT request. |
| 127 | */ |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 128 | mGetOperation = false; |
Lixin Yue | 6520831 | 2009-11-11 00:51:22 +0800 | [diff] [blame] | 129 | |
| 130 | /* |
| 131 | * Determine if the final bit is set |
| 132 | */ |
| 133 | if ((request & 0x80) == 0) { |
| 134 | finalBitSet = false; |
| 135 | } else { |
| 136 | finalBitSet = true; |
| 137 | mRequestFinished = true; |
| 138 | } |
| 139 | } else if ((request == 0x03) || (request == 0x83)) { |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 140 | /* |
| 141 | * It is a GET request. |
| 142 | */ |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 143 | mGetOperation = true; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 144 | |
Lixin Yue | 6520831 | 2009-11-11 00:51:22 +0800 | [diff] [blame] | 145 | // For Get request, final bit set is decided by server side logic |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 146 | finalBitSet = false; |
Lixin Yue | 6520831 | 2009-11-11 00:51:22 +0800 | [diff] [blame] | 147 | |
| 148 | if (request == 0x83) { |
| 149 | mRequestFinished = true; |
| 150 | } |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 151 | } else { |
Lixin Yue | 6520831 | 2009-11-11 00:51:22 +0800 | [diff] [blame] | 152 | throw new IOException("ServerOperation can not handle such request"); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | int length = in.read(); |
| 156 | length = (length << 8) + in.read(); |
| 157 | |
| 158 | /* |
| 159 | * Determine if the packet length is larger than this device can receive |
| 160 | */ |
Nick Pelly | 2e0da96 | 2009-06-30 16:28:54 -0700 | [diff] [blame] | 161 | if (length > ObexHelper.MAX_PACKET_SIZE_INT) { |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 162 | mParent.sendResponse(ResponseCodes.OBEX_HTTP_REQ_TOO_LARGE, null); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 163 | throw new IOException("Packet received was too large"); |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * Determine if any headers were sent in the initial request |
| 168 | */ |
| 169 | if (length > 3) { |
| 170 | byte[] data = new byte[length - 3]; |
| 171 | bytesReceived = in.read(data); |
| 172 | |
| 173 | while (bytesReceived != data.length) { |
| 174 | bytesReceived += in.read(data, bytesReceived, data.length - bytesReceived); |
| 175 | } |
| 176 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 177 | byte[] body = ObexHelper.updateHeaderSet(requestHeader, data); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 178 | |
| 179 | if (body != null) { |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 180 | mHasBody = true; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Tao Liejun | e80534f | 2009-09-09 17:18:49 +0800 | [diff] [blame] | 183 | if (mListener.getConnectionId() != -1 && requestHeader.mConnectionID != null) { |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 184 | mListener.setConnectionId(ObexHelper.convertToLong(requestHeader.mConnectionID)); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 185 | } else { |
Tao Liejun | e80534f | 2009-09-09 17:18:49 +0800 | [diff] [blame] | 186 | mListener.setConnectionId(1); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 189 | if (requestHeader.mAuthResp != null) { |
| 190 | if (!mParent.handleAuthResp(requestHeader.mAuthResp)) { |
| 191 | mExceptionString = "Authentication Failed"; |
| 192 | mParent.sendResponse(ResponseCodes.OBEX_HTTP_UNAUTHORIZED, null); |
| 193 | mClosed = true; |
| 194 | requestHeader.mAuthResp = null; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 195 | return; |
| 196 | } |
| 197 | } |
| 198 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 199 | if (requestHeader.mAuthChall != null) { |
| 200 | mParent.handleAuthChall(requestHeader); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 201 | // send the authResp to the client |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 202 | replyHeader.mAuthResp = new byte[requestHeader.mAuthResp.length]; |
| 203 | System.arraycopy(requestHeader.mAuthResp, 0, replyHeader.mAuthResp, 0, |
| 204 | replyHeader.mAuthResp.length); |
| 205 | requestHeader.mAuthResp = null; |
| 206 | requestHeader.mAuthChall = null; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 207 | |
| 208 | } |
| 209 | |
| 210 | if (body != null) { |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 211 | mPrivateInput.writeBytes(body, 1); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 212 | } else { |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 213 | while ((!mGetOperation) && (!finalBitSet)) { |
| 214 | sendReply(ResponseCodes.OBEX_HTTP_CONTINUE); |
| 215 | if (mPrivateInput.available() > 0) { |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 216 | break; |
| 217 | } |
| 218 | } |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 219 | } |
| 220 | } |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 221 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 222 | while ((!mGetOperation) && (!finalBitSet) && (mPrivateInput.available() == 0)) { |
| 223 | sendReply(ResponseCodes.OBEX_HTTP_CONTINUE); |
| 224 | if (mPrivateInput.available() > 0) { |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 225 | break; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // wait for get request finished !!!! |
Lixin Yue | 6520831 | 2009-11-11 00:51:22 +0800 | [diff] [blame] | 230 | while (mGetOperation && !mRequestFinished) { |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 231 | sendReply(ResponseCodes.OBEX_HTTP_CONTINUE); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 232 | } |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 235 | public boolean isValidBody() { |
| 236 | return mHasBody; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /** |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 240 | * Determines if the operation should continue or should wait. If it should |
| 241 | * continue, this method will continue the operation. |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 242 | * @param sendEmpty if <code>true</code> then this will continue the |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 243 | * operation even if no headers will be sent; if <code>false</code> |
| 244 | * then this method will only continue the operation if there are |
| 245 | * headers to send |
| 246 | * @param inStream if<code>true</code> the stream is input stream, otherwise |
| 247 | * output stream |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 248 | * @return <code>true</code> if the operation was completed; |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 249 | * <code>false</code> if no operation took place |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 250 | */ |
| 251 | public synchronized boolean continueOperation(boolean sendEmpty, boolean inStream) |
| 252 | throws IOException { |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 253 | if (!mGetOperation) { |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 254 | if (!finalBitSet) { |
| 255 | if (sendEmpty) { |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 256 | sendReply(ResponseCodes.OBEX_HTTP_CONTINUE); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 257 | return true; |
| 258 | } else { |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 259 | if ((mResponseSize > 3) || (mPrivateOutput.size() > 0)) { |
| 260 | sendReply(ResponseCodes.OBEX_HTTP_CONTINUE); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 261 | return true; |
| 262 | } else { |
| 263 | return false; |
| 264 | } |
| 265 | } |
| 266 | } else { |
| 267 | return false; |
| 268 | } |
| 269 | } else { |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 270 | sendReply(ResponseCodes.OBEX_HTTP_CONTINUE); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 271 | return true; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /** |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 276 | * Sends a reply to the client. If the reply is a OBEX_HTTP_CONTINUE, it |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 277 | * will wait for a response from the client before ending. |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 278 | * @param type the response code to send back to the client |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 279 | * @return <code>true</code> if the final bit was not set on the reply; |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 280 | * <code>false</code> if no reply was received because the operation |
| 281 | * ended, an abort was received, or the final bit was set in the |
| 282 | * reply |
Nick Pelly | 2e0da96 | 2009-06-30 16:28:54 -0700 | [diff] [blame] | 283 | * @throws IOException if an IO error occurs |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 284 | */ |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 285 | public synchronized boolean sendReply(int type) throws IOException { |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 286 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 287 | int bytesReceived; |
| 288 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 289 | long id = mListener.getConnectionId(); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 290 | if (id == -1) { |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 291 | replyHeader.mConnectionID = null; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 292 | } else { |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 293 | replyHeader.mConnectionID = ObexHelper.convertToByteArray(id); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 296 | byte[] headerArray = ObexHelper.createHeader(replyHeader, true); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 297 | int bodyLength = -1; |
| 298 | int orginalBodyLength = -1; |
| 299 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 300 | if (mPrivateOutput != null) { |
| 301 | bodyLength = mPrivateOutput.size(); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 302 | orginalBodyLength = bodyLength; |
| 303 | } |
| 304 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 305 | if ((ObexHelper.BASE_PACKET_LENGTH + headerArray.length) > mMaxPacketLength) { |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 306 | |
| 307 | int end = 0; |
| 308 | int start = 0; |
| 309 | |
| 310 | while (end != headerArray.length) { |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 311 | end = ObexHelper.findHeaderEnd(headerArray, start, mMaxPacketLength |
| 312 | - ObexHelper.BASE_PACKET_LENGTH); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 313 | if (end == -1) { |
| 314 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 315 | mClosed = true; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 316 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 317 | if (mPrivateInput != null) { |
| 318 | mPrivateInput.close(); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 321 | if (mPrivateOutput != null) { |
| 322 | mPrivateOutput.close(); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 323 | } |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 324 | mParent.sendResponse(ResponseCodes.OBEX_HTTP_INTERNAL_ERROR, null); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 325 | throw new IOException("OBEX Packet exceeds max packet size"); |
| 326 | } |
| 327 | byte[] sendHeader = new byte[end - start]; |
| 328 | System.arraycopy(headerArray, start, sendHeader, 0, sendHeader.length); |
| 329 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 330 | mParent.sendResponse(type, sendHeader); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 331 | start = end; |
| 332 | } |
| 333 | |
| 334 | if (bodyLength > 0) { |
| 335 | return true; |
| 336 | } else { |
| 337 | return false; |
| 338 | } |
| 339 | |
| 340 | } else { |
| 341 | out.write(headerArray); |
| 342 | } |
| 343 | |
Lixin Yue | 6520831 | 2009-11-11 00:51:22 +0800 | [diff] [blame] | 344 | // For Get operation: if response code is OBEX_HTTP_OK, then this is the |
| 345 | // last packet; so set finalBitSet to true. |
| 346 | if (mGetOperation && type == ResponseCodes.OBEX_HTTP_OK) { |
| 347 | finalBitSet = true; |
| 348 | } |
| 349 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 350 | if ((finalBitSet) || (headerArray.length < (mMaxPacketLength - 20))) { |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 351 | if (bodyLength > 0) { |
| 352 | /* |
| 353 | * Determine if I can send the whole body or just part of |
| 354 | * the body. Remember that there is the 3 bytes for the |
| 355 | * response message and 3 bytes for the header ID and length |
| 356 | */ |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 357 | if (bodyLength > (mMaxPacketLength - headerArray.length - 6)) { |
| 358 | bodyLength = mMaxPacketLength - headerArray.length - 6; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 361 | byte[] body = mPrivateOutput.readBytes(bodyLength); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 362 | |
| 363 | /* |
| 364 | * Since this is a put request if the final bit is set or |
| 365 | * the output stream is closed we need to send the 0x49 |
| 366 | * (End of Body) otherwise, we need to send 0x48 (Body) |
| 367 | */ |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 368 | if ((finalBitSet) || (mPrivateOutput.isClosed())) { |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 369 | if(mSendBodyHeader == true) { |
| 370 | out.write(0x49); |
| 371 | bodyLength += 3; |
| 372 | out.write((byte)(bodyLength >> 8)); |
| 373 | out.write((byte)bodyLength); |
| 374 | out.write(body); |
| 375 | } |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 376 | } else { |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 377 | if(mSendBodyHeader == true) { |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 378 | out.write(0x48); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 379 | bodyLength += 3; |
| 380 | out.write((byte)(bodyLength >> 8)); |
| 381 | out.write((byte)bodyLength); |
| 382 | out.write(body); |
| 383 | } |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
| 389 | if ((finalBitSet) && (type == ResponseCodes.OBEX_HTTP_OK) && (orginalBodyLength <= 0)) { |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 390 | if(mSendBodyHeader == true) { |
| 391 | out.write(0x49); |
| 392 | orginalBodyLength = 3; |
| 393 | out.write((byte)(orginalBodyLength >> 8)); |
| 394 | out.write((byte)orginalBodyLength); |
| 395 | } |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 398 | mResponseSize = 3; |
| 399 | mParent.sendResponse(type, out.toByteArray()); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 400 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 401 | if (type == ResponseCodes.OBEX_HTTP_CONTINUE) { |
| 402 | int headerID = mInput.read(); |
| 403 | int length = mInput.read(); |
| 404 | length = (length << 8) + mInput.read(); |
| 405 | if ((headerID != ObexHelper.OBEX_OPCODE_PUT) |
| 406 | && (headerID != ObexHelper.OBEX_OPCODE_PUT_FINAL) |
| 407 | && (headerID != ObexHelper.OBEX_OPCODE_GET) |
| 408 | && (headerID != ObexHelper.OBEX_OPCODE_GET_FINAL)) { |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 409 | |
| 410 | if (length > 3) { |
Anders Petersson | 4507b17 | 2010-10-27 08:38:30 +0200 | [diff] [blame] | 411 | byte[] temp = new byte[length - 3]; |
| 412 | // First three bytes already read, compensating for this |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 413 | bytesReceived = mInput.read(temp); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 414 | |
Anders Petersson | 4507b17 | 2010-10-27 08:38:30 +0200 | [diff] [blame] | 415 | while (bytesReceived != temp.length) { |
| 416 | bytesReceived += mInput.read(temp, bytesReceived, |
| 417 | temp.length - bytesReceived); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * Determine if an ABORT was sent as the reply |
| 423 | */ |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 424 | if (headerID == ObexHelper.OBEX_OPCODE_ABORT) { |
| 425 | mParent.sendResponse(ResponseCodes.OBEX_HTTP_OK, null); |
| 426 | mClosed = true; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 427 | isAborted = true; |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 428 | mExceptionString = "Abort Received"; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 429 | throw new IOException("Abort Received"); |
| 430 | } else { |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 431 | mParent.sendResponse(ResponseCodes.OBEX_HTTP_BAD_REQUEST, null); |
| 432 | mClosed = true; |
| 433 | mExceptionString = "Bad Request Received"; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 434 | throw new IOException("Bad Request Received"); |
| 435 | } |
| 436 | } else { |
| 437 | |
Lixin Yue | 6520831 | 2009-11-11 00:51:22 +0800 | [diff] [blame] | 438 | if ((headerID == ObexHelper.OBEX_OPCODE_PUT_FINAL)) { |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 439 | finalBitSet = true; |
Lixin Yue | 6520831 | 2009-11-11 00:51:22 +0800 | [diff] [blame] | 440 | } else if (headerID == ObexHelper.OBEX_OPCODE_GET_FINAL) { |
| 441 | mRequestFinished = true; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /* |
| 445 | * Determine if the packet length is larger then this device can receive |
| 446 | */ |
Nick Pelly | 2e0da96 | 2009-06-30 16:28:54 -0700 | [diff] [blame] | 447 | if (length > ObexHelper.MAX_PACKET_SIZE_INT) { |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 448 | mParent.sendResponse(ResponseCodes.OBEX_HTTP_REQ_TOO_LARGE, null); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 449 | throw new IOException("Packet received was too large"); |
| 450 | } |
| 451 | |
| 452 | /* |
| 453 | * Determine if any headers were sent in the initial request |
| 454 | */ |
| 455 | if (length > 3) { |
| 456 | byte[] data = new byte[length - 3]; |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 457 | bytesReceived = mInput.read(data); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 458 | |
| 459 | while (bytesReceived != data.length) { |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 460 | bytesReceived += mInput.read(data, bytesReceived, data.length |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 461 | - bytesReceived); |
| 462 | } |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 463 | byte[] body = ObexHelper.updateHeaderSet(requestHeader, data); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 464 | if (body != null) { |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 465 | mHasBody = true; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 466 | } |
Tao Liejun | e80534f | 2009-09-09 17:18:49 +0800 | [diff] [blame] | 467 | if (mListener.getConnectionId() != -1 && requestHeader.mConnectionID != null) { |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 468 | mListener.setConnectionId(ObexHelper |
| 469 | .convertToLong(requestHeader.mConnectionID)); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 470 | } else { |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 471 | mListener.setConnectionId(1); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 474 | if (requestHeader.mAuthResp != null) { |
| 475 | if (!mParent.handleAuthResp(requestHeader.mAuthResp)) { |
| 476 | mExceptionString = "Authentication Failed"; |
| 477 | mParent.sendResponse(ResponseCodes.OBEX_HTTP_UNAUTHORIZED, null); |
| 478 | mClosed = true; |
| 479 | requestHeader.mAuthResp = null; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 480 | return false; |
| 481 | } |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 482 | requestHeader.mAuthResp = null; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 485 | if (requestHeader.mAuthChall != null) { |
| 486 | mParent.handleAuthChall(requestHeader); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 487 | // send the auhtResp to the client |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 488 | replyHeader.mAuthResp = new byte[requestHeader.mAuthResp.length]; |
| 489 | System.arraycopy(requestHeader.mAuthResp, 0, replyHeader.mAuthResp, 0, |
| 490 | replyHeader.mAuthResp.length); |
| 491 | requestHeader.mAuthResp = null; |
| 492 | requestHeader.mAuthChall = null; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | if (body != null) { |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 496 | mPrivateInput.writeBytes(body, 1); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | } |
| 500 | return true; |
| 501 | } else { |
| 502 | return false; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | /** |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 507 | * Sends an ABORT message to the server. By calling this method, the |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 508 | * corresponding input and output streams will be closed along with this |
| 509 | * object. |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 510 | * @throws IOException if the transaction has already ended or if an OBEX |
| 511 | * server called this method |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 512 | */ |
| 513 | public void abort() throws IOException { |
| 514 | throw new IOException("Called from a server"); |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * Returns the headers that have been received during the operation. |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 519 | * Modifying the object returned has no effect on the headers that are sent |
| 520 | * or retrieved. |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 521 | * @return the headers received during this <code>Operation</code> |
Nick Pelly | 2e0da96 | 2009-06-30 16:28:54 -0700 | [diff] [blame] | 522 | * @throws IOException if this <code>Operation</code> has been closed |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 523 | */ |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 524 | public HeaderSet getReceivedHeader() throws IOException { |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 525 | ensureOpen(); |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 526 | return requestHeader; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | /** |
| 530 | * Specifies the headers that should be sent in the next OBEX message that |
| 531 | * is sent. |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 532 | * @param headers the headers to send in the next message |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 533 | * @throws IOException if this <code>Operation</code> has been closed or the |
| 534 | * transaction has ended and no further messages will be exchanged |
Nick Pelly | 2e0da96 | 2009-06-30 16:28:54 -0700 | [diff] [blame] | 535 | * @throws IllegalArgumentException if <code>headers</code> was not created |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 536 | * by a call to <code>ServerRequestHandler.createHeaderSet()</code> |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 537 | */ |
| 538 | public void sendHeaders(HeaderSet headers) throws IOException { |
| 539 | ensureOpen(); |
| 540 | |
| 541 | if (headers == null) { |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 542 | throw new IOException("Headers may not be null"); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | int[] headerList = headers.getHeaderList(); |
| 546 | if (headerList != null) { |
| 547 | for (int i = 0; i < headerList.length; i++) { |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 548 | replyHeader.setHeader(headerList[i], headers.getHeader(headerList[i])); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | /** |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 555 | * Retrieves the response code retrieved from the server. Response codes are |
| 556 | * defined in the <code>ResponseCodes</code> interface. |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 557 | * @return the response code retrieved from the server |
Nick Pelly | 2e0da96 | 2009-06-30 16:28:54 -0700 | [diff] [blame] | 558 | * @throws IOException if an error occurred in the transport layer during |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 559 | * the transaction; if this method is called on a |
| 560 | * <code>HeaderSet</code> object created by calling |
| 561 | * <code>createHeaderSet</code> in a <code>ClientSession</code> |
| 562 | * object; if this is called from a server |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 563 | */ |
| 564 | public int getResponseCode() throws IOException { |
| 565 | throw new IOException("Called from a server"); |
| 566 | } |
| 567 | |
| 568 | /** |
| 569 | * Always returns <code>null</code> |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 570 | * @return <code>null</code> |
| 571 | */ |
| 572 | public String getEncoding() { |
| 573 | return null; |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * Returns the type of content that the resource connected to is providing. |
| 578 | * E.g. if the connection is via HTTP, then the value of the content-type |
| 579 | * header field is returned. |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 580 | * @return the content type of the resource that the URL references, or |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 581 | * <code>null</code> if not known |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 582 | */ |
| 583 | public String getType() { |
| 584 | try { |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 585 | return (String)requestHeader.getHeader(HeaderSet.TYPE); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 586 | } catch (IOException e) { |
| 587 | return null; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | /** |
| 592 | * Returns the length of the content which is being provided. E.g. if the |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 593 | * connection is via HTTP, then the value of the content-length header field |
| 594 | * is returned. |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 595 | * @return the content length of the resource that this connection's URL |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 596 | * references, or -1 if the content length is not known |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 597 | */ |
| 598 | public long getLength() { |
| 599 | try { |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 600 | Long temp = (Long)requestHeader.getHeader(HeaderSet.LENGTH); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 601 | |
| 602 | if (temp == null) { |
| 603 | return -1; |
| 604 | } else { |
| 605 | return temp.longValue(); |
| 606 | } |
| 607 | } catch (IOException e) { |
| 608 | return -1; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | public int getMaxPacketSize() { |
Lixin Yue | 6520831 | 2009-11-11 00:51:22 +0800 | [diff] [blame] | 613 | return mMaxPacketLength - 6 - getHeaderLength(); |
| 614 | } |
| 615 | |
| 616 | public int getHeaderLength() { |
| 617 | long id = mListener.getConnectionId(); |
| 618 | if (id == -1) { |
| 619 | replyHeader.mConnectionID = null; |
| 620 | } else { |
| 621 | replyHeader.mConnectionID = ObexHelper.convertToByteArray(id); |
| 622 | } |
| 623 | |
| 624 | byte[] headerArray = ObexHelper.createHeader(replyHeader, false); |
| 625 | |
| 626 | return headerArray.length; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | /** |
| 630 | * Open and return an input stream for a connection. |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 631 | * @return an input stream |
Nick Pelly | 2e0da96 | 2009-06-30 16:28:54 -0700 | [diff] [blame] | 632 | * @throws IOException if an I/O error occurs |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 633 | */ |
| 634 | public InputStream openInputStream() throws IOException { |
| 635 | ensureOpen(); |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 636 | return mPrivateInput; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | /** |
| 640 | * Open and return a data input stream for a connection. |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 641 | * @return an input stream |
Nick Pelly | 2e0da96 | 2009-06-30 16:28:54 -0700 | [diff] [blame] | 642 | * @throws IOException if an I/O error occurs |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 643 | */ |
| 644 | public DataInputStream openDataInputStream() throws IOException { |
| 645 | return new DataInputStream(openInputStream()); |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * Open and return an output stream for a connection. |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 650 | * @return an output stream |
Nick Pelly | 2e0da96 | 2009-06-30 16:28:54 -0700 | [diff] [blame] | 651 | * @throws IOException if an I/O error occurs |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 652 | */ |
| 653 | public OutputStream openOutputStream() throws IOException { |
| 654 | ensureOpen(); |
| 655 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 656 | if (mPrivateOutputOpen) { |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 657 | throw new IOException("no more input streams available, stream already opened"); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 658 | } |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 659 | |
| 660 | if (!mRequestFinished) { |
| 661 | throw new IOException("no output streams available ,request not finished"); |
| 662 | } |
| 663 | |
| 664 | if (mPrivateOutput == null) { |
Lixin Yue | 6520831 | 2009-11-11 00:51:22 +0800 | [diff] [blame] | 665 | mPrivateOutput = new PrivateOutputStream(this, getMaxPacketSize()); |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 666 | } |
| 667 | mPrivateOutputOpen = true; |
| 668 | return mPrivateOutput; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | /** |
| 672 | * Open and return a data output stream for a connection. |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 673 | * @return an output stream |
Nick Pelly | 2e0da96 | 2009-06-30 16:28:54 -0700 | [diff] [blame] | 674 | * @throws IOException if an I/O error occurs |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 675 | */ |
| 676 | public DataOutputStream openDataOutputStream() throws IOException { |
| 677 | return new DataOutputStream(openOutputStream()); |
| 678 | } |
| 679 | |
| 680 | /** |
| 681 | * Closes the connection and ends the transaction |
Nick Pelly | 2e0da96 | 2009-06-30 16:28:54 -0700 | [diff] [blame] | 682 | * @throws IOException if the operation has already ended or is closed |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 683 | */ |
| 684 | public void close() throws IOException { |
| 685 | ensureOpen(); |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 686 | mClosed = true; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /** |
| 690 | * Verifies that the connection is open and no exceptions should be thrown. |
Nick Pelly | 2e0da96 | 2009-06-30 16:28:54 -0700 | [diff] [blame] | 691 | * @throws IOException if an exception needs to be thrown |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 692 | */ |
| 693 | public void ensureOpen() throws IOException { |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 694 | if (mExceptionString != null) { |
| 695 | throw new IOException(mExceptionString); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 696 | } |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 697 | if (mClosed) { |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 698 | throw new IOException("Operation has already ended"); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | /** |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 703 | * Verifies that additional information may be sent. In other words, the |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 704 | * operation is not done. |
| 705 | * <P> |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 706 | * Included to implement the BaseStream interface only. It does not do |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 707 | * anything on the server side since the operation of the Operation object |
| 708 | * is not done until after the handler returns from its method. |
Nick Pelly | 2e0da96 | 2009-06-30 16:28:54 -0700 | [diff] [blame] | 709 | * @throws IOException if the operation is completed |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 710 | */ |
| 711 | public void ensureNotDone() throws IOException { |
| 712 | } |
| 713 | |
| 714 | /** |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 715 | * Called when the output or input stream is closed. It does not do anything |
| 716 | * on the server side since the operation of the Operation object is not |
| 717 | * done until after the handler returns from its method. |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 718 | * @param inStream <code>true</code> if the input stream is closed; |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 719 | * <code>false</code> if the output stream is closed |
Nick Pelly | 2e0da96 | 2009-06-30 16:28:54 -0700 | [diff] [blame] | 720 | * @throws IOException if an IO error occurs |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 721 | */ |
| 722 | public void streamClosed(boolean inStream) throws IOException { |
| 723 | |
| 724 | } |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 725 | |
| 726 | public void noBodyHeader(){ |
| 727 | mSendBodyHeader = false; |
| 728 | } |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 729 | } |