Search in sources :

Example 1 with Response

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

the class BatikSVGRasterizerTest method rasterizeToResponseDoesNothingIfNotHttpResponse.

@Test
public void rasterizeToResponseDoesNothingIfNotHttpResponse() throws Exception {
    Response r = Mockito.mock(Response.class);
    when(this.container.getResponse()).thenReturn(r);
    this.mocker.getComponentUnderTest().rasterizeToResponse(VALID_SVG, 100, 200);
    Mockito.verifyZeroInteractions(r);
}
Also used : Response(org.xwiki.container.Response) ServletResponse(org.xwiki.container.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

Example 2 with Response

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

the class VfsResourceReferenceHandlerTest method setUp.

private void setUp(String scheme, String wikiName, String spaceName, String pageName, String attachmentName, List<String> path) throws Exception {
    Provider<ComponentManager> componentManagerProvider = this.mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
    when(componentManagerProvider.get()).thenReturn(this.mocker);
    String attachmentReferenceAsString = String.format("%s:%s:%s.%s@%s", scheme, wikiName, spaceName, pageName, attachmentName);
    this.reference = new VfsResourceReference(URI.create(attachmentReferenceAsString), path);
    ResourceReferenceSerializer<VfsResourceReference, URI> trueVfsResourceReferenceSerializer = this.mocker.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class, VfsResourceReference.class, URI.class), "truevfs");
    String truevfsURIFragment = String.format("%s://%s:%s.%s/%s/%s", scheme, wikiName, spaceName, pageName, attachmentName, StringUtils.join(path, '/'));
    when(trueVfsResourceReferenceSerializer.serialize(this.reference)).thenReturn(URI.create(truevfsURIFragment));
    Provider<XWikiContext> xwikiContextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
    XWikiContext xcontext = mock(XWikiContext.class);
    when(xwikiContextProvider.get()).thenReturn(xcontext);
    XWiki xwiki = mock(XWiki.class);
    when(xcontext.getWiki()).thenReturn(xwiki);
    DocumentReferenceResolver<String> documentReferenceResolver = mocker.registerMockComponent(DocumentReferenceResolver.TYPE_STRING);
    this.documentReference = new DocumentReference(wikiName, Arrays.asList(spaceName), pageName);
    String documentReferenceAsString = String.format("%s:%s.%s", wikiName, spaceName, pageName);
    when(documentReferenceResolver.resolve(documentReferenceAsString)).thenReturn(this.documentReference);
    XWikiDocument document = mock(XWikiDocument.class);
    when(xwiki.getDocument(this.documentReference, xcontext)).thenReturn(document);
    XWikiAttachment attachment = mock(XWikiAttachment.class);
    when(document.getAttachment(attachmentName)).thenReturn(attachment);
    when(attachment.getDate()).thenReturn(new Date());
    when(attachment.getContentSize(xcontext)).thenReturn(1000);
    when(attachment.getContentInputStream(xcontext)).thenReturn(createZipInputStream(StringUtils.join(path, '/'), "success!"));
    Container container = this.mocker.getInstance(Container.class);
    Response response = mock(Response.class);
    when(container.getResponse()).thenReturn(response);
    this.baos = new ByteArrayOutputStream();
    when(response.getOutputStream()).thenReturn(this.baos);
    // Register our custom Attach Driver in TrueVFS
    TConfig config = TConfig.current();
    // Note: Make sure we add our own Archive Detector to the existing Detector so that all archive formats
    // supported by TrueVFS are handled properly.
    config.setArchiveDetector(new TArchiveDetector(config.getArchiveDetector(), "attach", new AttachDriver(this.mocker)));
}
Also used : AttachDriver(org.xwiki.vfs.internal.attach.AttachDriver) ResourceReferenceSerializer(org.xwiki.resource.ResourceReferenceSerializer) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URI(java.net.URI) Date(java.util.Date) Provider(javax.inject.Provider) Response(org.xwiki.container.Response) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Container(org.xwiki.container.Container) ComponentManager(org.xwiki.component.manager.ComponentManager) TConfig(net.java.truevfs.access.TConfig) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) TArchiveDetector(net.java.truevfs.access.TArchiveDetector) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 3 with Response

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

the class AbstractContentResourceReferenceHandler method serveResource.

protected void serveResource(String resourceName, InputStream resourceStream) throws ResourceReferenceHandlerException {
    // Make sure the resource stream supports mark & reset which is needed in order be able to detect the
    // content type without affecting the stream (Tika may need to read a few bytes from the start of the
    // stream, in which case it will mark & reset the stream).
    // 
    // Note that even though the stream returned by TrueVFS returns true for markSupported() in practice it
    // doesn't! Thus we need to wrap the stream to make it support mark and reset.
    InputStream markResetSupportingStream = new BufferedInputStream(resourceStream);
    try {
        Response response = this.container.getResponse();
        response.setContentType(TikaUtils.detect(markResetSupportingStream, resourceName));
        IOUtils.copy(markResetSupportingStream, response.getOutputStream());
    } catch (Exception e) {
        throw new ResourceReferenceHandlerException(String.format("Failed to read resource [%s]", resourceName), e);
    } finally {
        IOUtils.closeQuietly(markResetSupportingStream);
    }
}
Also used : Response(org.xwiki.container.Response) ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException)

Example 4 with Response

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

the class DefaultContainer method getResponse.

@Override
public Response getResponse() {
    Response result = null;
    Stack<Response> responses = this.response.get();
    if (responses != null) {
        result = responses.peek();
    }
    return result;
}
Also used : Response(org.xwiki.container.Response)

Example 5 with Response

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

the class AbstractTemplateJobResourceReferenceHandler method tryTemplate.

protected boolean tryTemplate(String defaultContentType, String templateName) throws ResourceReferenceHandlerException {
    Template template = this.templates.getTemplate("job/" + templateName);
    if (template == null) {
        return false;
    }
    Response response = this.container.getResponse();
    try {
        // Set default content type (can be overwritten by the template itself)
        if (defaultContentType != null) {
            response.setContentType(defaultContentType);
        }
        Writer writer = new StringWriter();
        this.templates.render(template, writer);
        sendContent(writer.toString());
    } catch (Exception e) {
        throw new ResourceReferenceHandlerException("Failed to execute template [" + templateName + "]", e);
    }
    return true;
}
Also used : Response(org.xwiki.container.Response) ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) StringWriter(java.io.StringWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) IOException(java.io.IOException) ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) Template(org.xwiki.template.Template)

Aggregations

Response (org.xwiki.container.Response)6 ResourceReferenceHandlerException (org.xwiki.resource.ResourceReferenceHandlerException)3 BufferedInputStream (java.io.BufferedInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 ServletResponse (org.xwiki.container.servlet.ServletResponse)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 URI (java.net.URI)1 Date (java.util.Date)1 Provider (javax.inject.Provider)1 TArchiveDetector (net.java.truevfs.access.TArchiveDetector)1 TConfig (net.java.truevfs.access.TConfig)1 Test (org.junit.Test)1