Search in sources :

Example 61 with PathSegment

use of javax.ws.rs.core.PathSegment in project openolat by klemens.

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 62 with PathSegment

use of javax.ws.rs.core.PathSegment in project incubator-atlas by apache.

the class TaxonomyServiceTest method testGetSubTerms_instance.

@Test
public void testGetSubTerms_instance() throws Exception {
    MetadataService metadataService = createStrictMock(MetadataService.class);
    AtlasTypeDefStore typeDefStore = createStrictMock(AtlasTypeDefStore.class);
    ResourceProvider taxonomyResourceProvider = createStrictMock(ResourceProvider.class);
    ResourceProvider termResourceProvider = createStrictMock(ResourceProvider.class);
    UriInfo uriInfo = createNiceMock(UriInfo.class);
    URI uri = new URI("http://localhost:21000/api/atlas/v1/taxonomies/testTaxonomy/terms/testTerm/terms/testTerm2");
    JsonSerializer serializer = createStrictMock(JsonSerializer.class);
    PathSegment segment1 = createNiceMock(PathSegment.class);
    PathSegment segment2 = createNiceMock(PathSegment.class);
    PathSegment segment3 = createNiceMock(PathSegment.class);
    Capture<Request> requestCapture = newCapture();
    Collection<Map<String, Object>> resultPropertyMaps = new ArrayList<>();
    Map<String, Object> propertyMap = new HashMap<>();
    propertyMap.put("name", "testTaxonomy.testTerm.testTerm2");
    resultPropertyMaps.add(propertyMap);
    Result result = new Result(resultPropertyMaps);
    // set mock expectations
    expect(uriInfo.getRequestUri()).andReturn(uri);
    expect(uriInfo.getPathSegments()).andReturn(Arrays.asList(segment1, segment2, segment3));
    expect(segment3.getPath()).andReturn("testTerm2");
    expect(termResourceProvider.getResourceById(capture(requestCapture))).andReturn(result);
    expect(serializer.serialize(result, uriInfo)).andReturn("Taxonomy Term Get Response");
    expect(metadataService.getTypeDefinition(TaxonomyResourceProvider.TAXONOMY_TERM_TYPE)).andReturn(TaxonomyResourceProvider.TAXONOMY_TERM_TYPE + "-definition");
    replay(uriInfo, metadataService, taxonomyResourceProvider, termResourceProvider, serializer, segment1, segment2, segment3);
    // instantiate service and invoke method being tested
    TestTaxonomyService service = new TestTaxonomyService(metadataService, typeDefStore, taxonomyResourceProvider, termResourceProvider, serializer);
    Response response = service.getSubTerms(null, uriInfo, "testTaxonomy", "testTerm", "/terms/testTerm2");
    Request request = requestCapture.getValue();
    assertEquals(request.getQueryProperties().size(), 1);
    TermPath termPath = (TermPath) request.getQueryProperties().get("termPath");
    assertEquals(termPath.getFullyQualifiedName(), "testTaxonomy.testTerm.testTerm2");
    assertNull(request.getQueryString());
    assertEquals(response.getStatus(), 200);
    assertEquals(response.getEntity(), "Taxonomy Term Get Response");
    verify(uriInfo, taxonomyResourceProvider, termResourceProvider, serializer, segment1, segment2, segment3);
}
Also used : HashMap(java.util.HashMap) Request(org.apache.atlas.catalog.Request) ArrayList(java.util.ArrayList) JsonSerializer(org.apache.atlas.catalog.JsonSerializer) PathSegment(javax.ws.rs.core.PathSegment) URI(java.net.URI) MetadataService(org.apache.atlas.services.MetadataService) AtlasTypeDefStore(org.apache.atlas.store.AtlasTypeDefStore) Result(org.apache.atlas.catalog.Result) Response(javax.ws.rs.core.Response) TaxonomyResourceProvider(org.apache.atlas.catalog.TaxonomyResourceProvider) ResourceProvider(org.apache.atlas.catalog.ResourceProvider) TermPath(org.apache.atlas.catalog.TermPath) HashMap(java.util.HashMap) Map(java.util.Map) UriInfo(javax.ws.rs.core.UriInfo) Test(org.testng.annotations.Test)

Example 63 with PathSegment

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

the class TaskResourceFilterTest method getPathSegments.

private List<PathSegment> getPathSegments(String path) {
    String[] segments = path.split("/");
    List<PathSegment> pathSegments = new ArrayList<>();
    for (final String segment : segments) {
        pathSegments.add(new PathSegment() {

            @Override
            public String getPath() {
                return segment;
            }

            @Override
            public MultivaluedMap<String, String> getMatrixParameters() {
                return null;
            }
        });
    }
    return pathSegments;
}
Also used : ArrayList(java.util.ArrayList) PathSegment(javax.ws.rs.core.PathSegment) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 64 with PathSegment

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

