1   package eu.fbk.dkm.pikes.tintop.server;
2   
3   import eu.fbk.dkm.pikes.tintop.AnnotationPipeline;
4   import ixa.kaflib.KAFDocument;
5   import org.apache.log4j.Logger;
6   import org.glassfish.grizzly.http.server.HttpHandler;
7   import org.glassfish.grizzly.http.server.Request;
8   import org.glassfish.grizzly.http.server.Response;
9   
10  import java.io.IOException;
11  import java.text.SimpleDateFormat;
12  import java.util.Date;
13  import java.util.HashMap;
14  import java.util.Locale;
15  import java.util.regex.Matcher;
16  import java.util.regex.Pattern;
17  
18  /**
19   * Created with IntelliJ IDEA.
20   * User: alessio
21   * Date: 23/07/14
22   * Time: 09:12
23   * To change this template use File | Settings | File Templates.
24   */
25  
26  public class AbstractHandler extends HttpHandler {
27  
28  //	public final static Pattern ANNOTATOR_PATTERN = Pattern.compile("^annotator_(.*)");
29  	public final static Pattern META_PATTERN = Pattern.compile("^meta_(.*)");
30  	protected AnnotationPipeline pipeline;
31  
32  //	protected HashSet<String> annotators = new HashSet<>();
33  	protected HashMap<String, String> meta = new HashMap<>();
34  
35  	static Logger logger = Logger.getLogger(AbstractHandler.class.getName());
36  
37  	public AbstractHandler(AnnotationPipeline pipeline) {
38  		super();
39  		this.pipeline = pipeline;
40  	}
41  
42  	@Override
43  	public void service(Request request, Response response) throws Exception {
44  		request.setCharacterEncoding("UTF-8");
45  		response.setCharacterEncoding("UTF-8");
46  
47  
48  		for (String parameterLabel : request.getParameterMap().keySet()) {
49  
50  			Matcher matcher;
51  
52  //			matcher = ANNOTATOR_PATTERN.matcher(parameterLabel);
53  //			if (matcher.find()) {
54  //				annotators.add(matcher.group(1));
55  //			}
56  
57  			matcher = META_PATTERN.matcher(parameterLabel);
58  			if (matcher.find()) {
59  				String key = matcher.group(1);
60  				meta.put(key, request.getParameter(parameterLabel));
61  			}
62  		}
63  
64  		if (meta.get("uri") == null || meta.get("uri").length() == 0) {
65  			meta.put("uri", pipeline.getDefaultConfig().getProperty("default_uri"));
66  		}
67  	}
68  
69  	public void writeOutput(Response response, String contentType, String output) throws IOException {
70  		response.setContentType(contentType);
71  		response.addHeader("Access-Control-Allow-Origin", "*");
72  		response.getWriter().write(output);
73  	}
74  
75  	public static KAFDocument text2naf(String text, HashMap<String, String> meta) {
76  		KAFDocument doc = new KAFDocument("en", "v3");
77  		doc.setRawText(text);
78  
79  		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX", Locale.ENGLISH);
80  
81  		String date = "";
82  		try {
83  			date = format.format(new Date());
84  			if (meta.get("date") != null) {
85  				date = format.format(format.parse(meta.get("date")));
86  			}
87  		} catch (Exception e) {
88  			logger.error(e.getMessage());
89  		}
90  
91  		KAFDocument.FileDesc fileDesc = doc.createFileDesc();
92  		KAFDocument.Public aPublic = doc.createPublic();
93  
94  		if (meta != null) {
95  			fileDesc.author = meta.get("author");
96  			fileDesc.title = meta.get("title");
97  			fileDesc.filename = meta.get("filename");
98  			fileDesc.creationtime = date;
99  			aPublic.publicId = meta.get("id");
100 			aPublic.uri = meta.get("uri");
101 		}
102 
103 //		KAFDocument.Public p = doc.createPublic();
104 //		p.uri = "http://www.example.com";
105 //		p.publicId = "0";
106 //
107 //		KAFDocument.FileDesc d = doc.createFileDesc();
108 //		d.creationtime = date;
109 //		d.author = "Unknown author";
110 //		d.filename = "test.xml";
111 //		d.title = "Unknown title";
112 
113 		return doc;
114 	}
115 }