blob: 5b4d5ace3669af8f235d0c9c9495f5fab46770ac [file] [log] [blame]
Nick Pelly9439a7f2009-06-30 12:04:36 -07001/*
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
33package javax.obex;
34
35import java.io.DataInputStream;
36import java.io.DataOutputStream;
37import java.io.IOException;
38import java.io.InputStream;
39import java.io.OutputStream;
40
41/**
42 * The <code>Operation</code> interface provides ways to manipulate a single
Tao Liejun05ff98bb2009-07-13 15:57:11 -070043 * 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 Pelly9439a7f2009-06-30 12:04:36 -070047 * <code>sendHeaders()</code>.
48 * <P>
Tao Liejun05ff98bb2009-07-13 15:57:11 -070049 * <STRONG>Definition of methods inherited from <code>ContentConnection</code>
50 * </STRONG>
Nick Pelly9439a7f2009-06-30 12:04:36 -070051 * <P>
Tao Liejun05ff98bb2009-07-13 15:57:11 -070052 * <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 Pelly9439a7f2009-06-30 12:04:36 -070056 * 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 Liejun05ff98bb2009-07-13 15:57:11 -070061 * <code>getReceivedHeaders()</code> method. If new headers are set during the
Nick Pelly9439a7f2009-06-30 12:04:36 -070062 * operation, the new headers will be sent during the next packet exchange.
63 * <P>
64 * <STRONG>PUT example</STRONG>
65 * <P>
66 * <PRE>
Tao Liejun05ff98bb2009-07-13 15:57:11 -070067 * void putObjectViaOBEX(ClientSession conn, HeaderSet head, byte[] obj) throws IOException {
Nick Pelly9439a7f2009-06-30 12:04:36 -070068 * // Include the length header
69 * head.setHeader(head.LENGTH, new Long(obj.length));
Nick Pelly9439a7f2009-06-30 12:04:36 -070070 * // Initiate the PUT request
71 * Operation op = conn.put(head);
Nick Pelly9439a7f2009-06-30 12:04:36 -070072 * // Open the output stream to put the object to it
73 * DataOutputStream out = op.openDataOutputStream();
Nick Pelly9439a7f2009-06-30 12:04:36 -070074 * // Send the object to the server
75 * out.write(obj);
Nick Pelly9439a7f2009-06-30 12:04:36 -070076 * // 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 Pelly9439a7f2009-06-30 12:04:36 -070086 * // Send the initial GET request to the server
87 * Operation op = conn.get(head);
Nick Pelly9439a7f2009-06-30 12:04:36 -070088 * // Retrieve the length of the object being sent back
89 * int length = op.getLength();
Tao Liejun05ff98bb2009-07-13 15:57:11 -070090 * // Create space for the object
91 * byte[] obj = new byte[length];
Nick Pelly9439a7f2009-06-30 12:04:36 -070092 * // Get the object from the input stream
93 * DataInputStream in = trans.openDataInputStream();
94 * in.read(obj);
Nick Pelly9439a7f2009-06-30 12:04:36 -070095 * // End the transaction
96 * in.close();
97 * op.close();
Nick Pelly9439a7f2009-06-30 12:04:36 -070098 * return obj;
99 * }
100 * </PRE>
Tao Liejun05ff98bb2009-07-13 15:57:11 -0700101 *
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 Pelly9439a7f2009-06-30 12:04:36 -0700108 * <code>getResponseCode()</code> will do an implicit close on the
109 * <code>OutputStream</code> and therefore signal that the request is done.
Tao Liejun05ff98bb2009-07-13 15:57:11 -0700110 * <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 Pelly2e0da962009-06-30 16:28:54 -0700116 * @hide
Nick Pelly9439a7f2009-06-30 12:04:36 -0700117 */
118public interface Operation {
119
120 /**
Tao Liejun05ff98bb2009-07-13 15:57:11 -0700121 * Sends an ABORT message to the server. By calling this method, the
Nick Pelly9439a7f2009-06-30 12:04:36 -0700122 * corresponding input and output streams will be closed along with this
Tao Liejun05ff98bb2009-07-13 15:57:11 -0700123 * object. No headers are sent in the abort request. This will end the
Nick Pelly9439a7f2009-06-30 12:04:36 -0700124 * operation since <code>close()</code> will be called by this method.
Tao Liejun05ff98bb2009-07-13 15:57:11 -0700125 * @throws IOException if the transaction has already ended or if an OBEX
126 * server calls this method
Nick Pelly9439a7f2009-06-30 12:04:36 -0700127 */
Tao Liejun3998bf02009-07-02 19:29:09 +0800128 void abort() throws IOException;
Nick Pelly9439a7f2009-06-30 12:04:36 -0700129
130 /**
131 * Returns the headers that have been received during the operation.
Tao Liejun05ff98bb2009-07-13 15:57:11 -0700132 * Modifying the object returned has no effect on the headers that are sent
133 * or retrieved.
Nick Pelly9439a7f2009-06-30 12:04:36 -0700134 * @return the headers received during this <code>Operation</code>
Nick Pelly2e0da962009-06-30 16:28:54 -0700135 * @throws IOException if this <code>Operation</code> has been closed
Nick Pelly9439a7f2009-06-30 12:04:36 -0700136 */
Tao Liejun3998bf02009-07-02 19:29:09 +0800137 HeaderSet getReceivedHeader() throws IOException;
Nick Pelly9439a7f2009-06-30 12:04:36 -0700138
139 /**
140 * Specifies the headers that should be sent in the next OBEX message that
141 * is sent.
Nick Pelly9439a7f2009-06-30 12:04:36 -0700142 * @param headers the headers to send in the next message
Tao Liejun05ff98bb2009-07-13 15:57:11 -0700143 * @throws IOException if this <code>Operation</code> has been closed or the
144 * transaction has ended and no further messages will be exchanged
Nick Pelly2e0da962009-06-30 16:28:54 -0700145 * @throws IllegalArgumentException if <code>headers</code> was not created
Tao Liejun05ff98bb2009-07-13 15:57:11 -0700146 * by a call to <code>ServerRequestHandler.createHeaderSet()</code>
147 * or <code>ClientSession.createHeaderSet()</code>
Nick Pelly2e0da962009-06-30 16:28:54 -0700148 * @throws NullPointerException if <code>headers</code> if <code>null</code>
Nick Pelly9439a7f2009-06-30 12:04:36 -0700149 */
Tao Liejun3998bf02009-07-02 19:29:09 +0800150 void sendHeaders(HeaderSet headers) throws IOException;
Nick Pelly9439a7f2009-06-30 12:04:36 -0700151
152 /**
Tao Liejun05ff98bb2009-07-13 15:57:11 -0700153 * Returns the response code received from the server. Response codes are
154 * defined in the <code>ResponseCodes</code> class.
Nick Pelly9439a7f2009-06-30 12:04:36 -0700155 * @see ResponseCodes
Nick Pelly9439a7f2009-06-30 12:04:36 -0700156 * @return the response code retrieved from the server
Nick Pelly2e0da962009-06-30 16:28:54 -0700157 * @throws IOException if an error occurred in the transport layer during
Tao Liejun05ff98bb2009-07-13 15:57:11 -0700158 * the transaction; if this object was created by an OBEX server
Nick Pelly9439a7f2009-06-30 12:04:36 -0700159 */
Tao Liejun3998bf02009-07-02 19:29:09 +0800160 int getResponseCode() throws IOException;
Nick Pelly9439a7f2009-06-30 12:04:36 -0700161
Tao Liejun3998bf02009-07-02 19:29:09 +0800162 String getEncoding();
Nick Pelly9439a7f2009-06-30 12:04:36 -0700163
Tao Liejun3998bf02009-07-02 19:29:09 +0800164 long getLength();
Nick Pelly9439a7f2009-06-30 12:04:36 -0700165
Lixin Yue65208312009-11-11 00:51:22 +0800166 int getHeaderLength();
167
Tao Liejun3998bf02009-07-02 19:29:09 +0800168 String getType();
Nick Pelly9439a7f2009-06-30 12:04:36 -0700169
Tao Liejun3998bf02009-07-02 19:29:09 +0800170 InputStream openInputStream() throws IOException;
Nick Pelly9439a7f2009-06-30 12:04:36 -0700171
Tao Liejun3998bf02009-07-02 19:29:09 +0800172 DataInputStream openDataInputStream() throws IOException;
Nick Pelly9439a7f2009-06-30 12:04:36 -0700173
Tao Liejun3998bf02009-07-02 19:29:09 +0800174 OutputStream openOutputStream() throws IOException;
Nick Pelly9439a7f2009-06-30 12:04:36 -0700175
Tao Liejun3998bf02009-07-02 19:29:09 +0800176 DataOutputStream openDataOutputStream() throws IOException;
Nick Pelly9439a7f2009-06-30 12:04:36 -0700177
Tao Liejun3998bf02009-07-02 19:29:09 +0800178 void close() throws IOException;
Nick Pelly9439a7f2009-06-30 12:04:36 -0700179
Tao Liejun3998bf02009-07-02 19:29:09 +0800180 int getMaxPacketSize();
Matthew Xiefe3807a2013-07-18 17:31:50 -0700181
182 public void noBodyHeader();
Nick Pelly9439a7f2009-06-30 12:04:36 -0700183}