the class ResourceFilterTestHelper method setUpMockExpectations.

public void setUpMockExpectations(String requestPath, boolean authCheckResult, String requestMethod) {
    EasyMock.expect(request.getPath()).andReturn(requestPath).anyTimes();
    EasyMock.expect(request.getPathSegments()).andReturn(ImmutableList.copyOf(Iterables.transform(Arrays.asList(requestPath.split("/")), new Function<String, PathSegment>() {

        @Override
        public PathSegment apply(final String input) {
            return new PathSegment() {

                @Override
                public String getPath() {
                    return input;
                }

                @Override
                public MultivaluedMap<String, String> getMatrixParameters() {
                    return null;
                }
            };
        }
    }))).anyTimes();
    EasyMock.expect(request.getMethod()).andReturn(requestMethod).anyTimes();
    EasyMock.expect(req.getAttribute(AuthConfig.DRUID_ALLOW_UNSECURED_PATH)).andReturn(null).anyTimes();
    EasyMock.expect(req.getAttribute(AuthConfig.DRUID_AUTHORIZATION_CHECKED)).andReturn(null).anyTimes();
    AuthenticationResult authenticationResult = new AuthenticationResult("druid", "druid", null, null);
    EasyMock.expect(req.getAttribute(AuthConfig.DRUID_AUTHENTICATION_RESULT)).andReturn(authenticationResult).atLeastOnce();
    req.setAttribute(AuthConfig.DRUID_AUTHORIZATION_CHECKED, authCheckResult);
    EasyMock.expectLastCall().anyTimes();
    EasyMock.expect(authorizerMapper.getAuthorizer(EasyMock.anyString())).andReturn(new Authorizer() {

        @Override
        public Access authorize(AuthenticationResult authenticationResult1, Resource resource, Action action) {
            return new Access(authCheckResult);
        }
    }).atLeastOnce();
}
Also used : Function(com.google.common.base.Function) Action(org.apache.druid.server.security.Action) Authorizer(org.apache.druid.server.security.Authorizer) Resource(org.apache.druid.server.security.Resource) Access(org.apache.druid.server.security.Access) PathSegment(javax.ws.rs.core.PathSegment) AuthenticationResult(org.apache.druid.server.security.AuthenticationResult)

Example 65 with PathSegment

use of javax.ws.rs.core.PathSegment in project tomee by apache.

the class URITemplate method match.

public boolean match(String uri, MultivaluedMap<String, String> templateVariableToValue) {
    if (uri == null) {
        return (templateRegexPattern == null) ? true : false;
    }
    if (templateRegexPattern == null) {
        return false;
    }
    Matcher m = templateRegexPattern.matcher(uri);
    if (!m.matches() || template.equals(SLASH) && uri.startsWith(SLASH_QUOTE)) {
        if (uri.contains(";")) {
            // we might be trying to match one or few path segments
            // containing matrix
            // parameters against a clear path segment as in @Path("base").
            List<PathSegment> pList = JAXRSUtils.getPathSegments(template, false);
            List<PathSegment> uList = JAXRSUtils.getPathSegments(uri, false);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < uList.size(); i++) {
                final String segment;
                if (pList.size() > i && pList.get(i).getPath().indexOf('{') == -1) {
                    segment = uList.get(i).getPath();
                } else {
                    segment = HttpUtils.fromPathSegment(uList.get(i));
                }
                if (!segment.isEmpty()) {
                    sb.append(SLASH);
                }
                sb.append(segment);
            }
            uri = sb.toString();
            if (uri.isEmpty()) {
                uri = SLASH;
            }
            m = templateRegexPattern.matcher(uri);
            if (!m.matches()) {
                return false;
            }
        } else {
            return false;
        }
    }
    // Assign the matched template values to template variables
    int groupCount = m.groupCount();
    int i = 1;
    for (String name : variables) {
        while (i <= groupCount) {
            String value = m.group(i++);
            if ((value == null || value.length() == 0 && i < groupCount) && variables.size() + 1 < groupCount) {
                continue;
            }
            templateVariableToValue.add(name, value);
            break;
        }
    }
    // The right hand side value, might be used to further resolve
    // sub-resources.
    String finalGroup = i > groupCount ? SLASH : m.group(groupCount);
    if (finalGroup == null || finalGroup.startsWith(SLASH_QUOTE)) {
        finalGroup = SLASH;
    }
    templateVariableToValue.putSingle(FINAL_MATCH_GROUP, finalGroup);
    return true;
}
Also used : Matcher(java.util.regex.Matcher) PathSegment(javax.ws.rs.core.PathSegment)

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