0
|
1 package de.mpiwg.itgroup.escidoc.MPIWGServices.Restlet.services.search;
|
|
2
|
|
3
|
|
4
|
|
5 import java.io.IOException;
|
|
6 import java.io.InputStream;
|
|
7 import java.io.OutputStream;
|
|
8 import java.io.UnsupportedEncodingException;
|
|
9 import java.net.URLEncoder;
|
|
10 import java.util.Properties;
|
|
11
|
|
12 import javax.xml.transform.Transformer;
|
|
13 import javax.xml.transform.TransformerConfigurationException;
|
|
14 import javax.xml.transform.TransformerException;
|
|
15 import javax.xml.transform.TransformerFactory;
|
|
16 import javax.xml.transform.TransformerFactoryConfigurationError;
|
|
17 import javax.xml.transform.stream.StreamSource;
|
|
18
|
|
19 import org.apache.http.HttpEntity;
|
|
20 import org.apache.http.HttpResponse;
|
|
21 import org.apache.log4j.Logger;
|
|
22 import org.jdom.Document;
|
|
23 import org.jdom.JDOMException;
|
|
24 import org.jdom.input.SAXBuilder;
|
|
25 import org.jdom.output.XMLOutputter;
|
|
26 import org.jdom.transform.JDOMResult;
|
|
27 import org.jdom.transform.JDOMSource;
|
|
28 import org.jdom.transform.XSLTransformer;
|
|
29 import org.restlet.data.Form;
|
|
30 import org.restlet.data.MediaType;
|
|
31 import org.restlet.data.Status;
|
|
32 import org.restlet.representation.InputRepresentation;
|
|
33 import org.restlet.representation.Representation;
|
|
34 import org.restlet.representation.StreamRepresentation;
|
|
35 import org.restlet.representation.StringRepresentation;
|
|
36 import org.restlet.resource.Get;
|
|
37 import org.restlet.resource.Options;
|
|
38 import org.restlet.resource.Post;
|
|
39 import org.restlet.resource.ServerResource;
|
|
40 import org.w3c.dom.Entity;
|
|
41
|
|
42 import de.mpiwg.itgroup.eSciDoc.Tools.EScidocBasicHandler;
|
|
43
|
|
44 public class SimpleSearch extends ServerResource{
|
|
45
|
4
|
46
|
0
|
47 private String eScidocServer;
|
|
48 private String eScidocPort;
|
|
49 private Logger logger= Logger.getRootLogger();
|
|
50 private String eScidocUser;
|
|
51 private String eScidocUserPW;
|
4
|
52
|
|
53 private String SEARCH_CMM="escidoc:131281"; //TODO: make this configurable, also a list of CMM should be possible
|
0
|
54
|
|
55 public SimpleSearch() throws IOException{
|
|
56 InputStream is = getClass().getResourceAsStream("/de/mpiwg/itgroup/escidoc/MPIWGServices/Restlet/config/eScidocServer.config");
|
|
57 Properties prop = new Properties();
|
|
58 prop.load(is);
|
|
59 eScidocServer= (String)prop.get("eScidocServer");
|
|
60 eScidocPort=(String)prop.get("eScidocPort");
|
|
61 eScidocUser=(String)prop.get("eScidocUser");
|
|
62 eScidocUserPW=(String)prop.get("eScidocUserPW");
|
|
63 }
|
|
64 @Options
|
|
65 public void doOptions(Representation entity) {
|
|
66 Form responseHeaders = (Form) getResponse().getAttributes().get("org.restlet.http.headers");
|
|
67 if (responseHeaders == null) {
|
|
68 responseHeaders = new Form();
|
|
69 getResponse().getAttributes().put("org.restlet.http.headers", responseHeaders);
|
|
70 }
|
|
71 responseHeaders.add("Access-Control-Allow-Origin", "*");
|
|
72 responseHeaders.add("Access-Control-Allow-Methods", "POST,OPTIONS,GET");
|
|
73 responseHeaders.add("Access-Control-Allow-Headers", "Content-Type");
|
|
74 responseHeaders.add("Access-Control-Allow-Credentials", "false");
|
|
75 responseHeaders.add("Access-Control-Max-Age", "60");
|
|
76 }
|
|
77
|
|
78
|
|
79 @Get("html")
|
|
80 public Representation getHtml(){
|
|
81
|
|
82 // response header fuer cross-site.scripting
|
|
83 Form responseHeaders = (Form) getResponse().getAttributes().get("org.restlet.http.headers");
|
|
84 if (responseHeaders == null) {
|
|
85 responseHeaders = new Form();
|
|
86 getResponse().getAttributes().put("org.restlet.http.headers", responseHeaders);
|
|
87 }
|
|
88 responseHeaders.add("Access-Control-Allow-Origin", "*");
|
|
89
|
|
90 Form form = getRequest().getResourceRef().getQueryAsForm();
|
|
91
|
|
92 Representation returnRepr = doSearch(form);
|
|
93 if (returnRepr==null){
|
|
94 InputStream in = getClass().getResourceAsStream("/de/mpiwg/itgroup/escidoc/MPIWGServices/Restlet/staticPages/search.html");
|
|
95
|
|
96 return new InputRepresentation(in, MediaType.TEXT_HTML);
|
|
97 }
|
|
98 else
|
|
99 return returnRepr;
|
|
100
|
|
101 }
|
|
102
|
|
103
|
|
104 /**
|
|
105 * Bearbeite das Suchformular, gibt null zurueck, wenn das formular leer ist.
|
|
106 * @param searchForm
|
|
107 * @return
|
|
108 */
|
|
109 public Representation doSearch(Form searchForm){
|
|
110
|
|
111
|
|
112 String searchAll=searchForm.getValues("allMetadata");
|
|
113 String searchTitle=searchForm.getValues("title");
|
|
114 String searchAuthor=searchForm.getValues("author");
|
|
115
|
|
116 String queryString ="";
|
|
117 Boolean isSearch=false;
|
|
118 if (searchAll!=null && !searchAll.equals("")){
|
4
|
119 queryString="metadata="+searchAll;
|
0
|
120 isSearch=true;
|
|
121 } else if (searchTitle!=null && !searchTitle.equals("")){
|
4
|
122 queryString="any-title="+searchTitle;
|
|
123 queryString+=" or meta.archimedes.title="+searchTitle;
|
|
124 queryString+=" or meta.bib.title="+searchTitle;
|
0
|
125 isSearch=true;
|
|
126 } else if (searchAuthor!=null && !searchAuthor.equals("")){
|
4
|
127 queryString="dc-md.creator="+searchAuthor;
|
|
128 queryString+=" or meta.archimedes.author="+searchAuthor;
|
|
129 queryString+=" or meta.bib.author="+searchAuthor;
|
0
|
130 isSearch=true;
|
|
131 }
|
|
132 if (!isSearch) return null;
|
|
133
|
|
134 String query;
|
|
135 try {
|
4
|
136 query = "/srw/search/escidoc_all?maximumRecords=1000&query="+URLEncoder.encode("content-model.objid="+SEARCH_CMM+" and "+ queryString,"utf-8");
|
0
|
137 } catch (UnsupportedEncodingException e) {
|
|
138 logger.error("do Search, cannot encode:"+queryString);
|
|
139 setStatus(Status.SERVER_ERROR_INTERNAL);
|
|
140 return new StringRepresentation("<error>error</error>");
|
|
141 }
|
|
142
|
|
143 EScidocBasicHandler handler = new EScidocBasicHandler(eScidocServer, Integer.valueOf(eScidocPort),eScidocUser,eScidocUserPW);
|
|
144
|
|
145 HttpResponse result;
|
|
146 try {
|
|
147 result = handler.eScidocGet(query);
|
|
148 } catch (IOException e) {
|
|
149 logger.error("do Search, cannot encode:"+queryString);
|
|
150 setStatus(Status.SERVER_ERROR_INTERNAL);
|
|
151 return new StringRepresentation("<error>error</error>");
|
|
152 }
|
|
153
|
|
154 if (result.getStatusLine().getStatusCode()!=200){
|
|
155 setStatus(Status.SERVER_ERROR_INTERNAL);
|
|
156 logger.error("do Search, got Error:"+queryString);
|
|
157
|
|
158 HttpEntity ent = result.getEntity();
|
|
159 if (ent!=null){
|
|
160 InputStream in;
|
|
161 try {
|
|
162 in = ent.getContent();
|
|
163 } catch (Exception e) {
|
|
164 return new StringRepresentation("<error>error</error>");
|
|
165 }
|
|
166
|
|
167 return new InputRepresentation(in);
|
|
168 }
|
|
169 else {
|
|
170 return new StringRepresentation("<error>error</error>");
|
|
171 }
|
|
172 }
|
|
173
|
|
174 InputStream in;
|
|
175 try {
|
|
176 in = result.getEntity().getContent();
|
|
177
|
|
178 Document doc = new SAXBuilder().build(in);
|
|
179 InputStream xslt= getClass().getResourceAsStream("/de/mpiwg/itgroup/escidoc/MPIWGServices/Restlet/staticPages/searchResult.xsl");
|
|
180 //Document styleDoc = new SAXBuilder().build(xslt);
|
|
181
|
|
182 Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(xslt));
|
|
183 JDOMSource inxml = new JDOMSource(doc);
|
|
184 JDOMResult out = new JDOMResult();
|
|
185 transformer.transform(inxml, out);
|
|
186 //logger.debug(printXML(doc));
|
|
187
|
|
188 Document docresult = out.getDocument();
|
4
|
189
|
|
190
|
|
191
|
0
|
192 return new StringRepresentation(printXML(docresult),MediaType.TEXT_HTML);
|
|
193
|
4
|
194
|
|
195
|
|
196
|
0
|
197 } catch (Exception e) {
|
|
198 logger.trace(e);
|
|
199 return new StringRepresentation("<error>error</error>");
|
|
200 }
|
|
201
|
|
202
|
|
203
|
|
204
|
|
205 }
|
|
206
|
|
207 public String printXML(Document dom) throws IOException{
|
|
208 XMLOutputter out = new XMLOutputter();
|
|
209
|
|
210 String string = out.outputString(dom);
|
|
211 return string;
|
|
212
|
|
213 }
|
|
214 }
|