Search in sources :

Example 16 with IAttachment

use of com.ibm.websphere.jaxrs20.multipart.IAttachment in project quality-measure-and-cohort-service by Alvearie.

the class CohortHandlerTestBase method mockAttachment.

protected IAttachment mockAttachment(InputStream multipartData) throws IOException {
    IAttachment measurePart = mock(IAttachment.class);
    DataHandler zipHandler = mock(DataHandler.class);
    when(zipHandler.getInputStream()).thenReturn(multipartData);
    when(measurePart.getDataHandler()).thenReturn(zipHandler);
    return measurePart;
}
Also used : DataHandler(javax.activation.DataHandler) IAttachment(com.ibm.websphere.jaxrs20.multipart.IAttachment)

Example 17 with IAttachment

use of com.ibm.websphere.jaxrs20.multipart.IAttachment in project CSC480-22S by tenbergen.

the class FileDAO method FileFactory.

/**
 * Takes form-data from a POST request for a csv file and reconstructs the content within the file
 *
 * @param attachments form-data
 * @return FileDAO Instance
 * @throws Exception File Corruption Exception
 */
public static FileDAO FileFactory(List<IAttachment> attachments) throws Exception {
    List<String> csvLines = new ArrayList<>();
    for (IAttachment attachment : attachments) {
        if (attachment == null)
            continue;
        String fileName = attachment.getDataHandler().getName();
        if (fileName != null) {
            InputStream stream = attachment.getDataHandler().getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
            String line;
            try {
                while ((line = reader.readLine()) != null) if (!line.isEmpty())
                    csvLines.add(line);
                if (csvLines.size() == 0)
                    continue;
                reader.close();
                csvLines = csvLines.stream().map(str -> str.replaceAll("[!#$%^&*(){}|?<>:;]", "")).collect(Collectors.toList());
                return new FileDAO(fileName, csvLines);
            } catch (IOException ignored) {
            }
        }
    }
    throw new CPRException(Response.Status.BAD_REQUEST, "File corrupted. Try again.");
}
Also used : InputStreamReader(java.io.InputStreamReader) CPRException(edu.oswego.cs.util.CPRException) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) IAttachment(com.ibm.websphere.jaxrs20.multipart.IAttachment)

Example 18 with IAttachment

use of com.ibm.websphere.jaxrs20.multipart.IAttachment in project CSC480-22S by tenbergen.

the class PeerReviewAssignmentResource method uploadPeerReview.

/**
 * An endpoint for uploading a peer review for another team's uploaded assignment.
 *
 * @param attachments  Multipart-Form data that contains the uploaded file content.
 * @param courseID     The course id that assigned the peer review.
 * @param assignmentID The assignment that the peer review is for
 * @param srcTeamName  The team that is reviewing the assignment
 * @param destTeamName The team that is receiving the peer review
 * @return OK response if the file was successfully added
 * @throws WebApplicationException A endpoint parameter error
 */
@POST
@RolesAllowed({ "professor", "student" })
@Path("{courseID}/{assignmentID}/{srcTeamName}/{destTeamName}/{grade}/upload")
@Consumes({ MediaType.MULTIPART_FORM_DATA, MediaType.APPLICATION_OCTET_STREAM })
@Produces({ MediaType.MULTIPART_FORM_DATA, MediaType.APPLICATION_OCTET_STREAM })
public Response uploadPeerReview(List<IAttachment> attachments, @PathParam("courseID") String courseID, @PathParam("assignmentID") int assignmentID, @PathParam("srcTeamName") String srcTeamName, @PathParam("destTeamName") String destTeamName, @PathParam("grade") int grade) throws IOException {
    PeerReviewAssignmentInterface peerReviewAssignmentInterface = new PeerReviewAssignmentInterface();
    for (IAttachment attachment : attachments) {
        if (attachment == null)
            continue;
        String fileName = attachment.getDataHandler().getName();
        if (fileName.endsWith("pdf") || fileName.endsWith("docx")) {
            peerReviewAssignmentInterface.uploadPeerReview(courseID, assignmentID, srcTeamName, destTeamName, attachment);
            fileName = "from-" + srcTeamName + "-to-" + destTeamName + fileName.substring(fileName.indexOf("."));
            peerReviewAssignmentInterface.addPeerReviewSubmission(courseID, assignmentID, srcTeamName, destTeamName, fileName, grade);
        } else
            return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).build();
    }
    return Response.status(Response.Status.OK).entity("Successfully uploaded peer review.").build();
}
Also used : PeerReviewAssignmentInterface(edu.oswego.cs.database.PeerReviewAssignmentInterface) IAttachment(com.ibm.websphere.jaxrs20.multipart.IAttachment) RolesAllowed(javax.annotation.security.RolesAllowed)

