Search in sources :

Example 1 with AnnouncementAttachment

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);
}
Also used : AnnouncementAttachment(fi.otavanopisto.muikku.plugins.announcer.model.AnnouncementAttachment)

Example 2 with 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));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) AnnouncementAttachment(fi.otavanopisto.muikku.plugins.announcer.model.AnnouncementAttachment) EntityManager(javax.persistence.EntityManager)

Example 3 with AnnouncementAttachment

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();
}
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 4 with AnnouncementAttachment

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();
    }
}
Also used : AnnouncementAttachment(fi.otavanopisto.muikku.plugins.announcer.model.AnnouncementAttachment) ServletOutputStream(javax.servlet.ServletOutputStream) Part(javax.servlet.http.Part) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Aggregations

AnnouncementAttachment (fi.otavanopisto.muikku.plugins.announcer.model.AnnouncementAttachment)4 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)1 EntityManager (javax.persistence.EntityManager)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 Part (javax.servlet.http.Part)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 CacheControl (javax.ws.rs.core.CacheControl)1 EntityTag (javax.ws.rs.core.EntityTag)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1