UnauthorizedException
{
// Specify which formats we support using Accept headers
Set<RDFFormat> rdfFormats = RDFParserRegistry.getInstance().getKeys();
if (rdfFormats.isEmpty()) {
throw new RepositoryException("No tuple RDF parsers have been registered");
}
for (RDFFormat format : rdfFormats) {
// Determine a q-value that reflects the necessity of context
// support and the user specified preference
int qValue = 10;
if (requireContext && !format.supportsContexts()) {
// Prefer context-supporting formats over pure triple-formats
qValue -= 5;
}
if (preferredRDFFormat != null && !preferredRDFFormat.equals(format)) {
// Prefer specified format over other formats
qValue -= 2;
}
if (!format.supportsNamespaces()) {
// We like reusing namespace prefixes
qValue -= 1;
}
for (String mimeType : format.getMIMETypes()) {
String acceptParam = mimeType;
if (qValue < 10) {
acceptParam += ";q=0." + qValue;
}
method.addRequestHeader(ACCEPT_PARAM_NAME, acceptParam);
}
}
int httpCode = httpClient.executeMethod(method);
if (httpCode == HttpURLConnection.HTTP_OK) {
String mimeType = getResponseMIMEType(method);
try {
RDFFormat format = RDFFormat.matchMIMEType(mimeType, rdfFormats);
RDFParser parser = Rio.createParser(format, getValueFactory());
parser.setPreserveBNodeIDs(true);
parser.setRDFHandler(handler);
parser.parse(method.getResponseBodyAsStream(), method.getURI().getURI());
}
catch (UnsupportedRDFormatException e) {
throw new RepositoryException("Server responded with an unsupported file format: " + mimeType);
}
catch (RDFParseException e) {
throw new RepositoryException("Malformed query result from server", e);
}
}
else if (httpCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
throw new UnauthorizedException();
}
else {
ErrorInfo errInfo = getErrorInfo(method);
// Throw appropriate exception
if (errInfo.getErrorType() == ErrorType.MALFORMED_QUERY) {
throw new MalformedQueryException(errInfo.getErrorMessage());
}
else if (errInfo.getErrorType() == ErrorType.UNSUPPORTED_QUERY_LANGUAGE) {
throw new UnsupportedQueryLanguageException(errInfo.getErrorMessage());
}
else {
throw new RepositoryException(errInfo.toString());
}
}
}