Search in sources :

Example 6 with XWikiPluginManager

use of com.xpn.xwiki.plugin.XWikiPluginManager in project xwiki-platform by xwiki.

the class DownloadRevAction method render.

@Override
public String render(XWikiContext context) throws XWikiException {
    XWikiRequest request = context.getRequest();
    XWikiResponse response = context.getResponse();
    XWikiDocument doc = context.getDoc();
    String rev = request.getParameter("rev");
    String filename = getFileName();
    XWikiAttachment attachment;
    if (context.getWiki().hasAttachmentRecycleBin(context) && request.getParameter("rid") != null) {
        int recycleId = Integer.parseInt(request.getParameter("rid"));
        attachment = new XWikiAttachment(doc, filename);
        attachment = context.getWiki().getAttachmentRecycleBinStore().restoreFromRecycleBin(attachment, recycleId, context, true);
    } else if (request.getParameter("id") != null) {
        int id = Integer.parseInt(request.getParameter("id"));
        attachment = doc.getAttachmentList().get(id);
    } else {
        attachment = doc.getAttachment(filename);
    }
    if (attachment == null) {
        Object[] args = { filename };
        throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_ATTACHMENT_NOT_FOUND, "Attachment {0} not found", null, args);
    }
    synchronized (attachment) {
        try {
            attachment = attachment.getAttachmentRevision(rev, context);
            if (attachment == null) {
                throw new XWikiException();
            }
        } catch (XWikiException e) {
            String url = context.getDoc().getURL("viewattachrev", true, context);
            url += "/" + filename;
            if (request.getParameter("rid") != null) {
                url += "?rid=" + request.getParameter("rid");
            }
            try {
                context.getResponse().sendRedirect(url);
                return null;
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }
    XWikiPluginManager plugins = context.getWiki().getPluginManager();
    attachment = plugins.downloadAttachment(attachment, context);
    // Choose the right content type
    String mimetype = attachment.getMimeType(context);
    response.setContentType(mimetype);
    response.setDateHeader("Last-Modified", attachment.getDate().getTime());
    // Sending the content of the attachment
    try {
        response.setContentLength(attachment.getContentSize(context));
        IOUtils.copy(attachment.getContentInputStream(context), response.getOutputStream());
    } catch (IOException e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_SEND_RESPONSE_EXCEPTION, "Exception while sending response", e);
    }
    return null;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiPluginManager(com.xpn.xwiki.plugin.XWikiPluginManager) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) IOException(java.io.IOException) XWikiException(com.xpn.xwiki.XWikiException)

Example 7 with XWikiPluginManager

use of com.xpn.xwiki.plugin.XWikiPluginManager in project xwiki-platform by xwiki.

the class XWiki method preparePlugins.

private void preparePlugins(XWikiContext context) {
    setPluginManager(new XWikiPluginManager(getXWikiPreference("plugins", context), context));
    String plugins = getConfiguration().getProperty("xwiki.plugins", "");
    if (!plugins.equals("")) {
        getPluginManager().addPlugins(StringUtils.split(plugins, " ,"), context);
    }
}
Also used : XWikiPluginManager(com.xpn.xwiki.plugin.XWikiPluginManager) ParseGroovyFromString(com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString) IncludeServletAsString(com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)

Example 8 with XWikiPluginManager

use of com.xpn.xwiki.plugin.XWikiPluginManager in project xwiki-platform by xwiki.

the class XWiki method getPlugin.

/**
 * @deprecated
 */
@Deprecated
public XWikiPluginInterface getPlugin(String name, XWikiContext context) {
    XWikiPluginManager plugins = getPluginManager();
    Vector<String> pluginlist = plugins.getPlugins();
    for (String pluginname : pluginlist) {
        if (pluginname.equals(name)) {
            return plugins.getPlugin(pluginname);
        }
    }
    return null;
}
Also used : XWikiPluginManager(com.xpn.xwiki.plugin.XWikiPluginManager) ParseGroovyFromString(com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString) IncludeServletAsString(com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)

Example 9 with XWikiPluginManager

