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