Search in sources :

Example 51 with EntityTag

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

the class AnnouncerRESTService method getMessageAttachment.

@GET
@Path("/attachment/{ATTACHMENTNAME}")
@RESTPermit(handling = Handling.UNSECURED)
public Response getMessageAttachment(@PathParam("ATTACHMENTNAME") String attachmentName, @Context Request request) {
    if (StringUtils.isBlank(attachmentName)) {
        return Response.status(Response.Status.BAD_REQUEST).build();
    }
    AnnouncementAttachment announcementAttachment = announcementController.findAttachmentByName(attachmentName);
    if (announcementAttachment == null) {
        return Response.status(Response.Status.NOT_FOUND).build();
    }
    EntityTag tag = new EntityTag(announcementAttachment.getName());
    ResponseBuilder builder = request.evaluatePreconditions(tag);
    if (builder != null) {
        return builder.build();
    }
    CacheControl cacheControl = new CacheControl();
    cacheControl.setMustRevalidate(true);
    return Response.ok(announcementAttachment.getContent()).cacheControl(cacheControl).tag(tag).type(announcementAttachment.getContentType()).build();
}
Also used : AnnouncementAttachment(fi.otavanopisto.muikku.plugins.announcer.model.AnnouncementAttachment) EntityTag(javax.ws.rs.core.EntityTag) CacheControl(javax.ws.rs.core.CacheControl) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Example 52 with EntityTag

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

the class HtmlMaterialRESTService method findMaterial.

@GET
@Path("/{id}")
@RESTPermitUnimplemented
public Response findMaterial(@PathParam("id") Long id, @QueryParam("revision") Long revision, @Context Request request) {
    HtmlMaterial htmlMaterial = htmlMaterialController.findHtmlMaterialById(id);
    if (htmlMaterial == null) {
        return Response.status(Status.NOT_FOUND).build();
    } else {
        EntityTag tag = new EntityTag(DigestUtils.md5Hex(String.valueOf(revision == null ? htmlMaterial.getRevisionNumber() : revision)));
        ResponseBuilder builder = request.evaluatePreconditions(tag);
        if (builder != null) {
            return builder.build();
        }
        CacheControl cacheControl = new CacheControl();
        cacheControl.setMustRevalidate(true);
        if (revision == null) {
            return Response.ok(createRestModel(htmlMaterial)).build();
        } else {
            File fileRevision;
            try {
                fileRevision = coOpsApi.fileGet(id.toString(), revision);
            } catch (CoOpsNotImplementedException | CoOpsNotFoundException | CoOpsUsageException | CoOpsInternalErrorException | CoOpsForbiddenException e) {
                return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
            }
            if (fileRevision == null) {
                return Response.status(Status.NOT_FOUND).build();
            }
            return Response.ok(createRestModel(htmlMaterial, fileRevision)).build();
        }
    }
}
Also used : CoOpsUsageException(fi.foyt.coops.CoOpsUsageException) CoOpsNotImplementedException(fi.foyt.coops.CoOpsNotImplementedException) CoOpsInternalErrorException(fi.foyt.coops.CoOpsInternalErrorException) EntityTag(javax.ws.rs.core.EntityTag) CoOpsNotFoundException(fi.foyt.coops.CoOpsNotFoundException) CoOpsForbiddenException(fi.foyt.coops.CoOpsForbiddenException) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) CacheControl(javax.ws.rs.core.CacheControl) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) File(fi.foyt.coops.model.File) Path(javax.ws.rs.Path) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented) GET(javax.ws.rs.GET)

Example 53 with EntityTag

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

the class BinaryMaterialRESTService method getMaterialContent.

