use of org.restlet.representation.OutputRepresentation in project qi4j-sdk by Qi4j.
the class SPARQLResource method get.
@Override
public Representation get(final Variant variant) throws ResourceException {
try {
// TODO There's probably a helper somewhere that can do this more nicely
if (getRequest().getOriginalRef().getLastSegment().equals("sparqlhtml.xsl")) {
InputStream resourceAsStream = getClass().getResourceAsStream("sparqlhtml.xsl");
return new InputRepresentation(resourceAsStream, MediaType.TEXT_XML);
}
Form form;
if (getRequest().getMethod().equals(Method.POST)) {
form = new Form(getRequest().getEntity());
} else {
form = getRequest().getResourceRef().getQueryAsForm();
}
final RepositoryConnection conn = repository.getConnection();
String queryStr = form.getFirstValue("query");
if (queryStr == null) {
InputStream resourceAsStream = getClass().getResourceAsStream("sparqlform.html");
return new InputRepresentation(resourceAsStream, MediaType.TEXT_HTML);
}
Query query = getQuery(repository, conn, queryStr);
if (query instanceof TupleQuery) {
TupleQuery tQuery = (TupleQuery) query;
final TupleQueryResult queryResult = tQuery.evaluate();
if (variant.getMediaType().equals(MediaType.TEXT_HTML)) {
return new OutputRepresentation(MediaType.TEXT_XML) {
@Override
public void write(OutputStream outputStream) throws IOException {
try {
PrintWriter out = new PrintWriter(outputStream);
out.println("<?xml version='1.0' encoding='UTF-8'?>");
out.println("<?xml-stylesheet type=\"text/xsl\" href=\"query/sparqlhtml.xsl\"?>");
out.flush();
TupleQueryResultWriter qrWriter = new SPARQLResultsXMLWriter(new XMLWriter(outputStream) {
@Override
public void startDocument() throws IOException {
// Ignore
}
});
QueryResultUtil.report(queryResult, qrWriter);
} catch (Exception e) {
throw new IOException(e);
} finally {
try {
conn.close();
} catch (RepositoryException e) {
// Ignore
}
}
}
};
} else if (variant.getMediaType().equals(MediaType.APPLICATION_RDF_XML)) {
return new OutputRepresentation(MediaType.APPLICATION_XML) {
@Override
public void write(OutputStream outputStream) throws IOException {
try {
TupleQueryResultWriter qrWriter = new SPARQLResultsXMLWriter(new XMLWriter(outputStream));
QueryResultUtil.report(queryResult, qrWriter);
} catch (Exception e) {
throw new IOException(e);
} finally {
try {
conn.close();
} catch (RepositoryException e) {
// Ignore
}
}
}
};
} else if (variant.getMediaType().equals(RestApplication.APPLICATION_SPARQL_JSON)) {
return new OutputRepresentation(RestApplication.APPLICATION_SPARQL_JSON) {
@Override
public void write(OutputStream outputStream) throws IOException {
try {
TupleQueryResultWriter qrWriter = new SPARQLResultsJSONWriterFactory().getWriter(outputStream);
QueryResultUtil.report(queryResult, qrWriter);
} catch (Exception e) {
throw new IOException(e);
} finally {
try {
conn.close();
} catch (RepositoryException e) {
// Ignore
}
}
}
};
}
} else if (query instanceof GraphQuery) {
GraphQuery gQuery = (GraphQuery) query;
/*
queryResult = gQuery.evaluate();
registry = RDFWriterRegistry.getInstance();
view = GraphQueryResultView.getInstance();
*/
conn.close();
} else if (query instanceof BooleanQuery) {
BooleanQuery bQuery = (BooleanQuery) query;
/*
queryResult = bQuery.evaluate();
registry = BooleanQueryResultWriterRegistry.getInstance();
view = BooleanQueryResultView.getInstance();
*/
conn.close();
} else {
conn.close();
throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Unsupported query type: " + query.getClass().getName());
}
} catch (RepositoryException e) {
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
} catch (QueryEvaluationException e) {
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
}
return null;
}
use of org.restlet.representation.OutputRepresentation in project OpenAM by OpenRock.
the class XacmlService method exportXACML.
/**
* This version of exportXACML here for testing - it saves trying to mock the static getRealmFromRequest
* @param realm The realm
* @return Representation object wrapping the converted XACML
*/
@VisibleForTesting
Representation exportXACML(String realm) {
List<String> filters = new ArrayList<String>(Arrays.asList(getQuery().getValuesArray(QUERY_PARAM_STRING)));
PolicySet policySet;
try {
if (!checkPermission("READ")) {
throw new ResourceException(new Status(FORBIDDEN));
}
policySet = importExport.exportXACML(realm, getAdminToken(), filters);
getResponse().setStatus(Status.SUCCESS_OK);
} catch (EntitlementException e) {
debug.warning("Reading Policies failed", e);
throw new ResourceException(new Status(INTERNAL_ERROR, e.getLocalizedMessage(getRequestLocale()), null, null));
}
final PolicySet finalPolicySet = policySet;
Representation result = new OutputRepresentation(XACMLServiceEndpointApplication.APPLICATION_XML_XACML3) {
@Override
public void write(OutputStream outputStream) throws IOException {
try {
XACMLPrivilegeUtils.writeXMLToStream(finalPolicySet, outputStream);
} catch (EntitlementException e) {
throw new IOException(e);
}
}
};
// OPENAM-4974
Disposition disposition = new Disposition();
disposition.setType(Disposition.TYPE_ATTACHMENT);
disposition.setFilename(getPolicyAttachmentFileName(realm));
result.setDisposition(disposition);
return result;
}
Aggregations