Skip to content

Commit

Permalink
Improved performance of HEAD requests
Browse files Browse the repository at this point in the history
  • Loading branch information
hylkevds committed Nov 8, 2024
1 parent 4406612 commit 714b036
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,14 @@ private ServiceResponse handleGet(PersistenceManager pm, ServiceRequest request,
return errorResponse(response, version.getCannedResponse(Version.CannedResponseType.NOTHING_FOUND));
}
try {
if (request.isHead()) {
// Execute the query to check if it works. Doesn't actually fetch data.
query.setTop(0);
pm.get(path, query);
response.setMessage("");
response.setCode(200);
return response;
}
Object object = pm.get(path, query);
if (object == null) {
if (path.isValue() || path.isEntityProperty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class ServiceRequest {
private static final ThreadLocal<ServiceRequest> LOCAL_REQUEST = new ThreadLocal<>();

private String requestType;
private boolean head;
private String urlPath;
private String urlQuery;
private String contentString;
Expand Down Expand Up @@ -97,6 +98,21 @@ public ServiceRequest setRequestType(String requestType) {
return this;
}

/**
* Indicates the read request is a HEAD request and does not expect data in
* the response.
*
* @return true if it is a HEAD request.
*/
public boolean isHead() {
return head;
}

public ServiceRequest setHead(boolean head) {
this.head = head;
return this;
}

/**
* Get the content as a String.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import de.fraunhofer.iosb.ilt.frostserver.settings.CoreSettings;
import de.fraunhofer.iosb.ilt.frostserver.settings.annotation.DefaultValue;
import de.fraunhofer.iosb.ilt.frostserver.settings.annotation.DefaultValueBoolean;
import de.fraunhofer.iosb.ilt.frostserver.util.HttpMethod;
import de.fraunhofer.iosb.ilt.frostserver.util.StringHelper;
import de.fraunhofer.iosb.ilt.frostserver.util.user.PrincipalExtended;
import jakarta.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -137,8 +138,9 @@ public ServiceRequest serviceRequestFromHttpRequest(HttpServletRequest request)
return null;
}

final String method = request.getMethod();
String requestType = PluginManager.decodeRequestType(plugin, version, path, method, request.getContentType());
final HttpMethod method = HttpMethod.fromString(request.getMethod());
final String requestType = PluginManager.decodeRequestType(plugin, version, path, method, request.getContentType());
final boolean head = method == HttpMethod.HEAD;

final Map<String, List<String>> parameterMap = UrlHelper.splitQuery(request.getQueryString());
decodeAccepHeader(request, parameterMap);
Expand All @@ -149,6 +151,7 @@ public ServiceRequest serviceRequestFromHttpRequest(HttpServletRequest request)
.setQueryDefaults(queryDefaults)
.setVersion(version)
.setRequestType(requestType)
.setHead(head)
.setUrlPath(path)
.setUrlQuery(request.getQueryString() != null
? StringHelper.urlDecode(request.getQueryString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,8 @@ public static void testHeadRequest(SensorThingsService service, String url, fina
try {
headResponse = HTTPMethods.doHead(service, url);
if (headResponse.code != getStatusCode) {
LOGGER.error("Different status code for GET ({}) and HEAD ({})", getStatusCode, headResponse.code);
fail("Different status code for GET (" + getStatusCode + ") and HEAD (" + headResponse.code + ")");
LOGGER.error("Different status code for GET ({}) and HEAD ({}) on {}", getStatusCode, headResponse.code, url);
fail("Different status code for GET (" + getStatusCode + ") and HEAD (" + headResponse.code + ") on " + url);
}
} catch (IOException ex) {
LOGGER.error("Exception:", ex);
Expand Down

0 comments on commit 714b036

Please sign in to comment.