@GET
@Path("/{id}/content")
@RESTPermitUnimplemented
public Response getMaterialContent(@PathParam("id") Long id, @Context Request request) {
    BinaryMaterial material = binaryMaterialController.findBinaryMaterialById(id);
    if (material == null) {
        return Response.status(Status.NOT_FOUND).build();
    } else {
        EntityTag tag = new EntityTag(DigestUtils.md5Hex(String.valueOf(material.getVersion())));
        ResponseBuilder builder = request.evaluatePreconditions(tag);
        if (builder != null) {
            return builder.build();
        }
        CacheControl cacheControl = new CacheControl();
        cacheControl.setMustRevalidate(true);
        return Response.ok(material.getContent()).cacheControl(cacheControl).tag(tag).type(material.getContentType()).build();
    }
}
Also used : EntityTag(javax.ws.rs.core.EntityTag) BinaryMaterial(fi.otavanopisto.muikku.plugins.material.model.BinaryMaterial) CacheControl(javax.ws.rs.core.CacheControl) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Path(javax.ws.rs.Path) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented) GET(javax.ws.rs.GET)

Example 54 with EntityTag

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

the class WorkspaceRESTService method getWorkspaceFileContent.

@GET
@Path("/workspaces/{WORKSPACEID}/workspacefile/{FILEIDENTIFIER}")
@RESTPermit(handling = Handling.INLINE)
public Response getWorkspaceFileContent(@PathParam("WORKSPACEID") Long workspaceId, @PathParam("FILEIDENTIFIER") String fileIdentifier, @Context Request request) {
    WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceId);
    if (workspaceEntity == null)
        return Response.status(Status.BAD_REQUEST).build();
    WorkspaceEntityFile imageFile = workspaceEntityFileController.findWorkspaceEntityFile(workspaceEntity, fileIdentifier);
    if (imageFile == null)
        return Response.status(Status.NOT_FOUND).build();
    StreamingOutput output = s -> fileController.outputFileToStream("workspace", imageFile.getDiskName(), s);
    String contentType = imageFile.getContentType();
    String tagIdentifier = String.format("%d-%s-%d", imageFile.getWorkspaceEntity(), imageFile.getDiskName(), imageFile.getLastModified().getTime());
    EntityTag tag = new EntityTag(DigestUtils.md5Hex(String.valueOf(tagIdentifier)));
    ResponseBuilder builder = request.evaluatePreconditions(tag);
    if (builder != null) {
        return builder.build();
    }
    CacheControl cacheControl = new CacheControl();
    cacheControl.setMustRevalidate(true);
    return Response.ok().cacheControl(cacheControl).tag(tag).type(contentType).entity(output).build();
}
Also used : WorkspaceEntityFileRESTModel(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceEntityFileRESTModel) Arrays(java.util.Arrays) Produces(javax.ws.rs.Produces) WorkspaceCompositeReply(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceCompositeReply) EnvironmentRoleArchetype(fi.otavanopisto.muikku.model.users.EnvironmentRoleArchetype) WorkspaceMaterialFileFieldAnswerFile(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswerFile) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) Material(fi.otavanopisto.muikku.plugins.material.model.Material) Flag(fi.otavanopisto.muikku.model.users.Flag) StringUtils(org.apache.commons.lang3.StringUtils) User(fi.otavanopisto.muikku.schooldata.entity.User) WorkspaceEntityFileController(fi.otavanopisto.muikku.plugins.workspace.WorkspaceEntityFileController) WorkspaceMaterialAudioFieldAnswerClip(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAudioFieldAnswerClip) Map(java.util.Map) CourseMetaController(fi.otavanopisto.muikku.schooldata.CourseMetaController) WorkspaceMaterialCompositeReply(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceMaterialCompositeReply) Instance(javax.enterprise.inject.Instance) WorkspaceDetails(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceDetails) WorkspaceFeeInfo(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceFeeInfo) WorkspaceFolder(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceFolder) WorkspaceEntityController(fi.otavanopisto.muikku.schooldata.WorkspaceEntityController) AssessmentRequestController(fi.otavanopisto.muikku.plugins.assessmentrequest.AssessmentRequestController) SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) Set(java.util.Set) TempFileUtils(fi.otavanopisto.muikku.files.TempFileUtils) IOUtils(org.apache.commons.io.IOUtils) WorkspaceMaterialProducer(fi.otavanopisto.muikku.model.workspace.WorkspaceMaterialProducer) WorkspaceType(fi.otavanopisto.muikku.schooldata.entity.WorkspaceType) WorkspaceFieldIOException(fi.otavanopisto.muikku.plugins.workspace.fieldio.WorkspaceFieldIOException) DatatypeConverter(javax.xml.bind.DatatypeConverter) WorkspaceIndexer(fi.otavanopisto.muikku.plugins.search.WorkspaceIndexer) ZipOutputStream(java.util.zip.ZipOutputStream) GET(javax.ws.rs.GET) MuikkuPermissions(fi.otavanopisto.muikku.security.MuikkuPermissions) WorkspaceMaterialField(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialField) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented) Handling(fi.otavanopisto.security.rest.RESTPermit.Handling) WorkspaceVisitController(fi.otavanopisto.muikku.plugins.workspace.WorkspaceVisitController) ArrayList(java.util.ArrayList) SessionController(fi.otavanopisto.muikku.session.SessionController) WorkspaceJournalEntryRESTModel(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceJournalEntryRESTModel) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) WorkspaceMaterialReplyController(fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialReplyController) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) WorkspaceMaterialFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceMaterialFieldAnswer) IOException(java.io.IOException) MaterialViewRestrict(fi.otavanopisto.muikku.plugins.material.model.MaterialViewRestrict) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) Stateful(javax.ejb.Stateful) RestCatchSchoolDataExceptions(fi.otavanopisto.muikku.schooldata.RestCatchSchoolDataExceptions) WorkspaceUserEntityController(fi.otavanopisto.muikku.users.WorkspaceUserEntityController) SearchProvider(fi.otavanopisto.muikku.search.SearchProvider) WorkspaceMaterialFieldController(fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialFieldController) WorkspaceNodeType(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNodeType) MaterialController(fi.otavanopisto.muikku.plugins.material.MaterialController) WorkspaceMaterialController(fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialController) WorkspaceMaterialReplyState(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialReplyState) Date(java.util.Date) Path(javax.ws.rs.Path) BooleanPredicate(fi.otavanopisto.muikku.model.base.BooleanPredicate) QueryParam(javax.ws.rs.QueryParam) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultValue(javax.ws.rs.DefaultValue) Any(javax.enterprise.inject.Any) Workspace(fi.otavanopisto.muikku.schooldata.entity.Workspace) ZipEntry(java.util.zip.ZipEntry) Sort(fi.otavanopisto.muikku.search.SearchProvider.Sort) DELETE(javax.ws.rs.DELETE) Context(javax.ws.rs.core.Context) WorkspaceJournalEntry(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceJournalEntry) WorkspaceMaterialAssignmentType(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAssignmentType) Collection(java.util.Collection) WorkspaceEntityFile(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceEntityFile) WorkspaceMaterialContainsAnswersExeption(fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialContainsAnswersExeption) StreamingOutput(javax.ws.rs.core.StreamingOutput) Logger(java.util.logging.Logger) GradingController(fi.otavanopisto.muikku.schooldata.GradingController) Objects(java.util.Objects) EducationType(fi.otavanopisto.muikku.schooldata.entity.EducationType) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) UserEntityController(fi.otavanopisto.muikku.users.UserEntityController) Response(javax.ws.rs.core.Response) UserController(fi.otavanopisto.muikku.users.UserController) WorkspaceNode(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode) WorkspaceMaterialReply(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceMaterialReply) WorkspaceRoleArchetype(fi.otavanopisto.muikku.model.workspace.WorkspaceRoleArchetype) Request(javax.ws.rs.core.Request) CacheControl(javax.ws.rs.core.CacheControl) WorkspaceStudent(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceStudent) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) UserIndexer(fi.otavanopisto.muikku.plugins.search.UserIndexer) WorkspaceController(fi.otavanopisto.muikku.schooldata.WorkspaceController) PathParam(javax.ws.rs.PathParam) WorkspaceRootFolder(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceRootFolder) FlagController(fi.otavanopisto.muikku.users.FlagController) WorkspaceUser(fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser) HashMap(java.util.HashMap) WorkspaceMaterialDeleteError(fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialDeleteError) Level(java.util.logging.Level) MessagingWidget(fi.otavanopisto.muikku.controller.messaging.MessagingWidget) HashSet(java.util.HashSet) Inject(javax.inject.Inject) FileController(fi.otavanopisto.muikku.plugins.data.FileController) CollectionUtils(org.apache.commons.collections.CollectionUtils) Status(javax.ws.rs.core.Response.Status) OutputStream(java.io.OutputStream) POST(javax.ws.rs.POST) Iterator(java.util.Iterator) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceStaffMember(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceStaffMember) SearchResult(fi.otavanopisto.muikku.search.SearchResult) EntityTag(javax.ws.rs.core.EntityTag) WorkspaceJournalController(fi.otavanopisto.muikku.plugins.workspace.WorkspaceJournalController) PluginRESTService(fi.otavanopisto.muikku.plugin.PluginRESTService) RequestScoped(javax.enterprise.context.RequestScoped) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) QueryFieldController(fi.otavanopisto.muikku.plugins.material.QueryFieldController) WorkspaceMaterialFieldAnswerController(fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialFieldAnswerController) SchoolDataBridgeSessionController(fi.otavanopisto.muikku.schooldata.SchoolDataBridgeSessionController) PUT(javax.ws.rs.PUT) Comparator(java.util.Comparator) DigestUtils(org.apache.commons.codec.digest.DigestUtils) Collections(java.util.Collections) InputStream(java.io.InputStream) WorkspaceEntityFile(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceEntityFile) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) StreamingOutput(javax.ws.rs.core.StreamingOutput) EntityTag(javax.ws.rs.core.EntityTag) CacheControl(javax.ws.rs.core.CacheControl) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Example 55 with EntityTag

