Skip to content

Commit

Permalink
Correct timeout issue and display source after validation
Browse files Browse the repository at this point in the history
  • Loading branch information
zetaben committed Nov 14, 2011
1 parent bb0a42f commit e258e45
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 27 deletions.
38 changes: 33 additions & 5 deletions src/com/feedbooks/opds/webvalidator/OPDSValidatorWebServlet.java
@@ -1,16 +1,28 @@
package com.feedbooks.opds.webvalidator;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.net.URL;

import javax.servlet.http.*;

import org.json.JSONString;

import com.feedbooks.opds.*;
import com.google.appengine.api.urlfetch.HTTPMethod;
import com.google.appengine.api.urlfetch.HTTPRequest;
import com.google.appengine.api.urlfetch.HTTPResponse;
import com.google.appengine.api.urlfetch.URLFetchServiceFactory;
import com.google.appengine.repackaged.org.json.JSONObject;
import com.thaiopensource.xml.sax.ErrorHandlerImpl;
import static com.google.appengine.api.urlfetch.FetchOptions.Builder.*;

@SuppressWarnings("serial")
public class OPDSValidatorWebServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("application/json");

String uri = req.getParameter("uri");
if (uri != null && !uri.equals("") && uri.startsWith("http")) {
Validator v = new Validator();
Expand All @@ -20,16 +32,32 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp)
opds_version = ve;
}
v.SetOPDSVersion(opds_version);
ErrorHandlerImpl eh = new JSONErrorHandlerImpl(resp
.getOutputStream());
StringWriter validation_results=new StringWriter();
ErrorHandlerImpl eh = new JSONErrorHandlerImpl(validation_results);
v.SetErrorHandler(eh);
boolean hadErr = v.validate(req.getParameterValues("uri"));

URL url=new URL(req.getParameter("uri"));
HTTPResponse get =URLFetchServiceFactory.getURLFetchService().fetch(new HTTPRequest(url, HTTPMethod.GET,followRedirects().setDeadline(60.0)));
String source=new String(get.getContent());
String[] args={source};
String[] names={url.toString()};
boolean hadErr = v.validate(args,names);

if (hadErr) {
resp.setStatus(409);
}

eh.close();
//resp.getOutputStream();
BufferedWriter w=new BufferedWriter(resp.getWriter());
w.append("{ \"results\": ");
w.append(validation_results.getBuffer());
w.append(", \"source\": ");
w.append(JSONObject.quote(source));
w.append("}");


w.close();

} else {
resp.setStatus(400);
}
Expand Down
10 changes: 9 additions & 1 deletion war/css/main.css
Expand Up @@ -113,4 +113,12 @@ li span.err_type img {
.msg {
font-weight: bold;
line-height: 130%;
}
}

#source {
background-color:#FFF;
font-family: consolas,courrier,monospace;
padding:15px;

}

56 changes: 35 additions & 21 deletions war/index.html
@@ -1,11 +1,4 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- The HTML 4.01 Transitional DOCTYPE declaration-->
<!-- above set at the top of the file will set -->
<!-- the browser's rendering engine into -->
<!-- "Quirks Mode". Replacing this declaration -->
<!-- with a "Standards Mode" doctype is supported, -->
<!-- but may lead to some differences in layout. -->

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
Expand Down Expand Up @@ -78,7 +71,8 @@ <h2>Unofficial Validator</h2>
function validation_successful(errs){
//console.log("success");
res_init(true);
if(errs.length > 0 ){

if(errs.results.length > 0 ){
$("#results h3").append(" with warnings");
render_validation_results(errs);

Expand All @@ -88,6 +82,7 @@ <h2>Unofficial Validator</h2>

function validation_failed(req){
// console.log("failure");

res_init(false);
if(req.status==409){
var errs=$.parseJSON(req.responseText);
Expand All @@ -100,32 +95,51 @@ <h2>Unofficial Validator</h2>

function render_validation_results(errs){
var resu=" <ol id=\"error_loop\"> "
for(var i=0;i<errs.length;i++){
var errtab=[];
for(var i=0;i<errs.results.length;i++){

if(errs.results[i].severity){
var txt=""
if(errs[i].severity=="error"){

if(errs.results[i].severity=="error"){
txt="<img src=\"img/error.png\" alt=\"Error\" title=\"Error\" />"
}else if(errs[i].severity=="fatal"){
}else if(errs.results[i].severity=="fatal"){
txt="<img src=\"img/fatal.png\" alt=\"Fatal Error\" title=\"Fatal Error\" />"
}if(errs[i].severity=="warning"){
}if(errs.results[i].severity=="warning"){
txt="<img src=\"img/warning.png\" alt=\"Warning\" title=\"Warning\" />"
}if(errs[i].severity=="info"){
}if(errs.results[i].severity=="info"){
txt="<img src=\"img/info.png\" alt=\"Information\" title=\"Information\" />"
}
txt="<span class=\"err_type\">"+txt+"</span>"
txt+="<em>"
if(errs[i].line > 0 ){
txt+="Line "+errs[i].line
if(errs[i].column > 0){txt+=", "}
if(errs.results[i].line > 0 ){
txt+="<a href=\"#line"+errs.results[i].line+"\">"
txt+="Line "+errs.results[i].line
txt+="</a>"
errtab[errs.results[i].line]=errs.results[i].severity;
if(errs.results[i].column > 0){txt+=", "}
}
if(errs[i].column > 0){
txt+="Column "+errs[i].column
if(errs.results[i].column > 0){
txt+="Column "+errs.results[i].column
}
txt+="</em> \n<span class=\"msg\">"+errs[i].message+"</span>"
txt+="</em> \n<span class=\"msg\">"+errs.results[i].message+"</span>"
resu+="<li class=\"msg_err sev"+errs.results[i].severity+"\">"+txt+"</li>"
}else{
resu+="<li class=\"msg_err sevfatal\"><img src=\"img/fatal.png\" alt=\"Fatal Error\" title=\"Fatal Error\" /><em>Fatal</em>\n<span class=\"msg\">"+errs.results[i]+"</span></li>"
}
}
resu+="</ol>"

resu+="<li class=\"msg_err sev"+errs[i].severity+"\">"+txt+"</li>"
resu+="<h3>Source</h3> <div id=\"source\"> <ol>"
var lines=errs.source.split("\n");

for(var i=0;i<lines.length;i++){
resu+="<li id=\"line"+(i+1)+"\" "+(errtab[i+1] ? "class=\"sev"+errtab[i+1]+"\"" : "")+">"+lines[i].replace(/&/g,"&amp;").replace(/>/g,"&gt;").replace(/</g,"&lt;")+"</li>"
}
$("#results").append(resu+"</ol>")



$("#results").append(resu+"</ol></div>")
}

function form_submitted(){
Expand Down

0 comments on commit e258e45

Please sign in to comment.