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