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.DataInputStream; |
| 36 | import java.io.DataOutputStream; |
| 37 | import java.io.IOException; |
| 38 | import java.io.InputStream; |
| 39 | import java.io.OutputStream; |
| 40 | |
| 41 | /** |
| 42 | * The <code>Operation</code> interface provides ways to manipulate a single |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 43 | * OBEX PUT or GET operation. The implementation of this interface sends OBEX |
| 44 | * packets as they are built. If during the operation the peer in the operation |
| 45 | * ends the operation, an <code>IOException</code> is thrown on the next read |
| 46 | * from the input stream, write to the output stream, or call to |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 47 | * <code>sendHeaders()</code>. |
| 48 | * <P> |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 49 | * <STRONG>Definition of methods inherited from <code>ContentConnection</code> |
| 50 | * </STRONG> |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 51 | * <P> |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 52 | * <code>getEncoding()</code> will always return <code>null</code>. <BR> |
| 53 | * <code>getLength()</code> will return the length specified by the OBEX Length |
| 54 | * header or -1 if the OBEX Length header was not included. <BR> |
| 55 | * <code>getType()</code> will return the value specified in the OBEX Type |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 56 | * header or <code>null</code> if the OBEX Type header was not included.<BR> |
| 57 | * <P> |
| 58 | * <STRONG>How Headers are Handled</STRONG> |
| 59 | * <P> |
| 60 | * As headers are received, they may be retrieved through the |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 61 | * <code>getReceivedHeaders()</code> method. If new headers are set during the |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 62 | * operation, the new headers will be sent during the next packet exchange. |
| 63 | * <P> |
| 64 | * <STRONG>PUT example</STRONG> |
| 65 | * <P> |
| 66 | * <PRE> |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 67 | * void putObjectViaOBEX(ClientSession conn, HeaderSet head, byte[] obj) throws IOException { |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 68 | * // Include the length header |
| 69 | * head.setHeader(head.LENGTH, new Long(obj.length)); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 70 | * // Initiate the PUT request |
| 71 | * Operation op = conn.put(head); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 72 | * // Open the output stream to put the object to it |
| 73 | * DataOutputStream out = op.openDataOutputStream(); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 74 | * // Send the object to the server |
| 75 | * out.write(obj); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 76 | * // End the transaction |
| 77 | * out.close(); |
| 78 | * op.close(); |
| 79 | * } |
| 80 | * </PRE> |
| 81 | * <P> |
| 82 | * <STRONG>GET example</STRONG> |
| 83 | * <P> |
| 84 | * <PRE> |
| 85 | * byte[] getObjectViaOBEX(ClientSession conn, HeaderSet head) throws IOException { |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 86 | * // Send the initial GET request to the server |
| 87 | * Operation op = conn.get(head); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 88 | * // Retrieve the length of the object being sent back |
| 89 | * int length = op.getLength(); |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 90 | * // Create space for the object |
| 91 | * byte[] obj = new byte[length]; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 92 | * // Get the object from the input stream |
| 93 | * DataInputStream in = trans.openDataInputStream(); |
| 94 | * in.read(obj); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 95 | * // End the transaction |
| 96 | * in.close(); |
| 97 | * op.close(); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 98 | * return obj; |
| 99 | * } |
| 100 | * </PRE> |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 101 | * |
| 102 | * <H3>Client PUT Operation Flow</H3> For PUT operations, a call to |
| 103 | * <code>close()</code> the <code>OutputStream</code> returned from |
| 104 | * <code>openOutputStream()</code> or <code>openDataOutputStream()</code> will |
| 105 | * signal that the request is done. (In OBEX terms, the End-Of-Body header |
| 106 | * should be sent and the final bit in the request will be set.) At this point, |
| 107 | * the reply from the server may begin to be processed. A call to |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 108 | * <code>getResponseCode()</code> will do an implicit close on the |
| 109 | * <code>OutputStream</code> and therefore signal that the request is done. |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 110 | * <H3>Client GET Operation Flow</H3> For GET operation, a call to |
| 111 | * <code>openInputStream()</code> or <code>openDataInputStream()</code> will |
| 112 | * signal that the request is done. (In OBEX terms, the final bit in the request |
| 113 | * will be set.) A call to <code>getResponseCode()</code> will cause an implicit |
| 114 | * close on the <code>InputStream</code>. No further data may be read at this |
| 115 | * point. |
Nick Pelly | 2e0da96 | 2009-06-30 16:28:54 -0700 | [diff] [blame] | 116 | * @hide |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 117 | */ |
| 118 | public interface Operation { |
| 119 | |
| 120 | /** |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 121 | * Sends an ABORT message to the server. By calling this method, the |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 122 | * corresponding input and output streams will be closed along with this |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 123 | * object. No headers are sent in the abort request. This will end the |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 124 | * operation since <code>close()</code> will be called by this method. |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 125 | * @throws IOException if the transaction has already ended or if an OBEX |
| 126 | * server calls this method |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 127 | */ |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 128 | void abort() throws IOException; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * Returns the headers that have been received during the operation. |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 132 | * Modifying the object returned has no effect on the headers that are sent |
| 133 | * or retrieved. |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 134 | * @return the headers received during this <code>Operation</code> |
Nick Pelly | 2e0da96 | 2009-06-30 16:28:54 -0700 | [diff] [blame] | 135 | * @throws IOException if this <code>Operation</code> has been closed |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 136 | */ |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 137 | HeaderSet getReceivedHeader() throws IOException; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 138 | |
| 139 | /** |
| 140 | * Specifies the headers that should be sent in the next OBEX message that |
| 141 | * is sent. |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 142 | * @param headers the headers to send in the next message |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 143 | * @throws IOException if this <code>Operation</code> has been closed or the |
| 144 | * transaction has ended and no further messages will be exchanged |
Nick Pelly | 2e0da96 | 2009-06-30 16:28:54 -0700 | [diff] [blame] | 145 | * @throws IllegalArgumentException if <code>headers</code> was not created |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 146 | * by a call to <code>ServerRequestHandler.createHeaderSet()</code> |
| 147 | * or <code>ClientSession.createHeaderSet()</code> |
Nick Pelly | 2e0da96 | 2009-06-30 16:28:54 -0700 | [diff] [blame] | 148 | * @throws NullPointerException if <code>headers</code> if <code>null</code> |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 149 | */ |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 150 | void sendHeaders(HeaderSet headers) throws IOException; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 151 | |
| 152 | /** |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 153 | * Returns the response code received from the server. Response codes are |
| 154 | * defined in the <code>ResponseCodes</code> class. |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 155 | * @see ResponseCodes |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 156 | * @return the response code retrieved from the server |
Nick Pelly | 2e0da96 | 2009-06-30 16:28:54 -0700 | [diff] [blame] | 157 | * @throws IOException if an error occurred in the transport layer during |
Tao Liejun | 05ff98bb | 2009-07-13 15:57:11 -0700 | [diff] [blame] | 158 | * the transaction; if this object was created by an OBEX server |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 159 | */ |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 160 | int getResponseCode() throws IOException; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 161 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 162 | String getEncoding(); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 163 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 164 | long getLength(); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 165 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 166 | String getType(); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 167 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 168 | InputStream openInputStream() throws IOException; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 169 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 170 | DataInputStream openDataInputStream() throws IOException; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 171 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 172 | OutputStream openOutputStream() throws IOException; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 173 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 174 | DataOutputStream openDataOutputStream() throws IOException; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 175 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 176 | void close() throws IOException; |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 177 | |
Tao Liejun | 3998bf0 | 2009-07-02 19:29:09 +0800 | [diff] [blame] | 178 | int getMaxPacketSize(); |
Nick Pelly | 9439a7f | 2009-06-30 12:04:36 -0700 | [diff] [blame] | 179 | } |