Revision 54338
Added by Claudio Atzori over 5 years ago
modules/dnet-modular-oai-explorer-ui/trunk/src/main/java/eu/dnetlib/functionality/modular/ui/oai/objects/OaiRequest.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.functionality.modular.ui.oai.objects; |
2 | 2 |
|
3 |
import com.google.common.collect.Lists; |
|
4 |
import org.apache.http.message.BasicNameValuePair; |
|
5 |
|
|
3 | 6 |
import java.util.List; |
4 | 7 |
|
5 |
import org.apache.commons.httpclient.NameValuePair; |
|
6 |
|
|
7 |
import com.google.common.collect.Lists; |
|
8 |
|
|
9 | 8 |
public class OaiRequest { |
10 | 9 |
|
11 | 10 |
private String baseUrl; |
... | ... | |
63 | 62 |
this.token = token; |
64 | 63 |
} |
65 | 64 |
|
66 |
public NameValuePair[] toQueryParams() {
|
|
67 |
final List<NameValuePair> params = Lists.newArrayList(); |
|
65 |
public List<BasicNameValuePair> toQueryParams() {
|
|
66 |
final List<BasicNameValuePair> params = Lists.newArrayList();
|
|
68 | 67 |
|
69 | 68 |
if (verb != null && !verb.isEmpty()) { |
70 |
params.add(new NameValuePair("verb", verb)); |
|
69 |
params.add(new BasicNameValuePair("verb", verb));
|
|
71 | 70 |
} |
72 | 71 |
if (mdf != null && !mdf.isEmpty()) { |
73 |
params.add(new NameValuePair("metadataPrefix", mdf)); |
|
72 |
params.add(new BasicNameValuePair("metadataPrefix", mdf));
|
|
74 | 73 |
} |
75 | 74 |
if (set != null && !set.isEmpty()) { |
76 |
params.add(new NameValuePair("set", set)); |
|
75 |
params.add(new BasicNameValuePair("set", set));
|
|
77 | 76 |
} |
78 | 77 |
if (id != null && !id.isEmpty()) { |
79 |
params.add(new NameValuePair("identifier", id)); |
|
78 |
params.add(new BasicNameValuePair("identifier", id));
|
|
80 | 79 |
} |
81 | 80 |
if (token != null && !token.isEmpty()) { |
82 |
params.add(new NameValuePair("resumptionToken", token));
|
|
81 |
params.add(new BasicNameValuePair("resumptionToken", token));
|
|
83 | 82 |
} |
84 | 83 |
|
85 |
return params.toArray(new NameValuePair[params.size()]);
|
|
84 |
return params; |
|
86 | 85 |
} |
87 | 86 |
} |
modules/dnet-modular-oai-explorer-ui/trunk/src/main/java/eu/dnetlib/functionality/modular/ui/oai/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 |
} |
modules/dnet-modular-oai-explorer-ui/trunk/dnet-modular-oai-explorer-ui.iml | ||
---|---|---|
12 | 12 |
</content> |
13 | 13 |
<orderEntry type="inheritedJdk" /> |
14 | 14 |
<orderEntry type="sourceFolder" forTests="false" /> |
15 |
<orderEntry type="module" module-name="dnet-modular-ui" />
|
|
15 |
<orderEntry type="library" name="Maven: eu.dnetlib:dnet-modular-ui:3.0.14" level="project" />
|
|
16 | 16 |
<orderEntry type="library" name="Maven: org.springframework:spring-web:4.2.5.RELEASE" level="project" /> |
17 | 17 |
<orderEntry type="library" name="Maven: org.springframework:spring-aop:4.2.5.RELEASE" level="project" /> |
18 | 18 |
<orderEntry type="library" name="Maven: aopalliance:aopalliance:1.0" level="project" /> |
... | ... | |
42 | 42 |
<orderEntry type="library" name="Maven: org.apache.cxf:cxf-rt-ws-policy:3.1.5" level="project" /> |
43 | 43 |
<orderEntry type="library" name="Maven: org.apache.neethi:neethi:3.0.3" level="project" /> |
44 | 44 |
<orderEntry type="library" name="Maven: commons-lang:commons-lang:2.6" level="project" /> |
45 |
<orderEntry type="module" module-name="cnr-service-common" />
|
|
45 |
<orderEntry type="library" name="Maven: eu.dnetlib:cnr-service-common:2.1.7" level="project" />
|
|
46 | 46 |
<orderEntry type="library" name="Maven: org.antlr:stringtemplate:3.2" level="project" /> |
47 | 47 |
<orderEntry type="library" name="Maven: org.antlr:antlr:2.7.7" level="project" /> |
48 | 48 |
<orderEntry type="library" name="Maven: org.quartz-scheduler:quartz:2.2.2" level="project" /> |
... | ... | |
78 | 78 |
<orderEntry type="library" name="Maven: org.apache.maven:maven-model:3.2.3" level="project" /> |
79 | 79 |
<orderEntry type="library" name="Maven: org.codehaus.plexus:plexus-utils:3.0.17" level="project" /> |
80 | 80 |
<orderEntry type="library" scope="PROVIDED" name="Maven: javax.servlet:javax.servlet-api:3.1.0" level="project" /> |
81 |
<orderEntry type="library" name="Maven: commons-httpclient:commons-httpclient:3.1" level="project" /> |
|
82 |
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.0.4" level="project" /> |
|
83 |
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.2" level="project" /> |
|
81 |
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.5.3" level="project" /> |
|
82 |
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.4.6" level="project" /> |
|
83 |
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" /> |
|
84 |
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.9" level="project" /> |
|
84 | 85 |
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.9" level="project" /> |
85 | 86 |
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" /> |
86 | 87 |
<orderEntry type="library" name="Maven: log4j:log4j:1.2.17" level="project" /> |
modules/dnet-modular-oai-explorer-ui/trunk/pom.xml | ||
---|---|---|
27 | 27 |
<scope>provided</scope> |
28 | 28 |
</dependency> |
29 | 29 |
<dependency> |
30 |
<groupId>commons-httpclient</groupId>
|
|
31 |
<artifactId>commons-httpclient</artifactId>
|
|
32 |
<version>3.1</version>
|
|
30 |
<groupId>org.apache.httpcomponents</groupId>
|
|
31 |
<artifactId>httpclient</artifactId> |
|
32 |
<version>4.5.3</version>
|
|
33 | 33 |
</dependency> |
34 | 34 |
<dependency> |
35 | 35 |
<groupId>junit</groupId> |
Also available in: Unified diff
switched implementation to org.apache.httpcomponents:httpclient:4.5.3