use of org.olat.restapi.support.MultipartReader in project openolat by klemens.
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);
}
}
use of org.olat.restapi.support.MultipartReader in project openolat by klemens.
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);
}
}
use of org.olat.restapi.support.MultipartReader in project openolat by klemens.
the class CourseElementWebService method attachSinglePage.
/**
* This attaches a Single Page Element onto a given course. The element will
* be inserted underneath the supplied parentNodeId.
* @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 parentNodeId The node's id which will be the parent 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)
*/
@PUT
@Path("singlepage")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response attachSinglePage(@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 accessExpertRules = reader.getValue("accessExpertRules");
String filename = reader.getValue("filename", "attachment");
in = new FileInputStream(reader.getFile());
SinglePageCustomConfig config = new SinglePageCustomConfig(in, filename);
return attach(courseId, parentNodeId, "sp", 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);
}
}
use of org.olat.restapi.support.MultipartReader in project openolat by klemens.
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);
}
}
use of org.olat.restapi.support.MultipartReader in project openolat by klemens.
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);
}
}
Aggregations