Mercurial > hg > eSciDocMPIWGServicesRestlet
comparison src/de/mpiwg/itgroup/escidoc/MPIWGServices/Restlet/services/search/SimpleSearch.java @ 0:1aac45997235
first ingest
author | dwinter |
---|---|
date | Wed, 01 Dec 2010 17:35:51 +0100 |
parents | |
children | ffeb8be0bded |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1aac45997235 |
---|---|
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 | |
46 private String eScidocServer; | |
47 private String eScidocPort; | |
48 private Logger logger= Logger.getRootLogger(); | |
49 private String eScidocUser; | |
50 private String eScidocUserPW; | |
51 | |
52 public SimpleSearch() throws IOException{ | |
53 InputStream is = getClass().getResourceAsStream("/de/mpiwg/itgroup/escidoc/MPIWGServices/Restlet/config/eScidocServer.config"); | |
54 Properties prop = new Properties(); | |
55 prop.load(is); | |
56 eScidocServer= (String)prop.get("eScidocServer"); | |
57 eScidocPort=(String)prop.get("eScidocPort"); | |
58 eScidocUser=(String)prop.get("eScidocUser"); | |
59 eScidocUserPW=(String)prop.get("eScidocUserPW"); | |
60 } | |
61 @Options | |
62 public void doOptions(Representation entity) { | |
63 Form responseHeaders = (Form) getResponse().getAttributes().get("org.restlet.http.headers"); | |
64 if (responseHeaders == null) { | |
65 responseHeaders = new Form(); | |
66 getResponse().getAttributes().put("org.restlet.http.headers", responseHeaders); | |
67 } | |
68 responseHeaders.add("Access-Control-Allow-Origin", "*"); | |
69 responseHeaders.add("Access-Control-Allow-Methods", "POST,OPTIONS,GET"); | |
70 responseHeaders.add("Access-Control-Allow-Headers", "Content-Type"); | |
71 responseHeaders.add("Access-Control-Allow-Credentials", "false"); | |
72 responseHeaders.add("Access-Control-Max-Age", "60"); | |
73 } | |
74 | |
75 | |
76 @Get("html") | |
77 public Representation getHtml(){ | |
78 | |
79 // response header fuer cross-site.scripting | |
80 Form responseHeaders = (Form) getResponse().getAttributes().get("org.restlet.http.headers"); | |
81 if (responseHeaders == null) { | |
82 responseHeaders = new Form(); | |
83 getResponse().getAttributes().put("org.restlet.http.headers", responseHeaders); | |
84 } | |
85 responseHeaders.add("Access-Control-Allow-Origin", "*"); | |
86 | |
87 Form form = getRequest().getResourceRef().getQueryAsForm(); | |
88 | |
89 Representation returnRepr = doSearch(form); | |
90 if (returnRepr==null){ | |
91 InputStream in = getClass().getResourceAsStream("/de/mpiwg/itgroup/escidoc/MPIWGServices/Restlet/staticPages/search.html"); | |
92 | |
93 return new InputRepresentation(in, MediaType.TEXT_HTML); | |
94 } | |
95 else | |
96 return returnRepr; | |
97 | |
98 } | |
99 | |
100 | |
101 /** | |
102 * Bearbeite das Suchformular, gibt null zurueck, wenn das formular leer ist. | |
103 * @param searchForm | |
104 * @return | |
105 */ | |
106 public Representation doSearch(Form searchForm){ | |
107 | |
108 | |
109 String searchAll=searchForm.getValues("allMetadata"); | |
110 String searchTitle=searchForm.getValues("title"); | |
111 String searchAuthor=searchForm.getValues("author"); | |
112 | |
113 String queryString =""; | |
114 Boolean isSearch=false; | |
115 if (searchAll!=null && !searchAll.equals("")){ | |
116 queryString="escidoc.metadata="+searchAll; | |
117 isSearch=true; | |
118 } else if (searchTitle!=null && !searchTitle.equals("")){ | |
119 queryString="escidoc.any-title="+searchTitle; | |
120 isSearch=true; | |
121 } else if (searchAuthor!=null && !searchAuthor.equals("")){ | |
122 queryString="escidoc.creator="+searchAuthor; | |
123 isSearch=true; | |
124 } | |
125 if (!isSearch) return null; | |
126 | |
127 String query; | |
128 try { | |
129 query = "/srw/search/escidoc_all?maximumRecords=3&query="+URLEncoder.encode(queryString,"utf-8"); | |
130 } catch (UnsupportedEncodingException e) { | |
131 logger.error("do Search, cannot encode:"+queryString); | |
132 setStatus(Status.SERVER_ERROR_INTERNAL); | |
133 return new StringRepresentation("<error>error</error>"); | |
134 } | |
135 | |
136 EScidocBasicHandler handler = new EScidocBasicHandler(eScidocServer, Integer.valueOf(eScidocPort),eScidocUser,eScidocUserPW); | |
137 | |
138 HttpResponse result; | |
139 try { | |
140 result = handler.eScidocGet(query); | |
141 } catch (IOException e) { | |
142 logger.error("do Search, cannot encode:"+queryString); | |
143 setStatus(Status.SERVER_ERROR_INTERNAL); | |
144 return new StringRepresentation("<error>error</error>"); | |
145 } | |
146 | |
147 if (result.getStatusLine().getStatusCode()!=200){ | |
148 setStatus(Status.SERVER_ERROR_INTERNAL); | |
149 logger.error("do Search, got Error:"+queryString); | |
150 | |
151 HttpEntity ent = result.getEntity(); | |
152 if (ent!=null){ | |
153 InputStream in; | |
154 try { | |
155 in = ent.getContent(); | |
156 } catch (Exception e) { | |
157 return new StringRepresentation("<error>error</error>"); | |
158 } | |
159 | |
160 return new InputRepresentation(in); | |
161 } | |
162 else { | |
163 return new StringRepresentation("<error>error</error>"); | |
164 } | |
165 } | |
166 | |
167 InputStream in; | |
168 try { | |
169 in = result.getEntity().getContent(); | |
170 | |
171 Document doc = new SAXBuilder().build(in); | |
172 InputStream xslt= getClass().getResourceAsStream("/de/mpiwg/itgroup/escidoc/MPIWGServices/Restlet/staticPages/searchResult.xsl"); | |
173 //Document styleDoc = new SAXBuilder().build(xslt); | |
174 | |
175 Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(xslt)); | |
176 JDOMSource inxml = new JDOMSource(doc); | |
177 JDOMResult out = new JDOMResult(); | |
178 transformer.transform(inxml, out); | |
179 //logger.debug(printXML(doc)); | |
180 | |
181 Document docresult = out.getDocument(); | |
182 return new StringRepresentation(printXML(docresult),MediaType.TEXT_HTML); | |
183 | |
184 } catch (Exception e) { | |
185 logger.trace(e); | |
186 return new StringRepresentation("<error>error</error>"); | |
187 } | |
188 | |
189 | |
190 | |
191 | |
192 } | |
193 | |
194 public String printXML(Document dom) throws IOException{ | |
195 XMLOutputter out = new XMLOutputter(); | |
196 | |
197 String string = out.outputString(dom); | |
198 return string; | |
199 | |
200 } | |
201 } |