Search in sources :

Example 71 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.

the class AbstractExtensionRESTResource method resolveExtensionAuthor.

protected ExtensionAuthor resolveExtensionAuthor(String authorId) {
    ExtensionAuthor author = new ExtensionAuthor();
    XWikiContext xcontext = getXWikiContext();
    XWikiDocument document;
    try {
        document = xcontext.getWiki().getDocument(authorId, xcontext);
    } catch (XWikiException e) {
        document = null;
    }
    if (document != null && !document.isNew()) {
        author.setName(xcontext.getWiki().getPlainUserName(document.getDocumentReference(), xcontext));
        author.setUrl(document.getExternalURL("view", xcontext));
    } else {
        author.setName(authorId);
    }
    return author;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) ExtensionAuthor(org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionAuthor)

Example 72 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument 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 73 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.

the class ExtensionVersionRESTResource method getExtensionVersion.

@GET
public ExtensionVersion getExtensionVersion(@PathParam("extensionId") String extensionId, @PathParam("extensionVersion") String extensionVersion) throws XWikiException, QueryException {
    XWikiDocument extensionDocument = getExistingExtensionDocumentById(extensionId);
    checkRights(extensionDocument);
    return createExtension(extensionDocument, extensionVersion);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) GET(javax.ws.rs.GET)

Example 74 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.

the class FilesystemAttachmentStoreTest method documentUpdateOnDeleteTest.

@Test
public void documentUpdateOnDeleteTest() throws Exception {
    final List<XWikiAttachment> attachList = new ArrayList<XWikiAttachment>();
    attachList.add(this.mockAttach);
    this.doc.setAttachmentList(attachList);
    getMockery().checking(new Expectations() {

        {
            oneOf(mockAttachVersionStore).deleteArchive(mockAttach, mockContext, false);
            exactly(2).of(mockHibernateSession).delete(with(any(Object.class)));
            oneOf(mockHibernate).saveXWikiDoc(doc, mockContext, false);
            will(new CustomAction("Make sure the attachment has been removed from the list.") {

                public Object invoke(final Invocation invoc) {
                    final XWikiDocument document = (XWikiDocument) invoc.getParameter(0);
                    Assert.assertTrue("Attachment was not removed from the list.", document.getAttachmentList().size() == 0);
                    return null;
                }
            });
        }
    });
    this.createFile();
    this.attachStore.deleteXWikiAttachment(this.mockAttach, true, this.mockContext, false);
}
Also used : Expectations(org.jmock.Expectations) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) ArrayList(java.util.ArrayList) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Test(org.junit.Test)

Example 75 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.

the class FilesystemAttachmentVersioningStoreTest method setUp.

@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    Utils.setComponentManager(this.getComponentManager());
    final File tmpDir = new File(System.getProperty("java.io.tmpdir"));
    this.storageLocation = new File(tmpDir, "test-storage-location");
    this.fileTools = new FilesystemStoreTools(new FileStringEntityReferenceSerializer(), storageLocation, new DummyLockProvider());
    final AttachmentListMetadataSerializer serializer = new AttachmentListMetadataSerializer(new AttachmentMetadataSerializer());
    this.versionStore = new FilesystemAttachmentVersioningStore();
    FieldUtils.writeDeclaredField(this.versionStore, "fileTools", this.fileTools, true);
    FieldUtils.writeDeclaredField(this.versionStore, "metaSerializer", serializer, true);
    final XWikiDocument doc = new XWikiDocument(new DocumentReference("xwiki", "Main", "WebHome"));
    final XWikiAttachment version1 = new XWikiAttachment();
    version1.setVersion("1.1");
    version1.setFilename("attachment.txt");
    version1.setDoc(doc);
    version1.setAttachment_content(new StringAttachmentContent("I am version 1.1"));
    final XWikiAttachment version2 = new XWikiAttachment();
    version2.setVersion("1.2");
    version2.setFilename("attachment.txt");
    version2.setDoc(doc);
    version2.setAttachment_content(new StringAttachmentContent("I am version 1.2"));
    final XWikiAttachment version3 = new XWikiAttachment();
    version3.setVersion("1.3");
    version3.setFilename("attachment.txt");
    version3.setDoc(doc);
    version3.setAttachment_content(new StringAttachmentContent("I am version 1.3"));
    this.provider = this.fileTools.getAttachmentFileProvider(version1.getReference());
    this.archive = new ListAttachmentArchive(new ArrayList<XWikiAttachment>() {

        {
            add(version1);
            add(version2);
            add(version3);
        }
    });
}
Also used : FileStringEntityReferenceSerializer(org.xwiki.store.internal.FileStringEntityReferenceSerializer) FilesystemStoreTools(org.xwiki.store.filesystem.internal.FilesystemStoreTools) ArrayList(java.util.ArrayList) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) AttachmentListMetadataSerializer(org.xwiki.store.serialization.xml.internal.AttachmentListMetadataSerializer) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DummyLockProvider(org.xwiki.store.locks.dummy.internal.DummyLockProvider) File(java.io.File) DocumentReference(org.xwiki.model.reference.DocumentReference) ListAttachmentArchive(org.xwiki.store.legacy.doc.internal.ListAttachmentArchive) AttachmentMetadataSerializer(org.xwiki.store.serialization.xml.internal.AttachmentMetadataSerializer) Before(org.junit.Before)

Aggregations

XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)869 DocumentReference (org.xwiki.model.reference.DocumentReference)469 BaseObject (com.xpn.xwiki.objects.BaseObject)318 Test (org.junit.Test)284 XWikiContext (com.xpn.xwiki.XWikiContext)232 XWikiException (com.xpn.xwiki.XWikiException)178 ArrayList (java.util.ArrayList)99 XWiki (com.xpn.xwiki.XWiki)97 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)86 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)71 Document (com.xpn.xwiki.api.Document)48 EntityReference (org.xwiki.model.reference.EntityReference)48 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)41 Date (java.util.Date)41 IOException (java.io.IOException)40 HashMap (java.util.HashMap)33 QueryException (org.xwiki.query.QueryException)27 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)25 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)23 IncludeServletAsString (com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)23