001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.http.server.repository.statements;
007:
008: import static javax.servlet.http.HttpServletResponse.SC_OK;
009:
010: import java.io.OutputStream;
011: import java.nio.charset.Charset;
012: import java.util.Map;
013:
014: import javax.servlet.http.HttpServletRequest;
015: import javax.servlet.http.HttpServletResponse;
016:
017: import org.springframework.web.servlet.View;
018:
019: import org.openrdf.http.server.ServerHTTPException;
020: import org.openrdf.http.server.repository.RepositoryInterceptor;
021: import org.openrdf.model.Resource;
022: import org.openrdf.model.URI;
023: import org.openrdf.model.Value;
024: import org.openrdf.repository.RepositoryConnection;
025: import org.openrdf.repository.RepositoryException;
026: import org.openrdf.rio.RDFFormat;
027: import org.openrdf.rio.RDFHandlerException;
028: import org.openrdf.rio.RDFWriter;
029: import org.openrdf.rio.RDFWriterFactory;
030:
031: /**
032: * View used to export statements. Renders the statements as RDF using a
033: * serialization specified using a parameter or Accept header.
034: *
035: * @author Herko ter Horst
036: */
037: public class ExportStatementsView implements View {
038:
039: public static final String SUBJECT_KEY = "subject";
040:
041: public static final String PREDICATE_KEY = "predicate";
042:
043: public static final String OBJECT_KEY = "object";
044:
045: public static final String CONTEXTS_KEY = "contexts";
046:
047: public static final String USE_INFERENCING_KEY = "useInferencing";
048:
049: public static final String FACTORY_KEY = "factory";
050:
051: private static final ExportStatementsView INSTANCE = new ExportStatementsView();
052:
053: public static ExportStatementsView getInstance() {
054: return INSTANCE;
055: }
056:
057: private ExportStatementsView() {
058: }
059:
060: public String getContentType() {
061: return null;
062: }
063:
064: @SuppressWarnings("unchecked")
065: public void render(Map model, HttpServletRequest request,
066: HttpServletResponse response) throws Exception {
067: RepositoryConnection repositoryCon = RepositoryInterceptor
068: .getRepositoryConnection(request);
069:
070: Resource subj = (Resource) model.get(SUBJECT_KEY);
071: URI pred = (URI) model.get(PREDICATE_KEY);
072: Value obj = (Value) model.get(OBJECT_KEY);
073: Resource[] contexts = (Resource[]) model.get(CONTEXTS_KEY);
074: boolean useInferencing = (Boolean) model
075: .get(USE_INFERENCING_KEY);
076:
077: RDFWriterFactory rdfWriterFactory = (RDFWriterFactory) model
078: .get(FACTORY_KEY);
079:
080: RDFFormat rdfFormat = rdfWriterFactory.getRDFFormat();
081:
082: try {
083: OutputStream out = response.getOutputStream();
084: RDFWriter rdfWriter = rdfWriterFactory.getWriter(out);
085:
086: response.setStatus(SC_OK);
087:
088: String mimeType = rdfFormat.getDefaultMIMEType();
089: if (rdfFormat.hasCharset()) {
090: Charset charset = rdfFormat.getCharset();
091: mimeType += "; charset=" + charset.name();
092: }
093: response.setContentType(mimeType);
094:
095: String filename = "statements";
096: if (rdfFormat.getDefaultFileExtension() != null) {
097: filename += "." + rdfFormat.getDefaultFileExtension();
098: }
099: response.setHeader("Content-Disposition",
100: "attachment; filename=" + filename);
101:
102: repositoryCon.exportStatements(subj, pred, obj,
103: useInferencing, rdfWriter, contexts);
104:
105: out.close();
106: } catch (RDFHandlerException e) {
107: throw new ServerHTTPException("Serialization error: "
108: + e.getMessage(), e);
109: } catch (RepositoryException e) {
110: throw new ServerHTTPException("Repository error: "
111: + e.getMessage(), e);
112: }
113: }
114: }
|