Search in sources :

Example 51 with PathSegment

use of javax.ws.rs.core.PathSegment in project OpenOLAT by OpenOLAT.

the class CourseResourceFolderWebService method attachFileToCourseFolder.

private Response attachFileToCourseFolder(Long courseId, List<PathSegment> path, String filename, InputStream file, HttpServletRequest request) {
    if (!isAuthor(request)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    ICourse course = CoursesWebService.loadCourse(courseId);
    if (course == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    VFSContainer container = course.getCourseFolderContainer();
    for (PathSegment segment : path) {
        VFSItem item = container.resolve(segment.getPath());
        if (item instanceof VFSContainer) {
            container = (VFSContainer) item;
        } else if (item == null) {
            // create the folder
            container = container.createChildContainer(segment.getPath());
        }
    }
    VFSItem newFile;
    UserRequest ureq = RestSecurityHelper.getUserRequest(request);
    if (container.resolve(filename) != null) {
        VFSItem existingVFSItem = container.resolve(filename);
        if (existingVFSItem instanceof VFSContainer) {
            // already exists
            return Response.ok().build();
        }
        // check if it's locked
        boolean locked = CoreSpringFactory.getImpl(VFSLockManager.class).isLockedForMe(existingVFSItem, ureq.getIdentity(), ureq.getUserSession().getRoles());
        if (locked) {
            return Response.serverError().status(Status.UNAUTHORIZED).build();
        }
        if (existingVFSItem instanceof Versionable && ((Versionable) existingVFSItem).getVersions().isVersioned()) {
            Versionable existingVersionableItem = (Versionable) existingVFSItem;
            boolean ok = existingVersionableItem.getVersions().addVersion(ureq.getIdentity(), "REST upload", file);
            if (ok) {
                log.audit("");
            }
            newFile = (VFSLeaf) existingVersionableItem;
        } else {
            existingVFSItem.delete();
            newFile = container.createChildLeaf(filename);
            OutputStream out = ((VFSLeaf) newFile).getOutputStream(false);
            FileUtils.copy(file, out);
            FileUtils.closeSafely(out);
            FileUtils.closeSafely(file);
        }
    } else if (file != null) {
        newFile = container.createChildLeaf(filename);
        OutputStream out = ((VFSLeaf) newFile).getOutputStream(false);
        FileUtils.copy(file, out);
        FileUtils.closeSafely(out);
        FileUtils.closeSafely(file);
    } else {
        newFile = container.createChildContainer(filename);
    }
    if (newFile instanceof MetaTagged && ((MetaTagged) newFile).getMetaInfo() != null) {
        MetaInfo infos = ((MetaTagged) newFile).getMetaInfo();
        infos.setAuthor(ureq.getIdentity());
        infos.write();
    }
    return Response.ok().build();
}
Also used : Versionable(org.olat.core.util.vfs.version.Versionable) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) VFSContainer(org.olat.core.util.vfs.VFSContainer) OutputStream(java.io.OutputStream) MetaTagged(org.olat.core.commons.modules.bc.meta.tagged.MetaTagged) MetaInfo(org.olat.core.commons.modules.bc.meta.MetaInfo) VFSItem(org.olat.core.util.vfs.VFSItem) ICourse(org.olat.course.ICourse) PathSegment(javax.ws.rs.core.PathSegment) UserRequest(org.olat.core.gui.UserRequest) VFSLockManager(org.olat.core.util.vfs.VFSLockManager)

Example 52 with PathSegment

use of javax.ws.rs.core.PathSegment in project OpenOLAT by OpenOLAT.

the class CatalogWebService method getCatalogEntryKeyFromPath.

private Long getCatalogEntryKeyFromPath(List<PathSegment> path) {
    PathSegment lastPath = path.get(path.size() - 1);
    Long key = null;
    try {
        key = new Long(lastPath.getPath());
    } catch (NumberFormatException e) {
        key = null;
    }
    return key;
}
Also used : PathSegment(javax.ws.rs.core.PathSegment)

Example 53 with PathSegment

use of javax.ws.rs.core.PathSegment in project microservice_framework by CJSCommonPlatform.

the class UrlLinkFactoryTest method shouldReturnEventStreamsHeadUrlLink.

@SuppressWarnings("unchecked")
@Test
public void shouldReturnEventStreamsHeadUrlLink() throws Exception {
    final UUID streamId = UUID.randomUUID();
    final String eventStreamPathSegmentValue = "eventstreams";
    final String baseURI = "http://localhost:8080/context/";
    final String urlString = baseURI + "/HEAD/BACKWARD/2";
    final int pageSize = 2;
    final UriInfo uriInfo = mock(UriInfo.class);
    final UriBuilder uriBuilder = mock(UriBuilder.class);
    final List<PathSegment> pathSegmentList = mock(List.class);
    final PathSegment eventStreamPathSegment = mock(PathSegment.class);
    final PathSegment streamIdPathSegment = mock(PathSegment.class);
    when(uriInfo.getBaseUriBuilder()).thenReturn(uriBuilder);
    when(uriInfo.getPathSegments()).thenReturn(pathSegmentList);
    when(pathSegmentList.get(0)).thenReturn(eventStreamPathSegment);
    when(eventStreamPathSegment.getPath()).thenReturn(eventStreamPathSegmentValue);
    when(pathSegmentList.get(1)).thenReturn(streamIdPathSegment);
    when(streamIdPathSegment.getPath()).thenReturn(streamId.toString());
    when(uriBuilder.path(eventStreamPathSegmentValue)).thenReturn(uriBuilder);
    when(uriBuilder.path(streamId.toString())).thenReturn(uriBuilder);
    when(uriBuilder.path(HEAD)).thenReturn(uriBuilder);
    when(uriBuilder.path("BACKWARD")).thenReturn(uriBuilder);
    when(uriBuilder.path("2")).thenReturn(uriBuilder);
    when(uriBuilder.build()).thenReturn(new URL(urlString).toURI());
    when(positionValueFactory.getPositionValue(head())).thenReturn(HEAD);
    final URL link = urlLinkFactory.createHeadEventStreamsUrlLink(pageSize, uriInfo);
    assertThat(link.toString(), is(urlString));
}
Also used : PathSegment(javax.ws.rs.core.PathSegment) UUID(java.util.UUID) UriBuilder(javax.ws.rs.core.UriBuilder) UriInfo(javax.ws.rs.core.UriInfo) URL(java.net.URL) Test(org.junit.Test)

Example 54 with PathSegment

use of javax.ws.rs.core.PathSegment in project microservice_framework by CJSCommonPlatform.

the class UrlLinkFactoryTest method shouldReturnEventStreamsFirstUrlLink.

@SuppressWarnings("unchecked")
@Test
public void shouldReturnEventStreamsFirstUrlLink() throws Exception {
    final UUID streamId = UUID.randomUUID();
    final String eventStreamPathSegmentValue = "eventstreams";
    final String baseURI = "http://localhost:8080/context/";
    final String urlString = baseURI + "/1/FORWARD/2";
    final int pageSize = 2;
    final UriInfo uriInfo = mock(UriInfo.class);
    final UriBuilder uriBuilder = mock(UriBuilder.class);
    final List<PathSegment> pathSegmentList = mock(List.class);
    final PathSegment eventStreamPathSegment = mock(PathSegment.class);
    final PathSegment streamIdPathSegment = mock(PathSegment.class);
    when(uriInfo.getBaseUriBuilder()).thenReturn(uriBuilder);
    when(uriInfo.getPathSegments()).thenReturn(pathSegmentList);
    when(pathSegmentList.get(0)).thenReturn(eventStreamPathSegment);
    when(eventStreamPathSegment.getPath()).thenReturn(eventStreamPathSegmentValue);
    when(pathSegmentList.get(1)).thenReturn(streamIdPathSegment);
    when(streamIdPathSegment.getPath()).thenReturn(streamId.toString());
    when(uriBuilder.path(eventStreamPathSegmentValue)).thenReturn(uriBuilder);
    when(uriBuilder.path(streamId.toString())).thenReturn(uriBuilder);
    when(uriBuilder.path(FIRST)).thenReturn(uriBuilder);
    when(uriBuilder.path("FORWARD")).thenReturn(uriBuilder);
    when(uriBuilder.path("2")).thenReturn(uriBuilder);
    when(uriBuilder.build()).thenReturn(new URL(urlString).toURI());
    when(positionValueFactory.getPositionValue(first())).thenReturn(FIRST);
    final URL link = urlLinkFactory.createFirstEventStreamsUrlLink(pageSize, uriInfo);
    assertThat(link.toString(), is(urlString));
}
Also used : PathSegment(javax.ws.rs.core.PathSegment) UUID(java.util.UUID) UriBuilder(javax.ws.rs.core.UriBuilder) UriInfo(javax.ws.rs.core.UriInfo) URL(java.net.URL) Test(org.junit.Test)

Example 55 with PathSegment

use of javax.ws.rs.core.PathSegment in project narayana by jbosstm.

the class TxSupport method getUriBuilder.

public static UriBuilder getUriBuilder(UriInfo info, int npaths, String... paths) {
    UriBuilder builder = info.getBaseUriBuilder();
    if (npaths > 0) {
        List<PathSegment> segments = info.getPathSegments();
        for (int i = 0; i < npaths; i++) builder.path(segments.get(i).getPath());
    } else {
        String basePath = info.getMatchedURIs().get(0);
        builder.path(basePath);
    }
    for (String path : paths) builder.path(path);
    return builder;
}
Also used : PathSegment(javax.ws.rs.core.PathSegment) UriBuilder(javax.ws.rs.core.UriBuilder)

Aggregations

PathSegment (javax.ws.rs.core.PathSegment)67 UriInfo (javax.ws.rs.core.UriInfo)19 Test (org.junit.Test)18 ArrayList (java.util.ArrayList)12 Map (java.util.Map)11 UriBuilder (javax.ws.rs.core.UriBuilder)11 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)10 VFSContainer (org.olat.core.util.vfs.VFSContainer)10 VFSItem (org.olat.core.util.vfs.VFSItem)10 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)10 Response (javax.ws.rs.core.Response)9 HashMap (java.util.HashMap)7 List (java.util.List)7 SystemItemFilter (org.olat.core.util.vfs.filters.SystemItemFilter)7 WebApplicationException (javax.ws.rs.WebApplicationException)6 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)6 URL (java.net.URL)5 TreeMap (java.util.TreeMap)5 UUID (java.util.UUID)5 OutputStream (java.io.OutputStream)4