Search in sources :

Example 1 with Container

use of org.xwiki.container.Container in project xwiki-platform by xwiki.

the class Util method getResourceAsStream.

/**
 * Load resources from: 1. FileSystem 2. ServletContext 3. ClassPath in this order.
 *
 * @param resource resource path to load
 * @return InputStream of resource or null if not found
 */
public static InputStream getResourceAsStream(String resource) {
    File file = new File(resource);
    try {
        if (file.exists()) {
            return new FileInputStream(file);
        }
    } catch (Exception e) {
        // Probably running under -security, which prevents calling File.exists()
        LOGGER.debug("Failed load resource [" + resource + "] using a file path");
    }
    try {
        Container container = Utils.getComponent(Container.class);
        InputStream res = container.getApplicationContext().getResourceAsStream(resource);
        if (res != null) {
            return res;
        }
    } catch (Exception e) {
        LOGGER.debug("Failed to load resource [" + resource + "] using the application context");
    }
    return Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
}
Also used : Container(org.xwiki.container.Container) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) XWikiException(com.xpn.xwiki.XWikiException) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) MissingResourceException(java.util.MissingResourceException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SAXException(org.xml.sax.SAXException)

Example 2 with Container

use of org.xwiki.container.Container in project xwiki-platform by xwiki.

the class ResourceReferenceHandlerServlet method cleanupComponents.

private void cleanupComponents() throws ServletException {
    Container container;
    try {
        container = this.rootComponentManager.getInstance(Container.class);
    } catch (ComponentLookupException e) {
        throw new ServletException("Failed to locate a Container component", e);
    }
    Execution execution;
    try {
        execution = this.rootComponentManager.getInstance(Execution.class);
    } catch (ComponentLookupException e) {
        throw new ServletException("Failed to locate a Execution component", e);
    }
    // We must ensure we clean the ThreadLocal variables located in the Container and Execution components as
    // otherwise we will have a potential memory leak.
    container.removeRequest();
    container.removeResponse();
    container.removeSession();
    execution.removeContext();
}
Also used : ServletException(javax.servlet.ServletException) Container(org.xwiki.container.Container) Execution(org.xwiki.context.Execution) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 3 with Container

use of org.xwiki.container.Container in project xwiki-platform by xwiki.

the class DefaultCSRFTokenTest method configure.

@Before
public void configure() throws Exception {
    // set up mocked dependencies
    // document access bridge
    final DocumentAccessBridge mockDocumentAccessBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
    final CopyStringMatcher returnValue = new CopyStringMatcher(resubmitUrl + "?", "");
    getMockery().checking(new Expectations() {

        {
            allowing(mockDocumentAccessBridge).getDocumentURL(with(aNonNull(DocumentReference.class)), with("view"), with(returnValue), with(aNull(String.class)));
            will(returnValue);
            allowing(mockDocumentAccessBridge).getDocumentURL(with(aNull(DocumentReference.class)), with("view"), with(aNull(String.class)), with(aNull(String.class)));
            will(returnValue(mockDocumentUrl));
            allowing(mockDocumentAccessBridge).getCurrentDocumentReference();
            will(returnValue(null));
        }
    });
    // configuration
    final CSRFTokenConfiguration mockConfiguration = getComponentManager().getInstance(CSRFTokenConfiguration.class);
    getMockery().checking(new Expectations() {

        {
            allowing(mockConfiguration).isEnabled();
            will(returnValue(true));
        }
    });
    // request
    final HttpSession mockSession = getMockery().mock(HttpSession.class);
    final HttpServletRequest httpRequest = getMockery().mock(HttpServletRequest.class);
    final ServletRequest servletRequest = new ServletRequest(httpRequest);
    getMockery().checking(new Expectations() {

        {
            allowing(httpRequest).getRequestURL();
            will(returnValue(new StringBuffer(mockDocumentUrl)));
            allowing(httpRequest).getRequestURI();
            will(returnValue(mockDocumentUrl));
            allowing(httpRequest).getParameterMap();
            will(returnValue(new HashMap<String, String[]>()));
            allowing(httpRequest).getSession();
            will(returnValue(mockSession));
        }
    });
    // session
    getMockery().checking(new Expectations() {

        {
            allowing(mockSession).getAttribute(with(any(String.class)));
            will(returnValue(new HashMap<String, Object>()));
        }
    });
    // container
    final Container mockContainer = getComponentManager().getInstance(Container.class);
    getMockery().checking(new Expectations() {

        {
            allowing(mockContainer).getRequest();
            will(returnValue(servletRequest));
        }
    });
    // logging
    getMockery().checking(new Expectations() {

        {
            // Ignore all calls to debug()
            ignoring(any(Logger.class)).method("debug");
        }
    });
    this.csrf = getComponentManager().getInstance(CSRFToken.class);
}
Also used : Expectations(org.jmock.Expectations) ServletRequest(org.xwiki.container.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) DefaultCSRFToken(org.xwiki.csrf.internal.DefaultCSRFToken) HttpSession(javax.servlet.http.HttpSession) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) Logger(org.slf4j.Logger) HttpServletRequest(javax.servlet.http.HttpServletRequest) Container(org.xwiki.container.Container) DocumentReference(org.xwiki.model.reference.DocumentReference) Before(org.junit.Before)

