Skip to content

Commit

Permalink
Update servlet to use apache httpclient5
Browse files Browse the repository at this point in the history
Supposedly more performant, this client will hopefully release JVM resources faster than the base java.net implementation.

Resolves #40
  • Loading branch information
jordanpadams committed Oct 9, 2024
1 parent 73584db commit 598f39c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
"filename": "src/main/java/gov/nasa/pds/search/servlet/RegistryLegacyServlet.java",
"hashed_secret": "4fb813c304003b3813b35a85f05b7cb0c3994cc1",
"is_verified": false,
"line_number": 123,
"line_number": 131,
"is_secret": false
}
],
Expand Down Expand Up @@ -216,5 +216,5 @@
}
]
},
"generated_at": "2024-10-07T13:30:59Z"
"generated_at": "2024-10-09T21:36:12Z"
}
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@
<version>1.3.4</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.4</version>
</dependency>
</dependencies>

<!-- Project Information and Reports inherited from parent. -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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",
Expand Down Expand Up @@ -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);
}
Expand All @@ -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 {

}
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -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"));
}
Expand All @@ -203,3 +212,4 @@ private void setResponseHeader(String wt, HttpServletResponse response) {
response.addHeader(HttpHeaders.CONTENT_TYPE, contentType);
}
}

0 comments on commit 598f39c

Please sign in to comment.