Search in sources :

Example 46 with EntityTag

use of javax.ws.rs.core.EntityTag in project mycore by MyCoRe-Org.

the class MCRViewerResource method show.

@GET
@Path("{derivate}{path: (/[^?#]*)?}")
public Response show(@Context HttpServletRequest request, @Context Request jaxReq, @Context ServletContext context, @Context ServletConfig config) throws Exception {
    MCRContent content = getContent(request);
    String contentETag = content.getETag();
    Response.ResponseBuilder responseBuilder = null;
    EntityTag eTag = contentETag == null ? null : new EntityTag(contentETag);
    if (eTag != null) {
        responseBuilder = jaxReq.evaluatePreconditions(eTag);
    }
    if (responseBuilder == null) {
        responseBuilder = Response.ok(content.asByteArray(), MediaType.valueOf(content.getMimeType()));
    }
    if (eTag != null) {
        responseBuilder.tag(eTag);
    }
    if (content.isUsingSession()) {
        CacheControl cc = new CacheControl();
        cc.setPrivate(true);
        cc.setMaxAge(0);
        cc.setMustRevalidate(true);
        responseBuilder.cacheControl(cc);
    }
    return responseBuilder.build();
}
Also used : Response(javax.ws.rs.core.Response) EntityTag(javax.ws.rs.core.EntityTag) CacheControl(javax.ws.rs.core.CacheControl) MCRContent(org.mycore.common.content.MCRContent) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 47 with EntityTag

use of javax.ws.rs.core.EntityTag in project mycore by MyCoRe-Org.

the class MCRSassResource method getCSS.

@GET
@Path("{fileName:.+}")
@Produces("text/css")
public Response getCSS(@PathParam("fileName") String name, @Context Request request) {
    try {
        MCRServletContextResourceImporter importer = new MCRServletContextResourceImporter(context);
        Optional<String> cssFile = MCRSassCompilerManager.getInstance().getCSSFile(name, Stream.of(importer).collect(Collectors.toList()));
        if (cssFile.isPresent()) {
            CacheControl cc = new CacheControl();
            cc.setMaxAge(SECONDS_OF_ONE_DAY);
            String etagString = MCRSassCompilerManager.getInstance().getLastMD5(name).get();
            EntityTag etag = new EntityTag(etagString);
            Response.ResponseBuilder builder = request.evaluatePreconditions(etag);
            if (builder != null) {
                return builder.cacheControl(cc).tag(etag).build();
            }
            return Response.ok().status(Response.Status.OK).cacheControl(cc).tag(etag).entity(cssFile.get()).build();
        } else {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
    } catch (IOException | CompilationException e) {
        StreamingOutput so = (OutputStream os) -> e.printStackTrace(new PrintStream(os, true, "UTF-8"));
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(so).build();
    }
}
Also used : CompilationException(io.bit3.jsass.CompilationException) PrintStream(java.io.PrintStream) OutputStream(java.io.OutputStream) StreamingOutput(javax.ws.rs.core.StreamingOutput) IOException(java.io.IOException) MCRServletContextResourceImporter(org.mycore.sass.MCRServletContextResourceImporter) Response(javax.ws.rs.core.Response) EntityTag(javax.ws.rs.core.EntityTag) CacheControl(javax.ws.rs.core.CacheControl) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 48 with EntityTag

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

the class CommunicatorRESTService 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();
    }
    CommunicatorMessageAttachment communicatorMessageAttachment = communicatorAttachmentController.findByName(attachmentName);
    if (communicatorMessageAttachment == null) {
        return Response.status(Response.Status.NOT_FOUND).build();
    }
    EntityTag tag = new EntityTag(communicatorMessageAttachment.getName());
    ResponseBuilder builder = request.evaluatePreconditions(tag);
    if (builder != null) {
        return builder.build();
    }
    CacheControl cacheControl = new CacheControl();
    cacheControl.setMustRevalidate(true);
    return Response.ok(communicatorMessageAttachment.getContent()).cacheControl(cacheControl).tag(tag).type(communicatorMessageAttachment.getContentType()).build();
}
Also used : CommunicatorMessageAttachment(fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageAttachment) 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 49 with EntityTag

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

the class UserRESTService method findUserBasicInfo.

@GET
@Path("/users/{ID}/basicinfo")
@RESTPermitUnimplemented
public Response findUserBasicInfo(@Context Request request, @PathParam("ID") String id) {
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.FORBIDDEN).build();
    }
    UserEntity userEntity = null;
    SchoolDataIdentifier userIdentifier = SchoolDataIdentifier.fromId(id);
    if (userIdentifier == null) {
        if (!StringUtils.isNumeric(id)) {
            return Response.status(Response.Status.BAD_REQUEST).entity(String.format("Invalid user id %s", id)).build();
        }
        userEntity = userEntityController.findUserEntityById(NumberUtils.createLong(id));
        userIdentifier = new SchoolDataIdentifier(userEntity.getDefaultIdentifier(), userEntity.getDefaultSchoolDataSource().getIdentifier());
    } else {
        userEntity = userEntityController.findUserEntityByUserIdentifier(userIdentifier);
    }
    if (userEntity == null) {
        return Response.status(Response.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);
    schoolDataBridgeSessionController.startSystemSession();
    try {
        User user = userController.findUserByIdentifier(userIdentifier);
        if (user == null) {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
        // TODO: User image
        boolean hasImage = false;
        return Response.ok(new UserBasicInfo(userEntity.getId(), user.getFirstName(), user.getLastName(), user.getNickName(), user.getStudyProgrammeName(), hasImage, user.hasEvaluationFees(), user.getCurriculumIdentifier())).cacheControl(cacheControl).tag(tag).build();
    } finally {
        schoolDataBridgeSessionController.endSystemSession();
    }
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) UserBasicInfo(fi.otavanopisto.muikku.rest.model.UserBasicInfo) 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) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) Path(javax.ws.rs.Path) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented) GET(javax.ws.rs.GET)

Example 50 with EntityTag

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

the class UserRESTService method findUser.

@GET
@Path("/users/{ID}")
@RESTPermitUnimplemented
public Response findUser(@Context Request request, @PathParam("ID") Long id) {
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.FORBIDDEN).build();
    }
    UserEntity userEntity = userEntityController.findUserEntityById(id);
    if (userEntity == null) {
        return Response.status(Response.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);
    User user = userController.findUserByDataSourceAndIdentifier(userEntity.getDefaultSchoolDataSource(), userEntity.getDefaultIdentifier());
    if (user == null) {
        return Response.status(Response.Status.NOT_FOUND).build();
    }
    return Response.ok(createRestModel(userEntity, user)).cacheControl(cacheControl).tag(tag).build();
}
Also used : 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) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) Path(javax.ws.rs.Path) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented) GET(javax.ws.rs.GET)

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