Example 4 with Container

use of org.xwiki.container.Container in project xwiki-platform by xwiki.

the class WebJarsResourceReferenceHandlerTest method configure.

@Before
public void configure() throws Exception {
    Container container = this.componentManager.getInstance(Container.class);
    this.response = mock(ServletResponse.class);
    ByteArrayOutputStream responseOutputStream = new ByteArrayOutputStream();
    when(this.response.getOutputStream()).thenReturn(responseOutputStream);
    HttpServletResponse httpResponse = mock(HttpServletResponse.class);
    when(this.response.getHttpServletResponse()).thenReturn(httpResponse);
    when(container.getResponse()).thenReturn(this.response);
    this.request = mock(ServletRequest.class);
    HttpServletRequest httpRequest = mock(HttpServletRequest.class);
    when(this.request.getHttpServletRequest()).thenReturn(httpRequest);
    when(container.getRequest()).thenReturn(this.request);
    this.handler = this.componentManager.getComponentUnderTest();
    this.classLoader = mock(NamespaceURLClassLoader.class);
    ClassLoaderManager clm = this.componentManager.getInstance(ClassLoaderManager.class);
    when(clm.getURLClassLoader("wiki:wiki", true)).thenReturn(this.classLoader);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletResponse(org.xwiki.container.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(org.xwiki.container.servlet.ServletRequest) Container(org.xwiki.container.Container) NamespaceURLClassLoader(org.xwiki.classloader.NamespaceURLClassLoader) HttpServletResponse(javax.servlet.http.HttpServletResponse) ClassLoaderManager(org.xwiki.classloader.ClassLoaderManager) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Before(org.junit.Before)

Example 5 with Container

use of org.xwiki.container.Container in project xwiki-platform by xwiki.

the class XWikiServletContextListener method contextDestroyed.

@Override
public void contextDestroyed(ServletContextEvent sce) {
    SHUTDOWN_LOGGER.debug("Stopping XWiki...");
    // It's possible that the Component Manager failed to initialize some of the required components.
    if (this.componentManager != null) {
        // to do something on stop to do it.
        try {
            ObservationManager observationManager = this.componentManager.getInstance(ObservationManager.class);
            observationManager.notify(new ApplicationStoppedEvent(), this);
        } catch (ComponentLookupException e) {
        // Nothing to do here.
        // TODO: Log a warning
        }
        // below in an Event Listener and move it to the legacy module.
        try {
            ApplicationContextListenerManager applicationContextListenerManager = this.componentManager.getInstance(ApplicationContextListenerManager.class);
            Container container = this.componentManager.getInstance(Container.class);
            applicationContextListenerManager.destroyApplicationContext(container.getApplicationContext());
        } catch (ComponentLookupException ex) {
        // Nothing to do here.
        // TODO: Log a warning
        }
        // Make sure to dispose all components before leaving
        this.componentManager.dispose();
    }
    SHUTDOWN_LOGGER.debug("XWiki has been stopped!");
}
Also used : ApplicationContextListenerManager(org.xwiki.container.ApplicationContextListenerManager) Container(org.xwiki.container.Container) ObservationManager(org.xwiki.observation.ObservationManager) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) ApplicationStoppedEvent(org.xwiki.observation.event.ApplicationStoppedEvent)

Aggregations

Container (org.xwiki.container.Container)8 Execution (org.xwiki.context.Execution)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Before (org.junit.Before)2 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)2 ServletRequest (org.xwiki.container.servlet.ServletRequest)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiException (com.xpn.xwiki.XWikiException)1 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Date (java.util.Date)1