Search in sources :

Example 1 with Abdera

use of org.apache.abdera.Abdera in project dataverse by IQSS.

the class CollectionListManagerImpl method listCollectionContents.

@Override
public Feed listCollectionContents(IRI iri, AuthCredentials authCredentials, SwordConfiguration swordConfiguration) throws SwordServerException, SwordAuthException, SwordError {
    AuthenticatedUser user = swordAuth.auth(authCredentials);
    DataverseRequest dvReq = new DataverseRequest(user, request);
    urlManager.processUrl(iri.toString());
    String dvAlias = urlManager.getTargetIdentifier();
    if (urlManager.getTargetType().equals("dataverse") && dvAlias != null) {
        Dataverse dv = dataverseService.findByAlias(dvAlias);
        if (dv != null) {
            /**
             * We'll say having AddDataset is enough to use this API
             * endpoint, which means you are a Contributor to that
             * dataverse. If we let just anyone call this endpoint, they
             * will be able to see if the supplied dataverse is published or
             * not.
             */
            if (!permissionService.requestOn(dvReq, dv).has(Permission.AddDataset)) {
                throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "user " + user.getDisplayInfo().getTitle() + " is not authorized to list datasets in dataverse " + dv.getAlias());
            }
            Abdera abdera = new Abdera();
            Feed feed = abdera.newFeed();
            feed.setTitle(dv.getName());
            String baseUrl = urlManager.getHostnamePlusBaseUrlPath(iri.toString());
            List<Dataset> datasets = datasetService.findByOwnerId(dv.getId());
            for (Dataset dataset : datasets) {
                /**
                 * @todo Will this be performant enough with production
                 * data, say in the root dataverse? Remove this todo if
                 * there are no complaints. :)
                 */
                if (!permissionService.isUserAllowedOn(user, new UpdateDatasetCommand(dataset, dvReq), dataset)) {
                    continue;
                }
                String editUri = baseUrl + "/edit/study/" + dataset.getGlobalId();
                String editMediaUri = baseUrl + "/edit-media/study/" + dataset.getGlobalId();
                Entry entry = feed.addEntry();
                entry.setId(editUri);
                entry.setTitle(datasetService.getTitleFromLatestVersion(dataset.getId()));
                entry.setBaseUri(new IRI(editUri));
                entry.addLink(editMediaUri, "edit-media");
                feed.addEntry(entry);
            }
            Boolean dvHasBeenReleased = dv.isReleased();
            feed.addSimpleExtension(new QName(UriRegistry.SWORD_STATE, "dataverseHasBeenReleased"), dvHasBeenReleased.toString());
            return feed;
        } else {
            throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Could not find dataverse: " + dvAlias);
        }
    } else {
        throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Couldn't determine target type or identifer from URL: " + iri);
    }
}
Also used : IRI(org.apache.abdera.i18n.iri.IRI) SwordError(org.swordapp.server.SwordError) Dataset(edu.harvard.iq.dataverse.Dataset) QName(javax.xml.namespace.QName) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) Dataverse(edu.harvard.iq.dataverse.Dataverse) Abdera(org.apache.abdera.Abdera) DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) Entry(org.apache.abdera.model.Entry) Feed(org.apache.abdera.model.Feed) UpdateDatasetCommand(edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetCommand)

Example 2 with Abdera

use of org.apache.abdera.Abdera in project jaggery by wso2.

the class EntryHostObject method jsConstructor.

/**
 * Constructor the user will be using inside javaScript
 */
public static Scriptable jsConstructor(Context cx, Object[] args, Function ctorObj, boolean inNewExpr) {
    EntryHostObject entryHO = new EntryHostObject();
    entryHO.abdera = new Abdera();
    Factory factory = entryHO.abdera.getFactory();
    entryHO.entry = factory.newEntry();
    entryHO.context = cx;
    return entryHO;
}
Also used : Factory(org.apache.abdera.factory.Factory) LogFactory(org.apache.commons.logging.LogFactory) Abdera(org.apache.abdera.Abdera)

Example 3 with Abdera

use of org.apache.abdera.Abdera in project jaggery by wso2.

the class FeedHostObject method addEntry.

