Search in sources :

Example 66 with RepositoryHandler

use of org.olat.repository.handlers.RepositoryHandler in project openolat by klemens.

the class RepositoryEntriesResource method importFileResource.

private RepositoryEntry importFileResource(Identity identity, File fResource, String resourcename, String displayname, String softkey, int access) {
    RepositoryService repositoryService = CoreSpringFactory.getImpl(RepositoryService.class);
    RepositoryHandlerFactory handlerFactory = CoreSpringFactory.getImpl(RepositoryHandlerFactory.class);
    try {
        RepositoryHandler handler = null;
        for (String type : handlerFactory.getSupportedTypes()) {
            RepositoryHandler h = handlerFactory.getRepositoryHandler(type);
            ResourceEvaluation eval = h.acceptImport(fResource, fResource.getName());
            if (eval != null && eval.isValid()) {
                handler = h;
                break;
            }
        }
        RepositoryEntry addedEntry = null;
        if (handler != null) {
            Locale locale = I18nModule.getDefaultLocale();
            addedEntry = handler.importResource(identity, null, displayname, "", true, locale, fResource, fResource.getName());
            if (StringHelper.containsNonWhitespace(resourcename)) {
                addedEntry.setResourcename(resourcename);
            }
            if (StringHelper.containsNonWhitespace(softkey)) {
                addedEntry.setSoftkey(softkey);
            }
            if (access < RepositoryEntry.ACC_OWNERS || access > RepositoryEntry.ACC_USERS_GUESTS) {
                addedEntry.setAccess(RepositoryEntry.ACC_OWNERS);
            } else {
                addedEntry.setAccess(access);
            }
            addedEntry = repositoryService.update(addedEntry);
        }
        return addedEntry;
    } catch (Exception e) {
        log.error("Fail to import a resource", e);
        throw new WebApplicationException(e);
    }
}
Also used : ResourceEvaluation(org.olat.fileresource.types.ResourceEvaluation) Locale(java.util.Locale) WebApplicationException(javax.ws.rs.WebApplicationException) RepositoryHandlerFactory(org.olat.repository.handlers.RepositoryHandlerFactory) RepositoryHandler(org.olat.repository.handlers.RepositoryHandler) RepositoryEntry(org.olat.repository.RepositoryEntry) WebApplicationException(javax.ws.rs.WebApplicationException) RepositoryService(org.olat.repository.RepositoryService)

Example 67 with RepositoryHandler

use of org.olat.repository.handlers.RepositoryHandler in project openolat by klemens.

the class RepositoryEntryResource method getRepoFileById.

/**
 * Download the export zip file of a repository entry.
 * @response.representation.mediaType multipart/form-data
 * @response.representation.doc Download the resource file
 * @response.representation.200.mediaType application/zip
 * @response.representation.200.doc Download the repository entry as export zip file
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_REPOENTRYVO}
 * @response.representation.401.doc The roles of the authenticated user are not sufficient
 * @response.representation.404.doc The resource could not found
 * @response.representation.406.doc Download of this resource is not possible
 * @response.representation.409.doc The resource is locked
 * @param repoEntryKey
 * @param request The HTTP request
 * @return
 */
@GET
@Path("file")
@Produces({ "application/zip", MediaType.APPLICATION_OCTET_STREAM })
public Response getRepoFileById(@PathParam("repoEntryKey") String repoEntryKey, @Context HttpServletRequest request) {
    RepositoryEntry re = lookupRepositoryEntry(repoEntryKey);
    if (re == null)
        return Response.serverError().status(Status.NOT_FOUND).build();
    RepositoryHandler typeToDownload = RepositoryHandlerFactory.getInstance().getRepositoryHandler(re);
    if (typeToDownload == null)
        return Response.serverError().status(Status.NOT_FOUND).build();
    OLATResource ores = OLATResourceManager.getInstance().findResourceable(re.getOlatResource());
    if (ores == null)
        return Response.serverError().status(Status.NOT_FOUND).build();
    Identity identity = getIdentity(request);
    boolean isAuthor = RestSecurityHelper.isAuthor(request);
    boolean isOwner = repositoryManager.isOwnerOfRepositoryEntry(identity, re);
    if (!(isAuthor | isOwner))
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    boolean canDownload = re.getCanDownload() && typeToDownload.supportsDownload();
    if (!canDownload)
        return Response.serverError().status(Status.NOT_ACCEPTABLE).build();
    boolean isAlreadyLocked = typeToDownload.isLocked(ores);
    LockResult lockResult = null;
    try {
        lockResult = typeToDownload.acquireLock(ores, identity);
        if (lockResult == null || (lockResult != null && lockResult.isSuccess() && !isAlreadyLocked)) {
            MediaResource mr = typeToDownload.getAsMediaResource(ores, false);
            if (mr != null) {
                repositoryService.incrementDownloadCounter(re);
                // success
                return Response.ok(mr.getInputStream()).cacheControl(cc).build();
            } else
                return Response.serverError().status(Status.NO_CONTENT).build();
        } else
            return Response.serverError().status(Status.CONFLICT).build();
    } finally {
        if ((lockResult != null && lockResult.isSuccess() && !isAlreadyLocked))
            typeToDownload.releaseLock(lockResult);
    }
}
Also used : LockResult(org.olat.core.util.coordinate.LockResult) OLATResource(org.olat.resource.OLATResource) MediaResource(org.olat.core.gui.media.MediaResource) RepositoryEntry(org.olat.repository.RepositoryEntry) RepositoryHandler(org.olat.repository.handlers.RepositoryHandler) Identity(org.olat.core.id.Identity) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 68 with RepositoryHandler

