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