public void addEntry(Object entryObject) throws ScriptException {
    abdera = new Abdera();
    Factory factory = abdera.getFactory();
    entry = factory.newEntry();
    if (entryObject instanceof EntryHostObject) {
        EntryHostObject entryHostObject = (EntryHostObject) entryObject;
        entry = entryHostObject.getEntry();
        feed.addEntry(entry);
    } else if (entryObject instanceof NativeObject) {
        try {
            NativeObject nativeObject = (NativeObject) entryObject;
            ScriptableObject scriptableObject = (ScriptableObject) nativeObject;
            // author and authors processing
            Object authorProperty = ScriptableObject.getProperty(nativeObject, "author");
            if (authorProperty instanceof String) {
                entry.addAuthor((String) (authorProperty));
            }
            Object authorsProperty = ScriptableObject.getProperty(nativeObject, "authors");
            if (authorsProperty instanceof NativeArray) {
                NativeArray authorsPropertyArray = (NativeArray) authorsProperty;
                for (Object o1 : authorsPropertyArray.getIds()) {
                    int indexx = (Integer) o1;
                    String name = authorsPropertyArray.get(indexx, null).toString();
                    entry.addAuthor(name);
                }
            }
            // processing category
            Object categoryProperty = ScriptableObject.getProperty(nativeObject, "category");
            if (categoryProperty instanceof String) {
                entry.addCategory((String) (categoryProperty));
            }
            Object categoriesProperty = ScriptableObject.getProperty(nativeObject, "categories");
            if (categoriesProperty instanceof NativeArray) {
                NativeArray categoriesPropertyArray = (NativeArray) categoriesProperty;
                for (Object o1 : categoriesPropertyArray.getIds()) {
                    int indexC = (Integer) o1;
                    String name = categoriesPropertyArray.get(indexC, null).toString();
                    entry.addCategory(name);
                }
            }
            // process content
            Object content = ScriptableObject.getProperty(nativeObject, "content");
            if (content instanceof XMLObject) {
                entry.setContentAsXhtml(content.toString());
            } else if (content instanceof String) {
                entry.setContent(content.toString());
            } else {
                throw new ScriptException("Unsupported Content");
            }
            // process contributor
            Object contributorProperty = ScriptableObject.getProperty(nativeObject, "contributor");
            if (contributorProperty instanceof String) {
                entry.addContributor(contributorProperty.toString());
            }
            Object contributorsProperty = ScriptableObject.getProperty(nativeObject, "contributors");
            if (contributorsProperty instanceof NativeArray) {
                NativeArray contributorsPropertyArray = (NativeArray) contributorsProperty;
                for (Object o1 : contributorsPropertyArray.getIds()) {
                    int index = (Integer) o1;
                    String name = contributorsPropertyArray.get(index, null).toString();
                    entry.addContributor(name);
                }
            }
            // process id
            Object idProperty = ScriptableObject.getProperty(nativeObject, "id");
            if (idProperty instanceof String) {
                entry.setId((String) (idProperty));
            } else {
                entry.setId(FOMHelper.generateUuid());
            }
            // process link
            // TODO link object
            Object linkProperty = ScriptableObject.getProperty(nativeObject, "link");
            if (linkProperty instanceof String) {
                entry.addLink((String) (linkProperty));
            }
            Object linksProperty = ScriptableObject.getProperty(nativeObject, "links");
            if (linksProperty instanceof NativeArray) {
                NativeArray linksPropertyArray = (NativeArray) contributorsProperty;
                for (Object o1 : linksPropertyArray.getIds()) {
                    int index = (Integer) o1;
                    String name = linksPropertyArray.get(index, null).toString();
                    entry.addLink(name);
                }
            }
            // process published
            // TODO handle javascript date
            Object publishedProperty = ScriptableObject.getProperty(nativeObject, "published");
            if (publishedProperty instanceof String) {
                if (publishedProperty.equals("now")) {
                    entry.setPublished(new Date(System.currentTimeMillis()));
                } else {
                    entry.setPublished(publishedProperty.toString());
                }
            }
            // process rights
            Object rights = ScriptableObject.getProperty(nativeObject, "rights");
            if (rights instanceof XMLObject) {
                entry.setRightsAsXhtml(rights.toString());
            } else if (rights instanceof String) {
                entry.setRights(rights.toString());
            }
            // process summary
            Object summary = ScriptableObject.getProperty(nativeObject, "summary");
            if (summary instanceof XMLObject) {
                entry.setSummaryAsXhtml(summary.toString());
            } else if (summary instanceof String) {
                entry.setSummary(summary.toString());
            }
            // process title
            Object title = ScriptableObject.getProperty(nativeObject, "title");
            if (title instanceof XMLObject) {
                entry.setTitleAsXhtml(title.toString());
            } else if (title instanceof String) {
                entry.setTitle(title.toString());
            } else {
                throw new ScriptException("An Entry MUST have a title.");
            }
            // process updated
            Object updated = ScriptableObject.getProperty(nativeObject, "updated");
            if (updated instanceof String) {
                if (updated.equals("now")) {
                    entry.setUpdated(new Date(System.currentTimeMillis()));
                } else {
                    entry.setUpdated((String) updated);
                }
            }
        } catch (IRISyntaxException e) {
            throw new ScriptException(e);
        }
    } else if (!(entryObject instanceof EntryHostObject)) {
        throw new ScriptException("Invalid parameter");
    }
// log.info("New Added Entry" + entry);
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) Factory(org.apache.abdera.factory.Factory) LogFactory(org.apache.commons.logging.LogFactory) XMLObject(org.mozilla.javascript.xml.XMLObject) IRISyntaxException(org.apache.abdera.i18n.iri.IRISyntaxException) XMLObject(org.mozilla.javascript.xml.XMLObject) FileHostObject(org.jaggeryjs.hostobjects.file.FileHostObject) Date(java.util.Date) Abdera(org.apache.abdera.Abdera)

