Search in sources :

Example 1 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class WikiSkinUtils method getSkinResourceFromDocumentSkin.

private Resource<?> getSkinResourceFromDocumentSkin(String resource, XWikiDocument skinDocument, Skin skin) {
    if (skinDocument != null) {
        // Try to find a XWikiSkinFileOverrideClass object
        BaseObject obj = skinDocument.getXObject(XWikiSkinFileOverrideClassDocumentInitializer.DOCUMENT_REFERENCE, XWikiSkinFileOverrideClassDocumentInitializer.PROPERTY_PATH, resource, false);
        if (obj != null) {
            ObjectPropertyReference reference = new ObjectPropertyReference(XWikiSkinFileOverrideClassDocumentInitializer.PROPERTY_CONTENT, obj.getReference());
            return new ObjectPropertyWikiResource(getPath(reference), skin, reference, skinDocument.getAuthorReference(), this.xcontextProvider, obj.getLargeStringValue(XWikiSkinFileOverrideClassDocumentInitializer.PROPERTY_CONTENT));
        }
        // Try parsing the object property
        BaseProperty<ObjectPropertyReference> property = getSkinResourceProperty(resource, skinDocument);
        if (property != null) {
            ObjectPropertyReference reference = property.getReference();
            return new ObjectPropertyWikiResource(getPath(reference), skin, reference, skinDocument.getAuthorReference(), this.xcontextProvider, (String) property.getValue());
        }
        // Try parsing a document attachment
        // Convert "/" into "." in order to support wiki skin attachments to override some resources located in
        // subdirectories.
        String normalizedResourceName = StringUtils.replaceChars(resource, '/', '.');
        XWikiAttachment attachment = skinDocument.getAttachment(normalizedResourceName);
        if (attachment != null) {
            AttachmentReference reference = attachment.getReference();
            return new AttachmentWikiResource(getPath(reference), skin, reference, attachment.getAuthorReference(), this.xcontextProvider);
        }
    }
    return null;
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ObjectPropertyReference(org.xwiki.model.reference.ObjectPropertyReference) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 2 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class ExtensionVersionFileRESTResource method downloadLocalExtension.

private ResponseBuilder downloadLocalExtension(String extensionId, String extensionVersion) throws ResolveException, IOException, QueryException, XWikiException {
    XWikiDocument extensionDocument = getExistingExtensionDocumentById(extensionId);
    checkRights(extensionDocument);
    ResourceReference resourceReference = repositoryManager.getDownloadReference(extensionDocument, extensionVersion);
    ResponseBuilder response = null;
    if (ResourceType.ATTACHMENT.equals(resourceReference.getType())) {
        // It's an attachment
        AttachmentReference attachmentReference = this.attachmentResolver.resolve(resourceReference.getReference(), extensionDocument.getDocumentReference());
        XWikiContext xcontext = getXWikiContext();
        XWikiDocument document = xcontext.getWiki().getDocument(attachmentReference.getDocumentReference(), xcontext);
        checkRights(document);
        XWikiAttachment xwikiAttachment = document.getAttachment(attachmentReference.getName());
        response = getAttachmentResponse(xwikiAttachment);
    } else if (ResourceType.URL.equals(resourceReference.getType())) {
        // It's an URL
        URL url = new URL(resourceReference.getReference());
        DefaultHttpClient httpClient = new DefaultHttpClient();
        httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "XWikiExtensionRepository");
        httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
        httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
        ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(httpClient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault());
        httpClient.setRoutePlanner(routePlanner);
        HttpGet getMethod = new HttpGet(url.toString());
        HttpResponse subResponse;
        try {
            subResponse = httpClient.execute(getMethod);
        } catch (Exception e) {
            throw new IOException("Failed to request [" + getMethod.getURI() + "]", e);
        }
        response = Response.status(subResponse.getStatusLine().getStatusCode());
        // TODO: find a proper way to do a perfect proxy of the URL without directly using Restlet classes.
        // Should probably use javax.ws.rs.ext.MessageBodyWriter
        HttpEntity entity = subResponse.getEntity();
        InputRepresentation content = new InputRepresentation(entity.getContent(), entity.getContentType() != null ? new MediaType(entity.getContentType().getValue()) : MediaType.APPLICATION_OCTET_STREAM, entity.getContentLength());
        BaseObject extensionObject = getExtensionObject(extensionDocument);
        String type = getValue(extensionObject, XWikiRepositoryModel.PROP_EXTENSION_TYPE);
        Disposition disposition = new Disposition(Disposition.TYPE_ATTACHMENT);
        disposition.setFilename(extensionId + '-' + extensionVersion + '.' + type);
        content.setDisposition(disposition);
        response.entity(content);
    } else if (ExtensionResourceReference.TYPE.equals(resourceReference.getType())) {
        ExtensionResourceReference extensionResource;
        if (resourceReference instanceof ExtensionResourceReference) {
            extensionResource = (ExtensionResourceReference) resourceReference;
        } else {
            extensionResource = new ExtensionResourceReference(resourceReference.getReference());
        }
        response = downloadRemoteExtension(extensionResource);
    } else {
        throw new WebApplicationException(Status.NOT_FOUND);
    }
    return response;
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) HttpEntity(org.apache.http.HttpEntity) InputRepresentation(org.restlet.representation.InputRepresentation) WebApplicationException(javax.ws.rs.WebApplicationException) HttpGet(org.apache.http.client.methods.HttpGet) XWikiContext(com.xpn.xwiki.XWikiContext) HttpResponse(org.apache.http.HttpResponse) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) IOException(java.io.IOException) URL(java.net.URL) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) XWikiException(com.xpn.xwiki.XWikiException) URISyntaxException(java.net.URISyntaxException) WebApplicationException(javax.ws.rs.WebApplicationException) QueryException(org.xwiki.query.QueryException) IOException(java.io.IOException) ResolveException(org.xwiki.extension.ResolveException) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ProxySelectorRoutePlanner(org.apache.http.impl.conn.ProxySelectorRoutePlanner) Disposition(org.restlet.data.Disposition) MediaType(org.restlet.data.MediaType) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) ExtensionResourceReference(org.xwiki.repository.internal.reference.ExtensionResourceReference) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) ExtensionResourceReference(org.xwiki.repository.internal.reference.ExtensionResourceReference)

