Revision 54338
Added by Claudio Atzori over 5 years ago
OaiExplorerInternalController.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.functionality.modular.ui.oai; |
2 | 2 |
|
3 |
import java.io.InputStream; |
|
4 |
import java.io.StringWriter; |
|
5 |
|
|
6 |
import javax.xml.transform.Transformer; |
|
7 |
import javax.xml.transform.TransformerFactory; |
|
8 |
import javax.xml.transform.stream.StreamResult; |
|
9 |
import javax.xml.transform.stream.StreamSource; |
|
10 |
|
|
11 |
import org.apache.commons.httpclient.HttpClient; |
|
12 |
import org.apache.commons.httpclient.HttpStatus; |
|
13 |
import org.apache.commons.httpclient.methods.GetMethod; |
|
3 |
import com.google.common.base.Joiner; |
|
4 |
import eu.dnetlib.functionality.modular.ui.oai.objects.OaiRequest; |
|
5 |
import eu.dnetlib.functionality.modular.ui.oai.objects.ResponseDetails; |
|
6 |
import eu.dnetlib.miscutils.datetime.DateUtils; |
|
14 | 7 |
import org.apache.commons.lang.math.NumberUtils; |
15 | 8 |
import org.apache.commons.logging.Log; |
16 | 9 |
import org.apache.commons.logging.LogFactory; |
10 |
import org.apache.http.HttpStatus; |
|
11 |
import org.apache.http.client.methods.CloseableHttpResponse; |
|
12 |
import org.apache.http.client.methods.HttpGet; |
|
13 |
import org.apache.http.impl.client.HttpClients; |
|
17 | 14 |
import org.dom4j.Document; |
18 | 15 |
import org.dom4j.Node; |
19 | 16 |
import org.dom4j.io.SAXReader; |
... | ... | |
24 | 21 |
import org.springframework.web.bind.annotation.RequestMapping; |
25 | 22 |
import org.springframework.web.bind.annotation.ResponseBody; |
26 | 23 |
|
27 |
import eu.dnetlib.functionality.modular.ui.oai.objects.OaiRequest; |
|
28 |
import eu.dnetlib.functionality.modular.ui.oai.objects.ResponseDetails; |
|
29 |
import eu.dnetlib.miscutils.datetime.DateUtils; |
|
24 |
import javax.xml.transform.Transformer; |
|
25 |
import javax.xml.transform.TransformerFactory; |
|
26 |
import javax.xml.transform.stream.StreamResult; |
|
27 |
import javax.xml.transform.stream.StreamSource; |
|
28 |
import java.io.ByteArrayOutputStream; |
|
29 |
import java.io.OutputStream; |
|
30 |
import java.io.StringWriter; |
|
30 | 31 |
|
31 | 32 |
@Controller |
32 | 33 |
public class OaiExplorerInternalController { |
... | ... | |
37 | 38 |
|
38 | 39 |
@RequestMapping("/ui/oai_verb") |
39 | 40 |
public @ResponseBody String oaiVerb(@RequestBody(required=true) OaiRequest req) throws Exception { |
40 |
return applyXslt(callOaiVerb(req));
|
|
41 |
}
|
|
41 |
return callOaiVerb(req);
|
|
42 |
} |
|
42 | 43 |
|
43 | 44 |
@RequestMapping("/ui/test_oai_verb") |
44 | 45 |
public @ResponseBody ResponseDetails testOaiVerb(@RequestBody final OaiRequest req) throws Exception { |
... | ... | |
101 | 102 |
return response; |
102 | 103 |
} |
103 | 104 |
|
104 |
private InputStream callOaiVerb(final OaiRequest req) throws Exception { |
|
105 |
final GetMethod method = new GetMethod(req.getBaseUrl()); |
|
106 |
method.setRequestHeader("Content-type", "text/xml; charset=UTF-8"); |
|
107 |
method.setQueryString(req.toQueryParams()); |
|
108 |
|
|
109 |
int responseCode = (new HttpClient()).executeMethod(method); |
|
105 |
private String callOaiVerb(final OaiRequest req) throws Exception { |
|
110 | 106 |
|
111 |
if (HttpStatus.SC_OK != responseCode) { |
|
112 |
log.error("Error downloading from baseUrl: " + req.getBaseUrl()); |
|
113 |
throw new RuntimeException("Error: " + responseCode); |
|
114 |
} |
|
107 |
final HttpGet method = new HttpGet(!req.toQueryParams().isEmpty() ? req.getBaseUrl() + "?" + Joiner.on("&").join(req.toQueryParams()) : req.getBaseUrl()); |
|
115 | 108 |
|
116 |
return method.getResponseBodyAsStream();
|
|
109 |
method.addHeader("Content-type", "text/xml; charset=UTF-8");
|
|
117 | 110 |
|
111 |
try(CloseableHttpResponse response = HttpClients.createDefault().execute(method)) { |
|
112 |
|
|
113 |
int responseCode = response.getStatusLine().getStatusCode(); |
|
114 |
if (HttpStatus.SC_OK != responseCode) { |
|
115 |
log.error("Error downloading from baseUrl: " + req.getBaseUrl()); |
|
116 |
throw new RuntimeException("Error: " + responseCode); |
|
117 |
} |
|
118 |
|
|
119 |
OutputStream o = new ByteArrayOutputStream(); |
|
120 |
response.getEntity().writeTo(o); |
|
121 |
|
|
122 |
final TransformerFactory tfactory = TransformerFactory.newInstance(); |
|
123 |
|
|
124 |
final Transformer transformer = tfactory.newTransformer(new StreamSource(oaiXslt.getInputStream())); |
|
125 |
final StringWriter output = new StringWriter(); |
|
126 |
transformer.transform(new StreamSource(response.getEntity().getContent()), new StreamResult(output)); |
|
127 |
|
|
128 |
return output.toString(); |
|
129 |
} |
|
118 | 130 |
} |
119 | 131 |
|
120 | 132 |
private Document callOaiVerb(final OaiRequest req, final ResponseDetails details) { |
121 |
final GetMethod method = new GetMethod(req.getBaseUrl()); |
|
122 |
method.setRequestHeader("Content-type", "text/xml; charset=UTF-8");
|
|
123 |
method.setQueryString(req.toQueryParams());
|
|
133 |
|
|
134 |
final HttpGet method = new HttpGet(!req.toQueryParams().isEmpty() ? req.getBaseUrl() + "?" + Joiner.on("&").join(req.toQueryParams()) : req.getBaseUrl());
|
|
135 |
method.addHeader("Content-type", "text/xml; charset=UTF-8");
|
|
124 | 136 |
|
125 | 137 |
Document doc = null; |
126 | 138 |
|
127 | 139 |
final long start = DateUtils.now(); |
128 | 140 |
|
129 |
|
|
130 |
try { |
|
131 |
int responseCode = (new HttpClient()).executeMethod(method);
|
|
141 |
try(CloseableHttpResponse response = HttpClients.createDefault().execute(method)) { |
|
142 |
|
|
143 |
int responseCode = response.getStatusLine().getStatusCode();
|
|
132 | 144 |
|
133 | 145 |
details.setHttpCode(responseCode); |
134 | 146 |
details.setValid(HttpStatus.SC_OK == responseCode); |
135 | 147 |
|
136 | 148 |
if (HttpStatus.SC_OK == responseCode) { |
137 | 149 |
try { |
138 |
doc = new SAXReader().read(method.getResponseBodyAsStream());
|
|
150 |
doc = new SAXReader().read(response.getEntity().getContent());
|
|
139 | 151 |
} catch (Exception e) { |
140 | 152 |
details.setValid(false); |
141 | 153 |
details.setError(e.getMessage()); |
... | ... | |
152 | 164 |
return doc; |
153 | 165 |
} |
154 | 166 |
|
155 |
|
|
156 |
private String applyXslt(final InputStream input) throws Exception { |
|
157 |
final TransformerFactory tfactory = TransformerFactory.newInstance(); |
|
158 |
|
|
159 |
final Transformer transformer = tfactory.newTransformer(new StreamSource(oaiXslt.getInputStream())); |
|
160 |
final StringWriter output = new StringWriter(); |
|
161 |
transformer.transform(new StreamSource(input), new StreamResult(output)); |
|
162 |
|
|
163 |
return output.toString(); |
|
164 |
} |
|
165 | 167 |
} |
Also available in: Unified diff
switched implementation to org.apache.httpcomponents:httpclient:4.5.3