comparison src/de/mpiwg/itgroup/nimanager/persons/PersonService.java @ 0:1384a0d382fa

first input
author dwinter
date Thu, 30 Jun 2011 11:44:24 +0200
parents
children e3ecb88314a5
comparison
equal deleted inserted replaced
-1:000000000000 0:1384a0d382fa
1 package de.mpiwg.itgroup.nimanager.persons;
2
3 import java.io.IOException;
4 import java.io.UnsupportedEncodingException;
5 import java.net.URLDecoder;
6 import java.util.Collection;
7 import java.util.Map;
8 import java.util.Set;
9
10 import org.apache.log4j.Logger;
11 import org.apache.lucene.index.CorruptIndexException;
12 import org.openrdf.repository.RepositoryException;
13 import org.restlet.Context;
14 import org.restlet.data.Form;
15 import org.restlet.data.MediaType;
16 import org.restlet.data.Status;
17 import org.restlet.engine.component.ChildContext;
18 import org.restlet.representation.Representation;
19 import org.restlet.representation.StringRepresentation;
20 import org.restlet.resource.Options;
21 import org.restlet.resource.Post;
22 import org.restlet.resource.Put;
23 import org.restlet.resource.ServerResource;
24 import org.restlet.resource.Get;
25
26 import de.mpiwg.itgroup.nimanager.exceptions.TripleStoreHandlerException;
27 import de.mpiwg.itgroup.nimanager.luceneIndices.Importer;
28 import de.mpiwg.itgroup.nimanager.owl.MetaDataHandler;
29 import de.mpiwg.itgroup.nimanager.owl.TripleStoreHandler;
30 import de.mpiwg.itgroup.nimanager.owl.Util;
31
32 public class PersonService extends ServerResource {
33 private String NAMED_ENTITIES_ONTOLOGIE_URL;
34 private String VIRTUOSO_PW;
35 private String VIRTUOSO_USER;
36 private String virtuoso_server_url;
37 private String TRIPLE_INDEX_PATH;
38 private Logger rl = Logger.getRootLogger();
39 private MetaDataHandler mh;
40 private TripleStoreHandler th;
41
42
43
44 public PersonService(){
45 //mh = MetaDataHandler.getInstance();
46 ChildContext context = (ChildContext)Context.getCurrent();
47 TRIPLE_INDEX_PATH = context.getParameters().getFirstValue("de.mpwig.itgroup.personSearch.index");
48 virtuoso_server_url = context.getParameters().getFirstValue("de.mpwig.itgroup.personSearch.virtuoso.url");
49 VIRTUOSO_PW = context.getParameters().getFirstValue("de.mpwig.itgroup.personSearch.virtuoso.pw");
50 VIRTUOSO_USER = context.getParameters().getFirstValue("de.mpwig.itgroup.personSearch.virtuoso.user");
51
52 try {
53 th = new TripleStoreHandler(virtuoso_server_url, VIRTUOSO_USER, VIRTUOSO_PW);
54 } catch (TripleStoreHandlerException e) {
55 // TODO Auto-generated catch block
56 e.printStackTrace();
57 }
58
59 }
60 @Options
61 public void doOptions(Representation entity) {
62 Form responseHeaders = (Form) getResponse().getAttributes().get(
63 "org.restlet.http.headers");
64 if (responseHeaders == null) {
65 responseHeaders = new Form();
66 getResponse().getAttributes().put("org.restlet.http.headers",
67 responseHeaders);
68 }
69 responseHeaders.add("Access-Control-Allow-Origin", "*");
70 responseHeaders.add("Access-Control-Allow-Methods", "POST,OPTIONS,GET");
71 responseHeaders.add("Access-Control-Allow-Headers", "Content-Type");
72 responseHeaders.add("Access-Control-Allow-Credentials", "false");
73 responseHeaders.add("Access-Control-Max-Age", "60");
74 }
75
76 @Get("XML")
77 public Representation getXML() throws UnsupportedEncodingException{
78 String id = (String)getRequest().getAttributes().get("personId");
79 id = URLDecoder.decode(id,"utf-8");
80 if (id.startsWith("dbpedia:")){
81 String[] splitted=id.split(":");
82 return handleDBPedia(splitted[1]);
83 } else if (id.startsWith(NAMED_ENTITIES_ONTOLOGIE_URL)){
84 String[] splitted=id.split(":");
85 return handlePerson(id);
86 }
87 getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
88 return new StringRepresentation("<error>not supported</error>",MediaType.TEXT_XML);
89
90 }
91
92 private Representation handlePerson(String id) {
93 String personString =getPerson(id);
94 return new StringRepresentation(personString, MediaType.TEXT_XML);
95
96 }
97
98
99
100 private Representation handleDBPedia(String id) {
101 String personString = getPerson("<http://dbpedia.org/resource/"+id+">");
102 return new StringRepresentation(personString, MediaType.TEXT_XML);
103
104
105 }
106
107
108 private String getPerson(String id) {
109 Map<String, Object> values=null;
110 try {
111 //Collection props = mh.getDirectPropsForClass("http://xmlns.com/foaf/0.1/Person");
112 Collection props = mh.getDirectPropsForClass("http://dbpedia.org/ontology/Person",true);
113 values = th.getJenaRDFValues(props, id, mh.getOwlModel());
114 } catch (RepositoryException e) {
115 e.printStackTrace();
116 return null;
117 }
118
119 String xmlVersion = Util.rdf2xml(values);
120
121
122 return xmlVersion;
123 }
124
125 /**
126 * different modes :
127 *
128 *Indizierte alle werte des triples mit "personId"
129 *Im Body kann eine Liste von Predicate Ÿbegeben werden die indiziert werden sollen und sich im triplestore befinden mŸssen.
130 * Wird kein body Ÿbergeben ist das predicate="http://xmlns.com/foaf/0.1/name".
131 * Wird graphId zusŠtzlich Ÿbergeben, wird der Eintrag aus diesem Graphen indiziert, default ist: "file://newpersonsFromProjects"
132 * @param entity
133 * @return
134 */
135 @Put
136 public Representation doPut(Representation entity){
137 String id = (String) getRequest().getAttributes().get("personId");
138 String graphId = (String) getRequest().getAttributes().get("graphId");
139
140 try {
141 id = URLDecoder.decode(id,"utf-8");
142
143 } catch (UnsupportedEncodingException e1) {
144 setStatus(Status.CLIENT_ERROR_NOT_ACCEPTABLE);
145 return new StringRepresentation("not a valid url:"+id);
146
147 }
148 if (graphId==null || graphId.equals("")){
149 graphId="file://newpersonsFromProjects";
150 } else {
151 try {
152 graphId = URLDecoder.decode(graphId,"utf-8");
153 } catch (UnsupportedEncodingException e) {
154 setStatus(Status.CLIENT_ERROR_NOT_ACCEPTABLE);
155 return new StringRepresentation("not a valid graph Id:"+graphId);
156
157 }
158 }
159 String cnt;
160 try {
161 cnt = entity.getText();
162 rl.info(cnt);
163 } catch (IOException e) {
164 // TODO Auto-generated catch block
165 e.printStackTrace();
166 cnt="";
167 }
168
169 if (cnt.equals("")){
170 cnt="http://xmlns.com/foaf/0.1/name";
171 }
172 String[] predicates = cnt.split("\n");
173
174 int indexedTriples =doIndexing(graphId,id,predicates);
175 if (indexedTriples<0){
176 setStatus(Status.SERVER_ERROR_INTERNAL);
177 return new StringRepresentation("Konnte nicht indizieren!");
178 }
179 return new StringRepresentation(String.valueOf(indexedTriples));
180
181 //setStatus(Status.CLIENT_ERROR_NOT_ACCEPTABLE);
182 //return new StringRepresentation("Mode not known!");
183 }
184
185
186 private int doIndexing(String graphId,String identifier, String[] predicates) {
187 TripleStoreHandler th;
188 try {
189 th = new TripleStoreHandler(virtuoso_server_url, VIRTUOSO_USER, VIRTUOSO_PW);
190 } catch (TripleStoreHandlerException e) {
191 rl.error("indexing",e);
192 return 0;
193 }
194 Importer im = new Importer(graphId, predicates, th, TRIPLE_INDEX_PATH); // open the indexer
195 try {
196 im.initializeIndexer(false); //initialize
197 } catch (IOException e1) {
198 rl.error("indexing",e1);
199 return 0;
200 }
201 int count=0;
202 try {
203 count = im.writeStatementsToIndex(identifier);
204 } catch (RepositoryException e) {
205 rl.error("do Import",e);
206 count=-1;
207 } catch (CorruptIndexException e) {
208 rl.error("do Import",e);
209 count=-1;
210 } catch (IOException e) {
211 rl.error("do Import",e);
212 count=-1;
213 }
214
215 try {
216 im.close();
217 } catch (CorruptIndexException e) {
218 rl.error("do Import",e);
219 count=-1;
220 } catch (IOException e) {
221 rl.error("do Import",e);
222 count=-1;
223 }
224
225 return count;
226
227 }
228 }