Example 3 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class AbstractXWIKI14697DataMigration method setStore.

private void setStore(List<Object[]> attachments, Session session) {
    WikiReference wikiReference = getXWikiContext().getWikiReference();
    List<Long> fileAttachments = new ArrayList<>(attachments.size());
    List<Long> configuredAttachments = new ArrayList<>(attachments.size());
    for (Object[] attachment : attachments) {
        Long id = (Long) attachment[0];
        String filename = (String) attachment[1];
        String fullName = (String) attachment[2];
        DocumentReference documentReference = this.resolver.resolve(fullName, wikiReference);
        AttachmentReference attachmentReference = new AttachmentReference(filename, documentReference);
        if (isFile(attachmentReference)) {
            fileAttachments.add(id);
        } else {
            configuredAttachments.add(id);
        }
    }
    // Set file store
    setStore(session, fileAttachments, FileSystemStoreUtils.HINT);
    // Set configured store
    if (!configuredAttachments.isEmpty()) {
        String configuredStore = this.configuration.getProperty("xwiki.store.attachment.hint");
        if (configuredStore != null) {
            setStore(session, configuredAttachments, configuredStore);
        } else {
            this.logger.warn("The following attachment with the following ids have unknown store: ", configuredAttachments);
        }
    }
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ArrayList(java.util.ArrayList) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 4 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class OfficeAttachmentImporter method convertAttachmentContent.

/**
 * Converts the content of the specified office file to wiki syntax.
 *
 * @param attachmentReference specifies the office file whose content should be converted
 * @param filterStyles controls whether styles are filtered when converting the HTML produced by the office server
 *            to wiki syntax
 * @return the annotated XHTML text obtained from the specified office document
 * @throws Exception if converting the content of the specified attachment fails
 */
private String convertAttachmentContent(AttachmentReference attachmentReference, boolean filterStyles) throws Exception {
    InputStream officeFileStream = documentAccessBridge.getAttachmentContent(attachmentReference);
    String officeFileName = attachmentReference.getName();
    DocumentReference targetDocRef = attachmentReference.getDocumentReference();
    XDOMOfficeDocument xdomOfficeDocument;
    if (isPresentation(attachmentReference.getName())) {
        xdomOfficeDocument = presentationBuilder.build(officeFileStream, officeFileName, targetDocRef);
    } else {
        xdomOfficeDocument = documentBuilder.build(officeFileStream, officeFileName, targetDocRef, filterStyles);
    }
    // Attach the images extracted from the imported office document to the target wiki document.
    for (Map.Entry<String, byte[]> artifact : xdomOfficeDocument.getArtifacts().entrySet()) {
        AttachmentReference artifactReference = new AttachmentReference(artifact.getKey(), targetDocRef);
        documentAccessBridge.setAttachmentContent(artifactReference, artifact.getValue());
    }
    return xdomOfficeDocument.getContentAsString("annotatedxhtml/1.0");
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) InputStream(java.io.InputStream) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument)