use of com.xpn.xwiki.plugin.XWikiPluginManager in project xwiki-platform by xwiki.

the class MockitoDownloadActionTest method renderWhenAttachmentIsInANestedSpace.

@Test
public void renderWhenAttachmentIsInANestedSpace() throws Exception {
    DownloadAction action = new DownloadAction();
    XWikiContext xcontext = this.oldcore.getXWikiContext();
    // Set the Request URL
    XWikiServletRequestStub request = new XWikiServletRequestStub();
    request.setRequestURI("http://localhost:8080/xwiki/bin/download/space1/space2/page/file.ext");
    xcontext.setRequest(request);
    // Setup the returned attachment
    XWikiAttachment attachment = mock(XWikiAttachment.class);
    when(attachment.getContentSize(xcontext)).thenReturn(100);
    Date now = new Date();
    when(attachment.getDate()).thenReturn(now);
    when(attachment.getFilename()).thenReturn("file.ext");
    when(attachment.getContentInputStream(xcontext)).thenReturn(new ByteArrayInputStream("test".getBytes()));
    when(attachment.getMimeType(xcontext)).thenReturn("mimetype");
    // Set the current doc
    XWikiDocument document = mock(XWikiDocument.class);
    when(document.getAttachment("file.ext")).thenReturn(attachment);
    xcontext.setDoc(document);
    // Set the Plugin Manager
    XWikiPluginManager pluginManager = mock(XWikiPluginManager.class);
    when(pluginManager.downloadAttachment(attachment, xcontext)).thenReturn(attachment);
    doReturn(pluginManager).when(this.oldcore.getSpyXWiki()).getPluginManager();
    // Set the Response
    XWikiResponse response = mock(XWikiResponse.class);
    StubServletOutputStream ssos = new StubServletOutputStream();
    when(response.getOutputStream()).thenReturn(ssos);
    xcontext.setResponse(response);
    // Set the Resource Reference Manager used to parse the URL and extract the attachment name
    ResourceReferenceManager rrm = this.oldcore.getMocker().registerMockComponent(ResourceReferenceManager.class);
    when(rrm.getResourceReference()).thenReturn(new EntityResourceReference(new AttachmentReference("file.ext", new DocumentReference("wiki", Arrays.asList("space1", "space2"), "page")), EntityResourceAction.VIEW));
    // Note: we don't give PR and the attachment is not an authorized mime type.
    assertNull(action.render(xcontext));
    // This is the test, we verify what is set in the response
    verify(response).setContentType("mimetype");
    verify(response).setHeader("Accept-Ranges", "bytes");
    verify(response).addHeader("Content-disposition", "attachment; filename*=utf-8''file.ext");
    verify(response).setDateHeader("Last-Modified", now.getTime());
    verify(response).setContentLength(100);
    assertEquals("test", ssos.baos.toString());
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) XWikiContext(com.xpn.xwiki.XWikiContext) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Date(java.util.Date) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) XWikiPluginManager(com.xpn.xwiki.plugin.XWikiPluginManager) ResourceReferenceManager(org.xwiki.resource.ResourceReferenceManager) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

XWikiPluginManager (com.xpn.xwiki.plugin.XWikiPluginManager)9 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)5 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)4 XWikiContext (com.xpn.xwiki.XWikiContext)3 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)3 IncludeServletAsString (com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)3 IOException (java.io.IOException)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 ResourceReferenceManager (org.xwiki.resource.ResourceReferenceManager)3 XWikiException (com.xpn.xwiki.XWikiException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Date (java.util.Date)2 Test (org.junit.Test)2 AttachmentReference (org.xwiki.model.reference.AttachmentReference)2 EntityResourceReference (org.xwiki.resource.entity.EntityResourceReference)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiPluginInterface (com.xpn.xwiki.plugin.XWikiPluginInterface)1 XWikiCacheStoreInterface (com.xpn.xwiki.store.XWikiCacheStoreInterface)1 XWikiStoreInterface (com.xpn.xwiki.store.XWikiStoreInterface)1 FileNotFoundException (java.io.FileNotFoundException)1