use of org.apache.clerezza.rdf.core.serializedform.UnsupportedParsingFormatException in project stanbol by apache.
the class RepresentationReader method parseFromContent.
public Map<String, Representation> parseFromContent(RequestData content, MediaType acceptedMediaType) {
// (3) Parse the Representtion(s) form the entity stream
if (content.getMediaType().isCompatible(MediaType.APPLICATION_JSON_TYPE)) {
//parse from json
throw new UnsupportedOperationException("Parsing of JSON not yet implemented :(");
} else if (isSupported(content.getMediaType())) {
//from RDF serialisation
RdfValueFactory valueFactory = RdfValueFactory.getInstance();
Map<String, Representation> representations = new HashMap<String, Representation>();
Set<BlankNodeOrIRI> processed = new HashSet<BlankNodeOrIRI>();
Graph graph = new IndexedGraph();
try {
parser.parse(graph, content.getEntityStream(), content.getMediaType().toString());
} catch (UnsupportedParsingFormatException e) {
//String acceptedMediaType = httpHeaders.getFirst("Accept");
//throw an internal server Error, because we check in
//isReadable(..) for supported types and still we get here a
//unsupported format -> therefore it looks like an configuration
//error the server (e.g. a missing Bundle with the required bundle)
String message = "Unable to create the Parser for the supported format" + content.getMediaType() + " (" + e + ")";
log.error(message, e);
throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).entity(message).header(HttpHeaders.ACCEPT, acceptedMediaType).build());
} catch (RuntimeException e) {
//NOTE: Clerezza seams not to provide specific exceptions on
// parsing errors. Hence the catch for all RuntimeException
String message = "Unable to parse the provided RDF data (format: " + content.getMediaType() + ", message: " + e.getMessage() + ")";
log.error(message, e);
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity(message).header(HttpHeaders.ACCEPT, acceptedMediaType).build());
}
for (Iterator<Triple> st = graph.iterator(); st.hasNext(); ) {
BlankNodeOrIRI resource = st.next().getSubject();
if (resource instanceof IRI && processed.add(resource)) {
//build a new representation
representations.put(((IRI) resource).getUnicodeString(), valueFactory.createRdfRepresentation((IRI) resource, graph));
}
}
return representations;
} else {
//unsupported media type
String message = String.format("Parsed Content-Type '%s' is not one of the supported %s", content.getMediaType(), supportedMediaTypes);
log.info("Bad Request: {}", message);
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity(message).header(HttpHeaders.ACCEPT, acceptedMediaType).build());
}
}
Aggregations