}
@Override
public Object provide() {
// Return the field value for the field specified by the sourceName property.
final ContainerRequest request = getContainerRequest();
final FormDataMultiPart formDataMultiPart = getEntity(request);
final List<FormDataBodyPart> formDataBodyParts = formDataMultiPart.getFields(parameter.getSourceName());
final FormDataBodyPart formDataBodyPart = (formDataBodyParts != null) ? formDataBodyParts.get(0) : null;
MediaType mediaType = (formDataBodyPart != null) ? formDataBodyPart.getMediaType() : MediaType.TEXT_PLAIN_TYPE;
final MessageBodyWorkers messageBodyWorkers = request.getWorkers();
MessageBodyReader reader = messageBodyWorkers.getMessageBodyReader(
parameter.getRawType(),
parameter.getType(),
parameter.getAnnotations(),
mediaType);
if (reader != null && !isPrimitiveType(parameter.getRawType())) {
final InputStream in;
if (formDataBodyPart == null) {
if (parameter.getDefaultValue() != null) {
// Convert default value to bytes.
in = new ByteArrayInputStream(parameter.getDefaultValue().getBytes(MessageUtils.getCharset(mediaType)));
} else {
return null;
}
} else {
in = ((BodyPartEntity) formDataBodyPart.getEntity()).getInputStream();
}
try {
//noinspection unchecked
return reader.readFrom(
parameter.getRawType(),
parameter.getType(),
parameter.getAnnotations(),
mediaType,
request.getHeaders(),
in);
} catch (final IOException e) {
throw new FormDataParamException(e, parameter.getSourceName(), parameter.getDefaultValue());
}
} else if (extractor != null) {
final MultivaluedMap<String, String> map = new MultivaluedStringMap();
try {
if (formDataBodyPart != null) {
for (final FormDataBodyPart p : formDataBodyParts) {
mediaType = p.getMediaType();
reader = messageBodyWorkers.getMessageBodyReader(
String.class,
String.class,
parameter.getAnnotations(),
mediaType);
@SuppressWarnings("unchecked") final String value = (String) reader.readFrom(
String.class,
String.class,
parameter.getAnnotations(),
mediaType,
request.getHeaders(),
((BodyPartEntity) p.getEntity()).getInputStream());
map.add(parameter.getSourceName(), value);
}
}