Search in sources :

Example 26 with EntityTag

use of javax.ws.rs.core.EntityTag in project syncope by apache.

the class SyncopeEnduserSession method getService.

public <T> T getService(final String etag, final Class<T> serviceClass) {
    T serviceInstance = getService(serviceClass);
    WebClient.client(serviceInstance).match(new EntityTag(etag), false).type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON);
    return serviceInstance;
}
Also used : EntityTag(javax.ws.rs.core.EntityTag)

Example 27 with EntityTag

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

the class RepositoryEntryResource method getById.

/**
 * get a resource in the repository
 * @response.representation.200.qname {http://www.example.com}repositoryEntryVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc Get the repository resource
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_REPOENTRYVO}
 * @response.representation.404.doc The repository entry not found
 * @param repoEntryKey The key or soft key of the repository entry
 * @param request The REST request
 * @return
 */
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response getById(@PathParam("repoEntryKey") String repoEntryKey, @Context Request request) {
    RepositoryEntry re = lookupRepositoryEntry(repoEntryKey);
    if (re == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    Date lastModified = re.getLastModified();
    Response.ResponseBuilder response;
    if (lastModified == null) {
        EntityTag eTag = ObjectFactory.computeEtag(re);
        response = request.evaluatePreconditions(eTag);
        if (response == null) {
            RepositoryEntryVO vo = ObjectFactory.get(re);
            response = Response.ok(vo).tag(eTag).lastModified(lastModified);
        }
    } else {
        EntityTag eTag = ObjectFactory.computeEtag(re);
        response = request.evaluatePreconditions(lastModified, eTag);
        if (response == null) {
            RepositoryEntryVO vo = ObjectFactory.get(re);
            response = Response.ok(vo).tag(eTag).lastModified(lastModified);
        }
    }
    return response.build();
}
Also used : Response(javax.ws.rs.core.Response) RepositoryEntryVO(org.olat.restapi.support.vo.RepositoryEntryVO) EntityTag(javax.ws.rs.core.EntityTag) RepositoryEntry(org.olat.repository.RepositoryEntry) Date(java.util.Date) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 28 with EntityTag

use of javax.ws.rs.core.EntityTag in project muikku by otavanopisto.

the class UserRESTService method findStudent.

@GET
@Path("/students/{ID}")
@RESTPermit(handling = Handling.INLINE)
public Response findStudent(@Context Request request, @PathParam("ID") String id) {
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.FORBIDDEN).build();
    }
    SchoolDataIdentifier studentIdentifier = SchoolDataIdentifier.fromId(id);
    if (studentIdentifier == null) {
        return Response.status(Response.Status.BAD_REQUEST).entity(String.format("Invalid studentIdentifier %s", id)).build();
    }
    UserEntity userEntity = userEntityController.findUserEntityByUserIdentifier(studentIdentifier);
    if (userEntity == null) {
        return Response.status(Status.NOT_FOUND).entity("UserEntity not found").build();
    }
    // Bug fix #2966: REST endpoint should only return students
    EnvironmentUser environmentUser = environmentUserController.findEnvironmentUserByUserEntity(userEntity);
    if (environmentUser != null) {
        EnvironmentRoleEntity userRole = environmentUser.getRole();
        if (userRole == null || userRole.getArchetype() != EnvironmentRoleArchetype.STUDENT) {
            return Response.status(Status.NOT_FOUND).build();
        }
    }
    EntityTag tag = new EntityTag(DigestUtils.md5Hex(String.valueOf(userEntity.getVersion())));
    ResponseBuilder builder = request.evaluatePreconditions(tag);
    if (builder != null) {
        return builder.build();
    }
    CacheControl cacheControl = new CacheControl();
    cacheControl.setMustRevalidate(true);
    // TODO: There's no permission handling, this is relying on schooldatacontroller to check for permission
    User user = userController.findUserByIdentifier(studentIdentifier);
    if (user == null) {
        return Response.status(Status.NOT_FOUND).entity("User not found").build();
    }
    String emailAddress = userEmailEntityController.getUserDefaultEmailAddress(userEntity, true);
    Date studyStartDate = user.getStudyStartDate() != null ? Date.from(user.getStudyStartDate().toInstant()) : null;
    Date studyEndDate = user.getStudyEndDate() != null ? Date.from(user.getStudyEndDate().toInstant()) : null;
    Date studyTimeEnd = user.getStudyTimeEnd() != null ? Date.from(user.getStudyTimeEnd().toInstant()) : null;
    Student student = new Student(studentIdentifier.toId(), user.getFirstName(), user.getLastName(), user.getNickName(), user.getStudyProgrammeName(), false, user.getNationality(), user.getLanguage(), user.getMunicipality(), user.getSchool(), emailAddress, studyStartDate, studyEndDate, studyTimeEnd, user.getCurriculumIdentifier(), userEntity.getUpdatedByStudent());
    return Response.ok(student).cacheControl(cacheControl).tag(tag).build();
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) EnvironmentUser(fi.otavanopisto.muikku.model.users.EnvironmentUser) EnvironmentRoleEntity(fi.otavanopisto.muikku.model.users.EnvironmentRoleEntity) User(fi.otavanopisto.muikku.schooldata.entity.User) EnvironmentUser(fi.otavanopisto.muikku.model.users.EnvironmentUser) EntityTag(javax.ws.rs.core.EntityTag) CacheControl(javax.ws.rs.core.CacheControl) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) FlagStudent(fi.otavanopisto.muikku.model.users.FlagStudent) Student(fi.otavanopisto.muikku.rest.model.Student) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) Date(java.util.Date) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Example 29 with EntityTag

