Search in sources :

Example 61 with XWikiContext

use of com.xpn.xwiki.XWikiContext 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 62 with XWikiContext

use of com.xpn.xwiki.XWikiContext 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 63 with XWikiContext

use of com.xpn.xwiki.XWikiContext in project xwiki-platform by xwiki.

the class ExtensionXHTMLLinkTypeRenderer method beginLinkExtraAttributes.

@Override
protected void beginLinkExtraAttributes(ResourceReference reference, Map<String, String> spanAttributes, Map<String, String> anchorAttributes) {
    XWikiContext xcontext = this.xcontextProvider.get();
    String prefix = xcontext.getWiki().getWebAppPath(xcontext);
    if (prefix.isEmpty() || prefix.charAt(0) != '/') {
        prefix = '/' + prefix;
    }
    if (prefix.charAt(prefix.length() - 1) != '/') {
        prefix += '/';
    }
    // FIXME: need a way to get the rest URL prefix instead of hardcoding it here
    prefix += "rest";
    // Create an URI (because the API only produces URIs) with a stub context we will then remove since we actually
    // want a relative HREF
    UriBuilder builder = new UriBuilder("stubscheme:" + prefix, Resources.EXTENSION_VERSION_FILE);
    ExtensionResourceReference extensionReference = (ExtensionResourceReference) reference;
    if (extensionReference.getRepositoryId() != null) {
        builder.queryParam(ExtensionResourceReference.PARAM_REPOSITORYID, extensionReference.getRepositoryId());
    }
    if (extensionReference.getRepositoryType() != null) {
        builder.queryParam(ExtensionResourceReference.PARAM_REPOSITORYTYPE, extensionReference.getRepositoryType());
    }
    if (extensionReference.getRepositoryURI() != null) {
        builder.queryParam(ExtensionResourceReference.PARAM_REPOSITORYURI, extensionReference.getRepositoryURI());
    }
    URI uri = builder.build(extensionReference.getExtensionId(), extensionReference.getExtensionVersion());
    anchorAttributes.put(XHTMLLinkRenderer.HREF, uri.toString().substring("stubscheme://".length()));
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext) UriBuilder(org.xwiki.repository.UriBuilder) URI(java.net.URI) ExtensionResourceReference(org.xwiki.repository.internal.reference.ExtensionResourceReference)

Example 64 with XWikiContext

use of com.xpn.xwiki.XWikiContext in project xwiki-platform by xwiki.

the class FilesystemRecycleBinContentStore method save.

@Override
public void save(XWikiDocument document, long index, boolean bTransaction) throws XWikiException {
    final XWikiContext xcontext = this.xcontextProvider.get();
    final XWikiHibernateTransaction transaction = new XWikiHibernateTransaction(xcontext);
    final File contentFile = this.fileTools.getDeletedDocumentFileProvider(document.getDocumentReferenceWithLocale(), index).getDeletedDocumentContentFile();
    FileSerializer serializer = new DeletedDocumentContentFileSerializer(document, StandardCharsets.UTF_8.name());
    new FileSaveTransactionRunnable(contentFile, fileTools.getTempFile(contentFile), fileTools.getBackupFile(contentFile), fileTools.getLockForFile(contentFile), serializer).runIn(transaction);
    try {
        transaction.start();
    } catch (Exception e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SAVING_ATTACHMENT, "Exception while saving deleted document content.", e);
    }
}
Also used : FileSerializer(org.xwiki.store.FileSerializer) XWikiContext(com.xpn.xwiki.XWikiContext) FileSaveTransactionRunnable(org.xwiki.store.FileSaveTransactionRunnable) File(java.io.File) XWikiException(com.xpn.xwiki.XWikiException) XWikiException(com.xpn.xwiki.XWikiException)

Example 65 with XWikiContext

use of com.xpn.xwiki.XWikiContext in project xwiki-platform by xwiki.

the class DefaultWikiUserManager method getMembersGroupDocument.

private XWikiDocument getMembersGroupDocument(String wikiId) throws WikiUserManagerException {
    // Reference to the document
    DocumentReference memberGroupReference = new DocumentReference(wikiId, XWiki.SYSTEM_SPACE, "XWikiAllGroup");
    // Get the document
    try {
        XWikiContext xcontext = xcontextProvider.get();
        XWiki xwiki = xcontext.getWiki();
        return xwiki.getDocument(memberGroupReference, xcontext);
    } catch (XWikiException e) {
        throw new WikiUserManagerException(String.format("Fail to load the member group document [%s].", memberGroupReference.toString()), e);
    }
}
Also used : WikiUserManagerException(org.xwiki.wiki.user.WikiUserManagerException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

XWikiContext (com.xpn.xwiki.XWikiContext)564 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)203 XWikiException (com.xpn.xwiki.XWikiException)195 DocumentReference (org.xwiki.model.reference.DocumentReference)150 XWiki (com.xpn.xwiki.XWiki)106 BaseObject (com.xpn.xwiki.objects.BaseObject)104 Test (org.junit.Test)64 ArrayList (java.util.ArrayList)55 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)43 ExecutionContext (org.xwiki.context.ExecutionContext)43 Session (org.hibernate.Session)37 InitializationException (org.xwiki.component.phase.InitializationException)36 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)34 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)33 WikiReference (org.xwiki.model.reference.WikiReference)31 Before (org.junit.Before)29 EntityReference (org.xwiki.model.reference.EntityReference)28 QueryException (org.xwiki.query.QueryException)28 Execution (org.xwiki.context.Execution)27 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)24