use of org.olat.repository.handlers.RepositoryHandler in project openolat by klemens.

the class CourseNodeFactory method launchReferencedRepoEntryEditor.

/**
 * Launch an editor for the repository entry which is referenced in the given
 * course node. The editor is launched in a new tab.
 *
 * @param ureq
 * @param node
 */
public boolean launchReferencedRepoEntryEditor(UserRequest ureq, WindowControl wControl, CourseNode node) {
    RepositoryEntry repositoryEntry = node.getReferencedRepositoryEntry();
    if (repositoryEntry == null) {
        // do nothing
        return false;
    }
    RepositoryHandler typeToEdit = RepositoryHandlerFactory.getInstance().getRepositoryHandler(repositoryEntry);
    if (typeToEdit.supportsEdit(repositoryEntry.getOlatResource()) == EditionSupport.no) {
        log.error("Trying to edit repository entry which has no associated editor: " + typeToEdit);
        return false;
    }
    try {
        String businessPath = "[RepositoryEntry:" + repositoryEntry.getKey() + "][Editor:0]";
        NewControllerFactory.getInstance().launch(businessPath, ureq, wControl);
        return true;
    } catch (CorruptedCourseException e) {
        log.error("Course corrupted: " + repositoryEntry.getKey() + " (" + repositoryEntry.getOlatResource().getResourceableId() + ")", e);
        return false;
    }
}
Also used : CorruptedCourseException(org.olat.course.CorruptedCourseException) RepositoryEntry(org.olat.repository.RepositoryEntry) RepositoryHandler(org.olat.repository.handlers.RepositoryHandler)

Example 69 with RepositoryHandler

use of org.olat.repository.handlers.RepositoryHandler in project openolat by klemens.

the class BlogCourseNode method importNode.

@Override
public void importNode(File importDirectory, ICourse course, Identity owner, Locale locale, boolean withReferences) {
    if (withReferences) {
        RepositoryHandler handler = RepositoryHandlerFactory.getInstance().getRepositoryHandler(BlogFileResource.TYPE_NAME);
        importFeed(handler, importDirectory, owner, locale);
    } else {
        FeedNodeEditController.removeReference(getModuleConfiguration());
    }
}
Also used : RepositoryHandler(org.olat.repository.handlers.RepositoryHandler)

Example 70 with RepositoryHandler

use of org.olat.repository.handlers.RepositoryHandler in project openolat by klemens.

the class IQTESTCourseNode method importNode.

@Override
public void importNode(File importDirectory, ICourse course, Identity owner, Locale locale, boolean withReferences) {
    RepositoryEntryImportExport rie = new RepositoryEntryImportExport(importDirectory, getIdent());
    if (withReferences && rie.anyExportedPropertiesAvailable()) {
        File file = rie.importGetExportedFile();
        RepositoryHandler handlerQTI21 = RepositoryHandlerFactory.getInstance().getRepositoryHandler(ImsQTI21Resource.TYPE_NAME);
        RepositoryEntry re;
        if (handlerQTI21.acceptImport(file, "repo.zip").isValid()) {
            re = handlerQTI21.importResource(owner, rie.getInitialAuthor(), rie.getDisplayName(), rie.getDescription(), false, locale, rie.importGetExportedFile(), null);
            getModuleConfiguration().set(IQEditController.CONFIG_KEY_TYPE_QTI, IQEditController.CONFIG_VALUE_QTI21);
        } else {
            RepositoryHandler handlerQTI = RepositoryHandlerFactory.getInstance().getRepositoryHandler(TestFileResource.TYPE_NAME);
            re = handlerQTI.importResource(owner, rie.getInitialAuthor(), rie.getDisplayName(), rie.getDescription(), false, locale, rie.importGetExportedFile(), null);
        }
        IQEditController.setIQReference(re, getModuleConfiguration());
    } else {
        IQEditController.removeIQReference(getModuleConfiguration());
    }
}
Also used : RepositoryEntryImportExport(org.olat.repository.RepositoryEntryImportExport) RepositoryHandler(org.olat.repository.handlers.RepositoryHandler) RepositoryEntry(org.olat.repository.RepositoryEntry) File(java.io.File)

Aggregations

RepositoryHandler (org.olat.repository.handlers.RepositoryHandler)74 RepositoryEntry (org.olat.repository.RepositoryEntry)42 RepositoryEntryImportExport (org.olat.repository.RepositoryEntryImportExport)18 OLATResource (org.olat.resource.OLATResource)16 File (java.io.File)14 ICourse (org.olat.course.ICourse)10 RepositoryService (org.olat.repository.RepositoryService)10 ArrayList (java.util.ArrayList)8 CloseableModalController (org.olat.core.gui.control.generic.closablewrapper.CloseableModalController)8 MediaResource (org.olat.core.gui.media.MediaResource)8 OLATResourceable (org.olat.core.id.OLATResourceable)8 FormLayoutContainer (org.olat.core.gui.components.form.flexible.impl.FormLayoutContainer)6 LockResult (org.olat.core.util.coordinate.LockResult)6 VFSContainer (org.olat.core.util.vfs.VFSContainer)6 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)6 OrderedRepositoryHandler (org.olat.repository.handlers.RepositoryHandlerFactory.OrderedRepositoryHandler)6 URL (java.net.URL)4 GET (javax.ws.rs.GET)4 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4