Java Code Examples for org.glassfish.jersey.media.multipart.FormDataMultiPart#cleanup()

The following examples show how to use org.glassfish.jersey.media.multipart.FormDataMultiPart#cleanup() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PinotSchemaRestletResource.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
private Schema getSchemaFromMultiPart(FormDataMultiPart multiPart) {
  try {
    Map<String, List<FormDataBodyPart>> map = multiPart.getFields();
    if (!PinotSegmentUploadDownloadRestletResource.validateMultiPart(map, null)) {
      throw new ControllerApplicationException(LOGGER, "Found not exactly one file from the multi-part fields",
          Response.Status.BAD_REQUEST);
    }
    FormDataBodyPart bodyPart = map.values().iterator().next().get(0);
    try (InputStream inputStream = bodyPart.getValueAs(InputStream.class)) {
      return Schema.fromInputSteam(inputStream);
    } catch (IOException e) {
      throw new ControllerApplicationException(LOGGER,
          "Caught exception while de-serializing the schema from request body: " + e.getMessage(),
          Response.Status.BAD_REQUEST);
    }
  } finally {
    multiPart.cleanup();
  }
}
 
Example 2
Source File: LLCSegmentCompletionHandlers.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
/**
 * Extracts the segment file from the form into a local temporary file under file upload temporary directory.
 */
private static File extractSegmentFromFormToLocalTempFile(FormDataMultiPart form, String segmentName)
    throws IOException {
  try {
    Map<String, List<FormDataBodyPart>> map = form.getFields();
    Preconditions.checkState(PinotSegmentUploadDownloadRestletResource.validateMultiPart(map, segmentName),
        "Invalid multi-part for segment: %s", segmentName);
    FormDataBodyPart bodyPart = map.values().iterator().next().get(0);

    File localTempFile = new File(ControllerFilePathProvider.getInstance().getFileUploadTempDir(),
        getTempSegmentFileName(segmentName));
    try (InputStream inputStream = bodyPart.getValueAs(InputStream.class)) {
      Files.copy(inputStream, localTempFile.toPath());
    } catch (Exception e) {
      FileUtils.deleteQuietly(localTempFile);
      throw e;
    }
    return localTempFile;
  } finally {
    form.cleanup();
  }
}
 
Example 3
Source File: PinotSegmentUploadDownloadRestletResource.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
private File getFileFromMultipart(FormDataMultiPart multiPart, File dstFile)
    throws IOException {
  // Read segment file or segment metadata file and directly use that information to update zk
  Map<String, List<FormDataBodyPart>> segmentMetadataMap = multiPart.getFields();
  if (!validateMultiPart(segmentMetadataMap, null)) {
    throw new ControllerApplicationException(LOGGER, "Invalid multi-part form for segment metadata",
        Response.Status.BAD_REQUEST);
  }
  FormDataBodyPart segmentMetadataBodyPart = segmentMetadataMap.values().iterator().next().get(0);
  try (InputStream inputStream = segmentMetadataBodyPart.getValueAs(InputStream.class);
      OutputStream outputStream = new FileOutputStream(dstFile)) {
    IOUtils.copyLarge(inputStream, outputStream);
  } finally {
    multiPart.cleanup();
  }
  return dstFile;
}