001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2006-2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.http.webclient.repository.modify.add;
007:
008: import java.io.IOException;
009: import java.io.InputStream;
010: import java.util.HashMap;
011: import java.util.Map;
012: import java.util.TreeMap;
013:
014: import javax.servlet.http.HttpServletRequest;
015: import javax.servlet.http.HttpServletResponse;
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.repository.RepositoryConnection;
027: import org.openrdf.repository.RepositoryException;
028: import org.openrdf.repository.http.HTTPRepository;
029: import org.openrdf.rio.RDFFormat;
030: import org.openrdf.rio.RDFParseException;
031: import org.openrdf.rio.RDFParserFactory;
032: import org.openrdf.rio.RDFParserRegistry;
033:
034: /**
035: * @author Herko ter Horst
036: */
037: public class AddController extends SimpleFormController {
038:
039: final Logger logger = LoggerFactory.getLogger(this .getClass());
040:
041: @Override
042: protected void initBinder(HttpServletRequest request,
043: ServletRequestDataBinder binder) {
044: binder.registerCustomEditor(RDFFormat.class,
045: new RDFFormatPropertyEditor());
046: }
047:
048: @Override
049: protected ModelAndView onSubmit(HttpServletRequest request,
050: HttpServletResponse response, Object command,
051: BindException errors) throws Exception, IOException {
052: ModelAndView result = null;
053:
054: logger.info("Uploading data...");
055: RDFUpload rdfUpload = (RDFUpload) command;
056: InputStream in = rdfUpload.getInputStream();
057:
058: HTTPRepository repo = (HTTPRepository) request.getSession()
059: .getAttribute(SessionKeys.REPOSITORY_KEY);
060: RepositoryConnection conn = null;
061: try {
062: conn = repo.getConnection();
063: conn.add(in, rdfUpload.getBaseUri(), rdfUpload.getFormat());
064: conn.commit();
065: logger.info("Upload committed.");
066: } catch (RDFParseException e) {
067: logger.warn("Unable to upload file {}", e.getMessage());
068: errors.rejectValue("contents",
069: "repository.modify.add.error", new String[] { e
070: .getMessage() }, "Malformed document");
071: } catch (RepositoryException e) {
072: logger.error("Unable to upload file", e);
073: errors.reject("repository.error");
074: } finally {
075: if (conn != null) {
076: try {
077: conn.close();
078: } catch (RepositoryException e) {
079: e.printStackTrace();
080: }
081: }
082: if (in != null) {
083: in.close();
084: }
085: }
086:
087: if (errors.hasErrors()) {
088: result = showForm(request, response, errors, errors
089: .getModel());
090: } else {
091: result = new ModelAndView(getSuccessView(), "actionResult",
092: rdfUpload.getI18n());
093: }
094:
095: return result;
096: }
097:
098: @Override
099: protected Map<String, Object> referenceData(HttpServletRequest arg0)
100: throws Exception {
101: Map<String, Object> result = new HashMap<String, Object>();
102:
103: Map<String, String> rdfFormats = new TreeMap<String, String>();
104:
105: for (RDFParserFactory factory : RDFParserRegistry.getInstance()
106: .getAll()) {
107: RDFFormat format = factory.getRDFFormat();
108: rdfFormats.put(format.getName(), format.getName());
109: }
110:
111: result.put("formats", rdfFormats);
112:
113: return result;
114: }
115: }
|