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