Search in sources :

Example 1 with XWikiPluginManager

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

the class XWiki method flushCache.

public void flushCache(XWikiContext context) {
    // We need to flush the virtual wiki list
    this.initializedWikis = new ConcurrentHashMap<>();
    // We need to flush the group service cache
    if (this.groupService != null) {
        this.groupService.flushCache();
    }
    // If we use the Cache Store layer.. we need to flush it
    XWikiStoreInterface store = getStore();
    if ((store != null) && (store instanceof XWikiCacheStoreInterface)) {
        ((XWikiCacheStoreInterface) getStore()).flushCache();
    }
    // Flush renderers.. Groovy renderer has a cache
    getOldRendering().flushCache();
    getParseGroovyFromString().flushCache();
    XWikiPluginManager pmanager = getPluginManager();
    if (pmanager != null) {
        pmanager.flushCache(context);
    }
    // Make sure we call all classes flushCache function
    try {
        List<String> classes = getClassList(context);
        for (int i = 0; i < classes.size(); i++) {
            String className = classes.get(i);
            try {
                getClass(className, context).flushCache();
            } catch (Exception e) {
            }
        }
    } catch (Exception e) {
    }
}
Also used : XWikiCacheStoreInterface(com.xpn.xwiki.store.XWikiCacheStoreInterface) XWikiPluginManager(com.xpn.xwiki.plugin.XWikiPluginManager) ParseGroovyFromString(com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString) IncludeServletAsString(com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString) XWikiStoreInterface(com.xpn.xwiki.store.XWikiStoreInterface) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) IOException(java.io.IOException) JobException(org.xwiki.job.JobException) ParseException(org.xwiki.rendering.parser.ParseException) QueryException(org.xwiki.query.QueryException) URIException(org.apache.commons.httpclient.URIException) InvocationTargetException(java.lang.reflect.InvocationTargetException) HibernateException(org.hibernate.HibernateException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) NamingException(javax.naming.NamingException) FileNotFoundException(java.io.FileNotFoundException) MalformedURLException(java.net.MalformedURLException)

Example 2 with XWikiPluginManager

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

the class DownloadActionTest method before.

@Before
public void before() throws Exception {
    this.oldcore.registerMockEnvironment();
    this.request = mock(XWikiRequest.class);
    this.oldcore.getXWikiContext().setRequest(this.request);
    this.response = mock(XWikiResponse.class);
    this.oldcore.getXWikiContext().setResponse(this.response);
    this.ec = mock(XWikiEngineContext.class);
    this.oldcore.getXWikiContext().setEngineContext(this.ec);
    this.out = mock(ServletOutputStream.class);
    XWikiPluginManager pluginManager = new XWikiPluginManager();
    pluginManager.initInterface();
    this.documentReference = new DocumentReference("wiki", "space", "page");
    this.document = new XWikiDocument(this.documentReference);
    this.oldcore.getXWikiContext().setDoc(this.document);
    doReturn(pluginManager).when(this.oldcore.getSpyXWiki()).getPluginManager();
    when(this.ec.getMimeType("file.txt")).thenReturn("text/plain");
    when(this.response.getOutputStream()).thenReturn(this.out);
    when(this.oldcore.getMockRightService().hasAccessLevel(eq("programming"), any(), any(), any(XWikiContext.class))).thenReturn(false);
    // Mock what's needed for extracting the filename from the URL
    this.resourceReferenceManager = this.oldcore.getMocker().registerMockComponent(ResourceReferenceManager.class);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ServletOutputStream(javax.servlet.ServletOutputStream) XWikiPluginManager(com.xpn.xwiki.plugin.XWikiPluginManager) XWikiContext(com.xpn.xwiki.XWikiContext) ResourceReferenceManager(org.xwiki.resource.ResourceReferenceManager) DocumentReference(org.xwiki.model.reference.DocumentReference) Before(org.junit.Before)

Example 3 with XWikiPluginManager

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

the class MockitoDownloadActionTest method renderWhenZipExplorerPluginURL.

@Test
public void renderWhenZipExplorerPluginURL() 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/space/page/file.ext/some/path");
    xcontext.setRequest(request);
    // Set the current doc and current wiki
    XWikiDocument document = mock(XWikiDocument.class);
    when(document.getAttachment("path")).thenReturn(null);
    xcontext.setDoc(document);
    xcontext.setWikiId("wiki");
    xcontext.setAction("download");
    // 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("path", new DocumentReference("wiki", Arrays.asList("space", "page", "file.ext"), "some")), EntityResourceAction.VIEW));
    // 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");
    when(attachment.clone()).thenReturn(attachment);
    // Configure an existing doc in the store
    XWiki xwiki = this.oldcore.getSpyXWiki();
    XWikiDocument backwardCompatDocument = new XWikiDocument(new DocumentReference("wiki", "space", "page"));
    backwardCompatDocument.addAttachment(attachment);
    xwiki.saveDocument(backwardCompatDocument, xcontext);
    // Make sure the user has permission to access the doc
    doReturn(true).when(xwiki).checkAccess(eq("download"), any(XWikiDocument.class), any(XWikiContext.class));
    // Setup ExecutionContextManager & VelocityManager using in the context backup
    ExecutionContextManager ecm = this.oldcore.getMocker().registerMockComponent(ExecutionContextManager.class);
    ExecutionContext ec = this.oldcore.getExecutionContext();
    when(ecm.clone(ec)).thenReturn(ec);
    VelocityManager vm = this.oldcore.getMocker().registerMockComponent(VelocityManager.class);
    // Set the Plugin Manager
    XWikiPluginManager pluginManager = mock(XWikiPluginManager.class);
    when(pluginManager.downloadAttachment(attachment, xcontext)).thenReturn(attachment);
    doReturn(pluginManager).when(this.oldcore.getSpyXWiki()).getPluginManager();
    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) ExecutionContextManager(org.xwiki.context.ExecutionContextManager) XWikiContext(com.xpn.xwiki.XWikiContext) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) XWiki(com.xpn.xwiki.XWiki) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Date(java.util.Date) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ExecutionContext(org.xwiki.context.ExecutionContext) ByteArrayInputStream(java.io.ByteArrayInputStream) VelocityManager(org.xwiki.velocity.VelocityManager) ResourceReferenceManager(org.xwiki.resource.ResourceReferenceManager) XWikiPluginManager(com.xpn.xwiki.plugin.XWikiPluginManager) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 4 with XWikiPluginManager

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

