if (requestUser != null)
{user = requestUser;}
}
}
Source source = null;
final Object sourceAttrib = request.getAttribute(ATTR_XQUERY_SOURCE);
final Object urlAttrib = request.getAttribute(ATTR_XQUERY_URL);
if (sourceAttrib != null) {
String s;
if (sourceAttrib instanceof Item)
try {
s = ((Item) sourceAttrib).getStringValue();
} catch (final XPathException e) {
throw new ServletException("Failed to read XQuery source string from " +
"request attribute '" + ATTR_XQUERY_SOURCE + "': " + e.getMessage(), e);
}
else
{s = sourceAttrib.toString();}
source = new StringSource(s);
} else if (urlAttrib != null) {
DBBroker broker = null;
try {
broker = getPool().get(user);
source = SourceFactory.getSource(broker, moduleLoadPath, urlAttrib.toString(), true);
} catch (final Exception e) {
getLog().error(e.getMessage(), e);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
sendError(output, "Error", e.getMessage());
} finally {
getPool().release(broker);
}
} else {
final File f = new File(path);
if(!f.canRead()) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
sendError(output, "Cannot read source file", path);
return;
}
source = new FileSource(f, encoding, true);
}
if (source == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
sendError(output, "Source not found", path);
}
boolean reportErrors = false;
final String errorOpt = (String) request.getAttribute(ATTR_XQUERY_REPORT_ERRORS);
if (errorOpt != null)
{reportErrors = errorOpt.equalsIgnoreCase("YES");}
//allow source viewing for GET?
if("GET".equals(request.getMethod().toUpperCase())) {
String option;
boolean allowSource = false;
if((option = request.getParameter("_source")) != null)
allowSource = "yes".equals(option);
//Should we display the source of the XQuery or execute it
if(allowSource && descriptor != null) {
//show the source
//check are we allowed to show the xquery source - descriptor.xml
// System.out.println("path="+path);
if(descriptor.allowSource(path)) {
try {
source.validate(user, Permission.READ);
} catch (final PermissionDeniedException e) {
if (getDefaultUser().equals(user)) {
getAuthenticator().sendChallenge(request, response);
} else {
response.sendError(HttpServletResponse.SC_FORBIDDEN, "Permission to view XQuery source for: " + path + " denied. (no read access)");
}
return;
}
//Show the source of the XQuery
//writeResourceAs(resource, broker, stylesheet, encoding, "text/plain", outputProperties, response);
response.setContentType("text/plain; charset=" + getFormEncoding());
output.write(source.getContent());
output.flush();
return;
} else {
response.sendError(HttpServletResponse.SC_FORBIDDEN, "Permission to view XQuery source for: " + path + " denied. Must be explicitly defined in descriptor.xml");