Search in sources :

Example 1 with MultipartReader

use of org.olat.restapi.support.MultipartReader in project OpenOLAT by OpenOLAT.

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)

Example 2 with MultipartReader

use of org.olat.restapi.support.MultipartReader in project OpenOLAT by OpenOLAT.

the class CourseElementWebService method updateSinglePage.

/**
 * This updates a Single Page Element onto a given course.
 * @response.representation.mediaType multipart/form-data
 * @response.representation.doc The content of the single page
 * @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 nodeId The node's id of this single page
 * @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 filename The single page file name
 * @param file The file input stream
 * @param request The HTTP request
 * @return The persisted Single Page Element(fully populated)
 */
@POST
@Path("singlepage/{nodeId}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response updateSinglePage(@PathParam("courseId") Long courseId, @PathParam("nodeId") String nodeId, @Context HttpServletRequest request) {
    InputStream in = null;
    MultipartReader reader = null;
    try {
        reader = new MultipartReader(request);
        String shortTitle = reader.getValue("shortTitle");
        String longTitle = reader.getValue("longTitle");
        String objectives = reader.getValue("objectives");
        String visibilityExpertRules = reader.getValue("visibilityExpertRules");
        String filename = reader.getValue("filename", "attachment");
        String accessExpertRules = reader.getValue("accessExpertRules");
        in = new FileInputStream(reader.getFile());
        SinglePageCustomConfig config = new SinglePageCustomConfig(in, filename);
        return update(courseId, nodeId, 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)

Example 3 with MultipartReader

use of org.olat.restapi.support.MultipartReader in project OpenOLAT by OpenOLAT.

the class CourseElementWebService method updateStructure.

/**
 * This updates a Structure Element onto a given course.
 * @response.representation.mediaType application/x-www-form-urlencoded, multipart/form-data
 * @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 nodeId The node's id of this structure
 * @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 displayType The type of display (file, toc...)
 * @param filename The name of the file to be attached
 * @param file The file to be attached
 * @param request The HTTP request
 * @return The persisted structure element (fully populated)
 * @return The persisted structure element (fully populated)
 */
@POST
@Path("structure/{nodeId}")
@Consumes({ MediaType.APPLICATION_FORM_URLENCODED, MediaType.MULTIPART_FORM_DATA })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response updateStructure(@PathParam("courseId") Long courseId, @PathParam("nodeId") String nodeId, @Context HttpServletRequest request) {
    InputStream in = null;
    MultipartReader reader = null;
    try {
        reader = new MultipartReader(request);
        String shortTitle = reader.getValue("shortTitle");
        String longTitle = reader.getValue("longTitle");
        String objectives = reader.getValue("objectives");
        String visibilityExpertRules = reader.getValue("visibilityExpertRules");
        String accessExpertRules = reader.getValue("accessExpertRules");
        String displayType = reader.getValue("displayType", STCourseNodeEditController.CONFIG_VALUE_DISPLAY_TOC);
        String filename = reader.getValue("filename", "attachment");
        if (reader.getFile() != null) {
            in = new FileInputStream(reader.getFile());
        }
        CustomConfigDelegate config = new StructureFullConfig(displayType, in, filename);
        return update(courseId, nodeId, 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)

Example 4 with MultipartReader

use of org.olat.restapi.support.MultipartReader in project OpenOLAT by OpenOLAT.

the class CourseResourceFolderWebService method attachFileToCourseFolder.

private Response attachFileToCourseFolder(Long courseId, List<PathSegment> path, HttpServletRequest request) {
    MultipartReader partsReader = null;
    try {
        partsReader = new MultipartReader(request);
        File tmpFile = partsReader.getFile();
        InputStream in = new FileInputStream(tmpFile);
        String filename = partsReader.getValue("filename");
        return attachFileToCourseFolder(courseId, path, filename, in, request);
    } catch (FileNotFoundException e) {
        log.error("", e);
        return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();
    } finally {
        MultipartReader.closeQuietly(partsReader);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) MultipartReader(org.olat.restapi.support.MultipartReader) FileInputStream(java.io.FileInputStream)

Example 5 with MultipartReader

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

the class CoursesWebService method importCourse.

/**
 * Imports a course from a course archive zip file
 * @response.representation.200.qname {http://www.example.com}courseVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The metadatas of the imported course
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_COURSEVO}
 * @response.representation.401.doc The roles of the authenticated user are not sufficient
 * @param ownerUsername set the owner of the imported course to the user of this username.
 * @param request The HTTP request
 * @return It returns the imported course
 */
@POST
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Consumes({ MediaType.MULTIPART_FORM_DATA })
public Response importCourse(@QueryParam("ownerUsername") String ownerUsername, @Context HttpServletRequest request) {
    if (!isAuthor(request)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    UserRequest ureq = RestSecurityHelper.getUserRequest(request);
    Identity identity = null;
    // Set the owner of the imported course to the user defined in the parameter
    if (ownerUsername != null && !ownerUsername.isEmpty() && isAuthor(request)) {
        identity = BaseSecurityManager.getInstance().findIdentityByName(ownerUsername);
        if (identity == null) {
            return Response.serverError().status(Status.BAD_REQUEST).build();
        }
    }
    if (identity == null) {
        identity = ureq.getIdentity();
    }
    MultipartReader partsReader = null;
    try {
        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 membersOnlyRaw = partsReader.getValue("membersOnly");
            boolean membersonly = "true".equals(membersOnlyRaw);
            String softKey = partsReader.getValue("softkey");
            String displayName = partsReader.getValue("displayname");
            ICourse course = importCourse(ureq, identity, tmpFile, displayName, softKey, access, membersonly);
            CourseVO vo = ObjectFactory.get(course);
            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);
    }
    CourseVO vo = null;
    return Response.ok(vo).build();
}
Also used : CourseVO(org.olat.restapi.support.vo.CourseVO) ICourse(org.olat.course.ICourse) Identity(org.olat.core.id.Identity) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) File(java.io.File) RestSecurityHelper.getUserRequest(org.olat.restapi.security.RestSecurityHelper.getUserRequest) UserRequest(org.olat.core.gui.UserRequest) MultipartReader(org.olat.restapi.support.MultipartReader) WebApplicationException(javax.ws.rs.WebApplicationException) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

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