final String idPart = matcher.group(1);
final long id = Long.parseLong(idPart);
final String contentType = fileMatcher.getContentType();
final String file = fileMatcher.getFile();
final HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
final JbpmContext jbpmContext = JbpmConfiguration.getInstance().createJbpmContext();
try {
final ProcessDefinition processDefinition = jbpmContext.getGraphSession().getProcessDefinition(id);
if (processDefinition == null) {
try {
response.sendError(404, "Process definition " + id + " does not exist");
facesContext.responseComplete();
break;
} catch (IOException e) {
log.log(Level.SEVERE, "Failed to send 404 Not Found to client", e);
}
}
if (!processDefinition.getFileDefinition().hasFile(file)) {
try {
response.sendError(404, "Process definition " + id + " does not contain file '" + file + "'");
facesContext.responseComplete();
break;
} catch (IOException e) {
log.log(Level.SEVERE, "Failed to send 404 Not Found to client", e);
}
}
final byte[] bytes;
bytes = processDefinition.getFileDefinition().getBytes(file);
response.setContentLength(bytes.length);
response.setContentType(contentType);
try {
final OutputStream outputStream = response.getOutputStream();
try {
outputStream.write(bytes);
outputStream.flush();
} finally {
try {
outputStream.close();
} catch (IOException e) {
log.log(Level.WARNING, "Failed to close output stream", e);
}
}
} catch (IOException e) {
log.log(Level.SEVERE, "Failed to send process file", e);
}
facesContext.responseComplete();
if (log.isLoggable(Level.FINE)) {
log.fine("Sent process file '" + path + "'");
}
break;
} finally {
jbpmContext.close();
}
}
}
}