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