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.query;
007:
008: import java.io.ByteArrayOutputStream;
009: import java.util.Map;
010: import java.util.TreeMap;
011:
012: import javax.servlet.ServletException;
013: import javax.servlet.http.HttpServletRequest;
014: import javax.servlet.http.HttpServletResponse;
015:
016: import org.slf4j.Logger;
017: import org.slf4j.LoggerFactory;
018: import org.springframework.validation.BindException;
019: import org.springframework.web.bind.ServletRequestDataBinder;
020: import org.springframework.web.servlet.ModelAndView;
021:
022: import org.openrdf.http.webclient.SessionKeys;
023: import org.openrdf.http.webclient.properties.RDFFormatPropertyEditor;
024: import org.openrdf.query.GraphQueryResult;
025: import org.openrdf.query.MalformedQueryException;
026: import org.openrdf.query.QueryEvaluationException;
027: import org.openrdf.query.resultio.QueryResultIO;
028: import org.openrdf.repository.RepositoryConnection;
029: import org.openrdf.repository.RepositoryException;
030: import org.openrdf.repository.http.HTTPGraphQuery;
031: import org.openrdf.repository.http.HTTPQueryEvaluationException;
032: import org.openrdf.repository.http.HTTPRepository;
033: import org.openrdf.rio.RDFFormat;
034: import org.openrdf.rio.RDFHandlerException;
035: import org.openrdf.rio.RDFWriterFactory;
036: import org.openrdf.rio.RDFWriterRegistry;
037: import org.openrdf.rio.UnsupportedRDFormatException;
038:
039: public class ConstructQueryFormController extends QueryFormController {
040:
041: final Logger logger = LoggerFactory.getLogger(this .getClass());
042:
043: @Override
044: protected void initBinder(HttpServletRequest request,
045: ServletRequestDataBinder binder) throws ServletException {
046: binder.registerCustomEditor(RDFFormat.class,
047: new RDFFormatPropertyEditor());
048: }
049:
050: @Override
051: public ModelAndView onSubmit(HttpServletRequest request,
052: HttpServletResponse response, Object command,
053: BindException errors) throws Exception {
054: ModelAndView result = null;
055:
056: HTTPRepository repo = (HTTPRepository) request.getSession()
057: .getAttribute(SessionKeys.REPOSITORY_KEY);
058:
059: ConstructQueryInfo qInfo = (ConstructQueryInfo) command;
060:
061: String resultString = "";
062:
063: RepositoryConnection conn = null;
064: try {
065: conn = repo.getConnection();
066:
067: HTTPGraphQuery query = (HTTPGraphQuery) conn
068: .prepareGraphQuery(qInfo.getQueryLanguage(), qInfo
069: .getQueryString());
070: query.setIncludeInferred(qInfo.isIncludeInferred());
071: GraphQueryResult queryResult = null;
072: try {
073: queryResult = query.evaluate();
074: } catch (HTTPQueryEvaluationException e) {
075: if (e.isCausedByMalformedQueryException()) {
076: throw e.getCauseAsMalformedQueryException();
077: } else if (e.isCausedByRepositoryException()) {
078: throw e.getCauseAsRepositoryException();
079: } else if (e.isCausedByIOException()) {
080: throw e.getCauseAsIOException();
081: } else {
082: throw e;
083: }
084: }
085:
086: ByteArrayOutputStream out = new ByteArrayOutputStream();
087: try {
088: QueryResultIO.write(queryResult, qInfo
089: .getResultFormat(), out);
090: resultString = new String(out.toByteArray(), qInfo
091: .getResultFormat().getCharset().name());
092: } catch (RDFHandlerException e) {
093: // TODO Auto-generated catch block
094: e.printStackTrace();
095: } catch (UnsupportedRDFormatException e) {
096: // TODO Auto-generated catch block
097: e.printStackTrace();
098: }
099: } catch (RepositoryException e) {
100: errors.reject("repository.error");
101: } catch (MalformedQueryException e) {
102: errors.rejectValue("queryString",
103: "repository.query.error.malformed",
104: new String[] { e.getMessage() }, "Malformed query");
105: } catch (QueryEvaluationException e) {
106: errors.reject("repository.query.error.evaluation");
107: } finally {
108: // FIXME: check to see where this connection gets closed
109: // if (conn != null) {
110: // try {
111: // conn.close();
112: // }
113: // catch (RepositoryException e) {
114: // e.printStackTrace();
115: // }
116: // }
117: }
118:
119: if (errors.hasErrors()) {
120: result = showForm(request, response, errors, errors
121: .getModel());
122: } else {
123: @SuppressWarnings("unchecked")
124: Map<String, Object> model = errors.getModel();
125:
126: model.put("queryinfo", qInfo);
127: model.put("queryresult", resultString);
128:
129: result = new ModelAndView(getSuccessView(), model);
130: }
131:
132: return result;
133: }
134:
135: @Override
136: protected Map<String, Object> referenceData(
137: HttpServletRequest request) {
138: @SuppressWarnings("unchecked")
139: Map<String, Object> result = (Map<String, Object>) super
140: .referenceData(request);
141:
142: Map<String, String> resultFormats = new TreeMap<String, String>();
143:
144: for (RDFWriterFactory factory : RDFWriterRegistry.getInstance()
145: .getAll()) {
146: RDFFormat resultFormat = factory.getRDFFormat();
147: resultFormats.put(resultFormat.getName(), resultFormat
148: .getName());
149: }
150:
151: result.put("resultFormats", resultFormats);
152:
153: return result;
154: }
155: }
|