Search in sources :

Example 36 with PathSegment

use of javax.ws.rs.core.PathSegment in project druid by druid-io.

the class DatasourceResourceFilter method filter.

@Override
public ContainerRequest filter(ContainerRequest request) {
    if (getAuthConfig().isEnabled()) {
        // This is an experimental feature, see - https://github.com/druid-io/druid/pull/2424
        final String dataSourceName = request.getPathSegments().get(Iterables.indexOf(request.getPathSegments(), new Predicate<PathSegment>() {

            @Override
            public boolean apply(PathSegment input) {
                return input.getPath().equals("datasources");
            }
        }) + 1).getPath();
        Preconditions.checkNotNull(dataSourceName);
        final AuthorizationInfo authorizationInfo = (AuthorizationInfo) getReq().getAttribute(AuthConfig.DRUID_AUTH_TOKEN);
        Preconditions.checkNotNull(authorizationInfo, "Security is enabled but no authorization info found in the request");
        final Access authResult = authorizationInfo.isAuthorized(new Resource(dataSourceName, ResourceType.DATASOURCE), getAction(request));
        if (!authResult.isAllowed()) {
            throw new WebApplicationException(Response.status(Response.Status.FORBIDDEN).entity(String.format("Access-Check-Result: %s", authResult.toString())).build());
        }
    }
    return request;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) Access(io.druid.server.security.Access) Resource(io.druid.server.security.Resource) PathSegment(javax.ws.rs.core.PathSegment) AuthorizationInfo(io.druid.server.security.AuthorizationInfo) Predicate(com.google.common.base.Predicate)

Example 37 with PathSegment

use of javax.ws.rs.core.PathSegment in project druid by druid-io.

the class TaskResourceFilter method filter.

@Override
public ContainerRequest filter(ContainerRequest request) {
    if (getAuthConfig().isEnabled()) {
        // This is an experimental feature, see - https://github.com/druid-io/druid/pull/2424
        final String taskId = Preconditions.checkNotNull(request.getPathSegments().get(Iterables.indexOf(request.getPathSegments(), new Predicate<PathSegment>() {

            @Override
            public boolean apply(PathSegment input) {
                return input.getPath().equals("task");
            }
        }) + 1).getPath());
        Optional<Task> taskOptional = taskStorageQueryAdapter.getTask(taskId);
        if (!taskOptional.isPresent()) {
            throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity(String.format("Cannot find any task with id: [%s]", taskId)).build());
        }
        final String dataSourceName = Preconditions.checkNotNull(taskOptional.get().getDataSource());
        final AuthorizationInfo authorizationInfo = (AuthorizationInfo) getReq().getAttribute(AuthConfig.DRUID_AUTH_TOKEN);
        Preconditions.checkNotNull(authorizationInfo, "Security is enabled but no authorization info found in the request");
        final Access authResult = authorizationInfo.isAuthorized(new Resource(dataSourceName, ResourceType.DATASOURCE), getAction(request));
        if (!authResult.isAllowed()) {
            throw new WebApplicationException(Response.status(Response.Status.FORBIDDEN).entity(String.format("Access-Check-Result: %s", authResult.toString())).build());
        }
    }
    return request;
}
Also used : Task(io.druid.indexing.common.task.Task) WebApplicationException(javax.ws.rs.WebApplicationException) Access(io.druid.server.security.Access) Resource(io.druid.server.security.Resource) PathSegment(javax.ws.rs.core.PathSegment) AuthorizationInfo(io.druid.server.security.AuthorizationInfo)

Example 38 with PathSegment

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

the class SharedFolderWebService method getFiles.

public Response getFiles(Long repoEntryKey, List<PathSegment> path, UriInfo uriInfo, HttpServletRequest httpRequest, Request request) {
    RepositoryEntry re = repositoryManager.lookupRepositoryEntry(repoEntryKey);
    if (re == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    VFSContainer container = SharedFolderManager.getInstance().getNamedSharedFolder(re, true);
    if (container == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    if (!repositoryManager.isAllowedToLaunch(RestSecurityHelper.getIdentity(httpRequest), RestSecurityHelper.getRoles(httpRequest), re)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    VFSLeaf leaf = null;
    for (PathSegment seg : path) {
        VFSItem item = container.resolve(seg.getPath());
        if (item instanceof VFSLeaf) {
            leaf = (VFSLeaf) item;
            break;
        } else if (item instanceof VFSContainer) {
            container = (VFSContainer) item;
        }
    }
    if (leaf != null) {
        Date lastModified = new Date(leaf.getLastModified());
        Response.ResponseBuilder response = request.evaluatePreconditions(lastModified);
        if (response == null) {
            String mimeType = WebappHelper.getMimeType(leaf.getName());
            if (mimeType == null)
                mimeType = MediaType.APPLICATION_OCTET_STREAM;
            response = Response.ok(leaf.getInputStream(), mimeType).lastModified(lastModified).cacheControl(cc);
        }
        return response.build();
    }
    List<VFSItem> items = container.getItems(new SystemItemFilter());
    int count = 0;
    LinkVO[] links = new LinkVO[items.size()];
    for (VFSItem item : items) {
        UriBuilder baseUriBuilder = uriInfo.getBaseUriBuilder();
        UriBuilder repoUri = baseUriBuilder.path(SharedFolderWebService.class).path(repoEntryKey.toString()).path("files");
        for (PathSegment pathSegment : path) {
            repoUri.path(pathSegment.getPath());
        }
        String uri = repoUri.path(item.getName()).build().toString();
        links[count++] = new LinkVO("self", uri, item.getName());
    }
    return Response.ok(links).build();
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) VFSContainer(org.olat.core.util.vfs.VFSContainer) VFSItem(org.olat.core.util.vfs.VFSItem) RepositoryEntry(org.olat.repository.RepositoryEntry) PathSegment(javax.ws.rs.core.PathSegment) SystemItemFilter(org.olat.core.util.vfs.filters.SystemItemFilter) Date(java.util.Date) Response(javax.ws.rs.core.Response) LinkVO(org.olat.restapi.support.vo.LinkVO) UriBuilder(javax.ws.rs.core.UriBuilder)

Example 39 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 40 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)

Aggregations

PathSegment (javax.ws.rs.core.PathSegment)67 UriInfo (javax.ws.rs.core.UriInfo)18 Test (org.junit.Test)18 ArrayList (java.util.ArrayList)13 UriBuilder (javax.ws.rs.core.UriBuilder)11 Map (java.util.Map)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 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)9 Response (javax.ws.rs.core.Response)8 HashMap (java.util.HashMap)7 SystemItemFilter (org.olat.core.util.vfs.filters.SystemItemFilter)7 List (java.util.List)6 URI (java.net.URI)5 URL (java.net.URL)5 UUID (java.util.UUID)5 WebApplicationException (javax.ws.rs.WebApplicationException)5 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)5 Date (java.util.Date)4