}));
/*
* register our own handler, that receives subgraph messages
*/
Endpoint.registerHandler("/message/", new HttpHandler() {
/**
* Read out a query parameter
*
* @param responseParts
* the queries as array
* @param parameter
* the parameter to search for
* @return the content part after query
* @throws UnsupportedEncodingException
* error
*/
protected String getParameter(final String[] responseParts,
final String parameter) throws UnsupportedEncodingException {
for (final String item : responseParts) {
if (item.startsWith(parameter)) {
return URLDecoder.decode(
item.substring(parameter.length()), "UTF-8");
}
}
return null;
}
@Override
public void handle(final HttpExchange t) throws IOException {
/*
* get the sender's ip-address (hostname)
*/
String host = t.getRemoteAddress().getHostString();
if (!host.startsWith("http://")) {
host = "http://" + host;
}
log.debug(String.format("Receiving subgraph-request from: %s",
host));
/*
* get the queries
*/
final String response = Endpoint.getResponse(t);
final String[] responseParts = response.split("[&]");
if (responseParts.length > 0) {
/*
* the port the request came from (has to be given as query
* parameter, because when using windows, the sender's port
* is a random one, and not the one, that can be used for
* answer! but the host is right, so now we know the
* endpoint-url to answer!
*/
final String port = this.getParameter(responseParts, "from=");
host += ":" + port;
/*
* now get the query-part (subgraph message)
*/
final String queryParameter = this.getParameter(responseParts,
"query=");
if (queryParameter != null) {
/*
* forward this message to the subgraph executer ...
*/
EndpointNetwork.this.onMessage(queryParameter, host);
/*
* and return result
*/
t.getResponseHeaders()
.add("Content-type", "text/plain");
Endpoint.sendString(t, "OK");
} else { /*
* if no parameter given
*/
t.getResponseHeaders()
.add("Content-type", "text/plain");
final String answer = "Bad Request: query parameter missing";
Endpoint.sendString(t, answer);
return;
}
}
};
});
/*
* This is the handler that receives streams with XML results
*/
Endpoint.registerHandler("/answer/", new HttpHandler() {
@Override
public void handle(final HttpExchange t) throws IOException {
/*
* get the sender's ip-address (hostname)