Search in sources :

Example 46 with XWikiException

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

the class ModelFactory method fillObjectSummary.

private void fillObjectSummary(ObjectSummary objectSummary, Document doc, BaseObject xwikiObject, Boolean withPrettyNames) {
    objectSummary.setId(String.format("%s:%s", doc.getPrefixedFullName(), xwikiObject.getGuid()));
    objectSummary.setGuid(xwikiObject.getGuid());
    objectSummary.setPageId(doc.getPrefixedFullName());
    objectSummary.setPageVersion(doc.getVersion());
    objectSummary.setPageAuthor(doc.getAuthor());
    if (withPrettyNames) {
        XWikiContext xwikiContext = this.xcontextProvider.get();
        objectSummary.setPageAuthorName(xwikiContext.getWiki().getUserName(doc.getAuthor(), null, false, xwikiContext));
    }
    objectSummary.setWiki(doc.getWiki());
    objectSummary.setSpace(doc.getSpace());
    objectSummary.setPageName(doc.getName());
    objectSummary.setClassName(xwikiObject.getClassName());
    objectSummary.setNumber(xwikiObject.getNumber());
    String[] propertyNames = xwikiObject.getPropertyNames();
    if (propertyNames.length > 0) {
        try {
            objectSummary.setHeadline(serializePropertyValue(xwikiObject.get(propertyNames[0])));
        } catch (XWikiException e) {
        // Should never happen
        }
    }
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException)

Example 47 with XWikiException

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

the class XWikiAuthentication method authenticate.

@Override
public boolean authenticate(Request request, Response response) {
    /*
         * Browser authentication resource is a special resource that allows to trigger the authentication dialog box in
         * web browsers
         */
    if (request.getResourceRef().getPath().endsWith(BrowserAuthenticationResource.URI_PATTERN)) {
        return super.authenticate(request, response);
    }
    ComponentManager componentManager = (ComponentManager) getContext().getAttributes().get(Constants.XWIKI_COMPONENT_MANAGER);
    XWikiContext xwikiContext = Utils.getXWikiContext(componentManager);
    XWiki xwiki = Utils.getXWiki(componentManager);
    DocumentReferenceResolver<String> resolver;
    EntityReferenceSerializer<String> serializer;
    try {
        resolver = componentManager.getInstance(DocumentReferenceResolver.TYPE_STRING, "current");
        serializer = componentManager.getInstance(EntityReferenceSerializer.TYPE_STRING);
    } catch (ComponentLookupException e1) {
        return false;
    }
    /* By default set XWiki.Guest as the user that is sending the request. */
    xwikiContext.setUserReference(null);
    /*
         * After performing the authentication we should add headers to the response to allow applications to verify if
         * the authentication is still valid We are also adding the XWiki version at the same moment.
         */
    Series<Header> responseHeaders = (Series<Header>) response.getAttributes().get(HeaderConstants.ATTRIBUTE_HEADERS);
    if (responseHeaders == null) {
        responseHeaders = new Series<>(Header.class);
        response.getAttributes().put(HeaderConstants.ATTRIBUTE_HEADERS, responseHeaders);
    }
    responseHeaders.add("XWiki-User", serializer.serialize(xwikiContext.getUserReference()));
    responseHeaders.add("XWiki-Version", xwikiContext.getWiki().getVersion());
    // Try with standard XWiki auth
    try {
        XWikiUser xwikiUser = xwiki.checkAuth(xwikiContext);
        if (xwikiUser != null) {
            // Make sure the user is in the context
            xwikiContext.setUserReference(resolver.resolve(xwikiUser.getUser()));
            getLogger().fine(String.format("Authenticated as '%s'.", xwikiUser.getUser()));
            // the user has changed so we need to reset the header
            responseHeaders.set("XWiki-User", serializer.serialize(xwikiContext.getUserReference()));
            return true;
        }
    } catch (XWikiException e) {
        getLogger().log(Level.WARNING, "Exception occurred while authenticating.", e);
    }
    // Falback on restlet auth
    return super.authenticate(request, response);
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) Series(org.restlet.util.Series) XWikiUser(com.xpn.xwiki.user.api.XWikiUser) Header(org.restlet.data.Header) ComponentManager(org.xwiki.component.manager.ComponentManager) XWikiException(com.xpn.xwiki.XWikiException)

Example 48 with XWikiException

use of com.xpn.xwiki.XWikiException 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 49 with XWikiException

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

the class AbstractExtensionRESTResource method getExtensionRating.

protected ExtensionRating getExtensionRating(DocumentReference extensionDocumentReference) {
    ExtensionRating extensionRating = this.extensionObjectFactory.createExtensionRating();
    try {
        AverageRatingApi averageRating = new AverageRatingApi(ratingsManager.getAverageRating(extensionDocumentReference));
        extensionRating.setTotalVotes(averageRating.getNbVotes());
        extensionRating.setAverageVote(averageRating.getAverageVote());
    } catch (XWikiException e) {
        extensionRating.setTotalVotes(0);
        extensionRating.setAverageVote(0);
    }
    return extensionRating;
}
Also used : ExtensionRating(org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionRating) AverageRatingApi(org.xwiki.ratings.AverageRatingApi) XWikiException(com.xpn.xwiki.XWikiException)

Example 50 with XWikiException

use of com.xpn.xwiki.XWikiException 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)

Aggregations

XWikiException (com.xpn.xwiki.XWikiException)442 XWikiContext (com.xpn.xwiki.XWikiContext)156 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)147 DocumentReference (org.xwiki.model.reference.DocumentReference)98 BaseObject (com.xpn.xwiki.objects.BaseObject)88 IOException (java.io.IOException)57 QueryException (org.xwiki.query.QueryException)57 ArrayList (java.util.ArrayList)56 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)51 XWiki (com.xpn.xwiki.XWiki)48 XWikiRestException (org.xwiki.rest.XWikiRestException)44 Session (org.hibernate.Session)42 Document (com.xpn.xwiki.api.Document)38 InitializationException (org.xwiki.component.phase.InitializationException)36 WebApplicationException (javax.ws.rs.WebApplicationException)32 SQLException (java.sql.SQLException)31 ObjectNotFoundException (org.hibernate.ObjectNotFoundException)30 MigrationRequiredException (com.xpn.xwiki.store.migration.MigrationRequiredException)29 UnexpectedException (org.xwiki.store.UnexpectedException)29 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)25