use of javax.ws.rs.core.EntityTag in project muikku by otavanopisto.

the class MetaRESTService method getResources.

@GET
@Path("/resources")
@RESTPermit(handling = Handling.UNSECURED)
public Response getResources(@Context Request request, @QueryParam("format") String format) {
    EntityTag tag = new EntityTag(ETAG);
    ResponseBuilder builder = request.evaluatePreconditions(tag);
    if (builder != null) {
        return builder.build();
    }
    List<String> resources = new ArrayList<>();
    ResourceMethodRegistry registry = (ResourceMethodRegistry) dispatcher.getRegistry();
    Set<Entry<String, java.util.List<org.jboss.resteasy.core.ResourceInvoker>>> entries = registry.getBounded().entrySet();
    for (Entry<String, java.util.List<org.jboss.resteasy.core.ResourceInvoker>> entry : entries) {
        String path = entry.getKey();
        resources.add(path);
    }
    CacheControl cacheControl = new CacheControl();
    cacheControl.setMaxAge(-1);
    cacheControl.setPrivate(false);
    cacheControl.setMustRevalidate(false);
    Collections.sort(resources);
    if (StringUtils.isNotBlank(format) && "js".equals(format)) {
        try {
            return Response.ok(String.format("var META_RESOURCES = %s", new ObjectMapper().writeValueAsString(resources)), "text/javascript").cacheControl(cacheControl).tag(tag).build();
        } catch (JsonProcessingException e) {
            return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
        }
    } else {
        return Response.ok(resources).cacheControl(cacheControl).tag(tag).build();
    }
}
Also used : ArrayList(java.util.ArrayList) ResourceMethodRegistry(org.jboss.resteasy.core.ResourceMethodRegistry) Entry(java.util.Map.Entry) EntityTag(javax.ws.rs.core.EntityTag) ArrayList(java.util.ArrayList) List(java.util.List) CacheControl(javax.ws.rs.core.CacheControl) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Example 30 with EntityTag

use of javax.ws.rs.core.EntityTag in project muikku by otavanopisto.

the class ForumRESTService method findArea.

@GET
@Path("/areas/{AREAID}")
@RESTPermit(handling = Handling.INLINE)
public Response findArea(@Context Request request, @PathParam("AREAID") Long areaId) {
    ForumArea forumArea = forumController.getForumArea(areaId);
    if (forumArea != null) {
        if (!(forumArea instanceof EnvironmentForumArea)) {
            logger.severe(String.format("Trying to access forum %d via incorrect REST endpoint", forumArea.getId()));
            return Response.status(Status.NOT_FOUND).build();
        }
        if (sessionController.hasEnvironmentPermission(ForumResourcePermissionCollection.FORUM_ACCESSENVIRONMENTFORUM)) {
            Long numThreads = forumController.getThreadCount(forumArea);
            EntityTag tag = new EntityTag(DigestUtils.md5Hex(String.valueOf(forumArea.getVersion()) + String.valueOf(numThreads)));
            ResponseBuilder builder = request.evaluatePreconditions(tag);
            if (builder != null) {
                return builder.build();
            }
            CacheControl cacheControl = new CacheControl();
            cacheControl.setMustRevalidate(true);
            ForumAreaRESTModel result = new ForumAreaRESTModel(forumArea.getId(), forumArea.getName(), forumArea.getDescription(), forumArea.getGroup() != null ? forumArea.getGroup().getId() : null, numThreads);
            return Response.ok(result).cacheControl(cacheControl).tag(tag).build();
        } else {
            return Response.status(Status.FORBIDDEN).build();
        }
    } else {
        return Response.status(Status.NOT_FOUND).build();
    }
}
Also used : EntityTag(javax.ws.rs.core.EntityTag) ForumArea(fi.otavanopisto.muikku.plugins.forum.model.ForumArea) EnvironmentForumArea(fi.otavanopisto.muikku.plugins.forum.model.EnvironmentForumArea) CacheControl(javax.ws.rs.core.CacheControl) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) EnvironmentForumArea(fi.otavanopisto.muikku.plugins.forum.model.EnvironmentForumArea) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Aggregations

EntityTag (javax.ws.rs.core.EntityTag)77 Response (javax.ws.rs.core.Response)28 GET (javax.ws.rs.GET)27 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)25 Path (javax.ws.rs.Path)24 CacheControl (javax.ws.rs.core.CacheControl)20 Test (org.junit.Test)18 Produces (javax.ws.rs.Produces)16 Date (java.util.Date)10 Test (org.testng.annotations.Test)9 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)8 Timestamp (com.yahoo.rdl.Timestamp)5 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)5 RESTPermitUnimplemented (fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 List (java.util.List)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 ContainerResponse (org.everrest.core.impl.ContainerResponse)5 Principal (com.yahoo.athenz.auth.Principal)4