-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update servlet to use apache httpclient5
Supposedly more performant, this client will hopefully release JVM resources faster than the base java.net implementation. Resolves #40
- Loading branch information
1 parent
73584db
commit 598f39c
Showing
3 changed files
with
35 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,7 @@ | |
|
||
import java.io.IOException; | ||
import java.io.UnsupportedEncodingException; | ||
import java.net.URI; | ||
import java.net.URLEncoder; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.net.http.HttpResponse.BodyHandlers; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Enumeration; | ||
|
@@ -18,9 +13,17 @@ | |
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import org.apache.http.HttpHeaders; | ||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; | ||
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; | ||
import org.apache.hc.client5.http.impl.classic.HttpClients; | ||
import org.apache.hc.core5.http.ClassicHttpRequest; | ||
import org.apache.hc.core5.http.HttpEntity; | ||
import org.apache.hc.core5.http.HttpHeaders; | ||
import org.apache.hc.core5.http.io.entity.EntityUtils; | ||
import org.apache.hc.core5.http.io.support.ClassicRequestBuilder; | ||
import gov.nasa.pds.search.util.XssUtils; | ||
|
||
|
||
public class RegistryLegacyServlet extends HttpServlet { | ||
|
||
private static final long serialVersionUID = 6640363130974190821L; | ||
|
@@ -41,7 +44,7 @@ public class RegistryLegacyServlet extends HttpServlet { | |
new ArrayList<String>( | ||
List.of("q", "sort", "start", "rows", "fq", "fl", "wt", "json.wrf", "_", "facet.field", | ||
"facet", "facet.sort", "facet.mincount", "facet.method", "facet.excludeTerms", | ||
"facet.pivot", "facet.contains")); | ||
"facet.pivot", "facet.contains", "facet.limit")); | ||
private static List<String> SOLR_FACET_FIELDS = | ||
new ArrayList<String>(List.of("facet_agency", "facet_instrument", "facet_investigation", | ||
"facet_target", "facet_type", "facet_pds_model_version", "facet_primary_result_purpose", | ||
|
@@ -85,7 +88,7 @@ public void init(ServletConfig servletConfig) throws ServletException { | |
this.solrRequestHandler = DEFAULT_REQUEST_HANDLER; | ||
} | ||
|
||
LOG.info("Solr Server URL: " + this.solrServerUrl); | ||
LOG.fine("Solr Server URL: " + this.solrServerUrl); | ||
|
||
super.init(servletConfig); | ||
} | ||
|
@@ -106,21 +109,28 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) | |
String url = String.format("%s/%s/%s?%s", this.solrServerUrl, this.solrCollection, | ||
this.solrRequestHandler, queryString); | ||
|
||
HttpClient client = HttpClient.newHttpClient(); | ||
HttpRequest solrRequest = HttpRequest.newBuilder().uri(URI.create(url)).build(); | ||
|
||
|
||
HttpResponse<String> solrResponse = client.send(solrRequest, BodyHandlers.ofString()); | ||
|
||
response.setStatus(solrResponse.statusCode()); | ||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) { | ||
ClassicHttpRequest httpGet = ClassicRequestBuilder.get(url).build(); | ||
String resultContent = null; | ||
try (CloseableHttpResponse solrResponse = | ||
httpClient.execute(httpGet)) { | ||
response.setStatus(solrResponse.getCode()); | ||
setResponseHeader(request.getParameter("wt"), response); | ||
HttpEntity entity = solrResponse.getEntity(); | ||
|
||
setResponseHeader(request.getParameter("wt"), response); | ||
response.getOutputStream().write(solrResponse.body().getBytes()); | ||
// Get response information | ||
resultContent = EntityUtils.toString(entity); | ||
} | ||
response.getWriter().write(resultContent); | ||
} | ||
} catch (Exception e) { | ||
LOG.severe(e.getMessage()); | ||
e.printStackTrace(); | ||
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, | ||
"Internal system failure. Contact [email protected] for additional assistance."); | ||
} finally { | ||
|
||
} | ||
} | ||
|
||
|
@@ -173,7 +183,7 @@ private String getQueryString(HttpServletRequest request) throws UnsupportedEnco | |
return "q=*:*"; | ||
} | ||
|
||
LOG.info("Solr query: " + queryString); | ||
LOG.fine("Solr query: " + queryString); | ||
|
||
return queryString; | ||
} | ||
|
@@ -184,7 +194,6 @@ private String appendQueryParameters(String key, String[] parameterValues) | |
String queryString = ""; | ||
for (String v : Arrays.asList(parameterValues)) { | ||
value = XssUtils.clean(v); | ||
LOG.info("key: " + key + " value: " + value); | ||
queryString += | ||
String.format("%s=%s&", key, URLEncoder.encode(value, "UTF-8")); | ||
} | ||
|
@@ -203,3 +212,4 @@ private void setResponseHeader(String wt, HttpServletResponse response) { | |
response.addHeader(HttpHeaders.CONTENT_TYPE, contentType); | ||
} | ||
} | ||
|