From: <mrp...@us...> - 2014-03-19 18:21:19
|
Revision: 8001 http://sourceforge.net/p/bigdata/code/8001 Author: mrpersonick Date: 2014-03-19 18:21:16 +0000 (Wed, 19 Mar 2014) Log Message: ----------- added a helper servlet for the workbench Modified Paths: -------------- branches/RDR/bigdata-sails/src/java/com/bigdata/rdf/sail/webapp/RESTServlet.java Added Paths: ----------- branches/RDR/bigdata-sails/src/java/com/bigdata/rdf/sail/webapp/WorkbenchServlet.java Modified: branches/RDR/bigdata-sails/src/java/com/bigdata/rdf/sail/webapp/RESTServlet.java =================================================================== --- branches/RDR/bigdata-sails/src/java/com/bigdata/rdf/sail/webapp/RESTServlet.java 2014-03-19 16:44:49 UTC (rev 8000) +++ branches/RDR/bigdata-sails/src/java/com/bigdata/rdf/sail/webapp/RESTServlet.java 2014-03-19 18:21:16 UTC (rev 8001) @@ -57,6 +57,8 @@ private InsertServlet m_insertServlet; private DeleteServlet m_deleteServlet; private UpdateServlet m_updateServlet; + private WorkbenchServlet m_workbenchServlet; + /** * @see <a href="https://sourceforge.net/apps/trac/bigdata/ticket/584"> * DESCRIBE CACHE </a> @@ -80,12 +82,14 @@ m_updateServlet = new UpdateServlet(); m_deleteServlet = new DeleteServlet(); m_describeServlet = new DescribeCacheServlet(); + m_workbenchServlet = new WorkbenchServlet(); m_queryServlet.init(getServletConfig()); m_insertServlet.init(getServletConfig()); m_updateServlet.init(getServletConfig()); m_deleteServlet.init(getServletConfig()); m_describeServlet.init(getServletConfig()); + m_workbenchServlet.init(getServletConfig()); } @@ -120,6 +124,11 @@ m_describeServlet = null; } + if (m_workbenchServlet != null) { + m_workbenchServlet.destroy(); + m_workbenchServlet = null; + } + super.destroy(); } @@ -222,7 +231,11 @@ buildResponse(resp, HTTP_OK, MIME_TEXT_PLAIN); - } else if(req.getParameter("uri") != null) { + } else if (req.getParameter(WorkbenchServlet.ATTR_WORKBENCH) != null) { + + m_workbenchServlet.doPost(req, resp); + + } else if (req.getParameter("uri") != null) { // INSERT via w/ URIs m_insertServlet.doPost(req, resp); Added: branches/RDR/bigdata-sails/src/java/com/bigdata/rdf/sail/webapp/WorkbenchServlet.java =================================================================== --- branches/RDR/bigdata-sails/src/java/com/bigdata/rdf/sail/webapp/WorkbenchServlet.java (rev 0) +++ branches/RDR/bigdata-sails/src/java/com/bigdata/rdf/sail/webapp/WorkbenchServlet.java 2014-03-19 18:21:16 UTC (rev 8001) @@ -0,0 +1,183 @@ +/** +Copyright (C) SYSTAP, LLC 2006-2014. All rights reserved. + +Contact: + SYSTAP, LLC + 4501 Tower Road + Greensboro, NC 27410 + lic...@bi... + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; version 2 of the License. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ +package com.bigdata.rdf.sail.webapp; + +import java.io.IOException; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.log4j.Logger; +import org.openrdf.model.Graph; +import org.openrdf.model.impl.GraphImpl; +import org.openrdf.rio.RDFFormat; +import org.openrdf.rio.RDFParser; +import org.openrdf.rio.RDFParserFactory; +import org.openrdf.rio.RDFParserRegistry; +import org.openrdf.rio.helpers.StatementCollector; + +import com.bigdata.rdf.sail.webapp.client.MiniMime; +import com.bigdata.rdf.store.AbstractTripleStore; + +/** + * Helper servlet for workbench requests. + */ +public class WorkbenchServlet extends BigdataRDFServlet { + + /** + * + */ + private static final long serialVersionUID = 1L; + + static private final transient Logger log = Logger.getLogger(WorkbenchServlet.class); + + /** + * Flag to signify a workbench operation. + */ + static final transient String ATTR_WORKBENCH = "workbench"; + + /** + * Flag to signify a convert operation. POST an RDF document with a + * content type and an accept header for what it should be converted to. + */ + static final transient String ATTR_CONVERT = "convert"; + + + public WorkbenchServlet() { + + } + + @Override + protected void doPost(final HttpServletRequest req, + final HttpServletResponse resp) throws IOException { + + + if (req.getParameter(ATTR_CONVERT) != null) { + + // Convert from one format to another + doConvert(req, resp); + + } + + } + + /** + * Convert RDF data from one format to another. + */ + private void doConvert(final HttpServletRequest req, + final HttpServletResponse resp) throws IOException { + + final String baseURI = req.getRequestURL().toString(); + + final String namespace = getNamespace(req); + + final long timestamp = getTimestamp(req); + + final AbstractTripleStore tripleStore = getBigdataRDFContext() + .getTripleStore(namespace, timestamp); + + if (tripleStore == null) { + /* + * There is no such triple/quad store instance. + */ + buildResponse(resp, HTTP_NOTFOUND, MIME_TEXT_PLAIN); + return; + } + + final String contentType = req.getContentType(); + + if (log.isInfoEnabled()) + log.info("Request body: " + contentType); + + /** + * <a href="https://sourceforge.net/apps/trac/bigdata/ticket/620"> + * UpdateServlet fails to parse MIMEType when doing conneg. </a> + */ + + final RDFFormat requestBodyFormat = RDFFormat.forMIMEType(new MiniMime( + contentType).getMimeType()); + + if (requestBodyFormat == null) { + + buildResponse(resp, HTTP_BADREQUEST, MIME_TEXT_PLAIN, + "Content-Type not recognized as RDF: " + contentType); + + return; + + } + + final RDFParserFactory rdfParserFactory = RDFParserRegistry + .getInstance().get(requestBodyFormat); + + if (rdfParserFactory == null) { + + buildResponse(resp, HTTP_INTERNALERROR, MIME_TEXT_PLAIN, + "Parser factory not found: Content-Type=" + + contentType + ", format=" + requestBodyFormat); + + return; + + } + +// final String s= IOUtil.readString(req.getInputStream()); +// System.err.println(s); + + final Graph g = new GraphImpl(); + + try { + + /* + * There is a request body, so let's try and parse it. + */ + + final RDFParser rdfParser = rdfParserFactory + .getParser(); + + rdfParser.setValueFactory(tripleStore.getValueFactory()); + + rdfParser.setVerifyData(true); + + rdfParser.setStopAtFirstError(true); + + rdfParser + .setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE); + + rdfParser.setRDFHandler(new StatementCollector(g)); + + /* + * Run the parser, which will cause statements to be + * inserted. + */ + rdfParser.parse(req.getInputStream(), baseURI); + + sendGraph(req, resp, g); + + } catch (Throwable t) { + + throw BigdataRDFServlet.launderThrowable(t, resp, null); + + } + + } + +} Property changes on: branches/RDR/bigdata-sails/src/java/com/bigdata/rdf/sail/webapp/WorkbenchServlet.java ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |