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.QueryEvaluationException;
22: import org.openrdf.query.QueryResultUtil;
23: import org.openrdf.query.TupleQueryResult;
24: import org.openrdf.query.TupleQueryResultHandlerException;
25: import org.openrdf.query.resultio.TupleQueryResultFormat;
26: import org.openrdf.query.resultio.TupleQueryResultWriter;
27: import org.openrdf.query.resultio.TupleQueryResultWriterFactory;
28:
29: /**
30: * View used to render tuple query results. Renders results in a format
31: * specified using a parameter or Accept header.
32: *
33: * @author Herko ter Horst
34: * @author Arjohn Kampman
35: */
36: public class TupleQueryResultView extends QueryResultView {
37:
38: private Logger logger = LoggerFactory.getLogger(this .getClass());
39:
40: private static final TupleQueryResultView INSTANCE = new TupleQueryResultView();
41:
42: public static TupleQueryResultView getInstance() {
43: return INSTANCE;
44: }
45:
46: private TupleQueryResultView() {
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: TupleQueryResultWriterFactory qrWriterFactory = (TupleQueryResultWriterFactory) model
57: .get(FACTORY_KEY);
58: TupleQueryResultFormat qrFormat = qrWriterFactory
59: .getTupleQueryResultFormat();
60:
61: response.setStatus(SC_OK);
62: setContentType(response, qrFormat);
63: setContentDisposition(model, response, qrFormat);
64:
65: OutputStream out = response.getOutputStream();
66: try {
67: TupleQueryResultWriter qrWriter = qrWriterFactory
68: .getWriter(out);
69: TupleQueryResult tupleQueryResult = (TupleQueryResult) model
70: .get(QUERY_RESULT_KEY);
71: QueryResultUtil.report(tupleQueryResult, qrWriter);
72: } catch (QueryEvaluationException e) {
73: logger.error("Query evaluation error", e);
74: response.sendError(SC_INTERNAL_SERVER_ERROR,
75: "Query evaluation error: " + e.getMessage());
76: } catch (TupleQueryResultHandlerException e) {
77: logger.error("Serialization error", e);
78: response.sendError(SC_INTERNAL_SERVER_ERROR,
79: "Serialization error: " + e.getMessage());
80: } finally {
81: out.close();
82: }
83: }
84: }
|