use of org.apache.jena.atlas.web.MediaType in project jena by apache.
the class SPARQL_GSP_R method doGet.
@Override
protected void doGet(HttpAction action) {
// Assume success - do the set up before grabbing the lock.
// Sets content type.
MediaType mediaType = ActionLib.contentNegotationRDF(action);
ServletOutputStream output;
try {
output = action.response.getOutputStream();
} catch (IOException ex) {
ServletOps.errorOccurred(ex);
output = null;
}
TypedOutputStream out = new TypedOutputStream(output, mediaType);
Lang lang = RDFLanguages.contentTypeToLang(mediaType.getContentType());
if (action.verbose)
action.log.info(format("[%d] Get: Content-Type=%s, Charset=%s => %s", action.id, mediaType.getContentType(), mediaType.getCharset(), lang.getName()));
action.beginRead();
setCommonHeaders(action.response);
try {
Target target = determineTarget(action);
if (action.log.isDebugEnabled())
action.log.debug("GET->" + target);
boolean exists = target.exists();
if (!exists)
ServletOps.errorNotFound("No such graph: <" + target.name + ">");
// If we want to set the Content-Length, we need to buffer.
//response.setContentLength(??) ;
String ct = lang.getContentType().toHeaderString();
action.response.setContentType(ct);
Graph g = target.graph();
//Special case RDF/XML to be the plain (faster, less readable) form
RDFFormat fmt = (lang == Lang.RDFXML) ? RDFFormat.RDFXML_PLAIN : RDFWriterRegistry.defaultSerialization(lang);
try {
RDFDataMgr.write(out, g, fmt);
} catch (JenaException ex) {
// Good news - this happens before any output for RDF/XML-ABBREV.
if (fmt.getLang().equals(Lang.RDFXML))
ServletOps.errorBadRequest("Failed to write output in RDF/XML: " + ex.getMessage());
else
ServletOps.errorOccurred("Failed to write output: " + ex.getMessage(), ex);
}
ServletOps.success(action);
} finally {
action.endRead();
}
}
use of org.apache.jena.atlas.web.MediaType in project jena by apache.
the class SPARQL_GSP_RW method doPutPost.
private void doPutPost(HttpAction action, boolean overwrite) {
ContentType ct = FusekiLib.getContentType(action);
if (ct == null)
ServletOps.errorBadRequest("No Content-Type:");
if (matchContentType(ctMultipartMixed, ct)) {
ServletOps.error(HttpSC.UNSUPPORTED_MEDIA_TYPE_415, "multipart/mixed not supported");
}
UploadDetails details;
if (action.isTransactional())
details = addDataIntoTxn(action, overwrite);
else
details = addDataIntoNonTxn(action, overwrite);
MediaType mt = ConNeg.chooseCharset(action.request, DEF.jsonOffer, DEF.acceptJSON);
if (mt == null) {
// No return body.
if (details.getExistedBefore().equals(PreState.ABSENT))
ServletOps.successCreated(action);
else
ServletOps.successNoContent(action);
return;
}
ServletOps.uploadResponse(action, details);
}
use of org.apache.jena.atlas.web.MediaType in project jena by apache.
the class ResponseResultSet method doResponseResultSet$.
// If we refactor the conneg into a single function, we can split boolean and result set handling.
// One or the other argument must be null
private static void doResponseResultSet$(HttpAction action, ResultSet resultSet, Boolean booleanResult, Prologue qPrologue, AcceptList contentTypeOffer) {
HttpServletRequest request = action.request;
HttpServletResponse response = action.response;
long id = action.id;
if (resultSet == null && booleanResult == null) {
xlog.warn("doResponseResult: Both result set and boolean result are null");
throw new FusekiException("Both result set and boolean result are null");
}
if (resultSet != null && booleanResult != null) {
xlog.warn("doResponseResult: Both result set and boolean result are set");
throw new FusekiException("Both result set and boolean result are set");
}
String mimeType = null;
MediaType i = ConNeg.chooseContentType(request, contentTypeOffer, DEF.acceptRSXML);
if (i != null)
mimeType = i.getContentType();
// Override content type
// Does &output= override?
// Requested output type by the web form or &output= in the request.
// Expands short names
String outputField = ResponseOps.paramOutput(request, shortNamesResultSet);
if (outputField != null)
mimeType = outputField;
// Choose the serializer based on this.
String serializationType = mimeType;
// Set the HTTP respose header to this.
String contentType = mimeType;
// Stylesheet - change to application/xml.
final String stylesheetURL = ResponseOps.paramStylesheet(request);
if (stylesheetURL != null && Objects.equals(serializationType, contentTypeResultsXML))
contentType = contentTypeXML;
// Force to text/plain?
String forceAccept = ResponseOps.paramForceAccept(request);
if (forceAccept != null)
contentType = contentTypeTextPlain;
// Better : dispatch on MediaType
if (Objects.equals(serializationType, contentTypeResultsXML))
sparqlXMLOutput(action, contentType, resultSet, stylesheetURL, booleanResult);
else if (Objects.equals(serializationType, contentTypeResultsJSON))
jsonOutput(action, contentType, resultSet, booleanResult);
else if (Objects.equals(serializationType, contentTypeTextPlain))
textOutput(action, contentType, resultSet, qPrologue, booleanResult);
else if (Objects.equals(serializationType, contentTypeTextCSV))
csvOutput(action, contentType, resultSet, booleanResult);
else if (Objects.equals(serializationType, contentTypeTextTSV))
tsvOutput(action, contentType, resultSet, booleanResult);
else if (Objects.equals(serializationType, WebContent.contentTypeResultsThrift))
thriftOutput(action, contentType, resultSet, booleanResult);
else
ServletOps.errorBadRequest("Can't determine output serialization: " + serializationType);
}
use of org.apache.jena.atlas.web.MediaType in project jena by apache.
the class SPARQL_GSP_R method doHead.
@Override
protected void doHead(HttpAction action) {
action.beginRead();
setCommonHeaders(action.response);
try {
Target target = determineTarget(action);
if (action.log.isDebugEnabled())
action.log.debug("HEAD->" + target);
if (!target.exists()) {
ServletOps.successNotFound(action);
return;
}
MediaType mediaType = ActionLib.contentNegotationRDF(action);
ServletOps.success(action);
} finally {
action.endRead();
}
}
use of org.apache.jena.atlas.web.MediaType in project jena by apache.
the class RDFLanguages method getCharsetForContentType.
public static String getCharsetForContentType(String contentType) {
MediaType ct = MediaType.create(contentType);
if (ct.getCharset() != null)
return ct.getCharset();
String mt = ct.getContentType();
if (contentTypeNTriples.equals(mt))
return charsetUTF8;
if (contentTypeNTriplesAlt.equals(mt))
return charsetASCII;
if (contentTypeNQuads.equals(mt))
return charsetUTF8;
if (contentTypeNQuadsAlt1.equals(mt))
return charsetASCII;
if (contentTypeNQuadsAlt2.equals(mt))
return charsetASCII;
return charsetUTF8;
}
Aggregations