Search in sources :

Example 21 with MultipartReader

use of org.olat.restapi.support.MultipartReader in project openolat by klemens.

the class QuestionPoolWebService method importQuestionItems.

private Response importQuestionItems(HttpServletRequest request) {
    if (!isQuestionPoolManager(request)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    MultipartReader partsReader = null;
    try {
        Identity identity = RestSecurityHelper.getUserRequest(request).getIdentity();
        partsReader = new MultipartReader(request);
        File tmpFile = partsReader.getFile();
        long length = tmpFile.length();
        if (length > 0) {
            String filename = partsReader.getValue("filename");
            String language = partsReader.getValue("language");
            QuestionItemVOes voes = importQuestionItem(identity, filename, tmpFile, language, identity);
            return Response.ok(voes).build();
        }
        return Response.serverError().status(Status.NO_CONTENT).build();
    } catch (Exception e) {
        log.error("Error while importing a file", e);
    } finally {
        MultipartReader.closeQuietly(partsReader);
    }
    return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();
}
Also used : Identity(org.olat.core.id.Identity) File(java.io.File) MultipartReader(org.olat.restapi.support.MultipartReader)

Example 22 with MultipartReader

use of org.olat.restapi.support.MultipartReader in project openolat by klemens.

the class RepositoryEntriesResource method putResource.

/**
 * Import a resource in the repository
 * @response.representation.mediaType multipart/form-data
 * @response.representation.doc The file, its name and the resourcename
 * @response.representation.200.qname {http://www.example.com}repositoryEntryVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc Import the resource and return the repository entry
 * @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
 * @param filename The name of the imported file
 * @param file The file input stream
 * @param resourcename The name of the resource
 * @param displayname The display name
 * @param softkey The soft key (can be null)
 * @param request The HTTP request
 * @return
 */
@PUT
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Consumes({ MediaType.MULTIPART_FORM_DATA })
public Response putResource(@Context HttpServletRequest request) {
    if (!isAuthor(request)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    MultipartReader partsReader = null;
    try {
        Identity identity = RestSecurityHelper.getUserRequest(request).getIdentity();
        partsReader = new MultipartReader(request);
        File tmpFile = partsReader.getFile();
        long length = tmpFile.length();
        if (length > 0) {
            Long accessRaw = partsReader.getLongValue("access");
            int access = accessRaw != null ? accessRaw.intValue() : RepositoryEntry.ACC_OWNERS;
            String softkey = partsReader.getValue("softkey");
            String resourcename = partsReader.getValue("resourcename");
            String displayname = partsReader.getValue("displayname");
            RepositoryEntry re = importFileResource(identity, tmpFile, resourcename, displayname, softkey, access);
            RepositoryEntryVO vo = ObjectFactory.get(re);
            return Response.ok(vo).build();
        }
        return Response.serverError().status(Status.NO_CONTENT).build();
    } catch (Exception e) {
        log.error("Error while importing a file", e);
    } finally {
        MultipartReader.closeQuietly(partsReader);
    }
    return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();
}
Also used : RepositoryEntryVO(org.olat.restapi.support.vo.RepositoryEntryVO) RepositoryEntry(org.olat.repository.RepositoryEntry) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) Identity(org.olat.core.id.Identity) File(java.io.File) MultipartReader(org.olat.restapi.support.MultipartReader) WebApplicationException(javax.ws.rs.WebApplicationException) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 23 with MultipartReader

use of org.olat.restapi.support.MultipartReader in project openolat by klemens.

the class RepositoryEntryResource method replaceResource.

/**
 * Replace a resource in the repository and update its display name. The implementation is
 * limited to CP.
 * @response.representation.mediaType multipart/form-data
 * @response.representation.doc Import the resource file
 * @response.representation.200.qname {http://www.example.com}repositoryEntryVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc Replace the resource and return the updated repository entry
 * @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
 * @param repoEntryKey The key or soft key of the repository entry
 * @param filename The name of the file
 * @param file The file input stream
 * @param displayname The display name
 * @param request The HTTP request
 * @return
 */
@POST
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Consumes({ MediaType.MULTIPART_FORM_DATA })
public Response replaceResource(@PathParam("repoEntryKey") String repoEntryKey, @Context HttpServletRequest request) {
    if (!RestSecurityHelper.isAuthor(request)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    MultipartReader reader = null;
    try {
        final RepositoryEntry re = lookupRepositoryEntry(repoEntryKey);
        if (re == null) {
            return Response.serverError().status(Status.NOT_FOUND).build();
        }
        reader = new MultipartReader(request);
        File tmpFile = reader.getFile();
        String displayname = reader.getValue("displayname");
        String location = reader.getValue("location");
        String authors = reader.getValue("authors");
        String description = reader.getValue("description");
        String externalId = reader.getValue("externalId");
        String externalRef = reader.getValue("externalRef");
        String managedFlags = reader.getValue("managedFlags");
        Identity identity = RestSecurityHelper.getUserRequest(request).getIdentity();
        RepositoryEntry replacedRe;
        if (tmpFile == null) {
            replacedRe = repositoryManager.setDescriptionAndName(re, displayname, description, location, authors, externalId, externalRef, managedFlags, re.getLifecycle());
        } else {
            long length = tmpFile.length();
            if (length == 0) {
                return Response.serverError().status(Status.NO_CONTENT).build();
            }
            replacedRe = replaceFileResource(identity, re, tmpFile);
            if (replacedRe == null) {
                return Response.serverError().status(Status.NOT_FOUND).build();
            } else {
                replacedRe = repositoryManager.setDescriptionAndName(replacedRe, displayname, description, location, authors, externalId, externalRef, managedFlags, replacedRe.getLifecycle());
            }
        }
        RepositoryEntryVO vo = ObjectFactory.get(replacedRe);
        return Response.ok(vo).build();
    } catch (Exception e) {
        log.error("Error while importing a file", e);
    } finally {
        MultipartReader.closeQuietly(reader);
    }
    return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();
}
Also used : RepositoryEntryVO(org.olat.restapi.support.vo.RepositoryEntryVO) RepositoryEntry(org.olat.repository.RepositoryEntry) Identity(org.olat.core.id.Identity) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) File(java.io.File) MultipartReader(org.olat.restapi.support.MultipartReader) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Example 24 with MultipartReader

use of org.olat.restapi.support.MultipartReader in project openolat by klemens.

the class CourseElementWebService method attachTaskFile.

/**
 * This attaches a Task file onto a given task element.
 * @response.representation.mediaType application/x-www-form-urlencoded
 * @response.representation.doc The task node metadatas
 * @response.representation.200.qname {http://www.example.com}courseNodeVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The course node metadatas
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_COURSENODEVO}
 * @response.representation.401.doc The roles of the authenticated user are not sufficient
 * @response.representation.404.doc The course or parentNode not found
 * @response.representation.406.doc The course node is not of type task
 * @param courseId The course resourceable id
 * @param nodeId The node's id which will be the parent of this task file
 * @param request The HTTP request
 * @return The persisted task element (fully populated)
 */
@PUT
@Path("task/{nodeId}/file")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response attachTaskFile(@PathParam("courseId") Long courseId, @PathParam("nodeId") String nodeId, @Context HttpServletRequest request) {
    ICourse course = CoursesWebService.loadCourse(courseId);
    CourseNode node = getParentNode(course, nodeId);
    if (course == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    if (node == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    } else if (!(node instanceof TACourseNode)) {
        return Response.serverError().status(Status.NOT_ACCEPTABLE).build();
    }
    if (!isAuthorEditor(course, request)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    InputStream in = null;
    MultipartReader reader = null;
    try {
        reader = new MultipartReader(request);
        String filename = reader.getValue("filename", "task");
        String taskFolderPath = TACourseNode.getTaskFolderPathRelToFolderRoot(course, node);
        OlatRootFolderImpl taskFolder = new OlatRootFolderImpl(taskFolderPath, null);
        VFSLeaf singleFile = (VFSLeaf) taskFolder.resolve("/" + filename);
        if (singleFile == null) {
            singleFile = taskFolder.createChildLeaf("/" + filename);
        }
        File file = reader.getFile();
        if (file != null) {
            in = new FileInputStream(file);
            OutputStream out = singleFile.getOutputStream(false);
            IOUtils.copy(in, out);
            IOUtils.closeQuietly(out);
        } else {
            return Response.status(Status.NOT_ACCEPTABLE).build();
        }
    } catch (Exception e) {
        log.error("", e);
        return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();
    } finally {
        MultipartReader.closeQuietly(reader);
        IOUtils.closeQuietly(in);
    }
    return Response.ok().build();
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) OlatRootFolderImpl(org.olat.core.commons.modules.bc.vfs.OlatRootFolderImpl) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ICourse(org.olat.course.ICourse) CourseNode(org.olat.course.nodes.CourseNode) TACourseNode(org.olat.course.nodes.TACourseNode) STCourseNode(org.olat.course.nodes.STCourseNode) MSCourseNode(org.olat.course.nodes.MSCourseNode) AbstractFeedCourseNode(org.olat.course.nodes.AbstractFeedCourseNode) IQSURVCourseNode(org.olat.course.nodes.IQSURVCourseNode) IQTESTCourseNode(org.olat.course.nodes.IQTESTCourseNode) TACourseNode(org.olat.course.nodes.TACourseNode) File(java.io.File) MultipartReader(org.olat.restapi.support.MultipartReader) FileInputStream(java.io.FileInputStream) MalformedURLException(java.net.MalformedURLException) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 25 with MultipartReader

use of org.olat.restapi.support.MultipartReader in project openolat by klemens.

the class CourseElementWebService method attachStructurePostMultiparts.

/**
 * This attaches a Structure Element onto a given course. The element will be
 * inserted underneath the supplied parentNodeId.
 * @response.representation.mediaType application/x-www-form-urlencoded
 * @response.representation.doc The course node metadatas
 * @response.representation.200.qname {http://www.example.com}courseNodeVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The course node metadatas
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_COURSENODEVO}
 * @response.representation.401.doc The roles of the authenticated user are not sufficient
 * @response.representation.404.doc The course or parentNode not found
 * @param courseId The course resourceable's id
 * @param parentNodeId The node's id which will be the parent of this
 *          structure
 * @param position The node's position relative to its sibling nodes (optional)
 * @param shortTitle The node short title
 * @param longTitle The node long title
 * @param objectives The node learning objectives
 * @param visibilityExpertRules The rules to view the node (optional)
 * @param accessExpertRules The rules to access the node (optional)
 * @param request The HTTP request
 * @return The persisted structure element (fully populated)
 */
@POST
@Path("structure")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response attachStructurePostMultiparts(@PathParam("courseId") Long courseId, @Context HttpServletRequest request) {
    InputStream in = null;
    MultipartReader reader = null;
    try {
        reader = new MultipartReader(request);
        String parentNodeId = reader.getValue("parentNodeId");
        Integer position = reader.getIntegerValue("position");
        String shortTitle = reader.getValue("shortTitle");
        String longTitle = reader.getValue("longTitle");
        String objectives = reader.getValue("objectives");
        String visibilityExpertRules = reader.getValue("visibilityExpertRules");
        String displayType = reader.getValue("displayType", STCourseNodeEditController.CONFIG_VALUE_DISPLAY_TOC);
        String filename = reader.getValue("filename", "attachment");
        String accessExpertRules = reader.getValue("accessExpertRules");
        if (reader.getFile() != null) {
            in = new FileInputStream(reader.getFile());
        }
        CustomConfigDelegate config = new StructureFullConfig(displayType, in, filename);
        return attach(courseId, parentNodeId, "st", position, shortTitle, longTitle, objectives, visibilityExpertRules, accessExpertRules, config, request);
    } catch (Exception e) {
        log.error("", e);
        return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();
    } finally {
        MultipartReader.closeQuietly(reader);
        IOUtils.closeQuietly(in);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) MultipartReader(org.olat.restapi.support.MultipartReader) FileInputStream(java.io.FileInputStream) MalformedURLException(java.net.MalformedURLException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Aggregations

MultipartReader (org.olat.restapi.support.MultipartReader)28 Consumes (javax.ws.rs.Consumes)22 File (java.io.File)20 Produces (javax.ws.rs.Produces)18 FileInputStream (java.io.FileInputStream)16 InputStream (java.io.InputStream)16 POST (javax.ws.rs.POST)16 Path (javax.ws.rs.Path)16 Identity (org.olat.core.id.Identity)12 MalformedURLException (java.net.MalformedURLException)10 WebApplicationException (javax.ws.rs.WebApplicationException)8 RestSecurityHelper.getIdentity (org.olat.restapi.security.RestSecurityHelper.getIdentity)8 FileNotFoundException (java.io.FileNotFoundException)6 PUT (javax.ws.rs.PUT)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ICourse (org.olat.course.ICourse)4 RepositoryEntry (org.olat.repository.RepositoryEntry)4 RepositoryEntryVO (org.olat.restapi.support.vo.RepositoryEntryVO)4 OutputStream (java.io.OutputStream)2 Date (java.util.Date)2