use of org.apache.jena.atlas.web.MediaType in project jena by apache.
the class ConNeg method chooseCharset.
/**
* <p>Chooses the charset by using the Accept-Charset HTTP header.</p>
*
* <p>See {@link ConNeg#choose(String, AcceptList, MediaType)}.</p>
*
* @param httpRequest HTTP request
* @param myPrefs accept list
* @param defaultMediaType default media type
* @return media type chosen
*/
public static MediaType chooseCharset(HttpServletRequest httpRequest, AcceptList myPrefs, MediaType defaultMediaType) {
String a = httpRequest.getHeader(hAcceptCharset);
if (log.isDebugEnabled())
log.debug("Accept-Charset request: " + a);
MediaType item = choose(a, myPrefs, defaultMediaType);
if (log.isDebugEnabled())
log.debug("Charset chosen: " + item);
return item;
}
use of org.apache.jena.atlas.web.MediaType in project jena by apache.
the class ConNeg method choose.
/**
* <p>This method receives a HTTP header string, an {@link AcceptList} and a
* default {@link MediaType}.</p>
*
* <p>If the header string is null, it returns the given default MediaType.</p>
*
* <p>Otherwise it builds an {@link AcceptList} object with the header string
* and uses it to match against the given MediaType.</p>
*
* @param headerString HTTP header string
* @param myPrefs accept list
* @param defaultMediaType default media type
* @return a media type or <code>null</code> if none matched or if the
* HTTP header string and the default media type are <code>null</code>.
*/
private static MediaType choose(String headerString, AcceptList myPrefs, MediaType defaultMediaType) {
if (headerString == null)
return defaultMediaType;
AcceptList headerList = new AcceptList(headerString);
if (myPrefs == null) {
MediaType i = headerList.first();
if (i == null)
return defaultMediaType;
return i;
}
MediaType i = AcceptList.match(headerList, myPrefs);
if (i == null)
return defaultMediaType;
return i;
}
use of org.apache.jena.atlas.web.MediaType in project jena by apache.
the class ConNeg method match.
/**
* Match a single media type against a header string.
*
* @param headerString HTTP header string
* @param mediaRangeStr Semi-colon separated list of media types
* @return the matched media type or <code>null</code> if there was no match
*/
public static String match(String headerString, String mediaRangeStr) {
AcceptList l = new AcceptList(headerString);
// MediaType
MediaRange aItem = new MediaRange(mediaRangeStr);
MediaType m = l.match(aItem);
if (m == null)
return null;
return m.toHeaderString();
}
use of org.apache.jena.atlas.web.MediaType in project jena by apache.
the class REST_Quads_R method doGet.
@Override
protected void doGet(HttpAction action) {
MediaType mediaType = ActionLib.contentNegotationQuads(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 (lang == null)
lang = RDFLanguages.TRIG;
if (action.verbose)
action.log.info(format("[%d] Get: Content-Type=%s, Charset=%s => %s", action.id, mediaType.getContentType(), mediaType.getCharset(), lang.getName()));
if (!RDFLanguages.isQuads(lang))
ServletOps.errorBadRequest("Not a quads format: " + mediaType);
action.beginRead();
try {
DatasetGraph dsg = action.getActiveDSG();
action.response.setHeader("Content-type", lang.getContentType().toHeaderString());
// ActionLib.contentNegotationQuads above
// RDF/XML is not a choice but this code is general.
RDFFormat fmt = // Choose streaming.
(lang == Lang.RDFXML) ? RDFFormat.RDFXML_PLAIN : RDFWriterRegistry.defaultSerialization(lang);
try {
RDFDataMgr.write(out, dsg, fmt);
} catch (JenaException ex) {
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 ResponseDataset method doResponseDataset.
public static void doResponseDataset(HttpAction action, Dataset dataset) {
HttpServletRequest request = action.request;
HttpServletResponse response = action.response;
// Header request type
String mimeType = null;
MediaType i = ConNeg.chooseContentType(request, DEF.constructOffer, DEF.acceptTurtle);
if (i != null)
mimeType = i.getContentType();
String outputField = ResponseOps.paramOutput(request, shortNamesModel);
if (outputField != null)
mimeType = outputField;
String writerMimeType = mimeType;
if (mimeType == null) {
Fuseki.actionLog.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";
ServletOps.error(HttpSC.NOT_ACCEPTABLE_406, msg);
}
String contentType = mimeType;
String charset = charsetUTF8;
String forceAccept = ResponseOps.paramForceAccept(request);
if (forceAccept != null) {
contentType = forceAccept;
charset = charsetUTF8;
}
Lang lang = RDFLanguages.contentTypeToLang(contentType);
if (lang == null)
ServletOps.errorBadRequest("Can't determine output content type: " + contentType);
try {
ResponseResultSet.setHttpResponse(action, contentType, charset);
response.setStatus(HttpSC.OK_200);
ServletOutputStream out = response.getOutputStream();
try {
if (RDFLanguages.isQuads(lang))
RDFDataMgr.write(out, dataset, lang);
else
RDFDataMgr.write(out, dataset.getDefaultModel(), lang);
out.flush();
} catch (JenaException ex) {
// request (inappropriate content type).
if (lang.equals(Lang.RDFXML))
ServletOps.errorBadRequest("Failed to write output in RDF/XML: " + ex.getMessage());
else
ServletOps.errorOccurred("Failed to write output: " + ex.getMessage(), ex);
}
} catch (ActionErrorException ex) {
throw ex;
} catch (Exception ex) {
action.log.info("Exception while writing the response model: " + ex.getMessage(), ex);
ServletOps.errorOccurred("Exception while writing the response model: " + ex.getMessage(), ex);
}
}
Aggregations