use of fi.otavanopisto.muikku.plugins.announcer.model.AnnouncementAttachment in project muikku by otavanopisto.
the class AnnouncementAttachmentDAO method create.
public AnnouncementAttachment create(String name, String contentType, byte[] content) {
AnnouncementAttachment announcementAttachment = new AnnouncementAttachment();
announcementAttachment.setName(name);
announcementAttachment.setContentType(contentType);
announcementAttachment.setContent(content);
return persist(announcementAttachment);
}
use of fi.otavanopisto.muikku.plugins.announcer.model.AnnouncementAttachment in project muikku by otavanopisto.
the class AnnouncementAttachmentDAO method findByName.
public AnnouncementAttachment findByName(String name) {
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<AnnouncementAttachment> criteria = criteriaBuilder.createQuery(AnnouncementAttachment.class);
Root<AnnouncementAttachment> root = criteria.from(AnnouncementAttachment.class);
criteria.select(root);
criteria.where(criteriaBuilder.equal(root.get(AnnouncementAttachment_.name), name));
return getSingleResult(entityManager.createQuery(criteria));
}
use of fi.otavanopisto.muikku.plugins.announcer.model.AnnouncementAttachment 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();
}
use of fi.otavanopisto.muikku.plugins.announcer.model.AnnouncementAttachment in project muikku by otavanopisto.
the class AnnouncerAttachmentUploadServlet method doPost.
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (!sessionController.isLoggedIn()) {
sendResponse(resp, "Unauthorized", HttpServletResponse.SC_UNAUTHORIZED);
return;
}
Part file = req.getPart("upload");
if (file == null) {
sendResponse(resp, "Missing file", HttpServletResponse.SC_BAD_REQUEST);
return;
}
if (!file.getContentType().startsWith("image")) {
sendResponse(resp, "Content type is not supported", HttpServletResponse.SC_BAD_REQUEST);
return;
}
long fileSizeLimit = systemSettingsController.getUploadFileSizeLimit();
if (file.getSize() > fileSizeLimit) {
sendResponse(resp, "File too large", HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE);
return;
}
AnnouncementAttachment announcementAttachment = announcementController.createAttachement(file.getContentType(), IOUtils.toByteArray(file.getInputStream()));
if (announcementAttachment == null) {
sendResponse(resp, "Could not save attachment", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
String uploadedUrl = String.format("%s/rest/announcer/attachment/%s", baseUrl, announcementAttachment.getName());
UploadMeta uploadMeta = new UploadMeta(file.getName(), 1, uploadedUrl);
resp.setContentType("application/json");
ServletOutputStream servletOutputStream = resp.getOutputStream();
try {
(new ObjectMapper()).writeValue(servletOutputStream, uploadMeta);
} finally {
servletOutputStream.flush();
}
}
Aggregations