jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject [02/10] jena git commit: Valdiators for jSN or HTML output.
Date Sat, 27 May 2017 18:24:18 GMT
http://git-wip-us.apache.org/repos/asf/jena/blob/9a0294f5/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/json/QueryValidatorJSON.java
----------------------------------------------------------------------
diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/json/QueryValidatorJSON.java
b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/json/QueryValidatorJSON.java
new file mode 100644
index 0000000..39837557
--- /dev/null
+++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/json/QueryValidatorJSON.java
@@ -0,0 +1,157 @@
+/*
+ * 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.validation.json ;
+
+import static org.apache.jena.fuseki.validation.json.ValidatorJsonLib.getArg;
+import static org.apache.jena.fuseki.validation.json.ValidatorJsonLib.getArgOrNull;
+import static org.apache.jena.fuseki.validation.json.ValidatorJsonLib.jErrors;
+import static org.apache.jena.fuseki.validation.json.ValidatorJsonLib.jParseError;
+import static org.apache.jena.fuseki.validation.json.ValidatorJsonLib.jParseErrorCol;
+import static org.apache.jena.fuseki.validation.json.ValidatorJsonLib.jParseErrorLine;
+
+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 org.apache.jena.query.Query ;
+import org.apache.jena.query.QueryFactory ;
+import org.apache.jena.query.QueryParseException ;
+import org.apache.jena.query.Syntax ;
+import org.apache.jena.sparql.algebra.Algebra ;
+import org.apache.jena.sparql.algebra.Op ;
+import org.apache.jena.sparql.serializer.SerializationContext ;
+
+public class QueryValidatorJSON {
+
+    private static final long serialVersionUID = 5113754839333382031L;
+
+    public QueryValidatorJSON() {}
+
+    static final String paramQuery       = "query" ;
+    static final String paramSyntax      = "languageSyntax" ;
+    
+    static final String jInput           = "input" ;
+    
+    static final String jFormatted       = "formatted" ;
+    static final String jAlgebra         = "algebra" ;
+    static final String jAlgebraQuads    = "algebra-quads" ;
+    static final String jAlgebraOpt      = "algebra-opt" ;
+    static final String jAlgebraOptQuads = "algebra-opt-quads" ;
+
+    public static JsonObject execute(ValidationAction action) {
+        JsonBuilder obj = new JsonBuilder() ;
+        obj.startObject() ;
+
+        final String queryString = getArg(action, paramQuery) ;
+
+        String querySyntax = getArgOrNull(action, paramSyntax) ;
+        if ( querySyntax == null || querySyntax.equals("") )
+            querySyntax = "SPARQL" ;
+
+        Syntax language = Syntax.lookup(querySyntax) ;
+        if ( language == null ) {
+            ServletOps.errorBadRequest("Unknown syntax: " + querySyntax) ;
+            return null ;
+        }
+
+        boolean outputSPARQL = true ;
+        boolean outputAlgebra = true ;
+        boolean outputQuads = true ;
+        boolean outputOptimized = true ;
+        boolean outputOptimizedQuads = true ;
+
+        obj.key(jInput).value(queryString) ;
+
+        // Attempt to parse it.
+        Query query = null ;
+        try {
+            query = QueryFactory.create(queryString, "http://example/base/", language) ;
+        } catch (QueryParseException ex) {
+            obj.key(jErrors) ;
+            obj.startArray() ;      // Errors array
+            obj.startObject() ;
+            obj.key(jParseError).value(ex.getMessage()) ;
+            obj.key(jParseErrorLine).value(ex.getLine()) ;
+            obj.key(jParseErrorCol).value(ex.getColumn()) ;
+            obj.finishObject() ;
+            obj.finishArray() ;
+            
+            obj.finishObject() ; // Outer object
+            return obj.build().getAsObject() ;
+        }
+
+        if ( query != null ) {
+            
+            if ( outputSPARQL )
+                formatted(obj, query) ;
+
+            if ( outputAlgebra )
+                algebra(obj, query) ;
+
+            if ( outputQuads )
+                algebraQuads(obj, query) ;
+
+            if ( outputOptimized )
+                algebraOpt(obj, query) ;
+
+            if ( outputOptimizedQuads )
+                algebraOptQuads(obj, query) ;
+        }
+        
+        obj.finishObject() ;
+        return obj.build().getAsObject() ;
+    }
+
+    private static void formatted(JsonBuilder obj, Query query) {
+        IndentedLineBuffer out = new IndentedLineBuffer() ;
+        query.serialize(out) ;
+        obj.key(jFormatted).value(out.asString()) ;
+    }
+
+    private static void algebra(JsonBuilder obj, Query query) {
+        Op op = Algebra.compile(query) ;
+        obj.key(jAlgebra).value(string(query, op)) ;
+    }
+
+    private static void algebraQuads(JsonBuilder obj, Query query) {
+        Op op = Algebra.compile(query) ;
+        op = Algebra.toQuadForm(op) ;
+        obj.key(jAlgebraQuads).value(string(query, op)) ;
+    }
+
+    private static void algebraOpt(JsonBuilder obj, Query query) {
+        Op op = Algebra.compile(query) ;
+        op = Algebra.optimize(op) ;
+        obj.key(jAlgebraOpt).value(string(query, op)) ;
+    }
+
+    private static void algebraOptQuads(JsonBuilder obj, Query query) {
+        Op op = Algebra.compile(query) ;
+        op = Algebra.toQuadForm(op) ;
+        op = Algebra.optimize(op) ;
+        obj.key(jAlgebraOptQuads).value(string(query, op)) ;
+    }
+
+    private static String string(Query query, Op op) {
+        final SerializationContext sCxt = new SerializationContext(query) ;
+        IndentedLineBuffer out = new IndentedLineBuffer() ;
+        op.output(out, sCxt) ;
+        return out.asString() ;
+    }
+}

http://git-wip-us.apache.org/repos/asf/jena/blob/9a0294f5/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/json/UpdateValidatorJSON.java
----------------------------------------------------------------------
diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/json/UpdateValidatorJSON.java
b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/json/UpdateValidatorJSON.java
new file mode 100644
index 0000000..64837e6
--- /dev/null
+++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/json/UpdateValidatorJSON.java
@@ -0,0 +1,94 @@
+/**
+ * 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.validation.json;
+
+import static org.apache.jena.fuseki.validation.json.ValidatorJsonLib.getArg;
+import static org.apache.jena.fuseki.validation.json.ValidatorJsonLib.getArgOrNull;
+import static org.apache.jena.fuseki.validation.json.ValidatorJsonLib.jErrors;
+import static org.apache.jena.fuseki.validation.json.ValidatorJsonLib.jParseError;
+import static org.apache.jena.fuseki.validation.json.ValidatorJsonLib.jParseErrorCol;
+import static org.apache.jena.fuseki.validation.json.ValidatorJsonLib.jParseErrorLine;
+
+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 org.apache.jena.query.QueryParseException ;
+import org.apache.jena.query.Syntax ;
+import org.apache.jena.update.UpdateFactory ;
+import org.apache.jena.update.UpdateRequest ;
+
+public class UpdateValidatorJSON {
+
+    private static final long serialVersionUID = 8656799942497464291L;
+
+    public UpdateValidatorJSON() {}
+    
+    static final String paramUpdate           = "update" ;
+    static final String paramSyntax           = "languageSyntax" ;
+    
+    static final String jInput           = "input" ;
+    static final String jFormatted       = "formatted" ;
+
+    public static JsonObject execute(ValidationAction action) {
+        JsonBuilder obj = new JsonBuilder() ;
+        obj.startObject() ;
+        
+        final String updateString = getArg(action, paramUpdate) ;
+        String updateSyntax = getArgOrNull(action, paramSyntax) ;
+        if ( updateSyntax == null || updateSyntax.equals("") )
+            updateSyntax = "SPARQL" ;
+        
+        Syntax language = Syntax.lookup(updateSyntax) ;
+        if ( language == null ) {
+            ServletOps.errorBadRequest("Unknown syntax: " + updateSyntax) ;
+            return null ;
+        }
+        
+        obj.key(jInput).value(updateString) ;
+        UpdateRequest request = null ;
+        try {
+            request = UpdateFactory.create(updateString, "http://example/base/", language)
;
+        } catch (QueryParseException ex) {
+            obj.key(jErrors) ;
+            obj.startArray() ;      // Errors array
+            obj.startObject() ;
+            obj.key(jParseError).value(ex.getMessage()) ;
+            obj.key(jParseErrorLine).value(ex.getLine()) ;
+            obj.key(jParseErrorCol).value(ex.getColumn()) ;
+            obj.finishObject() ;
+            obj.finishArray() ;
+            
+            obj.finishObject() ; // Outer object
+            return obj.build().getAsObject() ;
+        }
+        
+        formatted(obj, request) ;
+        
+        obj.finishObject() ;
+        return obj.build().getAsObject() ;
+    }
+
+    private static void formatted(JsonBuilder obj, UpdateRequest updateRequest) {
+        IndentedLineBuffer out = new IndentedLineBuffer() ;
+        updateRequest.output(out) ;
+        obj.key(jFormatted).value(out.asString()) ;
+    }
+}
+

http://git-wip-us.apache.org/repos/asf/jena/blob/9a0294f5/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/json/ValidationAction.java
----------------------------------------------------------------------
diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/json/ValidationAction.java
b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/json/ValidationAction.java
new file mode 100644
index 0000000..29cb44c
--- /dev/null
+++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/json/ValidationAction.java
@@ -0,0 +1,95 @@
+/*
+ * 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.validation.json;
+
+import java.util.HashMap ;
+import java.util.Map ;
+
+import javax.servlet.http.HttpServletRequest ;
+import javax.servlet.http.HttpServletResponse ;
+
+import org.apache.jena.atlas.logging.Log ;
+import org.slf4j.Logger ;
+
+public class ValidationAction
+{
+    public final boolean verbose ;
+    public final long id ;
+    public final Logger log ;
+    private boolean startTimeIsSet = false ;
+    private boolean finishTimeIsSet = false ;
+
+    private long startTime = -2 ;
+    private long finishTime = -2 ;
+    
+    // Outcome.
+    public int statusCode = -1 ;
+    public String message = null ;
+    public int contentLength = -1 ;
+    public String contentType = null ;
+    
+    public Map <String, String> headers = new HashMap<>() ;
+    public HttpServletRequest request;
+    public HttpServletResponse response ;
+    
+    public ValidationAction(long id, Logger log, HttpServletRequest request, HttpServletResponse
response, boolean verbose) {
+        this.id = id ;
+        this.log = log ;
+        this.request = request ;
+        this.response = response ;
+        this.verbose = false ;
+    }
+
+    /** Reduce to a size that can be kept around for sometime */
+    public void minimize() {
+        this.request = null ;
+        this.response = null ;
+    }
+
+    public void setStartTime() {
+        if ( startTimeIsSet ) 
+            Log.warn(this,  "Start time reset") ;
+        startTimeIsSet = true ;
+        this.startTime = System.nanoTime() ;
+    }
+
+    public void setFinishTime() {
+        if ( finishTimeIsSet ) 
+            Log.warn(this,  "Finish time reset") ;
+        finishTimeIsSet = true ;
+        this.finishTime = System.nanoTime() ;
+    }
+
+    public HttpServletRequest getRequest()          { return request ; }
+
+    public HttpServletResponse getResponse()        { return response ; }
+    
+    /** Return the recorded time taken in milliseconds. 
+     *  {@link #setStartTime} and {@link #setFinishTime}
+     *  must have been called.
+     */
+    public long getTime()
+    {
+        if ( ! startTimeIsSet ) 
+            Log.warn(this,  "Start time not set") ;
+        if ( ! finishTimeIsSet ) 
+            Log.warn(this,  "Finish time not set") ;
+        return (finishTime-startTime)/(1000*1000) ;
+    }
+}

