Search in sources :

Example 41 with RepositoryEntryVO

use of org.olat.restapi.support.vo.RepositoryEntryVO 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 42 with RepositoryEntryVO

use of org.olat.restapi.support.vo.RepositoryEntryVO 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)

Aggregations

RepositoryEntryVO (org.olat.restapi.support.vo.RepositoryEntryVO)42 RepositoryEntry (org.olat.repository.RepositoryEntry)36 URI (java.net.URI)26 HttpResponse (org.apache.http.HttpResponse)26 Test (org.junit.Test)24 File (java.io.File)16 HttpEntity (org.apache.http.HttpEntity)14 HttpPut (org.apache.http.client.methods.HttpPut)14 URL (java.net.URL)12 Produces (javax.ws.rs.Produces)12 HttpGet (org.apache.http.client.methods.HttpGet)10 Identity (org.olat.core.id.Identity)8 Date (java.util.Date)6 GET (javax.ws.rs.GET)6 WebApplicationException (javax.ws.rs.WebApplicationException)6 RestSecurityHelper.getIdentity (org.olat.restapi.security.RestSecurityHelper.getIdentity)6 RepositoryEntryLifecycleVO (org.olat.restapi.support.vo.RepositoryEntryLifecycleVO)6 Consumes (javax.ws.rs.Consumes)5 HttpPost (org.apache.http.client.methods.HttpPost)4 Roles (org.olat.core.id.Roles)4