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;
}
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.");
}
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();
}
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();
}
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();
}
Aggregations