author | Eugene Ostroukhov <eugeneo@symbian.org> |
Wed, 28 Jul 2010 09:27:51 -0700 | |
changeset 455 | 5da55957c779 |
parent 52 | f577ea64429e |
permissions | -rw-r--r-- |
2 | 1 |
// Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 |
// Use of this source code is governed by a BSD-style license that can be |
|
3 |
// found in the LICENSE file. |
|
4 |
||
5 |
package org.chromium.sdk.internal.transport; |
|
6 |
||
7 |
import java.io.IOException; |
|
8 |
import java.io.StringWriter; |
|
9 |
import java.io.Writer; |
|
10 |
import java.util.HashMap; |
|
11 |
import java.util.Map; |
|
12 |
import java.util.logging.Level; |
|
13 |
import java.util.logging.Logger; |
|
14 |
||
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
15 |
import org.chromium.sdk.LineReader; |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
16 |
|
2 | 17 |
/** |
18 |
* A transport message encapsulating the data sent/received over the wire |
|
19 |
* (protocol headers and content). This class can serialize and deserialize |
|
20 |
* itself into a BufferedWriter according to the ChromeDevTools Protocol |
|
21 |
* specification. |
|
22 |
*/ |
|
23 |
public class Message { |
|
24 |
||
25 |
/** |
|
26 |
* This exception is thrown during Message deserialization whenever the input |
|
27 |
* is malformed. |
|
28 |
*/ |
|
29 |
public static class MalformedMessageException extends Exception { |
|
30 |
||
31 |
private static final long serialVersionUID = 1L; |
|
32 |
||
33 |
public MalformedMessageException() { |
|
34 |
super(); |
|
35 |
} |
|
36 |
||
37 |
public MalformedMessageException(String message) { |
|
38 |
super(message); |
|
39 |
} |
|
40 |
||
41 |
public MalformedMessageException(Throwable cause) { |
|
42 |
super(cause); |
|
43 |
} |
|
44 |
||
45 |
public MalformedMessageException(String message, Throwable cause) { |
|
46 |
super(message, cause); |
|
47 |
} |
|
48 |
||
49 |
} |
|
50 |
||
51 |
/** |
|
52 |
* Known ChromeDevTools Protocol headers (ToolHandler implementations |
|
53 |
* can add their own headers.) |
|
54 |
*/ |
|
55 |
public enum Header { |
|
56 |
CONTENT_LENGTH("Content-Length"), |
|
57 |
TOOL("Tool"), |
|
58 |
DESTINATION("Destination"), ; |
|
59 |
||
60 |
public final String name; |
|
61 |
||
62 |
Header(String value) { |
|
63 |
this.name = value; |
|
64 |
} |
|
65 |
} |
|
66 |
||
67 |
/** |
|
68 |
* The class logger. |
|
69 |
*/ |
|
70 |
private static final Logger LOGGER = Logger.getLogger(Message.class.getName()); |
|
71 |
||
72 |
/** |
|
73 |
* The end of protocol header line. |
|
74 |
*/ |
|
75 |
private static final String HEADER_TERMINATOR = "\r\n"; |
|
76 |
||
77 |
private final HashMap<String, String> headers; |
|
78 |
||
79 |
private final String content; |
|
80 |
||
81 |
public Message(Map<String, String> headers, String content) { |
|
82 |
this.headers = new HashMap<String, String>(headers); |
|
83 |
this.content = content; |
|
84 |
this.headers.put(Header.CONTENT_LENGTH.name, String.valueOf(content == null |
|
85 |
? 0 |
|
86 |
: content.length())); |
|
87 |
} |
|
88 |
||
89 |
/** |
|
90 |
* Sends a message through the specified writer. |
|
91 |
* |
|
92 |
* @param writer to send the message through |
|
93 |
* @throws IOException |
|
94 |
*/ |
|
95 |
public void sendThrough(Writer writer) throws IOException { |
|
96 |
String content = maskNull(this.content); |
|
97 |
for (Map.Entry<String, String> entry : this.headers.entrySet()) { |
|
98 |
writeNonEmptyHeader(writer, entry.getKey(), entry.getValue()); |
|
99 |
} |
|
100 |
writer.write(HEADER_TERMINATOR); |
|
101 |
if (content.length() > 0) { |
|
102 |
writer.write(content); |
|
103 |
} |
|
104 |
writer.flush(); |
|
105 |
} |
|
106 |
||
107 |
/** |
|
108 |
* Reads a message from the specified reader. |
|
109 |
* |
|
110 |
* @param reader to read message from |
|
111 |
* @return a new message, or {@code null} if input is invalid (end-of-stream |
|
112 |
* or bad message format) |
|
113 |
* @throws IOException |
|
114 |
* @throws MalformedMessageException if the input does not represent a valid |
|
115 |
* message |
|
116 |
*/ |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
117 |
public static Message fromBufferedReader(LineReader reader) |
2 | 118 |
throws IOException, MalformedMessageException { |
119 |
Map<String, String> headers = new HashMap<String, String>(); |
|
120 |
synchronized (reader) { |
|
121 |
while (true) { // read headers |
|
122 |
String line = reader.readLine(); |
|
123 |
if (line == null) { |
|
124 |
LOGGER.fine("End of stream"); |
|
125 |
return null; |
|
126 |
} |
|
127 |
if (line.length() == 0) { |
|
128 |
break; // end of headers |
|
129 |
} |
|
130 |
String[] nameValue = line.split(":", 2); |
|
131 |
if (nameValue.length != 2) { |
|
132 |
LOGGER.log(Level.SEVERE, "Bad header line: {0}", line); |
|
133 |
return null; |
|
134 |
} else { |
|
135 |
String trimmedValue = nameValue[1].trim(); |
|
136 |
headers.put(nameValue[0], trimmedValue); |
|
137 |
} |
|
138 |
} |
|
139 |
||
140 |
// Read payload if applicable |
|
141 |
String contentLengthStr = getHeader(headers, Header.CONTENT_LENGTH.name, "0"); |
|
142 |
int contentLength = Integer.valueOf(contentLengthStr.trim()); |
|
143 |
char[] content = new char[contentLength]; |
|
144 |
int totalRead = 0; |
|
145 |
LOGGER.log(Level.FINER, "Reading payload: {0} bytes", contentLength); |
|
146 |
while (totalRead < contentLength) { |
|
147 |
int readBytes = reader.read(content, totalRead, contentLength - totalRead); |
|
148 |
if (readBytes == -1) { |
|
149 |
// End-of-stream (browser closed?) |
|
150 |
LOGGER.fine("End of stream while reading content"); |
|
151 |
return null; |
|
152 |
} |
|
153 |
totalRead += readBytes; |
|
154 |
} |
|
155 |
||
156 |
// Construct response message |
|
157 |
String contentString = new String(content); |
|
158 |
return new Message(headers, contentString); |
|
159 |
} |
|
160 |
} |
|
161 |
||
162 |
/** |
|
163 |
* @return the "Tool" header value |
|
164 |
*/ |
|
165 |
public String getTool() { |
|
166 |
return getHeader(Header.TOOL.name, null); |
|
167 |
} |
|
168 |
||
169 |
/** |
|
170 |
* @return the "Destination" header value |
|
171 |
*/ |
|
172 |
public String getDestination() { |
|
173 |
return getHeader(Header.DESTINATION.name, null); |
|
174 |
} |
|
175 |
||
176 |
/** |
|
177 |
* @return the message content. Never {@code null} (for no content, returns an |
|
178 |
* empty String) |
|
179 |
*/ |
|
180 |
public String getContent() { |
|
181 |
return content; |
|
182 |
} |
|
183 |
||
184 |
/** |
|
185 |
* @param name of the header |
|
186 |
* @param defaultValue to return if the header is not found in the message |
|
187 |
* @return the {@code name} header value or {@code defaultValue} if the header |
|
188 |
* is not found in the message |
|
189 |
*/ |
|
190 |
public String getHeader(String name, String defaultValue) { |
|
191 |
return getHeader(this.headers, name, defaultValue); |
|
192 |
} |
|
193 |
||
194 |
private static String getHeader(Map<? extends String, String> headers, String headerName, |
|
195 |
String defaultValue) { |
|
196 |
String value = headers.get(headerName); |
|
197 |
if (value == null) { |
|
198 |
value = defaultValue; |
|
199 |
} |
|
200 |
return value; |
|
201 |
} |
|
202 |
||
203 |
private static String maskNull(String string) { |
|
204 |
return string == null |
|
205 |
? "" |
|
206 |
: string; |
|
207 |
} |
|
208 |
||
209 |
private static void writeNonEmptyHeader(Writer writer, String headerName, String headerValue) |
|
210 |
throws IOException { |
|
211 |
if (headerValue != null) { |
|
212 |
writeHeader(writer, headerName, headerValue); |
|
213 |
} |
|
214 |
} |
|
215 |
||
216 |
@Override |
|
217 |
public String toString() { |
|
218 |
StringWriter sw = new StringWriter(); |
|
219 |
try { |
|
220 |
this.sendThrough(sw); |
|
221 |
} catch (IOException e) { |
|
222 |
// never occurs |
|
223 |
} |
|
224 |
return sw.toString(); |
|
225 |
} |
|
226 |
||
227 |
private static void writeHeader(Writer writer, String name, String value) throws IOException { |
|
228 |
writer.append(name).append(':').append(value).append(HEADER_TERMINATOR); |
|
229 |
} |
|
230 |
} |