Thierry Boileau <[EMAIL PROTECTED]> wrote on 10/10/2008 03:20:15
AM:
> Hi John,
>
>
> I send you a sample test code that works correctly.
> I hope it will help you. Otherwise, could you send us a
> reproductible test code?
>
Hi, Thierry, and thanks for the reply! After doing more research on this
than I had planned, I found that there's a real problem in Java with
trying to use XHTML representations, with DOCTYPE entities, while behind a
Proxy server. The Restlet client code kind of masked what the underlying
problem was when I wrote the email. The code pasted below fails, for
example, when running behind a proxy.
I have gotten some client code to work by specifying a custom
EntityResolver in the DocumentBuilder, and then wrapping the Document in a
DomRepresentation. I wonder if there's a place in Restlet where a client
program can either A) specify that the DomBuilder should set the "don't
load external DTDs" feature, or B) can send in a custom EntityResolver
that returns an empty DTD?
Thanks for looking at this!
--------------------------------
John Wismar
[EMAIL PROTECTED]
package testDomRepresentation;
import org.restlet.Application;
import org.restlet.Client;
import org.restlet.Component;
import org.restlet.Restlet;
import org.restlet.Router;
import org.restlet.data.MediaType;
import org.restlet.data.Protocol;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.DomRepresentation;
import org.restlet.resource.StringRepresentation;
import org.restlet.util.NodeSet;
public class TestApplication extends Application {
public static void main(String[] args) throws Exception {
Component component = new Component();
component.getServers().add(Protocol.HTTP, 8182);
component.getDefaultHost().attachDefault(new TestApplication());
component.start();
Client client = new Client(Protocol.HTTP);
Response response = client.get("http://localhost:8182/");
DomRepresentation doc = response.getEntityAsDom();
String xpath = "//a";
NodeSet nodes = doc.getNodes(xpath); // This returns null
System.out.println(nodes.get(0).getTextContent());
component.stop();
}
@Override
public Restlet createRoot() {
Router router = new Router(getContext());
Restlet restlet = new Restlet() {
@Override
public void handle(Request request, Response response) {
response.setEntity(new StringRepresentation(
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML
1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">" +
"<html xmlns=\"http://www.w3.org/1999/xhtml\"
version=\"-//W3C//DTD XHTML 1.1//EN\" xml:lang=\"en\">" +
"<head>" +
"<meta http-equiv=\"Content-Type\"
content=\"application/xhtml+xml; charset=utf-8\" />" +
"</head>"+
"<body>" +
"<a href=\"/test/\">some text</a>" +
"</body>" +
"</html>",
MediaType.APPLICATION_XHTML_XML));
}
};
router.attachDefault(restlet);
return router;
}
}