Search in sources :

Example 26 with MultipartReader

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

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 27 with MultipartReader

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

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 28 with MultipartReader

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

the class ForumWebService method replyToPostAttachment.

/**
 * Upload the attachment of a message, as parameter:<br>
 * filename The name of the attachment<br>
 * file The attachment.
 * @response.representation.200.mediaType application/json, application/xml
 * @response.representation.200.doc Ok
 * @response.representation.404.doc The identity or the portrait not found
 * @param messageKey The key of the message
 * @param request The HTTP request
 * @return Ok
 */
@POST
@Path("posts/{messageKey}/attachments")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response replyToPostAttachment(@PathParam("messageKey") Long messageKey, @Context HttpServletRequest request) {
    InputStream in = null;
    MultipartReader partsReader = null;
    try {
        partsReader = new MultipartReader(request);
        File tmpFile = partsReader.getFile();
        in = new FileInputStream(tmpFile);
        String filename = partsReader.getValue("filename");
        return attachToPost(messageKey, filename, in, request);
    } catch (FileNotFoundException e) {
        log.error("", e);
        return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();
    } finally {
        MultipartReader.closeQuietly(partsReader);
        IOUtils.closeQuietly(in);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) 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) 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