use of javax.ws.rs.core.EntityTag in project graylog2-server by Graylog2.

the class CollectorResource method listCollectors.

@GET
@Timed
@RequiresPermissions(SidecarRestPermissions.COLLECTORS_READ)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "List all collectors")
public Response listCollectors(@Context HttpHeaders httpHeaders) {
    String ifNoneMatch = httpHeaders.getHeaderString("If-None-Match");
    Boolean etagCached = false;
    Response.ResponseBuilder builder = Response.noContent();
    // check if client is up to date with a known valid etag
    if (ifNoneMatch != null) {
        EntityTag etag = new EntityTag(ifNoneMatch.replaceAll("\"", ""));
        if (etagService.isPresent(etag.toString())) {
            etagCached = true;
            builder = Response.notModified();
            builder.tag(etag);
        }
    }
    // fetch collector list from database if client is outdated
    if (!etagCached) {
        final List<Collector> result = this.collectorService.all();
        CollectorListResponse collectorListResponse = CollectorListResponse.create(result.size(), result);
        // add new etag to cache
        String etagString = collectorsToEtag(collectorListResponse);
        EntityTag collectorsEtag = new EntityTag(etagString);
        builder = Response.ok(collectorListResponse);
        builder.tag(collectorsEtag);
        etagService.put(collectorsEtag.toString());
    }
    // set cache control
    CacheControl cacheControl = new CacheControl();
    cacheControl.setNoTransform(true);
    cacheControl.setPrivate(true);
    builder.cacheControl(cacheControl);
    return builder.build();
}
Also used : CollectorListResponse(org.graylog.plugins.sidecar.rest.responses.CollectorListResponse) Response(javax.ws.rs.core.Response) CollectorSummaryResponse(org.graylog.plugins.sidecar.rest.responses.CollectorSummaryResponse) CollectorListResponse(org.graylog.plugins.sidecar.rest.responses.CollectorListResponse) Collector(org.graylog.plugins.sidecar.rest.models.Collector) EntityTag(javax.ws.rs.core.EntityTag) CacheControl(javax.ws.rs.core.CacheControl) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

EntityTag (javax.ws.rs.core.EntityTag)73 GET (javax.ws.rs.GET)24 Response (javax.ws.rs.core.Response)24 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)23 Path (javax.ws.rs.Path)21 CacheControl (javax.ws.rs.core.CacheControl)20 Test (org.junit.Test)18 Produces (javax.ws.rs.Produces)13 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 List (java.util.List)5 ContainerResponse (org.everrest.core.impl.ContainerResponse)5 Principal (com.yahoo.athenz.auth.Principal)4 EnvironmentUser (fi.otavanopisto.muikku.model.users.EnvironmentUser)4 ArrayList (java.util.ArrayList)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4