001 /***************************************************************************/
002 /* Copyright (C) 2010-2011, Sebastian Hellmann */
003 /* Note: If you need parts of NLP2RDF in another licence due to licence */
004 /* incompatibility, please mail hellmann@informatik.uni-leipzig.de */
005 /* */
006 /* This file is part of NLP2RDF. */
007 /* */
008 /* NLP2RDF is free software; you can redistribute it and/or modify */
009 /* it under the terms of the GNU General Public License as published by */
010 /* the Free Software Foundation; either version 3 of the License, or */
011 /* (at your option) any later version. */
012 /* */
013 /* NLP2RDF is distributed in the hope that it will be useful, */
014 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
015 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
016 /* GNU General Public License for more details. */
017 /* */
018 /* You should have received a copy of the GNU General Public License */
019 /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
020 /***************************************************************************/
021
022 package org.nlp2rdf.webservice;
023
024
025 import com.hp.hpl.jena.ontology.OntModel;
026 import com.hp.hpl.jena.ontology.OntModelSpec;
027 import com.hp.hpl.jena.rdf.model.ModelFactory;
028 import com.hp.hpl.jena.rdf.model.RDFWriter;
029 import com.hp.hpl.jena.shared.JenaException;
030 import org.slf4j.Logger;
031 import org.slf4j.LoggerFactory;
032
033 import java.io.*;
034 import java.net.URL;
035 import java.net.URLConnection;
036 import java.net.URLEncoder;
037 import java.util.HashMap;
038 import java.util.Map;
039
040 /**
041 * User: Sebastian Hellmann - http://bis.informatik.uni-leipzig.de/SebastianHellmann
042 */
043 public class SampleClient {
044
045 private static Logger log = LoggerFactory.getLogger(SampleClient.class);
046 public static String exampleText = "That's a lot of nuts! That'll be four bucks, baby! You want fries with that? ";
047
048
049 public static void main(String[] args) throws Exception {
050 String nifServiceUrl1 = "http://localhost:8080/webservice/NIFConverter";
051 String nifServiceUrl2 = "http://localhost:8080/webservice/NIFStemmer";
052
053 //convert the text 2 RDF
054 OntModel m = convertToNIF(nifServiceUrl1, exampleText);
055 log.info("Conversion finished. Retrieved " + m.size() + " triples ");
056
057 //apply the stemmer
058 m = postToNIFService(nifServiceUrl2, m);
059 log.info("retrieved " + m.size() + " triples ");
060 }
061
062 public static OntModel convertToNIF(String nifServiceUrl, String text) throws Exception {
063 Map<String, String> m = new HashMap<String, String>();
064 m.put("input", text);
065 m.put("type", "text");
066 return postToNIFService(nifServiceUrl, m);
067
068 }
069
070
071 public static OntModel postToNIFService(String nifServiceUrl, OntModel model) throws Exception {
072 Map<String, String> m = new HashMap<String, String>();
073 ByteArrayOutputStream baos = new ByteArrayOutputStream();
074 RDFWriter writer = model.getWriter("RDF/XML");
075 writer.write(model, baos, "");
076 m.put("input", baos.toString());
077 m.put("type", "nif-owl");
078 return postToNIFService(nifServiceUrl, m);
079
080 }
081
082 public static OntModel postToNIFService(String nifServiceUrl, Map<String, String> parameterMap) throws Exception {
083 StringBuffer data = new StringBuffer();
084 boolean first = true;
085 for (String key : parameterMap.keySet()) {
086 String value = parameterMap.get(key);
087 // Construct data
088 if (first) {
089 first = false;
090 } else {
091 data.append("&");
092 }
093 data.append(URLEncoder.encode(key, "UTF-8")).append("=").append(URLEncoder.encode(value, "UTF-8"));
094
095 }
096 // Send data
097 URL url = new URL(nifServiceUrl);
098 URLConnection conn = url.openConnection();
099 conn.setDoOutput(true);
100 conn.setDoInput(true);
101
102 OntModel ret = null;
103 OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
104 try {
105 wr.write(data.toString());
106 wr.flush();
107
108 //buffer the stream as a String to get possible error messages
109 BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
110 StringBuilder sb = new StringBuilder();
111 String inputLine;
112 while ((inputLine = in.readLine()) != null) {
113 sb.append(inputLine);
114 }
115 in.close();
116 //make a new inputstream from the string
117 ByteArrayInputStream bais = new ByteArrayInputStream(sb.toString().getBytes());
118
119 ret = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, ModelFactory.createDefaultModel());
120 try {
121 // Get the response with jena
122 ret.read(bais, "");
123 } catch (JenaException e) {
124 //if Jena can not parse the method
125 String msg = "An error occured, while reading the nif model with Jena: \n" + sb.toString();
126 msg = (msg.length() > 500) ? msg.substring(0, 500) : msg;
127 log.error(msg, e);
128 }
129 } finally {
130 wr.close();
131 }
132 return ret;
133
134 }
135 }