Search in sources :

Example 16 with MultipartReader

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

the class UserWebService method postPortrait.

/**
 * Upload the portrait of an user
 * @response.representation.200.mediaType application/octet-stream
 * @response.representation.200.doc The portrait as image
 * @response.representation.401.doc Not authorized
 * @response.representation.404.doc The identity or the portrait not found
 * @param identityKey The user key identifier of the user being searched
 * @param file The image
 * @param request The REST request
 * @return The image
 */
@POST
@Path("{identityKey}/portrait")
@Consumes({ MediaType.MULTIPART_FORM_DATA })
public Response postPortrait(@PathParam("identityKey") Long identityKey, @Context HttpServletRequest request) {
    MultipartReader partsReader = null;
    try {
        IdentityShort identity = BaseSecurityManager.getInstance().loadIdentityShortByKey(identityKey);
        if (identity == null) {
            return Response.serverError().status(Status.NOT_FOUND).build();
        }
        Identity authIdentity = getUserRequest(request).getIdentity();
        if (!isUserManager(request) && !identity.getKey().equals(authIdentity.getKey())) {
            return Response.serverError().status(Status.UNAUTHORIZED).build();
        }
        partsReader = new MultipartReader(request);
        File tmpFile = partsReader.getFile();
        String filename = partsReader.getFilename();
        DisplayPortraitManager.getInstance().setPortrait(tmpFile, filename, identity.getName());
        return Response.ok().build();
    } catch (Throwable e) {
        throw new WebApplicationException(e);
    } finally {
        MultipartReader.closeQuietly(partsReader);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) IdentityShort(org.olat.basesecurity.IdentityShort) Identity(org.olat.core.id.Identity) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) File(java.io.File) MultipartReader(org.olat.restapi.support.MultipartReader) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 17 with MultipartReader

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

the class CertificationWebService method postCertificate.

/**
 * Upload a new certificate.
 *
 * @response.representation.200.doc if the certificate was uploaded
 * @response.representation.401.doc The roles of the authenticated user are not sufficient
 * @response.representation.404.doc The identity or the resource cannot be found
 * @param identityKey The owner of the certificate
 * @param resourceKey The primary key of the resource of the repository entry of the course.
 * @param request The request
 * @return Nothing special
 */
@POST
@Path("{identityKey}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response postCertificate(@PathParam("identityKey") Long identityKey, @PathParam("resourceKey") Long resourceKey, @Context HttpServletRequest request) {
    if (!isAdmin(request)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    MultipartReader partsReader = null;
    try {
        partsReader = new MultipartReader(request);
        File tmpFile = partsReader.getFile();
        String courseTitle = partsReader.getValue("courseTitle");
        String creationDateStr = partsReader.getValue("creationDate");
        Date creationDate = null;
        if (StringHelper.containsNonWhitespace(creationDateStr)) {
            creationDate = ObjectFactory.parseDate(creationDateStr);
        }
        CertificatesManager certificatesManager = CoreSpringFactory.getImpl(CertificatesManager.class);
        BaseSecurity baseSecurity = CoreSpringFactory.getImpl(BaseSecurity.class);
        Identity assessedIdentity = baseSecurity.loadIdentityByKey(identityKey);
        if (assessedIdentity == null) {
            return Response.serverError().status(Response.Status.NOT_FOUND).build();
        }
        OLATResourceManager resourceManager = CoreSpringFactory.getImpl(OLATResourceManager.class);
        OLATResource resource = resourceManager.findResourceById(resourceKey);
        if (resource == null) {
            certificatesManager.uploadStandaloneCertificate(assessedIdentity, creationDate, courseTitle, resourceKey, tmpFile);
        } else {
            certificatesManager.uploadCertificate(assessedIdentity, creationDate, resource, tmpFile);
        }
        return Response.ok().build();
    } catch (Throwable e) {
        throw new WebApplicationException(e);
    } finally {
        MultipartReader.closeQuietly(partsReader);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) CertificatesManager(org.olat.course.certificate.CertificatesManager) OLATResourceManager(org.olat.resource.OLATResourceManager) OLATResource(org.olat.resource.OLATResource) Identity(org.olat.core.id.Identity) File(java.io.File) MultipartReader(org.olat.restapi.support.MultipartReader) Date(java.util.Date) BaseSecurity(org.olat.basesecurity.BaseSecurity) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 18 with MultipartReader

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

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)

Example 19 with MultipartReader

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

the class VFSWebservice method addFileToFolder.

private Response addFileToFolder(UriInfo uriInfo, List<PathSegment> path, HttpServletRequest request) {
    InputStream in = null;
    MultipartReader partsReader = null;
    try {
        partsReader = new MultipartReader(request);
        File tmpFile = partsReader.getFile();
        if (tmpFile != null) {
            in = new FileInputStream(tmpFile);
        }
        String filename = partsReader.getValue("filename");
        String foldername = partsReader.getValue("foldername");
        return putFile(foldername, filename, in, uriInfo, path);
    } 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)

Example 20 with MultipartReader

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

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)

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