use of com.xpn.xwiki.web.XWikiResponse in project xwiki-platform by xwiki.
the class ChartingPlugin method outputFile.
public void outputFile(String filename, XWikiContext context) throws IOException {
File ofile = getTempFile(filename);
byte[] bytes = readFile(ofile);
XWikiResponse response = context.getResponse();
context.setFinished(true);
response.setDateHeader("Last-Modified", ofile.lastModified());
response.setContentLength(bytes.length);
response.setContentType(context.getEngineContext().getMimeType(filename));
OutputStream os = response.getOutputStream();
os.write(bytes);
}
use of com.xpn.xwiki.web.XWikiResponse in project xwiki-platform by xwiki.
the class SchedulerPlugin method prepareJobStubContext.
/**
* Create and feed a stub context for the job execution thread. Stub context data are retrieved from job object
* fields "contextUser", "contextLang", "contextDatabase". If one of this field is empty (this would typically
* happen on the first schedule operation), it is instead retrieved from the passed context, and the job object is
* updated with this value. This mean that this method may modify the passed object.
*
* @param job the job for which the context will be prepared
* @param context the XWikiContext at preparation time. This is a real context associated with a servlet request
* @return the stub context prepared with job data
*/
private XWikiContext prepareJobStubContext(BaseObject job, XWikiContext context) throws SchedulerPluginException {
boolean jobNeedsUpdate = false;
String cUser = job.getStringValue("contextUser");
if (cUser.equals("")) {
// The context user has not been filled yet.
// We can suppose it's the first scheduling. Let's assume it's the context user
cUser = context.getUser();
job.setStringValue("contextUser", cUser);
jobNeedsUpdate = true;
}
String cLang = job.getStringValue("contextLang");
if (cLang.equals("")) {
cLang = context.getLanguage();
job.setStringValue("contextLang", cLang);
jobNeedsUpdate = true;
}
String iDb = context.getWikiId();
String cDb = job.getStringValue("contextDatabase");
if (cDb.equals("") || !cDb.equals(iDb)) {
cDb = context.getWikiId();
job.setStringValue("contextDatabase", cDb);
jobNeedsUpdate = true;
}
if (jobNeedsUpdate) {
try {
context.setWikiId(cDb);
XWikiDocument jobHolder = context.getWiki().getDocument(job.getName(), context);
jobHolder.setMinorEdit(true);
context.getWiki().saveDocument(jobHolder, context);
} catch (XWikiException e) {
throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_UNABLE_TO_PREPARE_JOB_CONTEXT, "Failed to prepare context for job with job name " + job.getStringValue("jobName"), e);
} finally {
context.setWikiId(iDb);
}
}
// lets now build the stub context
XWikiContext scontext = context.clone();
scontext.setWiki(context.getWiki());
context.getWiki().getStore().cleanUp(context);
// We are sure the context request is a real servlet request
// So we force the dummy request with the current host
XWikiServletRequestStub dummy = new XWikiServletRequestStub();
dummy.setHost(context.getRequest().getHeader("x-forwarded-host"));
dummy.setScheme(context.getRequest().getScheme());
dummy.setContextPath(context.getRequest().getContextPath());
XWikiServletRequest request = new XWikiServletRequest(dummy);
scontext.setRequest(request);
// Force forged context response to a stub response, since the current context response
// will not mean anything anymore when running in the scheduler's thread, and can cause
// errors.
XWikiResponse stub = new XWikiServletResponseStub();
scontext.setResponse(stub);
// feed the dummy context
scontext.setUser(cUser);
scontext.setLanguage(cLang);
scontext.setWikiId(cDb);
scontext.setMainXWiki(context.getMainXWiki());
if (scontext.getURL() == null) {
try {
scontext.setURL(new URL("http://www.mystuburl.com/"));
} catch (Exception e) {
// the URL is well formed, I promise
}
}
com.xpn.xwiki.web.XWikiURLFactory xurf = context.getURLFactory();
if (xurf == null) {
xurf = context.getWiki().getURLFactoryService().createURLFactory(context.getMode(), context);
}
scontext.setURLFactory(xurf);
try {
XWikiDocument cDoc = context.getWiki().getDocument(job.getDocumentReference(), context);
scontext.setDoc(cDoc);
} catch (Exception e) {
throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_UNABLE_TO_PREPARE_JOB_CONTEXT, "Failed to prepare context for job with job name " + job.getStringValue("jobName"), e);
}
return scontext;
}
use of com.xpn.xwiki.web.XWikiResponse in project xwiki-platform by xwiki.
the class GraphVizPlugin method outputDotImage.
/**
* Executes GraphViz and writes the resulting image (in the requested format) into the response.
*
* @param content the dot source code
* @param extension the output file extension
* @param dot which engine to execute: {@code dot} if {@code true}, {@code neato} if {@code false}
* @param context the current request context
* @throws IOException if writing the input or output files to the disk fails, or if writing the response body fails
*/
public void outputDotImage(String content, String extension, boolean dot, XWikiContext context) throws IOException {
byte[] dotbytes = getDotImage(content, extension, dot);
XWikiResponse response = context.getResponse();
context.setFinished(true);
response.setContentLength(dotbytes.length);
response.setContentType(context.getEngineContext().getMimeType("toto." + extension));
OutputStream os = response.getOutputStream();
os.write(dotbytes);
os.flush();
}
use of com.xpn.xwiki.web.XWikiResponse in project xwiki-platform by xwiki.
the class GraphVizPlugin method outputDotImageFromFile.
/**
* Writes an already generated result from the temporary file into the response.
*
* @param filename the name of the temporary file, previously returned by
* {@link #writeDotImage(String, String, boolean)}
* @param context the current request context
* @throws IOException if reading the file from the disk fails, or if writing the response body fails
*/
public void outputDotImageFromFile(String filename, XWikiContext context) throws IOException {
File ofile = getTempFile(filename);
byte[] dotbytes = readDotImage(ofile);
XWikiResponse response = context.getResponse();
context.setFinished(true);
response.setDateHeader("Last-Modified", ofile.lastModified());
response.setContentLength(dotbytes.length);
response.setContentType(context.getEngineContext().getMimeType(filename));
OutputStream os = response.getOutputStream();
os.write(dotbytes);
}
use of com.xpn.xwiki.web.XWikiResponse in project xwiki-platform by xwiki.
the class SVGPlugin method outputSVGImage.
public void outputSVGImage(String content, String extension, int height, int width, XWikiContext context) throws IOException, SVGConverterException {
byte[] svgbytes = getSVGImage(content, extension, height, width);
XWikiResponse response = context.getResponse();
context.setFinished(true);
response.setContentLength(svgbytes.length);
response.setContentType(context.getEngineContext().getMimeType("toto." + extension));
OutputStream os = response.getOutputStream();
os.write(svgbytes);
os.flush();
}
Aggregations