Search in sources :

Example 1 with CommunicatorMessageAttachment

use of fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageAttachment in project muikku by otavanopisto.

the class CommunicatorAttachmentUploadServlet 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;
    }
    CommunicatorMessageAttachment communicatorMessageAttachment = communicatorAttachmentController.create(file.getContentType(), IOUtils.toByteArray(file.getInputStream()));
    if (communicatorMessageAttachment == null) {
        sendResponse(resp, "Could not save attachment", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return;
    }
    String uploadedUrl = String.format("%s/rest/communicator/attachment/%s", baseUrl, communicatorMessageAttachment.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 : ServletOutputStream(javax.servlet.ServletOutputStream) Part(javax.servlet.http.Part) CommunicatorMessageAttachment(fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageAttachment) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 2 with CommunicatorMessageAttachment

use of fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageAttachment 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 3 with CommunicatorMessageAttachment

use of fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageAttachment in project muikku by otavanopisto.

the class CommunicatorMessageAttachmentDAO method create.

public CommunicatorMessageAttachment create(String name, String contentType, byte[] content) {
    CommunicatorMessageAttachment communicatorMessageAttachment = new CommunicatorMessageAttachment();
    communicatorMessageAttachment.setName(name);
    communicatorMessageAttachment.setContentType(contentType);
    communicatorMessageAttachment.setContent(content);
    return persist(communicatorMessageAttachment);
}
Also used : CommunicatorMessageAttachment(fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageAttachment)

Example 4 with CommunicatorMessageAttachment

use of fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageAttachment in project muikku by otavanopisto.

the class CommunicatorMessageAttachmentDAO method findByName.

public CommunicatorMessageAttachment findByName(String name) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<CommunicatorMessageAttachment> criteria = criteriaBuilder.createQuery(CommunicatorMessageAttachment.class);
    Root<CommunicatorMessageAttachment> root = criteria.from(CommunicatorMessageAttachment.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.equal(root.get(CommunicatorMessageAttachment_.name), name));
    return getSingleResult(entityManager.createQuery(criteria));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) CommunicatorMessageAttachment(fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageAttachment)

Aggregations

CommunicatorMessageAttachment (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageAttachment)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