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();
}
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);
}
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;
}
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();
}
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;
}
Aggregations