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

The following examples show how to use org.glassfish.jersey.media.multipart.FormDataMultiPart#getFields() . 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;
}
 
Example 4
Source File: AttachmentAction.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
@JaxrsMethodDescribe(value = "上传附件.", action = ActionUpload.class)
@POST
@Path("upload/work/{workId}")
@Produces(HttpMediaType.APPLICATION_JSON_UTF_8)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void upload(FormDataMultiPart form, @Suspended final AsyncResponse asyncResponse, @Context HttpServletRequest request,
				   @JaxrsParameterDescribe("工作标识") @PathParam("workId") String workId,
				   @JaxrsParameterDescribe("位置") @FormDataParam("site") String site,
				   @JaxrsParameterDescribe("附件名称") @FormDataParam(FILENAME_FIELD) String fileName,
				   @JaxrsParameterDescribe("天印扩展字段") @FormDataParam("extraParam") String extraParam,
				   @FormDataParam(FILE_FIELD) byte[] bytes,
				   @FormDataParam(FILE_FIELD) final FormDataContentDisposition disposition) {
	ActionResult<ActionUpload.Wo> result = new ActionResult<>();
	EffectivePerson effectivePerson = this.effectivePerson(request);
	try {
		if(StringUtils.isEmpty(extraParam)){
			extraParam = this.request2Json(request);
		}
		if(bytes==null){
			Map<String, List<FormDataBodyPart>> map = form.getFields();
			for(String key: map.keySet()){
				FormDataBodyPart part = map.get(key).get(0);
				if("application".equals(part.getMediaType().getType())){
					bytes = part.getValueAs(byte[].class);
					break;
				}
			}
		}
		result = new ActionUpload().execute(effectivePerson, workId, site, fileName, bytes, disposition,
				extraParam);
	} catch (Exception e) {
		logger.error(e, effectivePerson, request, null);
		result.error(e);
	}
	asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
}
 
Example 5
Source File: AttachmentAction.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
@JaxrsMethodDescribe(value = "上传附件.", action = ActionUploadWithWorkCompleted.class)
@POST
@Path("upload/workcompleted/{workCompletedId}")
@Produces(HttpMediaType.APPLICATION_JSON_UTF_8)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void uploadWithWorkCompleted(FormDataMultiPart form, @Suspended final AsyncResponse asyncResponse,
		@Context HttpServletRequest request,
		@JaxrsParameterDescribe("已完成工作标识") @PathParam("workCompletedId") String workCompletedId,
		@JaxrsParameterDescribe("位置") @FormDataParam("site") String site,
		@JaxrsParameterDescribe("附件名称") @FormDataParam(FILENAME_FIELD) String fileName,
		@JaxrsParameterDescribe("天印扩展字段") @FormDataParam("extraParam") String extraParam,
		@FormDataParam(FILE_FIELD) byte[] bytes,
		@FormDataParam(FILE_FIELD) final FormDataContentDisposition disposition) {
	ActionResult<ActionUploadWithWorkCompleted.Wo> result = new ActionResult<>();
	EffectivePerson effectivePerson = this.effectivePerson(request);
	try {
		if(StringUtils.isEmpty(extraParam)){
			extraParam = this.request2Json(request);
		}
		if(bytes==null){
			Map<String, List<FormDataBodyPart>> map = form.getFields();
			for(String key: map.keySet()){
				FormDataBodyPart part = map.get(key).get(0);
				if("application".equals(part.getMediaType().getType())){
					bytes = part.getValueAs(byte[].class);
					break;
				}
			}
		}
		result = new ActionUploadWithWorkCompleted().execute(effectivePerson, workCompletedId, site, fileName,
				bytes, disposition, extraParam);
	} catch (Exception e) {
		logger.error(e, effectivePerson, request, null);
		result.error(e);
	}
	asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));

}
 
Example 6
Source File: AttachmentAction.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
@JaxrsMethodDescribe(value = "更新附件.", action = ActionUpdate.class)
@PUT
@Path("update/{id}/work/{workId}")
@Produces(HttpMediaType.APPLICATION_JSON_UTF_8)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void update(FormDataMultiPart form, @Suspended final AsyncResponse asyncResponse, @Context HttpServletRequest request,
		@JaxrsParameterDescribe("附件标识") @PathParam("id") String id,
		@JaxrsParameterDescribe("工作标识") @PathParam("workId") String workId,
		@JaxrsParameterDescribe("附件名称") @FormDataParam(FILENAME_FIELD) String fileName,
		@JaxrsParameterDescribe("天印扩展字段") @FormDataParam("extraParam") String extraParam,
		@FormDataParam(FILE_FIELD) byte[] bytes,
		@JaxrsParameterDescribe("附件") @FormDataParam(FILE_FIELD) final FormDataContentDisposition disposition) {
	ActionResult<ActionUpdate.Wo> result = new ActionResult<>();
	EffectivePerson effectivePerson = this.effectivePerson(request);
	try {
		if(StringUtils.isEmpty(extraParam)){
			extraParam = this.request2Json(request);
		}
		if(bytes==null){
			Map<String, List<FormDataBodyPart>> map = form.getFields();
			for(String key: map.keySet()){
				FormDataBodyPart part = map.get(key).get(0);
				if("application".equals(part.getMediaType().getType())){
					bytes = part.getValueAs(byte[].class);
					break;
				}
			}
		}
		result = new ActionUpdate().execute(effectivePerson, id, workId, fileName, bytes, disposition, extraParam);
	} catch (Exception e) {
		logger.error(e, effectivePerson, request, null);
		result.error(e);
	}
	asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
}
 
Example 7
Source File: AttachmentAction.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
/** 与update方法同,为了兼容ntko对于附件上传只能设置post方法 */
@JaxrsMethodDescribe(value = "更新附件.", action = ActionUpdate.class)
@POST
@Path("update/{id}/work/{workId}")
@Produces(HttpMediaType.APPLICATION_JSON_UTF_8)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void updatePost(FormDataMultiPart form, @Suspended final AsyncResponse asyncResponse, @Context HttpServletRequest request,
		@JaxrsParameterDescribe("附件标识") @PathParam("id") String id,
		@JaxrsParameterDescribe("工作标识") @PathParam("workId") String workId,
		@JaxrsParameterDescribe("附件名称") @FormDataParam(FILENAME_FIELD) String fileName,
		@JaxrsParameterDescribe("天印扩展字段") @FormDataParam("extraParam") String extraParam,
		@FormDataParam(FILE_FIELD) byte[] bytes,
		@JaxrsParameterDescribe("附件") @FormDataParam(FILE_FIELD) final FormDataContentDisposition disposition) {
	ActionResult<ActionUpdate.Wo> result = new ActionResult<>();
	EffectivePerson effectivePerson = this.effectivePerson(request);
	try {
		if(StringUtils.isEmpty(extraParam)){
			extraParam = this.request2Json(request);
		}
		if(bytes==null){
			Map<String, List<FormDataBodyPart>> map = form.getFields();
			for(String key: map.keySet()){
				FormDataBodyPart part = map.get(key).get(0);
				if("application".equals(part.getMediaType().getType())){
					bytes = part.getValueAs(byte[].class);
					break;
				}
			}
		}
		result = new ActionUpdate().execute(effectivePerson, id, workId, fileName, bytes, disposition, extraParam);
	} catch (Exception e) {
		logger.error(e, effectivePerson, request, null);
		result.error(e);
	}
	asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
}