Search in sources :

Example 11 with FormLayoutMedia

use of org.akaza.openclinica.domain.datamap.FormLayoutMedia in project OpenClinica by OpenClinica.

the class OpenRosaServices method getMediaFile.

/**
     * @api {get} /rest2/openrosa/:studyOID/downloadMedia Download media
     * @apiName getMediaFile
     * @apiPermission admin
     * @apiVersion 3.8.0
     * @apiParam {String} studyOID Study Oid.
     * @apiGroup Form
     * @apiDescription Downloads media associated with a form, including images and video.
     */
@GET
@Path("/{studyOID}/downloadMedia")
public Response getMediaFile(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathParam("studyOID") String studyOID, @QueryParam("formLayoutMediaId") String formLayoutMediaId, @RequestHeader("Authorization") String authorization, @Context ServletContext context) throws Exception {
    if (!mayProceedPreview(studyOID))
        return null;
    FormLayoutMedia media = formLayoutMediaDao.findById(Integer.valueOf(formLayoutMediaId));
    File image = new File(Utils.getCrfMediaSysPath() + media.getPath() + media.getName());
    FileInputStream fis = new FileInputStream(image);
    StreamingOutput stream = new MediaStreamingOutput(fis);
    ResponseBuilder builder = Response.ok(stream);
    // Set content type, if known
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    String type = fileNameMap.getContentTypeFor(media.getPath() + media.getName());
    if (type != null && !type.isEmpty())
        builder = builder.header("Content-Type", type);
    return builder.build();
}
Also used : FormLayoutMedia(org.akaza.openclinica.domain.datamap.FormLayoutMedia) StreamingOutput(javax.ws.rs.core.StreamingOutput) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) FileNameMap(java.net.FileNameMap) MediaFile(org.akaza.openclinica.web.pform.manifest.MediaFile) File(java.io.File) FileInputStream(java.io.FileInputStream) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 12 with FormLayoutMedia

use of org.akaza.openclinica.domain.datamap.FormLayoutMedia in project OpenClinica by OpenClinica.

the class OpenRosaServices method getManifest.

/**
     * @api {get} /rest2/openrosa/:studyOID/manifest Get Form Manifest
     * @apiName getManifest
     * @apiPermission admin
     * @apiVersion 3.8.0
     * @apiParam {String} studyOID Study Oid.
     * @apiGroup Form
     * @apiDescription Gets additional information on a particular Form, including links to associated media.
     */
@GET
@Path("/{studyOID}/manifest")
@Produces(MediaType.TEXT_XML)
public String getManifest(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathParam("studyOID") String studyOID, @QueryParam("formId") String uniqueId, @RequestHeader("Authorization") String authorization, @Context ServletContext context) throws Exception {
    if (!mayProceedPreview(studyOID))
        return null;
    String formLayoutOid = getFormLayoutOid(uniqueId);
    FormLayout formLayout = formLayoutDao.findByOcOID(formLayoutOid);
    Manifest manifest = new Manifest();
    List<FormLayoutMedia> mediaList = formLayoutMediaDao.findByFormLayoutIdForNoteTypeMedia(formLayout.getFormLayoutId());
    String urlBase = getCoreResources().getDataInfo().getProperty("sysURL").split("/MainMenu")[0];
    if (mediaList != null && mediaList.size() > 0) {
        for (FormLayoutMedia media : mediaList) {
            MediaFile mediaFile = new MediaFile();
            mediaFile.setFilename(media.getName());
            File image = new File(Utils.getCrfMediaSysPath() + media.getPath() + media.getName());
            mediaFile.setHash(DigestUtils.md5Hex(media.getName()) + Double.toString(image.length()));
            mediaFile.setDownloadUrl(urlBase + "/rest2/openrosa/" + studyOID + "/downloadMedia?formLayoutMediaId=" + media.getFormLayoutMediaId());
            manifest.add(mediaFile);
        }
    }
    // Add user list
    MediaFile userList = new MediaFile();
    LinkedHashMap<String, Object> subjectContextCache = (LinkedHashMap<String, Object>) context.getAttribute("subjectContextCache");
    if (subjectContextCache != null) {
        String userXml = getUserXml(context);
        userList.setHash((DigestUtils.md5Hex(userXml)));
    }
    userList.setFilename("users.xml");
    userList.setDownloadUrl(urlBase + "/rest2/openrosa/" + studyOID + "/downloadUsers");
    manifest.add(userList);
    try {
        // Create the XML manifest using a Castor mapping file.
        XMLContext xmlContext = new XMLContext();
        Mapping mapping = xmlContext.createMapping();
        mapping.loadMapping(getCoreResources().getURL("openRosaManifestMapping.xml"));
        xmlContext.addMapping(mapping);
        Marshaller marshaller = xmlContext.createMarshaller();
        StringWriter writer = new StringWriter();
        marshaller.setWriter(writer);
        marshaller.marshal(manifest);
        // Set response headers
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        Date currentDate = new Date();
        cal.setTime(currentDate);
        SimpleDateFormat format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss zz");
        format.setCalendar(cal);
        response.setHeader("Content-Type", "text/xml; charset=UTF-8");
        response.setHeader("Date", format.format(currentDate));
        response.setHeader("X-OpenRosa-Version", "1.0");
        return writer.toString();
    } catch (Exception e) {
        LOGGER.error(e.getMessage());
        LOGGER.error(ExceptionUtils.getStackTrace(e));
        return "<Error>" + e.getMessage() + "</Error>";
    }
}
Also used : FormLayout(org.akaza.openclinica.domain.datamap.FormLayout) MediaFile(org.akaza.openclinica.web.pform.manifest.MediaFile) Marshaller(org.exolab.castor.xml.Marshaller) XMLContext(org.exolab.castor.xml.XMLContext) FormLayoutMedia(org.akaza.openclinica.domain.datamap.FormLayoutMedia) Calendar(java.util.Calendar) Mapping(org.exolab.castor.mapping.Mapping) Manifest(org.akaza.openclinica.web.pform.manifest.Manifest) Date(java.util.Date) WebApplicationException(javax.ws.rs.WebApplicationException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) StringWriter(java.io.StringWriter) MediaFile(org.akaza.openclinica.web.pform.manifest.MediaFile) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

FormLayoutMedia (org.akaza.openclinica.domain.datamap.FormLayoutMedia)12 Date (java.util.Date)4 GET (javax.ws.rs.GET)4 Path (javax.ws.rs.Path)4 FormLayout (org.akaza.openclinica.domain.datamap.FormLayout)4 Query (org.hibernate.Query)4 File (java.io.File)3 IOException (java.io.IOException)3 Calendar (java.util.Calendar)3 Produces (javax.ws.rs.Produces)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 MediaFile (org.akaza.openclinica.web.pform.manifest.MediaFile)3 StringWriter (java.io.StringWriter)2 SimpleDateFormat (java.text.SimpleDateFormat)2 CrfBean (org.akaza.openclinica.domain.datamap.CrfBean)2 CrfVersion (org.akaza.openclinica.domain.datamap.CrfVersion)2 XForm (org.akaza.openclinica.web.pform.formlist.XForm)2 XFormList (org.akaza.openclinica.web.pform.formlist.XFormList)2 Mapping (org.exolab.castor.mapping.Mapping)2 Marshaller (org.exolab.castor.xml.Marshaller)2