Search in sources :

Example 6 with XWikiResponse

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);
}
Also used : XWikiResponse(com.xpn.xwiki.web.XWikiResponse) OutputStream(java.io.OutputStream) File(java.io.File)

Example 7 with XWikiResponse

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;
}
Also used : XWikiServletRequest(com.xpn.xwiki.web.XWikiServletRequest) XWikiServletRequestStub(com.xpn.xwiki.web.XWikiServletRequestStub) XWikiResponse(com.xpn.xwiki.web.XWikiResponse) XWikiContext(com.xpn.xwiki.XWikiContext) URL(java.net.URL) XWikiException(com.xpn.xwiki.XWikiException) SchedulerException(org.quartz.SchedulerException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiServletResponseStub(com.xpn.xwiki.web.XWikiServletResponseStub) XWikiException(com.xpn.xwiki.XWikiException)

Example 8 with XWikiResponse

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();
}
Also used : XWikiResponse(com.xpn.xwiki.web.XWikiResponse) OutputStream(java.io.OutputStream)

Example 9 with XWikiResponse

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);
}
Also used : XWikiResponse(com.xpn.xwiki.web.XWikiResponse) OutputStream(java.io.OutputStream) File(java.io.File)

Example 10 with XWikiResponse

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();
}
Also used : XWikiResponse(com.xpn.xwiki.web.XWikiResponse) OutputStream(java.io.OutputStream)

Aggregations

XWikiResponse (com.xpn.xwiki.web.XWikiResponse)11 OutputStream (java.io.OutputStream)5 XWikiContext (com.xpn.xwiki.XWikiContext)4 URL (java.net.URL)4 File (java.io.File)3 XWikiServletResponseStub (com.xpn.xwiki.web.XWikiServletResponseStub)2 Test (org.junit.Test)2 XWikiException (com.xpn.xwiki.XWikiException)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 XWikiRequest (com.xpn.xwiki.web.XWikiRequest)1 XWikiServletRequest (com.xpn.xwiki.web.XWikiServletRequest)1 XWikiServletRequestStub (com.xpn.xwiki.web.XWikiServletRequestStub)1 XWikiURLFactory (com.xpn.xwiki.web.XWikiURLFactory)1 CachePolicy (com.xpn.xwiki.web.sx.SxSource.CachePolicy)1 IOException (java.io.IOException)1 Date (java.util.Date)1 Before (org.junit.Before)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 SchedulerException (org.quartz.SchedulerException)1