1   package eu.fbk.dkm.pikes.tintop.server;
2   
3   import eu.fbk.dkm.pikes.rdf.RDFGenerator;
4   import eu.fbk.dkm.pikes.rdf.Renderer;
5   import eu.fbk.dkm.pikes.resources.NAFFilter;
6   import eu.fbk.dkm.pikes.tintop.AnnotationPipeline;
7   import ixa.kaflib.KAFDocument;
8   import org.glassfish.grizzly.http.server.Request;
9   import org.glassfish.grizzly.http.server.Response;
10  import org.eclipse.rdf4j.model.Model;
11  import org.slf4j.Logger;
12  import org.slf4j.LoggerFactory;
13  
14  import java.io.StringWriter;
15  import java.util.HashMap;
16  
17  /**
18   * Created with IntelliJ IDEA.
19   * User: alessio
20   * Date: 21/07/14
21   * Time: 15:30
22   * To change this template use File | Settings | File Templates.
23   */
24  
25  public class EverythingHandler extends AbstractHandler {
26  
27  	private static final Logger LOGGER = LoggerFactory.getLogger(EverythingHandler.class);
28  
29  	public EverythingHandler(AnnotationPipeline pipeline) {
30  		super(pipeline);
31  	}
32  
33  	@Override
34  	public void service(Request request, Response response) throws Exception {
35  
36  		super.service(request, response);
37  
38  		String host = request.getHeader("x-forwarded-for");
39  
40  		String referer = request.getHeader("referer");
41  		String okReferer = pipeline.getDefaultConfig().getProperty("back_referer");
42  
43  		boolean backLink = false;
44  		if (okReferer != null || referer != null) {
45  			backLink = true;
46  		}
47  
48  //		if (referer != null && okReferer != null && referer.equals(okReferer)) {
49  //			backLink = true;
50  //		}
51  
52  		String text = request.getParameter("text");
53  
54  		// Log for stats
55  //		LOGGER.info("[SENTENCE]");
56  //		LOGGER.info("Host: {}", host);
57  //		LOGGER.info("Text: {}", text);
58  
59  		KAFDocument doc = text2naf(text, meta);
60  
61  		doc = pipeline.parseFromString(doc.toString());
62  
63  		String viewString;
64  		viewString = doc.toString();
65  		try {
66  
67  			HashMap<String, Object> demoProperties = new HashMap<>();
68  			demoProperties.put("renderer.template.title", "PIKES demo");
69  			if (backLink) {
70  				demoProperties.put("renderer.template.backlink", "javascript:history.back();");
71  			}
72  			else {
73  				demoProperties.put("renderer.template.backlink", pipeline.getDefaultConfig().getProperty("back_alt_link"));
74  				demoProperties.put("renderer.template.backlabel", pipeline.getDefaultConfig().getProperty("back_alt_text"));
75  			}
76  
77  			boolean fusion = request.getParameter("rdf_fusion") != null;
78  			boolean normalization = request.getParameter("rdf_compaction") != null;
79  
80  			demoProperties.put("generator.fusion", fusion);
81  			demoProperties.put("generator.normalization", normalization);
82  
83  			NAFFilter filter = NAFFilter.builder().withProperties(pipeline.getDefaultConfig(), "filter").build();
84  			RDFGenerator generator = RDFGenerator.builder().withProperties(demoProperties, "generator").build();
85  			Renderer renderer = Renderer.builder().withProperties(demoProperties, "renderer").build();
86  
87  			filter.filter(doc);
88  			final Model model = generator.generate(doc, null);
89  			StringWriter writer = new StringWriter();
90  			renderer.renderAll(writer, doc, model, null, null);
91  			viewString = writer.toString();
92  
93  		} catch (Exception e) {
94  			e.printStackTrace();
95  			viewString = "Unable to show graph. <br /><br />\n<pre>" + doc.toString().replace("<", "&lt;").replace(">", "&gt;") + "</pre>";
96  		}
97  
98  		super.writeOutput(response, "text/html", viewString);
99  	}
100 }