Example 5 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class URIClassLoaderTest method testFindResource.

/**
 * Verify that resource located in a URI with an attachmentjar protocol can be found.
 */
@Test
public void testFindResource() throws Exception {
    URLStreamHandlerFactory urlStreamHandlerFactory = getComponentManager().getInstance(URLStreamHandlerFactory.class);
    URIClassLoader cl = new URIClassLoader(new URI[] { new URI("attachmentjar://page%40filename1"), new URI("http://some/url"), new URI("attachmentjar://filename2") }, urlStreamHandlerFactory);
    Assert.assertEquals(3, cl.getURLs().length);
    Assert.assertEquals("attachmentjar://page%40filename1", cl.getURLs()[0].toString());
    Assert.assertEquals("http://some/url", cl.getURLs()[1].toString());
    Assert.assertEquals("attachmentjar://filename2", cl.getURLs()[2].toString());
    final AttachmentReference attachmentName1 = new AttachmentReference("filename1", new DocumentReference("wiki", "space", "page"));
    final AttachmentReference attachmentName2 = new AttachmentReference("filename2", new DocumentReference("wiki", "space", "page"));
    getMockery().checking(new Expectations() {

        {
            allowing(URIClassLoaderTest.this.arf).resolve("page@filename1");
            will(returnValue(attachmentName1));
            oneOf(URIClassLoaderTest.this.dab).getAttachmentContent(attachmentName1);
            will(returnValue(new ByteArrayInputStream(createJarFile("/nomatch"))));
            allowing(URIClassLoaderTest.this.arf).resolve("filename2");
            will(returnValue(attachmentName2));
            oneOf(URIClassLoaderTest.this.dab).getAttachmentContent(attachmentName2);
            will(returnValue(new ByteArrayInputStream(createJarFile("/something"))));
        }
    });
    Assert.assertEquals("jar:attachmentjar://filename2!/something", cl.findResource("/something").toString());
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) Expectations(org.jmock.Expectations) ByteArrayInputStream(java.io.ByteArrayInputStream) URLStreamHandlerFactory(java.net.URLStreamHandlerFactory) URI(java.net.URI) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

AttachmentReference (org.xwiki.model.reference.AttachmentReference)64 DocumentReference (org.xwiki.model.reference.DocumentReference)44 Test (org.junit.Test)31 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)17 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)14 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)9 XWikiContext (com.xpn.xwiki.XWikiContext)8 EntityReference (org.xwiki.model.reference.EntityReference)7 Expectations (org.jmock.Expectations)6 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)4 XWikiException (com.xpn.xwiki.XWikiException)3 InputStream (java.io.InputStream)3 URL (java.net.URL)3 Mockery (org.jmock.Mockery)3 JUnit4Mockery (org.jmock.integration.junit4.JUnit4Mockery)3 XDOMOfficeDocument (org.xwiki.officeimporter.document.XDOMOfficeDocument)3 ImageBlock (org.xwiki.rendering.block.ImageBlock)3