String cause = e.getCause().getMessage();
if ( cause != null )
error += "\n" + cause;
}
StringReader is = new StringReader(error);
ServletOutputStream os = response.getOutputStream();
response.setContentType("text/plain");
IOUtils.copy(is, os);
os.close();
return true;
}
if (log.isDebugEnabled()) {
log.debug("outFormat=" + outFormat+
" queryResult: contentType=" + queryResult.getContentType()+
" isEmpty=" +queryResult.isEmpty());
}
// set the content type now (although this might be changed below)
// (this should fix 284: "empty reponse with incorrect content type")
response.setContentType(queryResult.getContentType());
String result = queryResult.getResult();
if ( queryResult.isEmpty() ) {
if ( requestedEntity != null ) {
response.sendError(HttpServletResponse.SC_NOT_FOUND, requestedEntity);
return true;
}
if ( ! forceCompletion ) {
// we have the condition: !forceCompletion AND requestedEntity is null AND query result is empty.
return false; // dispatch NO completed here.
}
}
if ( "Application/rdf+xml".equalsIgnoreCase(queryResult.getContentType()) ) {
// convert to HTML?
if ( Util.yes(request, "xslt") ) {
String XSLT_RESOURCE = "rdf.xslt";
InputStream xslt = getClass().getClassLoader().getResourceAsStream(XSLT_RESOURCE);
if ( xslt != null ) {
result = XSLTCreator.create(result, xslt);
}
else {
result = "Cannot find resource: " + XSLT_RESOURCE;
}
response.setContentType("text/html");
}
// put stylesheet at beginning of the result?
else if ( Util.yes(request, "xslti") ) {
// what type? I've tried:
// type="text/xsl"
// type="text/xml"
// type="application/xslt+xml"
// without success.
//
String type="application/xslt+xml";
String xmlHeader = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\n";
xmlHeader += "<?xml-stylesheet type=\"" +type+ "\" href=\"" +
request.getContextPath()+ "/rdf.xslt" + "\"?>\n";
result = xmlHeader + result;
response.setContentType("Application/rdf+xml");
}
else {
response.setContentType("Application/rdf+xml");
}
}
else if ( "text/html".equalsIgnoreCase(queryResult.getContentType()) ) {
String queryComm = "\n<!-- Query:\n\n" +Util.toHtmlComment(query)+ "\n-->\n\n";
String pre, pos = "";
if ( "html-frag".equals(outFormat) ) {
pre = queryComm;
pos = "";
}
else {
pre = "<html><head><title>Query result</title>" +
"<link rel=stylesheet href=\"" +
request.getContextPath()+ "/main.css\" type=\"text/css\">" +
"</head><body>\n" +
queryComm + "\n" +
"<br/><div align=\"center\">\n"
;
/*
* TODO better to have this piece as a specific "header" parameter
*/
if (requestedEntity != null) {
pre += "<b>" + requestedEntity + "</b><br/><br/>\n";
pos += "<font color=\"gray\" size=\"-2\"><br/>" +OntVersion.getFullTitle()+ "</font>";
}
pos += "</div></body></html>";
}
result = pre + result + pos;
response.setContentType(queryResult.getContentType());
}
/*
* The following checks the case when the request was for "html-frag", which
* is actually dispatched as CSV when using AllegroGraph 4.4. In this
* case, do the conversion from CSV to the requested HTML:
*/
else if ("html-frag".equals(outFormat) && Util.contentTypeIsCsv(queryResult.getContentType())) {
result = Util.csv2html(result);
response.setContentType("text/html");
}
/*
* The following checks the case when there was no explicit outFormat and the
* dispatch generated a CSV format. if so, do the conversion from CSV to the requested HTML:
*/
else if (outFormat == null && Util.contentTypeIsCsv(queryResult.getContentType())) {
result = Util.csv2html(result);
response.setContentType("text/html");
String queryComm = "\n<!-- Query:\n\n" +Util.toHtmlComment(query)+ "\n-->\n\n";
String pre = "<html><head><title>Query result</title>" +
"<link rel=stylesheet href=\"" +
request.getContextPath()+ "/main.css\" type=\"text/css\">" +
"</head><body>\n" +
queryComm
;
String pos = "</body></html>";
result = pre + result + pos;
}
else {
response.setContentType(queryResult.getContentType());
}
ServletOutputStream os = response.getOutputStream();
IOUtils.write(result, os, "UTF-8");
os.close();
return true;
}