My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* BlueCove - Java library for Bluetooth
* Copyright (C) 2007-2009 Vlad Skarzhevskyy
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @author vlads
* @version $Id$
*/
package com.intel.bluetooth.obex;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.obex.ResponseCodes;

import com.intel.bluetooth.DebugLog;

class OBEXServerOperationPut extends OBEXServerOperation implements OBEXOperationReceive, OBEXOperationDelivery {

protected OBEXServerOperationPut(OBEXServerSessionImpl session, OBEXHeaderSetImpl receivedHeaders,
boolean finalPacket) throws IOException {
super(session, receivedHeaders);
this.inputStream = new OBEXOperationInputStream(this);
processIncommingData(receivedHeaders, finalPacket);
}

/*
* (non-Javadoc)
*
* @see javax.microedition.io.InputConnection#openInputStream()
*/
public InputStream openInputStream() throws IOException {
if (isClosed) {
throw new IOException("operation closed");
}
if (inputStreamOpened) {
throw new IOException("input stream already open");
}
DebugLog.debug("openInputStream");
inputStreamOpened = true;
return inputStream;
}

/*
* (non-Javadoc)
*
* @see javax.microedition.io.OutputConnection#openOutputStream()
*/
public OutputStream openOutputStream() throws IOException {
if (isClosed) {
throw new IOException("operation closed");
}
if (outputStream != null) {
throw new IOException("output stream already open");
}
outputStream = new OBEXOperationOutputStream(session.mtu, this);
return outputStream;
}

/*
* (non-Javadoc)
*
* @see javax.microedition.io.Connection#close()
*/
public void close() throws IOException {
DebugLog.debug("server close put operation");
if (inputStream != null) {
inputStream.close();
inputStream = null;
}
if (outputStream != null) {
outputStream.close();
outputStream = null;
}
super.close();
}

protected boolean readRequestPacket() throws IOException {
byte[] b = session.readPacket();
int opcode = b[0] & 0xFF;
boolean finalPacket = ((opcode & OBEXOperationCodes.FINAL_BIT) != 0);
if (finalPacket) {
DebugLog.debug("server operation got final packet");
finalPacketReceived = true;
}
switch (opcode) {
case OBEXOperationCodes.PUT_FINAL:
case OBEXOperationCodes.PUT:
OBEXHeaderSetImpl requestHeaders = OBEXHeaderSetImpl.readHeaders(b[0], b, 3);
if (!session.handleAuthenticationResponse(requestHeaders)) {
errorReceived = true;
session.writePacket(ResponseCodes.OBEX_HTTP_UNAUTHORIZED, null);
} else {
OBEXHeaderSetImpl.appendHeaders(this.receivedHeaders, requestHeaders);
processIncommingData(requestHeaders, finalPacket);
}
break;
case OBEXOperationCodes.ABORT:
processAbort();
break;
default:
errorReceived = true;
DebugLog.debug0x("server operation invalid request", OBEXUtils.toStringObexResponseCodes(opcode), opcode);
session.writePacket(ResponseCodes.OBEX_HTTP_BAD_REQUEST, null);
}
return finalPacket;
}

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.obex.OBEXOperationReceive#receiveData(com.intel.bluetooth.obex.OBEXOperationInputStream)
*/
public void receiveData(OBEXOperationInputStream is) throws IOException {
if (finalPacketReceived || errorReceived) {
is.appendData(null, true);
return;
}
DebugLog.debug("server operation reply continue");
session.writePacket(OBEXOperationCodes.OBEX_RESPONSE_CONTINUE, sendHeaders);
sendHeaders = null;
readRequestPacket();
}

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.obex.OBEXOperationDelivery#deliverPacket(boolean, byte[])
*/
public void deliverPacket(boolean finalPacket, byte[] buffer) throws IOException {
if (session.requestSent) {
// TODO Consider moving readRequestPacket() to the begging of the function
readRequestPacket();
if (session.requestSent) {
throw new IOException("Client not requesting data");
}
}
OBEXHeaderSetImpl dataHeaders = OBEXSessionBase.createOBEXHeaderSetImpl();
int opcode = OBEXOperationCodes.OBEX_RESPONSE_CONTINUE;
int dataHeaderID = OBEXHeaderSetImpl.OBEX_HDR_BODY;
if (finalPacket) {
// opcode = OBEXOperationCodes.OBEX_RESPONSE_SUCCESS;
dataHeaderID = OBEXHeaderSetImpl.OBEX_HDR_BODY_END;
}
dataHeaders.setHeader(dataHeaderID, buffer);
if (sendHeaders != null) {
OBEXHeaderSetImpl.appendHeaders(dataHeaders, sendHeaders);
sendHeaders = null;
}
session.writePacket(opcode, dataHeaders);
readRequestPacket();
}

private void processAbort() throws IOException {
isAborted = true;
session.writePacket(OBEXOperationCodes.OBEX_RESPONSE_SUCCESS, null);
close();
throw new IOException("Operation aborted by client");
}

}

Change log

r2915 by skarzhevskyy on Mar 13, 2009   Diff
Updated javadocs and Copyright
Go to: 
Project members, sign in to write a code review

Older revisions

r2643 by skarzhevskyy on Dec 22, 2008   Diff
Server to handle authentication
challenge in all Operations
r2640 by skarzhevskyy on Dec 22, 2008   Diff
change internal writePacket function
to accept headers
r2604 by skarzhevskyy on Dec 17, 2008   Diff
Work on OBEX TCK
All revisions of this file

File info

Size: 5525 bytes, 178 lines

File properties

svn:mime-type
text/plain
svn:eol-style
native
svn:keywords
Date Author Id Revision
Powered by Google Project Hosting