forked from chromiumembedded/java-cef
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceHandler.java
More file actions
72 lines (60 loc) · 2.41 KB
/
ResourceHandler.java
File metadata and controls
72 lines (60 loc) · 2.41 KB
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
package tests.detailed.handler;
import org.cef.callback.CefCallback;
import org.cef.handler.CefLoadHandler;
import org.cef.handler.CefResourceHandlerAdapter;
import org.cef.misc.IntRef;
import org.cef.misc.StringRef;
import org.cef.network.CefRequest;
import org.cef.network.CefResponse;
import java.nio.ByteBuffer;
public class ResourceHandler extends CefResourceHandlerAdapter {
private int startPos = 0;
private static final String html = new String("<html>\n"
+ " <head>\n"
+ " <title>ResourceHandler Test</title>\n"
+ " </head>\n"
+ " <body>\n"
+ " <h1>ResourceHandler Test</h1>\n"
+ " <p>You have entered the URL: http://www.foo.bar. This page is generated by the application itself and<br/>\n"
+ " no HTTP request was sent to the internet.\n"
+ " <p>See class <u>tests.handler.ResourceHandler</u> and the <u>RequestHandler</u> implementation for details.</p>\n"
+ " </body>\n"
+ "</html>");
@Override
public boolean processRequest(CefRequest request, CefCallback callback) {
System.out.println("processRequest: " + request);
startPos = 0;
callback.Continue();
return true;
}
@Override
public void getResponseHeaders(
CefResponse response, IntRef response_length, StringRef redirectUrl) {
System.out.println("getResponseHeaders: " + response);
response_length.set(html.length());
response.setMimeType("text/html");
response.setStatus(200);
}
@Override
public boolean readResponse(
byte[] data_out, int bytes_to_read, IntRef bytes_read, CefCallback callback) {
int length = html.length();
if (startPos >= length) return false;
// Extract up to bytes_to_read bytes from the html data
int endPos = startPos + bytes_to_read;
String dataToSend =
(endPos > length) ? html.substring(startPos) : html.substring(startPos, endPos);
// Copy extracted bytes into data_out and set the read length
// to bytes_read.
ByteBuffer result = ByteBuffer.wrap(data_out);
result.put(dataToSend.getBytes());
bytes_read.set(dataToSend.length());
startPos = endPos;
return true;
}
@Override
public void cancel() {
System.out.println("cancel");
startPos = 0;
}
}