http://git-wip-us.apache.org/repos/asf/jena/blob/9a0294f5/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/json/ValidationError.java
----------------------------------------------------------------------
diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/json/ValidationError.java
b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/json/ValidationError.java
new file mode 100644
index 0000000..7a9ffdf
--- /dev/null
+++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/json/ValidationError.java
@@ -0,0 +1,24 @@
+/**
+ * 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.validation.json;
+
+public class ValidationError {
+
+}
+

http://git-wip-us.apache.org/repos/asf/jena/blob/9a0294f5/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/json/ValidatorJsonLib.java
----------------------------------------------------------------------
diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/json/ValidatorJsonLib.java
b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/json/ValidatorJsonLib.java
new file mode 100644
index 0000000..631aabb
--- /dev/null
+++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/json/ValidatorJsonLib.java
@@ -0,0 +1,85 @@
+/*
+ * 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.validation.json;
+
+import static org.apache.jena.riot.WebContent.charsetUTF8;
+import static org.apache.jena.riot.WebContent.contentTypeJSON;
+
+import java.util.concurrent.atomic.AtomicLong;
+
+import javax.servlet.http.HttpServletResponse ;
+
+import org.apache.jena.fuseki.Fuseki ;
+import org.apache.jena.fuseki.servlets.ServletOps ;
+import org.apache.jena.web.HttpSC ;
+import org.slf4j.Logger ;
+
+/** Validation operations for JSON output */ 
+public abstract class ValidatorJsonLib
+{
+    private static Logger vLog = Fuseki.validationLog ;
+    public static final String jErrors          = "errors" ;
+    public static final String jWarnings        = "warning" ;
+
+    public static final String jParseError      = "parse-error" ;
+    public static final String jParseErrorLine  = "parse-error-line" ;
+    public static final String jParseErrorCol   = "parse-error-column" ;
+
+    public static final String respService      = "X-Service" ;
+    
+    protected static AtomicLong counter = new AtomicLong(0);
+    
+    static void setHeaders(HttpServletResponse httpResponse)
+    {
+        httpResponse.setCharacterEncoding(charsetUTF8) ;
+        httpResponse.setContentType(contentTypeJSON) ;
+        httpResponse.setHeader(respService, "Jena Fuseki Validator : http://jena.apache.org/")
;
+    }
+
+    static String getArg(ValidationAction action, String paramName) {
+        String arg = getArgOrNull(action, paramName) ;
+        if ( arg == null ) {
+            ServletOps.error(HttpSC.BAD_REQUEST_400, "No parameter given: " + paramName)
;
+            return null ;
+        }
+        return arg ;
+    }
+
+    static String getArgOrNull(ValidationAction action, String paramName) {
+        String[] args = getArgs(action, paramName) ;
+
+        if ( args == null || args.length == 0 )
+            return null ;
+
+        if ( args.length > 1 ) {
+            ServletOps.error(HttpSC.BAD_REQUEST_400, "Too many ("+args.length+") parameter
values: "+paramName) ;
+            return null ;
+        }
+        
+        return args[0] ;
+    }
+    
+    static String[] getArgs(ValidationAction action, String paramName) {
+        String[] args = action.request.getParameterValues(paramName) ;
+        if ( args == null || args.length == 0 )
+            return null ;
+        return args ;
+    }
+}    
+


Mime
View raw message