{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");
return;
}
}
}
//-------------------------------
// URI baseUri;
// try {
// baseUri = new URI(request.getScheme(),
// null/*user info?*/, request.getLocalName(), request.getLocalPort(),
// request.getRequestURI(), null, null);
// } catch(URISyntaxException e) {
// baseUri = null;
// }
final String requestAttr = (String) request.getAttribute(ATTR_XQUERY_ATTRIBUTE);
DBBroker broker = null;
try {
broker = getPool().get(user);
final XQuery xquery = broker.getXQueryService();
CompiledXQuery query = xquery.getXQueryPool().borrowCompiledXQuery(broker, source);
XQueryContext context;
if (query==null) {
context = xquery.newContext(AccessContext.REST);
context.setModuleLoadPath(moduleLoadPath);
try {
query = xquery.compile(context, source);
} catch (final XPathException ex) {
throw new EXistException("Cannot compile xquery: "+ ex.getMessage(), ex);
} catch (final IOException ex) {
throw new EXistException("I/O exception while compiling xquery: " + ex.getMessage() ,ex);
}
} else {
context = query.getContext();
context.setModuleLoadPath(moduleLoadPath);
}
final Properties outputProperties = new Properties();
outputProperties.put("base-uri", collectionURI.toString());
context.declareVariable(RequestModule.PREFIX + ":request", new HttpRequestWrapper(request, getFormEncoding(), getContainerEncoding()));
context.declareVariable(ResponseModule.PREFIX + ":response", new HttpResponseWrapper(response));
context.declareVariable(SessionModule.PREFIX + ":session", ( session != null ? new HttpSessionWrapper( session ) : null ) );
final String timeoutOpt = (String) request.getAttribute(ATTR_TIMEOUT);
if (timeoutOpt != null) {
try {
final long timeout = Long.parseLong(timeoutOpt);
context.getWatchDog().setTimeout(timeout);
} catch (final NumberFormatException e) {
throw new EXistException("Bad timeout option: " + timeoutOpt);
}
}
final String maxNodesOpt = (String) request.getAttribute(ATTR_MAX_NODES);
if (maxNodesOpt != null) {
try{
final int maxNodes = Integer.parseInt(maxNodesOpt);
context.getWatchDog().setMaxNodes(maxNodes);
} catch (final NumberFormatException e) {
throw new EXistException("Bad max-nodes option: " + maxNodesOpt);
}
}
DebuggeeFactory.checkForDebugRequest(request, context);
Sequence resultSequence;
try {
resultSequence = xquery.execute(query, null, outputProperties);
} finally {
context.runCleanupTasks();
xquery.getXQueryPool().returnCompiledXQuery(source, query);
}
final String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE);
if (mediaType != null) {
if (!response.isCommitted())
{if (MimeTable.getInstance().isTextContent(mediaType)) {
response.setContentType(mediaType + "; charset=" + getFormEncoding());
response.setCharacterEncoding(getFormEncoding());
} else
response.setContentType(mediaType);}
} else {
String contentType = this.contentType;
try {
contentType = getServletContext().getMimeType(path);
if (contentType == null)
{contentType = this.contentType;}
} catch (final Throwable e) {
contentType = this.contentType;
} finally {
if (MimeTable.getInstance().isTextContent(contentType))
{contentType += "; charset=" + getFormEncoding();}
response.setContentType(contentType );
}
}
if (requestAttr != null && (XmldbURI.API_LOCAL.equals(collectionURI.getApiName())) ) {
request.setAttribute(requestAttr, resultSequence);
} else {
final Serializer serializer = broker.getSerializer();
serializer.reset();
final SerializerPool serializerPool = SerializerPool.getInstance();
final SAXSerializer sax = (SAXSerializer) serializerPool.borrowObject(SAXSerializer.class);