use of org.apache.jena.atlas.web.MediaType in project jena by apache.
the class REST_Quads method doHead.
@Override
protected void doHead(HttpAction action) {
action.beginRead();
try {
MediaType mediaType = HttpAction.contentNegotationQuads(action);
success(action);
} finally {
action.endRead();
}
}
use of org.apache.jena.atlas.web.MediaType in project jena by apache.
the class SPARQL_REST_R method doGet.
@Override
protected void doGet(HttpAction action) {
// Assume success - do the set up before grabbing the lock.
// Sets content type.
MediaType mediaType = HttpAction.contentNegotationRDF(action);
ServletOutputStream output;
try {
output = action.response.getOutputStream();
} catch (IOException ex) {
errorOccurred(ex);
output = null;
}
TypedOutputStream out = new TypedOutputStream(output, mediaType);
Lang lang = RDFLanguages.contentTypeToLang(mediaType.getContentType());
if (action.verbose)
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 (log.isDebugEnabled())
log.debug("GET->" + target);
boolean exists = target.exists();
if (!exists)
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);
RDFDataMgr.write(out, g, fmt);
success(action);
} finally {
action.endRead();
}
}
use of org.apache.jena.atlas.web.MediaType in project jena by apache.
the class ResponseModel method doResponseModel.
public static void doResponseModel(HttpAction action, Model model) {
HttpServletRequest request = action.request;
HttpServletResponse response = action.response;
// Header request type
String mimeType = null;
// TODO Use MediaType throughout.
MediaType i = ConNeg.chooseContentType(request, DEF.rdfOffer, DEF.acceptRDFXML);
if (i != null)
mimeType = i.getContentType();
String outputField = ResponseOps.paramOutput(request, shortNamesModel);
if (outputField != null)
mimeType = outputField;
String writerMimeType = mimeType;
if (mimeType == null) {
Fuseki.requestLog.warn("Can't find MIME type for response");
String x = WebLib.getAccept(request);
String msg;
if (x == null)
msg = "No Accept: header";
else
msg = "Accept: " + x + " : Not understood";
error(HttpSC.NOT_ACCEPTABLE_406, msg);
}
String contentType = mimeType;
String charset = WebContent.charsetUTF8;
String forceAccept = ResponseOps.paramForceAccept(request);
if (forceAccept != null) {
contentType = forceAccept;
charset = WebContent.charsetUTF8;
}
Lang lang = RDFLanguages.contentTypeToLang(contentType);
if (lang == null)
errorBadRequest("Can't determine output content type: " + contentType);
try {
ResponseResultSet.setHttpResponse(request, response, contentType, charset);
response.setStatus(HttpSC.OK_200);
ServletOutputStream out = response.getOutputStream();
RDFDataMgr.write(out, model, lang);
out.flush();
} catch (Exception ex) {
slog.info("Exception while writing the response model: " + ex.getMessage(), ex);
errorOccurred("Exception while writing the response model: " + ex.getMessage(), ex);
}
}
use of org.apache.jena.atlas.web.MediaType in project jena by apache.
the class TestQuery method query_describe_conneg.
@Test
public void query_describe_conneg() throws IOException {
try (CloseableHttpClient client = HttpOp.createPoolingHttpClient()) {
String query = "DESCRIBE ?s WHERE {?s ?p ?o}";
for (MediaType type : rdfOfferTest.entries()) {
String contentType = type.toHeaderString();
try (QueryEngineHTTP qExec = (QueryEngineHTTP) QueryExecutionFactory.sparqlService(serviceQuery(), query)) {
qExec.setModelContentType(contentType);
qExec.setClient(client);
Model m = qExec.execDescribe();
String x = qExec.getHttpResponseContentType();
assertEquals(contentType, x);
assertFalse(m.isEmpty());
}
}
}
}
use of org.apache.jena.atlas.web.MediaType in project jena by apache.
the class ConNeg method chooseContentType.
/**
* <p>Choose the content media type by extracting the Accept HTTP header from
* the HTTP request and choosing
* (see {@link ConNeg#choose(String, AcceptList, MediaType)}) a content media
* type that matches the header.</p>
*
* @param httpRequest HTTP request
* @param myPrefs accept list
* @param defaultMediaType default media type
* @return media type chosen
*/
public static MediaType chooseContentType(HttpServletRequest httpRequest, AcceptList myPrefs, MediaType defaultMediaType) {
String a = WebLib.getAccept(httpRequest);
if (log.isDebugEnabled())
log.debug("Accept request: " + a);
MediaType item = choose(a, myPrefs, defaultMediaType);
if (log.isDebugEnabled())
log.debug("Content type chosen: " + item);
return item;
}
Aggregations