1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package com.intel.bluetooth.obex;
26
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.OutputStream;
30
31 import javax.obex.ResponseCodes;
32
33 import com.intel.bluetooth.DebugLog;
34
35 class OBEXServerOperationPut extends OBEXServerOperation implements OBEXOperationReceive, OBEXOperationDelivery {
36
37 protected OBEXServerOperationPut(OBEXServerSessionImpl session, OBEXHeaderSetImpl receivedHeaders,
38 boolean finalPacket) throws IOException {
39 super(session, receivedHeaders);
40 this.inputStream = new OBEXOperationInputStream(this);
41 processIncommingData(receivedHeaders, finalPacket);
42 }
43
44
45
46
47
48
49 public InputStream openInputStream() throws IOException {
50 if (isClosed) {
51 throw new IOException("operation closed");
52 }
53 if (inputStreamOpened) {
54 throw new IOException("input stream already open");
55 }
56 DebugLog.debug("openInputStream");
57 inputStreamOpened = true;
58 return inputStream;
59 }
60
61
62
63
64
65
66 public OutputStream openOutputStream() throws IOException {
67 if (isClosed) {
68 throw new IOException("operation closed");
69 }
70 if (outputStream != null) {
71 throw new IOException("output stream already open");
72 }
73 outputStream = new OBEXOperationOutputStream(session.mtu, this);
74 return outputStream;
75 }
76
77
78
79
80
81
82 public void close() throws IOException {
83 DebugLog.debug("server close put operation");
84 if (inputStream != null) {
85 inputStream.close();
86 inputStream = null;
87 }
88 if (outputStream != null) {
89 outputStream.close();
90 outputStream = null;
91 }
92 super.close();
93 }
94
95 protected boolean readRequestPacket() throws IOException {
96 byte[] b = session.readPacket();
97 int opcode = b[0] & 0xFF;
98 boolean finalPacket = ((opcode & OBEXOperationCodes.FINAL_BIT) != 0);
99 if (finalPacket) {
100 DebugLog.debug("server operation got final packet");
101 finalPacketReceived = true;
102 }
103 switch (opcode) {
104 case OBEXOperationCodes.PUT_FINAL:
105 case OBEXOperationCodes.PUT:
106 OBEXHeaderSetImpl requestHeaders = OBEXHeaderSetImpl.readHeaders(b[0], b, 3);
107 if (!session.handleAuthenticationResponse(requestHeaders)) {
108 errorReceived = true;
109 session.writePacket(ResponseCodes.OBEX_HTTP_UNAUTHORIZED, null);
110 } else {
111 OBEXHeaderSetImpl.appendHeaders(this.receivedHeaders, requestHeaders);
112 processIncommingData(requestHeaders, finalPacket);
113 }
114 break;
115 case OBEXOperationCodes.ABORT:
116 processAbort();
117 break;
118 default:
119 errorReceived = true;
120 DebugLog.debug0x("server operation invalid request", OBEXUtils.toStringObexResponseCodes(opcode), opcode);
121 session.writePacket(ResponseCodes.OBEX_HTTP_BAD_REQUEST, null);
122 }
123 return finalPacket;
124 }
125
126
127
128
129
130
131 public void receiveData(OBEXOperationInputStream is) throws IOException {
132 if (finalPacketReceived || errorReceived) {
133 is.appendData(null, true);
134 return;
135 }
136 DebugLog.debug("server operation reply continue");
137 session.writePacket(OBEXOperationCodes.OBEX_RESPONSE_CONTINUE, sendHeaders);
138 sendHeaders = null;
139 readRequestPacket();
140 }
141
142
143
144
145
146
147 public void deliverPacket(boolean finalPacket, byte[] buffer) throws IOException {
148 if (session.requestSent) {
149
150 readRequestPacket();
151 if (session.requestSent) {
152 throw new IOException("Client not requesting data");
153 }
154 }
155 OBEXHeaderSetImpl dataHeaders = OBEXSessionBase.createOBEXHeaderSetImpl();
156 int opcode = OBEXOperationCodes.OBEX_RESPONSE_CONTINUE;
157 int dataHeaderID = OBEXHeaderSetImpl.OBEX_HDR_BODY;
158 if (finalPacket) {
159
160 dataHeaderID = OBEXHeaderSetImpl.OBEX_HDR_BODY_END;
161 }
162 dataHeaders.setHeader(dataHeaderID, buffer);
163 if (sendHeaders != null) {
164 OBEXHeaderSetImpl.appendHeaders(dataHeaders, sendHeaders);
165 sendHeaders = null;
166 }
167 session.writePacket(opcode, dataHeaders);
168 readRequestPacket();
169 }
170
171 private void processAbort() throws IOException {
172 isAborted = true;
173 session.writePacket(OBEXOperationCodes.OBEX_RESPONSE_SUCCESS, null);
174 close();
175 throw new IOException("Operation aborted by client");
176 }
177
178 }