01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.http.server.repository;
07:
08: import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
09: import static javax.servlet.http.HttpServletResponse.SC_OK;
10:
11: import java.io.IOException;
12: import java.io.OutputStream;
13: import java.util.Map;
14:
15: import javax.servlet.http.HttpServletRequest;
16: import javax.servlet.http.HttpServletResponse;
17:
18: import org.slf4j.Logger;
19: import org.slf4j.LoggerFactory;
20:
21: import org.openrdf.query.GraphQueryResult;
22: import org.openrdf.query.QueryEvaluationException;
23: import org.openrdf.query.QueryResultUtil;
24: import org.openrdf.rio.RDFFormat;
25: import org.openrdf.rio.RDFHandlerException;
26: import org.openrdf.rio.RDFWriter;
27: import org.openrdf.rio.RDFWriterFactory;
28:
29: /**
30: * View used to render graph query results. Renders the graph as RDF using a
31: * serialization specified using a parameter or Accept header.
32: *
33: * @author Herko ter Horst
34: * @author Arjohn Kampman
35: */
36: public class GraphQueryResultView extends QueryResultView {
37:
38: private Logger logger = LoggerFactory.getLogger(this .getClass());
39:
40: private static final GraphQueryResultView INSTANCE = new GraphQueryResultView();
41:
42: public static GraphQueryResultView getInstance() {
43: return INSTANCE;
44: }
45:
46: private GraphQueryResultView() {
47: }
48:
49: public String getContentType() {
50: return null;
51: }
52:
53: @SuppressWarnings("unchecked")
54: public void render(Map model, HttpServletRequest request,
55: HttpServletResponse response) throws IOException {
56: RDFWriterFactory rdfWriterFactory = (RDFWriterFactory) model
57: .get(FACTORY_KEY);
58: RDFFormat rdfFormat = rdfWriterFactory.getRDFFormat();
59:
60: response.setStatus(SC_OK);
61: setContentType(response, rdfFormat);
62: setContentDisposition(model, response, rdfFormat);
63:
64: OutputStream out = response.getOutputStream();
65: try {
66: RDFWriter rdfWriter = rdfWriterFactory.getWriter(out);
67: GraphQueryResult graphQueryResult = (GraphQueryResult) model
68: .get(QUERY_RESULT_KEY);
69: QueryResultUtil.report(graphQueryResult, rdfWriter);
70: } catch (QueryEvaluationException e) {
71: logger.error("Query evaluation error", e);
72: response.sendError(SC_INTERNAL_SERVER_ERROR,
73: "Query evaluation error: " + e.getMessage());
74: } catch (RDFHandlerException e) {
75: logger.error("Serialization error", e);
76: response.sendError(SC_INTERNAL_SERVER_ERROR,
77: "Serialization error: " + e.getMessage());
78: } finally {
79: out.close();
80: }
81: }
82: }
|