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.webclient.repository.extract;
007:
008: import java.io.IOException;
009: import java.util.HashMap;
010: import java.util.Map;
011: import java.util.TreeMap;
012:
013: import javax.servlet.http.HttpServletRequest;
014: import javax.servlet.http.HttpServletResponse;
015: import javax.servlet.http.HttpSession;
016:
017: import org.slf4j.Logger;
018: import org.slf4j.LoggerFactory;
019: import org.springframework.validation.BindException;
020: import org.springframework.web.bind.ServletRequestDataBinder;
021: import org.springframework.web.servlet.ModelAndView;
022: import org.springframework.web.servlet.mvc.SimpleFormController;
023:
024: import org.openrdf.http.webclient.SessionKeys;
025: import org.openrdf.http.webclient.properties.RDFFormatPropertyEditor;
026: import org.openrdf.http.webclient.properties.ResourcePropertyEditor;
027: import org.openrdf.http.webclient.properties.UriPropertyEditor;
028: import org.openrdf.http.webclient.properties.ValuePropertyEditor;
029: import org.openrdf.model.Resource;
030: import org.openrdf.model.URI;
031: import org.openrdf.model.Value;
032: import org.openrdf.repository.RepositoryConnection;
033: import org.openrdf.repository.RepositoryException;
034: import org.openrdf.repository.http.HTTPRepository;
035: import org.openrdf.rio.RDFFormat;
036: import org.openrdf.rio.RDFHandlerException;
037: import org.openrdf.rio.RDFWriter;
038: import org.openrdf.rio.RDFWriterFactory;
039: import org.openrdf.rio.RDFWriterRegistry;
040: import org.openrdf.rio.Rio;
041:
042: public class ExtractionController extends SimpleFormController {
043:
044: final Logger logger = LoggerFactory.getLogger(this .getClass());
045:
046: @Override
047: protected void initBinder(HttpServletRequest request,
048: ServletRequestDataBinder binder) {
049: HttpSession session = request.getSession();
050: HTTPRepository repo = (HTTPRepository) session
051: .getAttribute(SessionKeys.REPOSITORY_KEY);
052:
053: binder.registerCustomEditor(Resource.class,
054: new ResourcePropertyEditor(repo.getValueFactory()));
055: binder.registerCustomEditor(URI.class, new UriPropertyEditor(
056: repo.getValueFactory()));
057: binder.registerCustomEditor(Value.class,
058: new ValuePropertyEditor(repo.getValueFactory()));
059:
060: binder.registerCustomEditor(RDFFormat.class,
061: new RDFFormatPropertyEditor());
062: }
063:
064: @Override
065: public ModelAndView onSubmit(HttpServletRequest request,
066: HttpServletResponse response, Object command,
067: BindException errors) {
068: HTTPRepository repo = (HTTPRepository) request.getSession()
069: .getAttribute(SessionKeys.REPOSITORY_KEY);
070:
071: ExtractionSettings settings = (ExtractionSettings) command;
072: RDFFormat format = settings.getResultFormat();
073:
074: RepositoryConnection conn = null;
075: try {
076: response.setContentType(format.getDefaultMIMEType());
077: String filename = "extract";
078: if (format.getDefaultFileExtension() != null) {
079: filename += "." + format.getDefaultFileExtension();
080: }
081: response.setHeader("Content-Disposition",
082: "attachment; filename=" + filename);
083:
084: RDFWriter writer = Rio.createWriter(format, response
085: .getOutputStream());
086:
087: conn = repo.getConnection();
088: conn.exportStatements(settings.getSubject(), settings
089: .getPredicate(), settings.getObject(), settings
090: .isIncludeInferred(), writer, settings
091: .getContexts());
092: } catch (RepositoryException e) {
093: e.printStackTrace();
094: } catch (IOException e) {
095: // TODO Auto-generated catch block
096: e.printStackTrace();
097: } catch (RDFHandlerException e) {
098: // TODO Auto-generated catch block
099: e.printStackTrace();
100: } finally {
101: try {
102: response.getOutputStream().close();
103: } catch (IOException e) {
104: e.printStackTrace();
105: }
106: if (conn != null) {
107: try {
108: conn.close();
109: } catch (RepositoryException e) {
110: e.printStackTrace();
111: }
112: }
113: }
114:
115: return null;
116: }
117:
118: @Override
119: protected Map<String, Object> referenceData(
120: HttpServletRequest request) {
121: Map<String, Object> result = new HashMap<String, Object>();
122:
123: Map<String, String> resultFormats = new TreeMap<String, String>();
124:
125: for (RDFWriterFactory factory : RDFWriterRegistry.getInstance()
126: .getAll()) {
127: RDFFormat resultFormat = factory.getRDFFormat();
128: resultFormats.put(resultFormat.getName(), resultFormat
129: .getName());
130: }
131:
132: result.put("resultFormats", resultFormats);
133:
134: return result;
135: }
136: }
|