the class DefaultRenderingCache method buildCachedItem.

/**
 * Create cached item with all dependencies.
 *
 * @param context current xwiki context
 * @param renderedContent rendered page content
 * @return properly cached item
 */
private CachedItem buildCachedItem(XWikiContext context, String renderedContent) {
    CachedItem cachedItem = new CachedItem();
    for (RenderingCacheAware component : this.renderingCacheAwareProvider.get()) {
        cachedItem.extensions.put(component, component.getCacheResources(context));
    }
    // support for legacy core -> build non-blocking list (lazy)
    if (this.legacyRenderingCacheAware == null) {
        this.legacyRenderingCacheAware = new LinkedList<RenderingCacheAware>();
        XWikiPluginManager pluginManager = context.getWiki().getPluginManager();
        for (String pluginName : pluginManager.getPlugins()) {
            XWikiPluginInterface plugin = pluginManager.getPlugin(pluginName);
            if (plugin instanceof RenderingCacheAware) {
                this.legacyRenderingCacheAware.add((RenderingCacheAware) plugin);
            }
        }
    }
    for (RenderingCacheAware component : this.legacyRenderingCacheAware) {
        cachedItem.extensions.put(component, component.getCacheResources(context));
    }
    cachedItem.rendered = renderedContent;
    return cachedItem;
}
Also used : XWikiPluginManager(com.xpn.xwiki.plugin.XWikiPluginManager) XWikiPluginInterface(com.xpn.xwiki.plugin.XWikiPluginInterface)

Example 5 with XWikiPluginManager

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

the class DownloadAction method render.

@Override
public String render(XWikiContext context) throws XWikiException {
    XWikiRequest request = context.getRequest();
    XWikiResponse response = context.getResponse();
    XWikiDocument doc = context.getDoc();
    String filename = getFileName();
    XWikiAttachment attachment = getAttachment(request, doc, filename);
    Map<String, Object> backwardCompatibilityContextObjects = null;
    if (attachment == null) {
        // If some plugins extend the Download URL format for the Standard Scheme the document in the context will
        // most likely not have a reference that corresponds to what the plugin expects. For example imagine that
        // the URL is a Zip Explorer URL like .../download/space/page/attachment/index.html. This will be parsed
        // as space.page.attachment@index.html by the Standard URL scheme parsers. Thus the attachment won't be
        // found since index.html is not the correct attachment for the Zip Explorer plugin's URL format.
        // 
        // Thus in order to preserve backward compatibility for existing plugins that have custom URL formats
        // extending the Download URL format, we parse again the URL by considering that it doesn't contain any
        // Nested Space. This also means that those plugins will need to completely reparse the URL if they wish to
        // support Nested Spaces.
        // 
        // Also note that this code below is not compatible with the notion of having several URL schemes. The real
        // fix will be to not allow plugins to support custom URL formats and instead to have them register new
        // Actions if they need a different URL format.
        Pair<XWikiDocument, XWikiAttachment> result = extractAttachmentAndDocumentFromURLWithoutSupportingNestedSpaces(request, context);
        if (result == null) {
            throwNotFoundException(filename);
        }
        XWikiDocument backwardCompatibilityDocument = result.getLeft();
        attachment = result.getRight();
        // Set the new doc as the context doc so that plugins see it as the context doc
        backwardCompatibilityContextObjects = new HashMap<>();
        pushDocumentInContext(backwardCompatibilityContextObjects, backwardCompatibilityDocument.getDocumentReference());
    }
    try {
        XWikiPluginManager plugins = context.getWiki().getPluginManager();
        attachment = plugins.downloadAttachment(attachment, context);
        if (attachment == null) {
            throwNotFoundException(filename);
        }
        // This will throw an exception if the attachment content isn't available
        try {
            attachment.getContentSize(context);
        } catch (XWikiException e) {
            Object[] args = { filename };
            throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_ATTACHMENT_NOT_FOUND, "Attachment content {0} not found", null, args);
        }
        long lastModifiedOnClient = request.getDateHeader("If-Modified-Since");
        long lastModifiedOnServer = attachment.getDate().getTime();
        if (lastModifiedOnClient != -1 && lastModifiedOnClient >= lastModifiedOnServer) {
            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            return null;
        }
        // Sending the content of the attachment
        if (request.getHeader(RANGE_HEADER_NAME) != null) {
            try {
                if (sendPartialContent(attachment, request, response, context)) {
                    return null;
                }
            } catch (IOException ex) {
            // Broken response...
            }
        }
        sendContent(attachment, request, response, filename, context);
        return null;
    } finally {
        if (backwardCompatibilityContextObjects != null) {
            popDocumentFromContext(backwardCompatibilityContextObjects);
        }
    }
}
Also used : XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) IOException(java.io.IOException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiPluginManager(com.xpn.xwiki.plugin.XWikiPluginManager) XWikiException(com.xpn.xwiki.XWikiException)

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