log.info("readFrom: mediaType {} | accepted {} | charset {}",
new Object[]{mediaType,acceptedMediaType,charset});
// (2) read the Content from the request (this needs to deal with
// MediaType.APPLICATION_FORM_URLENCODED_TYPE and
// MediaType.MULTIPART_FORM_DATA_TYPE requests!
RequestData content;
if(mediaType.isCompatible(MediaType.APPLICATION_FORM_URLENCODED_TYPE)) {
try {
content = MessageBodyReaderUtils.formForm(entityStream, charset,
"encoding",Arrays.asList("entity","content"));
} catch (IllegalArgumentException e) {
log.info("Bad Request: {}",e);
throw new WebApplicationException(
Response.status(Status.BAD_REQUEST).entity(e.toString()).
header(HttpHeaders.ACCEPT, acceptedMediaType).build());
}
if(content.getMediaType() == null){
String message = String.format(
"Missing parameter %s used to specify the media type" +
"(supported values: %s",
"encoding",supportedMediaTypes);
log.info("Bad Request: {}",message);
throw new WebApplicationException(
Response.status(Status.BAD_REQUEST).entity(message).
header(HttpHeaders.ACCEPT, acceptedMediaType).build());
}
if(!isSupported(content.getMediaType())){
String message = String.format(
"Unsupported Content-Type specified by parameter " +
"encoding=%s (supported: %s)",
content.getMediaType().toString(),supportedMediaTypes);
log.info("Bad Request: {}",message);
throw new WebApplicationException(
Response.status(Status.BAD_REQUEST).
entity(message).
header(HttpHeaders.ACCEPT, acceptedMediaType).build());
}
} else if(mediaType.isCompatible(MediaType.MULTIPART_FORM_DATA_TYPE)){
log.info("read from MimeMultipart");
List<RequestData> contents;
try {
contents = MessageBodyReaderUtils.fromMultipart(entityStream, mediaType);
} catch (IllegalArgumentException e) {
log.info("Bad Request: {}",e.toString());
throw new WebApplicationException(
Response.status(Status.BAD_REQUEST).entity(e.toString()).
header(HttpHeaders.ACCEPT, acceptedMediaType).build());
}
if(contents.isEmpty()){
String message = "Request does not contain any Mime BodyParts.";
log.info("Bad Request: {}",message);
throw new WebApplicationException(
Response.status(Status.BAD_REQUEST).entity(message).
header(HttpHeaders.ACCEPT, acceptedMediaType).build());
} else if(contents.size()>1){
//print warnings about ignored parts
log.warn("{} Request contains more than one Parts: others than " +
"the first will be ignored",
MediaType.MULTIPART_FORM_DATA_TYPE);
for(int i=1;i<contents.size();i++){
RequestData ignored = contents.get(i);
log.warn(" ignore Content {}: Name {}| MediaType {}",
new Object[] {i+1,ignored.getName(),ignored.getMediaType()});
}
}
content = contents.get(0);
if(content.getMediaType() == null){
String message = String.format(
"MediaType not specified for mime body part for file %s. " +
"The media type must be one of the supported values: %s",
content.getName(), supportedMediaTypes);
log.info("Bad Request: {}",message);
throw new WebApplicationException(
Response.status(Status.BAD_REQUEST).entity(message).
header(HttpHeaders.ACCEPT, acceptedMediaType).build());
}
if(!isSupported(content.getMediaType())){
String message = String.format(
"Unsupported Content-Type %s specified for mime body part " +
"for file %s (supported: %s)",
content.getMediaType(),content.getName(),supportedMediaTypes);
log.info("Bad Request: {}",message);
throw new WebApplicationException(
Response.status(Status.BAD_REQUEST).
entity(message).
header(HttpHeaders.ACCEPT, acceptedMediaType).build());
}
} else {
content = new RequestData(mediaType, null, entityStream);
}
long readingCompleted = System.currentTimeMillis();
log.info(" ... reading request data {}ms",readingCompleted-start);
Map<String,Representation> parsed = parseFromContent(content,acceptedMediaType);
long parsingCompleted = System.currentTimeMillis();