Example 4 with Abdera

use of org.apache.abdera.Abdera in project jaggery by wso2.

the class FeedHostObject method jsConstructor.

/**
 * Constructor the user will be using inside javaScript
 */
public static Scriptable jsConstructor(Context cx, Object[] args, Function ctorObj, boolean inNewExpr) throws ScriptException {
    int argsCount = args.length;
    if (argsCount > 2) {
        HostObjectUtil.invalidNumberOfArgs(HOST_OBJECT_NAME, HOST_OBJECT_NAME, argsCount, true);
    }
    Abdera abdera = new Abdera();
    Factory factory = abdera.getFactory();
    feed = factory.newFeed();
    feedHostObject = new FeedHostObject();
    ctx = cx;
    if (argsCount == 0) {
        return feedHostObject;
    }
    if (argsCount == 1) {
        if (!(args[0] instanceof String)) {
            HostObjectUtil.invalidArgsError(HOST_OBJECT_NAME, HOST_OBJECT_NAME, "1", "string", args[0], true);
        }
        FeedHostObject.jsFunction_getFeed(cx, null, args, null);
    }
    return feedHostObject;
}
Also used : Factory(org.apache.abdera.factory.Factory) LogFactory(org.apache.commons.logging.LogFactory) Abdera(org.apache.abdera.Abdera)

Example 5 with Abdera

use of org.apache.abdera.Abdera in project mycore by MyCoRe-Org.

the class MCRSwordCollectionManager method listCollectionContents.

@Override
public Feed listCollectionContents(IRI collectionIRI, AuthCredentials authCredentials, SwordConfiguration config) throws SwordServerException, SwordAuthException, SwordError {
    String collection = MCRSwordUtil.ParseLinkUtil.CollectionIRI.getCollectionNameFromCollectionIRI(collectionIRI);
    String path = collectionIRI.getPath();
    LOGGER.info(MessageFormat.format("List Collection: {0}", collection));
    Feed feed = new Abdera().newFeed();
    if (MCRSword.getCollectionNames().contains(collection)) {
        MCRSwordCollectionProvider collectionProvider = MCRSword.getCollection(collection);
        collectionProvider.getAuthHandler().authentication(authCredentials);
        if (collectionProvider.isVisible()) {
            Integer paginationFromIRI = MCRSwordUtil.ParseLinkUtil.CollectionIRI.getPaginationFromCollectionIRI(collectionIRI);
            final int start = (paginationFromIRI - 1) * MCRSwordConstants.MAX_ENTRYS_PER_PAGE;
            collectionProvider.getIDSupplier().get(start, MCRSwordConstants.MAX_ENTRYS_PER_PAGE).stream().map(id -> {
                try {
                    return collectionProvider.getMetadataProvider().provideListMetadata(id);
                } catch (SwordError swordError) {
                    LOGGER.warn("Error while creating feed for [{}]! (Will be removed from List)", id);
                    return null;
                }
            }).filter(Objects::nonNull).forEach(feed::addEntry);
            MCRSwordUtil.BuildLinkUtil.addPaginationLinks(collectionIRI, collection, feed, collectionProvider);
        }
    } else {
        throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, HttpServletResponse.SC_NOT_FOUND, "The collection '" + collection + "' does not exist!");
    }
    return feed;
}
Also used : SwordError(org.swordapp.server.SwordError) MCRSwordCollectionProvider(org.mycore.sword.application.MCRSwordCollectionProvider) Feed(org.apache.abdera.model.Feed) Abdera(org.apache.abdera.Abdera)

Aggregations

Abdera (org.apache.abdera.Abdera)6 Factory (org.apache.abdera.factory.Factory)4 LogFactory (org.apache.commons.logging.LogFactory)4 Feed (org.apache.abdera.model.Feed)2 SwordError (org.swordapp.server.SwordError)2 Dataset (edu.harvard.iq.dataverse.Dataset)1 Dataverse (edu.harvard.iq.dataverse.Dataverse)1 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)1 DataverseRequest (edu.harvard.iq.dataverse.engine.command.DataverseRequest)1 UpdateDatasetCommand (edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetCommand)1 Date (java.util.Date)1 QName (javax.xml.namespace.QName)1 IRI (org.apache.abdera.i18n.iri.IRI)1 IRISyntaxException (org.apache.abdera.i18n.iri.IRISyntaxException)1 Entry (org.apache.abdera.model.Entry)1 FileHostObject (org.jaggeryjs.hostobjects.file.FileHostObject)1 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)1 XMLObject (org.mozilla.javascript.xml.XMLObject)1 MCRSwordCollectionProvider (org.mycore.sword.application.MCRSwordCollectionProvider)1