Modified: jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/servlets/SPARQL_UberServlet.java URL: http://svn.apache.org/viewvc/jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/servlets/SPARQL_UberServlet.java?rev=1550957&r1=1550956&r2=1550957&view=diff ============================================================================== --- jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/servlets/SPARQL_UberServlet.java (original) +++ jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/servlets/SPARQL_UberServlet.java Sat Dec 14 17:37:48 2013 @@ -140,7 +140,7 @@ public abstract class SPARQL_UberServlet String method = request.getMethod() ; DatasetRef desc = action.dsRef ; if ( ! desc.isActive() ) - error(HttpSC.SERVICE_UNAVAILABLE_503, "Dataset not currently active"); + ServletOps.error(HttpSC.SERVICE_UNAVAILABLE_503, "Dataset not currently active"); String trailing = findTrailing(uri, desc.name) ; String qs = request.getQueryString() ; @@ -179,7 +179,7 @@ public abstract class SPARQL_UberServlet { // SPARQL Query if ( ! allowQuery(action)) - errorForbidden("Forbidden: SPARQL query") ; + ServletOps.errorForbidden("Forbidden: SPARQL query") ; executeRequest(action, queryServlet, desc.query) ; return ; } @@ -188,7 +188,7 @@ public abstract class SPARQL_UberServlet { // SPARQL Update if ( ! allowQuery(action)) - errorForbidden("Forbidden: SPARQL query") ; + ServletOps.errorForbidden("Forbidden: SPARQL query") ; executeRequest(action, updateServlet, desc.update) ; return ; } @@ -199,8 +199,8 @@ public abstract class SPARQL_UberServlet return ; } - errorBadRequest("Malformed request") ; - errorForbidden("Forbidden: SPARQL Graph Store Protocol : Read operation : "+method) ; + ServletOps.errorBadRequest("Malformed request") ; + ServletOps.errorForbidden("Forbidden: SPARQL Graph Store Protocol : Read operation : "+method) ; } final boolean checkForPossibleService = true ; @@ -219,7 +219,7 @@ public abstract class SPARQL_UberServlet if ( hasParams ) // ?? Revisit to include query-on-one-graph //errorBadRequest("Can't invoke a query-string service on a direct named graph") ; - errorNotFound("Not found: dataset='"+printName(desc.name)+"' service='"+printName(trailing)+"'"); + ServletOps.errorNotFound("Not found: dataset='"+printName(desc.name)+"' service='"+printName(trailing)+"'"); // There is a trailing part - not a service, no params ==> GSP direct naming. doGraphStoreProtocol(action) ; @@ -248,13 +248,13 @@ public abstract class SPARQL_UberServlet else if ( desc.readWriteGraphStore.isActive() ) executeRequest(action, restServlet_RW, desc.readWriteGraphStore) ; else - errorMethodNotAllowed(method) ; + ServletOps.errorMethodNotAllowed(method) ; return ; } // Graphs Store Protocol, indirect naming, write if ( ! allowREST_W(action)) - errorForbidden("Forbidden: SPARQL Graph Store Protocol : Write operation : "+method) ; + ServletOps.errorForbidden("Forbidden: SPARQL Graph Store Protocol : Write operation : "+method) ; executeRequest(action, restServlet_RW, desc.readWriteGraphStore) ; return ; } @@ -262,7 +262,7 @@ public abstract class SPARQL_UberServlet private void executeRequest(HttpAction action, ActionSPARQL servlet, ServiceRef service) { if ( service.endpoints.size() == 0 ) - errorMethodNotAllowed(action.request.getMethod()) ; + ServletOps.errorMethodNotAllowed(action.request.getMethod()) ; servlet.executeLifecycle(action) ; }
Modified: jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/servlets/SPARQL_Update.java URL: http://svn.apache.org/viewvc/jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/servlets/SPARQL_Update.java?rev=1550957&r1=1550956&r2=1550957&view=diff ============================================================================== --- jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/servlets/SPARQL_Update.java (original) +++ jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/servlets/SPARQL_Update.java Sat Dec 14 17:37:48 2013 @@ -111,7 +111,7 @@ public class SPARQL_Update extends SPARQ executeForm(action) ; return ; } - error(HttpSC.UNSUPPORTED_MEDIA_TYPE_415, "Bad content type: " + action.request.getContentType()) ; + ServletOps.error(HttpSC.UNSUPPORTED_MEDIA_TYPE_415, "Bad content type: " + action.request.getContentType()) ; } protected static List<String> paramsForm = Arrays.asList(paramRequest, paramUpdate, @@ -124,7 +124,7 @@ public class SPARQL_Update extends SPARQ HttpServletRequest request = action.request ; if ( ! HttpNames.METHOD_POST.equalsIgnoreCase(request.getMethod()) ) - errorMethodNotAllowed("SPARQL Update : use POST") ; + ServletOps.errorMethodNotAllowed("SPARQL Update : use POST") ; ContentType incoming = FusekiLib.getContentType(action) ; String ctStr = ( incoming == null ) ? WebContent.contentTypeSPARQLUpdate : incoming.getContentType() ; @@ -134,7 +134,7 @@ public class SPARQL_Update extends SPARQ { String charset = request.getCharacterEncoding() ; if ( charset != null && ! charset.equalsIgnoreCase(WebContent.charsetUTF8) ) - errorBadRequest("Bad charset: "+charset) ; + ServletOps.errorBadRequest("Bad charset: "+charset) ; validate(action, paramsPOST) ; return ; } @@ -143,20 +143,20 @@ public class SPARQL_Update extends SPARQ { int x = countParamOccurences(request, paramUpdate) + countParamOccurences(request, paramRequest) ; if ( x == 0 ) - errorBadRequest("SPARQL Update: No 'update=' parameter") ; + ServletOps.errorBadRequest("SPARQL Update: No 'update=' parameter") ; if ( x != 1 ) - errorBadRequest("SPARQL Update: Multiple 'update=' parameters") ; + ServletOps.errorBadRequest("SPARQL Update: Multiple 'update=' parameters") ; String requestStr = request.getParameter(paramUpdate) ; if ( requestStr == null ) requestStr = request.getParameter(paramRequest) ; if ( requestStr == null ) - errorBadRequest("SPARQL Update: No update= in HTML form") ; + ServletOps.errorBadRequest("SPARQL Update: No update= in HTML form") ; validate(action, paramsForm) ; return ; } - error(HttpSC.UNSUPPORTED_MEDIA_TYPE_415, "Must be "+WebContent.contentTypeSPARQLUpdate+" or "+WebContent.contentTypeForm+" (got "+ctStr+")") ; + ServletOps.error(HttpSC.UNSUPPORTED_MEDIA_TYPE_415, "Must be "+WebContent.contentTypeSPARQLUpdate+" or "+WebContent.contentTypeForm+" (got "+ctStr+")") ; } protected void validate(HttpAction action, Collection<String> params) @@ -168,7 +168,7 @@ public class SPARQL_Update extends SPARQ { String name = en.nextElement() ; if ( ! params.contains(name) ) - warning(action, "SPARQL Update: Unrecognize request parameter (ignored): "+name) ; + ServletOps.warning(action, "SPARQL Update: Unrecognize request parameter (ignored): "+name) ; } } } @@ -177,7 +177,7 @@ public class SPARQL_Update extends SPARQ { InputStream input = null ; try { input = action.request.getInputStream() ; } - catch (IOException ex) { errorOccurred(ex) ; } + catch (IOException ex) { ServletOps.errorOccurred(ex) ; } if ( action.verbose ) { @@ -185,14 +185,14 @@ public class SPARQL_Update extends SPARQ String requestStr = null ; try { requestStr = IO.readWholeFileAsUTF8(input) ; } catch (IOException ex) { IO.exception(ex) ; } - requestLog.info(format("[%d] Update = %s", action.id, formatForLog(requestStr))) ; + requestLog.info(format("[%d] Update = %s", action.id, ServletOps.formatForLog(requestStr))) ; input = new ByteArrayInputStream(requestStr.getBytes()); requestStr = null; } execute(action, input) ; - successNoContent(action) ; + ServletOps.successNoContent(action) ; } private void executeForm(HttpAction action) @@ -210,7 +210,7 @@ public class SPARQL_Update extends SPARQ ByteArrayInputStream input = new ByteArrayInputStream(b); requestStr = null; // free it early at least execute(action, input); - successPage(action,"Update succeeded") ; + ServletOps.successPage(action,"Update succeeded") ; } private void execute(HttpAction action, InputStream input) @@ -227,8 +227,8 @@ public class SPARQL_Update extends SPARQ // TODO implement a spill-to-disk version of this req = UpdateFactory.read(usingList, input, UpdateParseBase, Syntax.syntaxARQ); } - catch (UpdateException ex) { errorBadRequest(ex.getMessage()) ; return ; } - catch (QueryParseException ex) { errorBadRequest(messageForQPE(ex)) ; return ; } + catch (UpdateException ex) { ServletOps.errorBadRequest(ex.getMessage()) ; return ; } + catch (QueryParseException ex) { ServletOps.errorBadRequest(messageForQPE(ex)) ; return ; } } action.beginWrite() ; @@ -241,16 +241,16 @@ public class SPARQL_Update extends SPARQ } catch (UpdateException ex) { action.abort() ; incCounter(action.srvRef, UpdateExecErrors) ; - errorOccurred(ex.getMessage()) ; + ServletOps.errorOccurred(ex.getMessage()) ; } catch (QueryParseException ex) { action.abort() ; // Counter inc'ed further out. - errorBadRequest(messageForQPE(ex)) ; + ServletOps.errorBadRequest(messageForQPE(ex)) ; } catch (Throwable ex) { if ( ! ( ex instanceof ActionErrorException ) ) { try { action.abort() ; } catch (Exception ex2) {} - errorOccurred(ex.getMessage(), ex) ; + ServletOps.errorOccurred(ex.getMessage(), ex) ; } } finally { action.endWrite(); } } @@ -299,7 +299,7 @@ public class SPARQL_Update extends SPARQ return NodeFactory.createURI(iri.toString()) ; } catch (Exception ex) { - errorBadRequest("SPARQL Update: bad IRI: "+x) ; + ServletOps.errorBadRequest("SPARQL Update: bad IRI: "+x) ; return null ; } Modified: jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/servlets/SPARQL_Upload.java URL: http://svn.apache.org/viewvc/jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/servlets/SPARQL_Upload.java?rev=1550957&r1=1550956&r2=1550957&view=diff ============================================================================== --- jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/servlets/SPARQL_Upload.java (original) +++ jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/servlets/SPARQL_Upload.java Sat Dec 14 17:37:48 2013 @@ -79,7 +79,7 @@ public class SPARQL_Upload extends Actio // Only allows one file in the upload. boolean isMultipart = ServletFileUpload.isMultipartContent(action.request); if ( ! isMultipart ) - error(HttpSC.BAD_REQUEST_400 , "Not a file upload") ; + ServletOps.error(HttpSC.BAD_REQUEST_400 , "Not a file upload") ; long count = upload(action, "http://example/upload-base/") ; try { action.response.setContentType("text/html") ; @@ -103,9 +103,9 @@ public class SPARQL_Upload extends Actio out.println("</body>") ; out.println("</html>") ; out.flush() ; - success(action) ; + ServletOps.success(action) ; } - catch (Exception ex) { errorOccurred(ex) ; } + catch (Exception ex) { ServletOps.errorOccurred(ex) ; } } // Also used by SPARQL_REST @@ -142,7 +142,7 @@ public class SPARQL_Upload extends Actio { // If anything went wrong, try to backout. try { action.abort() ; } catch (Exception ex2) {} - errorOccurred(ex.getMessage()) ; + ServletOps.errorOccurred(ex.getMessage()) ; return -1 ; } finally { action.endWrite() ; } @@ -192,18 +192,18 @@ public class SPARQL_Upload extends Actio { IRI iri = IRIResolver.parseIRI(value) ; if ( iri.hasViolation(false) ) - errorBadRequest("Bad IRI: "+graphName) ; + ServletOps.errorBadRequest("Bad IRI: "+graphName) ; if ( iri.getScheme() == null ) - errorBadRequest("Bad IRI: no IRI scheme name: "+graphName) ; + ServletOps.errorBadRequest("Bad IRI: no IRI scheme name: "+graphName) ; if ( iri.getScheme().equalsIgnoreCase("http") || iri.getScheme().equalsIgnoreCase("https")) { // Redundant?? if ( iri.getRawHost() == null ) - errorBadRequest("Bad IRI: no host name: "+graphName) ; + ServletOps.errorBadRequest("Bad IRI: no host name: "+graphName) ; if ( iri.getRawPath() == null || iri.getRawPath().length() == 0 ) - errorBadRequest("Bad IRI: no path: "+graphName) ; + ServletOps.errorBadRequest("Bad IRI: no path: "+graphName) ; if ( iri.getRawPath().charAt(0) != '/' ) - errorBadRequest("Bad IRI: Path does not start '/': "+graphName) ; + ServletOps.errorBadRequest("Bad IRI: Path does not start '/': "+graphName) ; } } } @@ -216,7 +216,7 @@ public class SPARQL_Upload extends Actio // Process the input stream name = item.getName() ; if ( name == null || name.equals("") || name.equals("UNSET FILE NAME") ) - errorBadRequest("No name for content - can't determine RDF syntax") ; + ServletOps.errorBadRequest("No name for content - can't determine RDF syntax") ; String contentTypeHeader = item.getContentType() ; ct = ContentType.create(contentTypeHeader) ; @@ -251,7 +251,7 @@ public class SPARQL_Upload extends Actio return Pair.create(graphName, graphTmp) ; } catch (ActionErrorException ex) { throw ex ; } - catch (Exception ex) { errorOccurred(ex) ; return null ; } + catch (Exception ex) { ServletOps.errorOccurred(ex) ; return null ; } } @Override Modified: jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/servlets/ServletBase.java URL: http://svn.apache.org/viewvc/jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/servlets/ServletBase.java?rev=1550957&r1=1550956&r2=1550957&view=diff ============================================================================== --- jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/servlets/ServletBase.java (original) +++ jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/servlets/ServletBase.java Sat Dec 14 17:37:48 2013 @@ -18,8 +18,6 @@ package org.apache.jena.fuseki.servlets ; -import java.io.IOException ; -import java.io.PrintWriter ; import java.util.concurrent.atomic.AtomicLong ; import javax.servlet.http.HttpServlet ; @@ -29,9 +27,8 @@ import javax.servlet.http.HttpServletRes import org.apache.jena.atlas.lib.StrUtils ; import org.apache.jena.fuseki.Fuseki ; import org.apache.jena.fuseki.HttpNames ; -import org.apache.jena.web.HttpSC ; -// +// Move statics to a lib (ActionLib?) public abstract class ServletBase extends HttpServlet { private static AtomicLong requestIdAlloc = new AtomicLong(0) ; @@ -65,134 +62,6 @@ public abstract class ServletBase extend response.addHeader("Fuseki-Request-ID", Long.toString(id)) ; } - protected static void responseSendError(HttpServletResponse response, int statusCode, String message) { - try { - response.sendError(statusCode, message) ; - } catch (IOException ex) { - errorOccurred(ex) ; - } catch (IllegalStateException ex) {} - } - - protected static void responseSendError(HttpServletResponse response, int statusCode) { - try { - response.sendError(statusCode) ; - } catch (IOException ex) { - errorOccurred(ex) ; - } - } - - protected static String wholeRequestURL(HttpServletRequest request) { - StringBuffer sb = request.getRequestURL() ; - String queryString = request.getQueryString() ; - if ( queryString != null ) { - sb.append("?") ; - sb.append(queryString) ; - } - return sb.toString() ; - } - - protected static void successNoContent(HttpAction action) { - success(action, HttpSC.NO_CONTENT_204) ; - } - - protected static void success(HttpAction action) { - success(action, HttpSC.OK_200) ; - } - - protected static void successCreated(HttpAction action) { - success(action, HttpSC.CREATED_201) ; - } - - // When 404 is no big deal e.g. HEAD - protected static void successNotFound(HttpAction action) { - success(action, HttpSC.NOT_FOUND_404) ; - } - - // - protected static void success(HttpAction action, int httpStatusCode) { - action.response.setStatus(httpStatusCode) ; - } - - protected static void successPage(HttpAction action, String message) { - try { - action.response.setContentType("text/html") ; - action.response.setStatus(HttpSC.OK_200) ; - PrintWriter out = action.response.getWriter() ; - out.println("<html>") ; - out.println("<head>") ; - out.println("</head>") ; - out.println("<body>") ; - out.println("<h1>Success</h1>") ; - if ( message != null ) { - out.println("<p>") ; - out.println(message) ; - out.println("</p>") ; - } - out.println("</body>") ; - out.println("</html>") ; - out.flush() ; - } catch (IOException ex) { - errorOccurred(ex) ; - } - } - - protected static void warning(HttpAction action, String string) { - action.log.warn(string) ; - } - - protected static void warning(HttpAction action, String string, Throwable thorwable) { - action.log.warn(string, thorwable) ; - } - - protected static void errorBadRequest(String string) { - error(HttpSC.BAD_REQUEST_400, string) ; - } - - protected static void errorNotFound(String string) { - error(HttpSC.NOT_FOUND_404, string) ; - } - - protected static void errorNotImplemented(String msg) { - error(HttpSC.NOT_IMPLEMENTED_501, msg) ; - } - - protected static void errorMethodNotAllowed(String method) { - error(HttpSC.METHOD_NOT_ALLOWED_405, "HTTP method not allowed: " + method) ; - } - - protected static void errorForbidden(String msg) { - if ( msg != null ) - error(HttpSC.FORBIDDEN_403, msg) ; - else - error(HttpSC.FORBIDDEN_403, "Forbidden") ; - } - - protected static void error(int statusCode) { - throw new ActionErrorException(null, null, statusCode) ; - } - - protected static void error(int statusCode, String string) { - throw new ActionErrorException(null, string, statusCode) ; - } - - protected static void errorOccurred(String message) { - errorOccurred(message, null) ; - } - - protected static void errorOccurred(Throwable ex) { - errorOccurred(null, ex) ; - } - - protected static void errorOccurred(String message, Throwable ex) { - throw new ActionErrorException(ex, message, HttpSC.INTERNAL_SERVER_ERROR_500) ; - } - - protected static String formatForLog(String string) { - string = string.replace('\n', ' ') ; - string = string.replace('\r', ' ') ; - return string ; - } - static String varyHeaderSetting = StrUtils.strjoin(",", HttpNames.hAccept, HttpNames.hAcceptEncoding, HttpNames.hAcceptCharset) ; Added: jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/servlets/ServletOps.java URL: http://svn.apache.org/viewvc/jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/servlets/ServletOps.java?rev=1550957&view=auto ============================================================================== --- jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/servlets/ServletOps.java (added) +++ jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/servlets/ServletOps.java Sat Dec 14 17:37:48 2013 @@ -0,0 +1,149 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jena.fuseki.servlets; + +import java.io.IOException ; +import java.io.PrintWriter ; + +import javax.servlet.http.HttpServletResponse ; + +import org.apache.jena.web.HttpSC ; + +public class ServletOps { + + public static void responseSendError(HttpServletResponse response, int statusCode, String message) { + try { + response.sendError(statusCode, message) ; + } catch (IOException ex) { + errorOccurred(ex) ; + } catch (IllegalStateException ex) {} + } + + public static void responseSendError(HttpServletResponse response, int statusCode) { + try { + response.sendError(statusCode) ; + } catch (IOException ex) { + errorOccurred(ex) ; + } + } + + public static void successNoContent(HttpAction action) { + success(action, HttpSC.NO_CONTENT_204) ; + } + + public static void success(HttpAction action) { + success(action, HttpSC.OK_200) ; + } + + public static void successCreated(HttpAction action) { + success(action, HttpSC.CREATED_201) ; + } + + // When 404 is no big deal e.g. HEAD + public static void successNotFound(HttpAction action) { + success(action, HttpSC.NOT_FOUND_404) ; + } + + // + public static void success(HttpAction action, int httpStatusCode) { + action.response.setStatus(httpStatusCode) ; + } + + public static void successPage(HttpAction action, String message) { + try { + action.response.setContentType("text/html") ; + action.response.setStatus(HttpSC.OK_200) ; + PrintWriter out = action.response.getWriter() ; + out.println("<html>") ; + out.println("<head>") ; + out.println("</head>") ; + out.println("<body>") ; + out.println("<h1>Success</h1>") ; + if ( message != null ) { + out.println("<p>") ; + out.println(message) ; + out.println("</p>") ; + } + out.println("</body>") ; + out.println("</html>") ; + out.flush() ; + } catch (IOException ex) { + errorOccurred(ex) ; + } + } + + public static void warning(HttpAction action, String string) { + action.log.warn(string) ; + } + + public static void warning(HttpAction action, String string, Throwable thorwable) { + action.log.warn(string, thorwable) ; + } + + public static void errorBadRequest(String string) { + error(HttpSC.BAD_REQUEST_400, string) ; + } + + public static void errorNotFound(String string) { + error(HttpSC.NOT_FOUND_404, string) ; + } + + public static void errorNotImplemented(String msg) { + error(HttpSC.NOT_IMPLEMENTED_501, msg) ; + } + + public static void errorMethodNotAllowed(String method) { + error(HttpSC.METHOD_NOT_ALLOWED_405, "HTTP method not allowed: " + method) ; + } + + public static void errorForbidden(String msg) { + if ( msg != null ) + error(HttpSC.FORBIDDEN_403, msg) ; + else + error(HttpSC.FORBIDDEN_403, "Forbidden") ; + } + + public static void error(int statusCode) { + throw new ActionErrorException(null, null, statusCode) ; + } + + public static void error(int statusCode, String string) { + throw new ActionErrorException(null, string, statusCode) ; + } + + public static void errorOccurred(String message) { + errorOccurred(message, null) ; + } + + public static void errorOccurred(Throwable ex) { + errorOccurred(null, ex) ; + } + + public static void errorOccurred(String message, Throwable ex) { + throw new ActionErrorException(ex, message, HttpSC.INTERNAL_SERVER_ERROR_500) ; + } + + public static String formatForLog(String string) { + string = string.replace('\n', ' ') ; + string = string.replace('\r', ' ') ; + return string ; + } + +} + Added: jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/servlets/Upload.java URL: http://svn.apache.org/viewvc/jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/servlets/Upload.java?rev=1550957&view=auto ============================================================================== --- jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/servlets/Upload.java (added) +++ jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/servlets/Upload.java Sat Dec 14 17:37:48 2013 @@ -0,0 +1,143 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jena.fuseki.servlets; + +import static java.lang.String.format ; + +import java.io.IOException ; +import java.io.InputStream ; +import java.util.zip.GZIPInputStream ; + +import org.apache.commons.fileupload.FileItemIterator ; +import org.apache.commons.fileupload.FileItemStream ; +import org.apache.commons.fileupload.servlet.ServletFileUpload ; +import org.apache.jena.atlas.io.IO ; +import org.apache.jena.atlas.web.ContentType ; +import org.apache.jena.fuseki.FusekiLib ; +import org.apache.jena.riot.Lang ; +import org.apache.jena.riot.RDFLanguages ; +import org.apache.jena.riot.RiotParseException ; +import org.apache.jena.riot.WebContent ; +import org.apache.jena.riot.lang.StreamRDFCounting ; +import org.apache.jena.riot.system.StreamRDF ; +import org.apache.jena.riot.system.StreamRDFLib ; + +public class Upload { + public static void incomingData(HttpAction action, StreamRDF dest, boolean isGraph) { + ContentType ct = FusekiLib.getContentType(action) ; + + if ( WebContent.contentTypeMultiFormData.equalsIgnoreCase(ct.getContentType()) ) + { + fileUploadWorker(action, dest, isGraph) ; + return ; + } + // Single graph (or quads) in body. + + String base = ActionLib.wholeRequestURL(action.request) ; // XXX Actually wrong?! + Lang lang = RDFLanguages.contentTypeToLang(ct.getContentType()) ; + if ( lang == null ) { + ServletOps.errorBadRequest("Unknown content type for triples: " + ct) ; + return ; + } + InputStream input = null ; + try { input = action.request.getInputStream() ; } + catch (IOException ex) { IO.exception(ex) ; } + + int len = action.request.getContentLength() ; + if ( action.verbose ) { + if ( len >= 0 ) + action.log.info(format("[%d] Body: Content-Length=%d, Content-Type=%s, Charset=%s => %s", action.id, len, + ct.getContentType(), ct.getCharset(), lang.getName())) ; + else + action.log.info(format("[%d] Body: Content-Type=%s, Charset=%s => %s", action.id, ct.getContentType(), + ct.getCharset(), lang.getName())) ; + } + + ActionSPARQL.parse(action, dest, input, lang, base) ; + } + + /** Process an HTTP upload of RDF files (triples or quads) + * Stream straight into a graph or dataset -- unlike SPARQL_Upload the destination + * is known at the start of the multipart file body + */ + + public static void fileUploadWorker(HttpAction action, StreamRDF dest, boolean isGraph) { + String base = ActionLib.wholeRequestURL(action.request) ; + String item = (isGraph)?"quad":"triple" ; + ServletFileUpload upload = new ServletFileUpload(); + long count = -1 ; + + //log.info(format("[%d] Upload: Field=%s ignored", action.id, fieldName)) ; + + try { + FileItemIterator iter = upload.getItemIterator(action.request); + while (iter.hasNext()) { + FileItemStream fileStream = iter.next(); + if (fileStream.isFormField()) + ServletOps.errorBadRequest("Only files accept in multipart file upload") ; + //Ignore the field name. + //String fieldName = fileStream.getFieldName(); + + InputStream stream = fileStream.openStream(); + // Process the input stream + String contentTypeHeader = fileStream.getContentType() ; + ContentType ct = ContentType.create(contentTypeHeader) ; + Lang lang = RDFLanguages.contentTypeToLang(ct.getContentType()) ; + + if ( lang == null ) { + String name = fileStream.getName() ; + if ( name == null || name.equals("") ) + ServletOps.errorBadRequest("No name for content - can't determine RDF syntax") ; + lang = RDFLanguages.filenameToLang(name) ; + if (name.endsWith(".gz")) + stream = new GZIPInputStream(stream); + } + if ( lang == null ) + // Desperate. + lang = RDFLanguages.RDFXML ; + + String printfilename = fileStream.getName() ; + if ( printfilename == null || printfilename.equals("") ) + printfilename = "<none>" ; + + // Before + // action.log.info(format("[%d] Filename: %s, Content-Type=%s, Charset=%s => %s", + // action.id, printfilename, ct.getContentType(), ct.getCharset(), lang.getName())) ; + + StreamRDFCounting countingDest = StreamRDFLib.count(dest) ; + try { + ActionSPARQL.parse(action, countingDest, stream, lang, base); + long c = countingDest.count() ; + + action.log.info(format("[%d] Filename: %s, Content-Type=%s, Charset=%s => %s : %d %s%s", + action.id, printfilename, ct.getContentType(), ct.getCharset(), lang.getName(), + c, item, (c==1)?"":"s")) ; + } catch (RiotParseException ex) { + action.log.info(format("[%d] Filename: %s, Content-Type=%s, Charset=%s => %s : %s", + action.id, printfilename, ct.getContentType(), ct.getCharset(), lang.getName(), + ex.getMessage())) ; + throw ex ; + } + } + } + catch (ActionErrorException ex) { throw ex ; } + catch (Exception ex) { ServletOps.errorOccurred(ex) ; } + } +} + Modified: jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/validation/DataValidator.java URL: http://svn.apache.org/viewvc/jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/validation/DataValidator.java?rev=1550957&r1=1550956&r2=1550957&view=diff ============================================================================== --- jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/validation/DataValidator.java (original) +++ jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/validation/DataValidator.java Sat Dec 14 17:37:48 2013 @@ -26,6 +26,7 @@ import java.util.List ; import org.apache.jena.atlas.json.JsonBuilder ; import org.apache.jena.atlas.json.JsonObject ; +import org.apache.jena.fuseki.servlets.ServletOps ; import org.apache.jena.riot.* ; import org.apache.jena.riot.system.ErrorHandler ; import org.apache.jena.riot.system.StreamRDF ; @@ -52,7 +53,7 @@ public class DataValidator extends Valid Lang language = RDFLanguages.shortnameToLang(syntax) ; if ( language == null ) { - errorBadRequest("Unknown syntax: " + syntax) ; + ServletOps.errorBadRequest("Unknown syntax: " + syntax) ; return null ; } Modified: jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/validation/IRIValidator.java URL: http://svn.apache.org/viewvc/jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/validation/IRIValidator.java?rev=1550957&r1=1550956&r2=1550957&view=diff ============================================================================== --- jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/validation/IRIValidator.java (original) +++ jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/validation/IRIValidator.java Sat Dec 14 17:37:48 2013 @@ -24,6 +24,7 @@ import java.util.List ; import org.apache.jena.atlas.json.JsonBuilder ; import org.apache.jena.atlas.json.JsonObject ; +import org.apache.jena.fuseki.servlets.ServletOps ; import org.apache.jena.iri.IRI ; import org.apache.jena.iri.IRIFactory ; import org.apache.jena.iri.Violation ; @@ -48,7 +49,7 @@ public class IRIValidator extends Valida String args[] = getArgs(action, paramIRI) ; if ( args.length == 0 ) - errorBadRequest("No IRIs supplied"); + ServletOps.errorBadRequest("No IRIs supplied"); obj.key(jIRIs) ; obj.startArray() ; Modified: jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/validation/QueryValidator.java URL: http://svn.apache.org/viewvc/jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/validation/QueryValidator.java?rev=1550957&r1=1550956&r2=1550957&view=diff ============================================================================== --- jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/validation/QueryValidator.java (original) +++ jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/validation/QueryValidator.java Sat Dec 14 17:37:48 2013 @@ -21,6 +21,7 @@ package org.apache.jena.fuseki.validatio import org.apache.jena.atlas.io.IndentedLineBuffer ; import org.apache.jena.atlas.json.JsonBuilder ; import org.apache.jena.atlas.json.JsonObject ; +import org.apache.jena.fuseki.servlets.ServletOps ; import com.hp.hpl.jena.query.Query ; import com.hp.hpl.jena.query.QueryFactory ; @@ -62,7 +63,7 @@ public class QueryValidator extends Vali Syntax language = Syntax.lookup(querySyntax) ; if ( language == null ) { - errorBadRequest("Unknown syntax: " + querySyntax) ; + ServletOps.errorBadRequest("Unknown syntax: " + querySyntax) ; return null ; } Modified: jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/validation/UpdateValidator.java URL: http://svn.apache.org/viewvc/jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/validation/UpdateValidator.java?rev=1550957&r1=1550956&r2=1550957&view=diff ============================================================================== --- jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/validation/UpdateValidator.java (original) +++ jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/validation/UpdateValidator.java Sat Dec 14 17:37:48 2013 @@ -21,6 +21,7 @@ package org.apache.jena.fuseki.validatio import org.apache.jena.atlas.io.IndentedLineBuffer ; import org.apache.jena.atlas.json.JsonBuilder ; import org.apache.jena.atlas.json.JsonObject ; +import org.apache.jena.fuseki.servlets.ServletOps ; import com.hp.hpl.jena.query.QueryParseException ; import com.hp.hpl.jena.query.Syntax ; @@ -48,7 +49,7 @@ public class UpdateValidator extends Val Syntax language = Syntax.lookup(updateSyntax) ; if ( language == null ) { - errorBadRequest("Unknown syntax: " + updateSyntax) ; + ServletOps.errorBadRequest("Unknown syntax: " + updateSyntax) ; return null ; } Modified: jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/validation/ValidatorBaseJson.java URL: http://svn.apache.org/viewvc/jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/validation/ValidatorBaseJson.java?rev=1550957&r1=1550956&r2=1550957&view=diff ============================================================================== --- jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/validation/ValidatorBaseJson.java (original) +++ jena/branches/jena-fuseki-new-ui/src/main/java/org/apache/jena/fuseki/validation/ValidatorBaseJson.java Sat Dec 14 17:37:48 2013 @@ -31,7 +31,9 @@ import org.apache.jena.atlas.json.JsonOb import org.apache.jena.fuseki.Fuseki ; import org.apache.jena.fuseki.HttpNames ; import org.apache.jena.fuseki.servlets.ActionErrorException ; +import org.apache.jena.fuseki.servlets.ActionLib ; import org.apache.jena.fuseki.servlets.ServletBase ; +import org.apache.jena.fuseki.servlets.ServletOps ; import org.apache.jena.riot.WebContent ; import org.apache.jena.web.HttpSC ; import org.slf4j.Logger ; @@ -85,11 +87,11 @@ public abstract class ValidatorBaseJson if ( ex.exception != null ) ex.exception.printStackTrace(System.err) ; if ( ex.message != null ) - responseSendError(response, ex.rc, ex.message) ; + ServletOps.responseSendError(response, ex.rc, ex.message) ; else - responseSendError(response, ex.rc) ; + ServletOps.responseSendError(response, ex.rc) ; } catch (Throwable th) { - responseSendError(response, HttpSC.INTERNAL_SERVER_ERROR_500, "Internal Error") ; + ServletOps.responseSendError(response, HttpSC.INTERNAL_SERVER_ERROR_500, "Internal Error") ; } action.setFinishTime() ; printResponse(action) ; @@ -106,7 +108,7 @@ public abstract class ValidatorBaseJson private void printRequest(ValidationAction action) { - String url = wholeRequestURL(action.request) ; + String url = ActionLib.wholeRequestURL(action.request) ; String method = action.request.getMethod() ; action.log.info(format("[%d] %s %s", action.id, method, url)) ; @@ -170,7 +172,7 @@ public abstract class ValidatorBaseJson protected static String getArg(ValidationAction action, String paramName) { String arg = getArgOrNull(action, paramName) ; if ( arg == null ) { - error(HttpSC.BAD_REQUEST_400, "No parameter given: " + paramName) ; + ServletOps.error(HttpSC.BAD_REQUEST_400, "No parameter given: " + paramName) ; return null ; } return arg ; @@ -183,7 +185,7 @@ public abstract class ValidatorBaseJson return null ; if ( args.length > 1 ) { - error(HttpSC.BAD_REQUEST_400, "Too many ("+args.length+") parameter values: "+paramName) ; + ServletOps.error(HttpSC.BAD_REQUEST_400, "Too many ("+args.length+") parameter values: "+paramName) ; return null ; }