Example 19 with IAttachment

use of com.ibm.websphere.jaxrs20.multipart.IAttachment in project CSC480-22S by tenbergen.

the class studentAssignmentResource method addFileToAssignment.

/**
 * File is uploaded as form-data and passed back as a List<IAttachment>
 * The attachment is processed in FileDao.FileFactory, which reads and
 * reconstructs the file through inputStream and outputStream respectively
 *
 * @param attachments  type List<IAttachment>: file(s) passed back as form-data
 * @param courseID     type String
 * @param assignmentID type int
 * @return Response
 */
@POST
@RolesAllowed({ "professor", "student" })
@Produces({ MediaType.MULTIPART_FORM_DATA, "application/pdf" })
@Path("/courses/{courseID}/assignments/{assignmentID}/{teamName}/upload")
public Response addFileToAssignment(List<IAttachment> attachments, @PathParam("courseID") String courseID, @PathParam("assignmentID") int assignmentID, @PathParam("teamName") String teamName) throws IOException {
    for (IAttachment attachment : attachments) {
        if (attachment == null)
            continue;
        String fileName = attachment.getDataHandler().getName();
        String fileExt = fileName.substring(fileName.indexOf("."));
        if (!fileName.endsWith("pdf") && !fileName.endsWith("docx"))
            return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).build();
        new AssignmentInterface().writeToAssignment(FileDAO.fileFactory(teamName.concat(fileExt), courseID, attachment, assignmentID, teamName));
    }
    return Response.status(Response.Status.OK).entity("Successfully uploaded assignment.").build();
}
Also used : AssignmentInterface(edu.oswego.cs.rest.database.AssignmentInterface) IAttachment(com.ibm.websphere.jaxrs20.multipart.IAttachment) RolesAllowed(javax.annotation.security.RolesAllowed)

Example 20 with IAttachment

use of com.ibm.websphere.jaxrs20.multipart.IAttachment in project CSC480-22S by tenbergen.

the class ProfessorAssignmentResource method addFileToAssignment.

/**
 * File is uploaded as form-data and passed back as a List<IAttachment>
 * The attachment is processed in FileDao.FileFactory, which reads and
 * reconstructs the file through inputStream and outputStream respectively
 *
 * @param attachments  type List<IAttachment>: file(s) passed back as form-data
 * @param courseID     type String
 * @param assignmentID type int
 * @return Response
 */
@POST
@RolesAllowed("professor")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({ MediaType.MULTIPART_FORM_DATA, "application/pdf" })
@Path("/courses/{courseID}/assignments/{assignmentID}/upload")
public Response addFileToAssignment(List<IAttachment> attachments, @PathParam("courseID") String courseID, @PathParam("assignmentID") int assignmentID) throws Exception {
    for (IAttachment attachment : attachments) {
        if (attachment == null)
            continue;
        String fileName = attachment.getDataHandler().getName();
        if (!fileName.endsWith("pdf") && !fileName.endsWith("zip") && !fileName.endsWith("docx"))
            return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).build();
        new AssignmentInterface().writeToAssignment(FileDAO.fileFactory(fileName, courseID, attachment, assignmentID));
    }
    return Response.status(Response.Status.OK).entity("Successfully added file to assignment.").build();
}
Also used : AssignmentInterface(edu.oswego.cs.rest.database.AssignmentInterface) IAttachment(com.ibm.websphere.jaxrs20.multipart.IAttachment) RolesAllowed(javax.annotation.security.RolesAllowed)

Aggregations

IAttachment (com.ibm.websphere.jaxrs20.multipart.IAttachment)31 Response (javax.ws.rs.core.Response)22 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)19 IMultipartBody (com.ibm.websphere.jaxrs20.multipart.IMultipartBody)16 ByteArrayInputStream (java.io.ByteArrayInputStream)16 FhirServerConfig (com.ibm.cohort.fhir.client.config.FhirServerConfig)15 Test (org.junit.Test)14 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 Patient (org.hl7.fhir.r4.model.Patient)9 PatientListMeasureEvaluation (com.ibm.cohort.engine.api.service.model.PatientListMeasureEvaluation)8 FhirContext (ca.uhn.fhir.context.FhirContext)7 IParser (ca.uhn.fhir.parser.IParser)7 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)7 ApiOperation (io.swagger.annotations.ApiOperation)7 ApiResponse (io.swagger.annotations.ApiResponse)7 ApiResponses (io.swagger.annotations.ApiResponses)7 File (java.io.File)7 Consumes (javax.ws.rs.Consumes)7 POST (javax.ws.rs.POST)7