Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.phoebus.channelfinder.common;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

/**
* Utility class for merging search parameters from URL query string and JSON request body.
*
* <p>Merging strategy:
*
* <ul>
* <li>For regular search parameters (e.g., ~name, property names, ~tag): values from both URL
* and body are added as separate values under the same key in the MultiValueMap.
* <li>For control parameters (~size, ~from, ~search_after, ~track_total_hits): URL values take
* precedence over body values (body values ignored if URL value exists).
* </ul>
*/
public final class SearchParameterMergerUtil {

private static final String CONTROL_SIZE = "~size";
private static final String CONTROL_FROM = "~from";
private static final String CONTROL_SEARCH_AFTER = "~search_after";
private static final String CONTROL_TRACK_TOTAL_HITS = "~track_total_hits";

private SearchParameterMergerUtil() {
// Utility class - private constructor
}

private static boolean isControlParameter(String key) {
return CONTROL_SIZE.equals(key)
|| CONTROL_FROM.equals(key)
|| CONTROL_SEARCH_AFTER.equals(key)
|| CONTROL_TRACK_TOTAL_HITS.equals(key);
}

/**
* Merges URL request parameters with body parameters.
*
* <p>URL parameters take precedence for control keys. For regular search parameters, body values
* are added as additional values for the same key in the MultiValueMap.
*
* @param urlParams URL query parameters (may be null or empty)
* @param bodyParams JSON request body as a map (may be null or empty)
* @return merged parameters as a MultiValueMap
*/
public static MultiValueMap<String, String> mergeParameters(
MultiValueMap<String, String> urlParams, Map<String, String> bodyParams) {
MultiValueMap<String, String> merged = new LinkedMultiValueMap<>();

// Add URL parameters first
if (urlParams != null && !urlParams.isEmpty()) {
for (Map.Entry<String, List<String>> entry : urlParams.entrySet()) {
merged.put(entry.getKey(), new LinkedList<>(entry.getValue()));
}
}

// Merge body parameters if present
if (bodyParams != null && !bodyParams.isEmpty()) {
mergeBodyParams(merged, bodyParams);
}

return merged;
}

private static void mergeBodyParams(
MultiValueMap<String, String> merged, Map<String, String> bodyParams) {
for (Map.Entry<String, String> entry : bodyParams.entrySet()) {
String key = entry.getKey();
String bodyValue = entry.getValue();

if (bodyValue == null || bodyValue.trim().isEmpty()) {
continue; // Skip empty body values
}

if (isControlParameter(key)) {
// For control parameters, URL takes precedence
if (!merged.containsKey(key)) {
merged.set(key, bodyValue);
}
} else {
// For regular search parameters, add the body value to the same key.
merged.add(key, bodyValue);
}
}
}
}
53 changes: 47 additions & 6 deletions src/main/java/org/phoebus/channelfinder/web/v0/api/IChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import java.util.List;
import java.util.Map;
import org.phoebus.channelfinder.entity.Channel;
import org.phoebus.channelfinder.entity.SearchResult;
import org.springframework.util.MultiValueMap;
Expand All @@ -27,7 +28,10 @@ public interface IChannel {
@Operation(
summary = "Query channels",
description =
"Query a collection of Channel instances based on tags, property values, and channel names.",
"Query a collection of Channel instances based on tags, property values, and channel names. "
+ "Search parameters can be provided via URL query string or JSON request body, or both. "
+ "URL parameters take precedence for control parameters (~size, ~from, ~search_after, ~track_total_hits). "
+ "Regular search parameters from URL and body are combined as separate values in the query.",
operationId = "queryChannels",
tags = {"Channel"})
@ApiResponses(
Expand All @@ -49,12 +53,25 @@ public interface IChannel {
@GetMapping
List<Channel> query(
@Parameter(description = SEARCH_PARAM_DESCRIPTION) @RequestParam
MultiValueMap<String, String> allRequestParams);
MultiValueMap<String, String> allRequestParams,
@Parameter(
description =
"Optional JSON request body containing search parameters. Used to bypass URL length limitations.")
@RequestBody(required = false)
Map<String, String> searchParamsBody);

// Backward-compatible overload when no request body is provided.
default List<Channel> query(MultiValueMap<String, String> allRequestParams) {
return query(allRequestParams, null);
}

@Operation(
summary = "Combined query for channels",
description =
"Query for a collection of Channel instances and get a count and the first 10k hits.",
"Query for a collection of Channel instances and get a count and the first 10k hits. "
+ "Search parameters can be provided via URL query string or JSON request body, or both. "
+ "URL parameters take precedence for control parameters (~size, ~from, ~search_after, ~track_total_hits). "
+ "Regular search parameters from URL and body are combined as separate values in the query.",
operationId = "combinedQueryChannels",
tags = {"Channel"})
@ApiResponses(
Expand All @@ -77,11 +94,25 @@ List<Channel> query(
@GetMapping("/combined")
SearchResult combinedQuery(
@Parameter(description = SEARCH_PARAM_DESCRIPTION) @RequestParam
MultiValueMap<String, String> allRequestParams);
MultiValueMap<String, String> allRequestParams,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can just do two methods rather than mutually exclusive inputs.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well this is not part of the current change so I would address this as a separate issue

@Parameter(
description =
"Optional JSON request body containing search parameters. Used to bypass URL length limitations.")
@RequestBody(required = false)
Map<String, String> searchParamsBody);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like a generic Map<String, String> better to use an actual record

record QueryOptions(String name, Map<String, String> properties, List tags) {}

You can see it makes the swagger much much nicer.


// Backward-compatible overload when no request body is provided.
default SearchResult combinedQuery(MultiValueMap<String, String> allRequestParams) {
return combinedQuery(allRequestParams, null);
}

@Operation(
summary = "Count channels matching query",
description = "Get the number of channels matching the given query parameters.",
description =
"Get the number of channels matching the given query parameters. "
+ "Search parameters can be provided via URL query string or JSON request body, or both. "
+ "URL parameters take precedence for control parameters (~size, ~from, ~search_after, ~track_total_hits). "
+ "Regular search parameters from URL and body are combined as separate values in the query.",
operationId = "countChannels",
tags = {"Channel"})
@ApiResponses(
Expand All @@ -98,7 +129,17 @@ SearchResult combinedQuery(
@GetMapping("/count")
long queryCount(
@Parameter(description = SEARCH_PARAM_DESCRIPTION) @RequestParam
MultiValueMap<String, String> allRequestParams);
MultiValueMap<String, String> allRequestParams,
@Parameter(
description =
"Optional JSON request body containing search parameters. Used to bypass URL length limitations.")
@RequestBody(required = false)
Map<String, String> searchParamsBody);

// Backward-compatible overload when no request body is provided.
default long queryCount(MultiValueMap<String, String> allRequestParams) {
return queryCount(allRequestParams, null);
}

@Operation(
summary = "Get channel by name",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,25 @@
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import java.util.Map;
import org.phoebus.channelfinder.common.CFResourceDescriptors;
import org.phoebus.channelfinder.entity.Scroll;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.server.ResponseStatusException;

public interface IChannelScroll {

@Operation(
summary = "Scroll query for channels",
description = "Retrieve a collection of Channel instances based on multi-parameter search.",
description =
"Retrieve a collection of Channel instances based on multi-parameter search. "
+ "Search parameters can be provided via URL query string or JSON request body, or both. "
+ "URL parameters take precedence for control parameters (~size, ~from, ~search_after, ~track_total_hits). "
+ "Regular search parameters from URL and body are combined as separate values in the query.",
operationId = "scrollQueryChannels",
tags = {"ChannelScroll"})
@ApiResponses(
Expand All @@ -35,12 +41,25 @@ public interface IChannelScroll {
@GetMapping
Scroll query(
@Parameter(description = CFResourceDescriptors.SEARCH_PARAM_DESCRIPTION) @RequestParam
MultiValueMap<String, String> allRequestParams);
MultiValueMap<String, String> allRequestParams,
@Parameter(
description =
"Optional JSON request body containing search parameters. Used to bypass URL length limitations.")
@RequestBody(required = false)
Map<String, String> searchParamsBody);

// Backward-compatible overload when no request body is provided.
default Scroll query(MultiValueMap<String, String> allRequestParams) {
return query(allRequestParams, null);
}

@Operation(
summary = "Scroll query by scrollId",
description =
"Retrieve a collection of Channel instances using a scrollId and search parameters.",
"Retrieve a collection of Channel instances using a scrollId and search parameters. "
+ "Search parameters can be provided via URL query string or JSON request body, or both. "
+ "URL parameters take precedence for control parameters (~size, ~from, ~search_after, ~track_total_hits). "
+ "Regular search parameters from URL and body are combined as separate values in the query.",
operationId = "scrollQueryById",
tags = {"ChannelScroll"})
@ApiResponses(
Expand All @@ -59,5 +78,15 @@ Scroll query(
@Parameter(description = "Scroll ID from previous query") @PathVariable("scrollId")
String scrollId,
@Parameter(description = CFResourceDescriptors.SEARCH_PARAM_DESCRIPTION) @RequestParam
MultiValueMap<String, String> searchParameters);
MultiValueMap<String, String> searchParameters,
@Parameter(
description =
"Optional JSON request body containing search parameters. Used to bypass URL length limitations.")
@RequestBody(required = false)
Map<String, String> searchParamsBody);

// Backward-compatible overload when no request body is provided.
default Scroll query(String scrollId, MultiValueMap<String, String> searchParameters) {
return query(scrollId, searchParameters, null);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.phoebus.channelfinder.web.v0.controller;

import java.util.List;
import java.util.Map;
import org.phoebus.channelfinder.common.SearchParameterMergerUtil;
import org.phoebus.channelfinder.entity.Channel;
import org.phoebus.channelfinder.entity.SearchResult;
import org.phoebus.channelfinder.service.ChannelService;
Expand All @@ -22,18 +24,27 @@ public ChannelController(ChannelService channelService) {
}

@Override
public List<Channel> query(MultiValueMap<String, String> allRequestParams) {
return channelService.query(allRequestParams);
public List<Channel> query(
MultiValueMap<String, String> allRequestParams, Map<String, String> searchParamsBody) {
MultiValueMap<String, String> mergedParams =
SearchParameterMergerUtil.mergeParameters(allRequestParams, searchParamsBody);
return channelService.query(mergedParams);
}

@Override
public SearchResult combinedQuery(MultiValueMap<String, String> allRequestParams) {
return channelService.combinedQuery(allRequestParams);
public SearchResult combinedQuery(
MultiValueMap<String, String> allRequestParams, Map<String, String> searchParamsBody) {
MultiValueMap<String, String> mergedParams =
SearchParameterMergerUtil.mergeParameters(allRequestParams, searchParamsBody);
return channelService.combinedQuery(mergedParams);
}

@Override
public long queryCount(MultiValueMap<String, String> allRequestParams) {
return channelService.queryCount(allRequestParams);
public long queryCount(
MultiValueMap<String, String> allRequestParams, Map<String, String> searchParamsBody) {
MultiValueMap<String, String> mergedParams =
SearchParameterMergerUtil.mergeParameters(allRequestParams, searchParamsBody);
return channelService.queryCount(mergedParams);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.phoebus.channelfinder.web.v0.controller;

import java.util.Map;
import org.phoebus.channelfinder.common.SearchParameterMergerUtil;
import org.phoebus.channelfinder.entity.Scroll;
import org.phoebus.channelfinder.service.ChannelScrollService;
import org.phoebus.channelfinder.web.v0.api.IChannelScroll;
Expand All @@ -20,12 +22,20 @@ public ChannelScrollController(ChannelScrollService channelScrollService) {
}

@Override
public Scroll query(MultiValueMap<String, String> allRequestParams) {
return channelScrollService.search(null, allRequestParams);
public Scroll query(
MultiValueMap<String, String> allRequestParams, Map<String, String> searchParamsBody) {
MultiValueMap<String, String> mergedParams =
SearchParameterMergerUtil.mergeParameters(allRequestParams, searchParamsBody);
return channelScrollService.search(null, mergedParams);
}

@Override
public Scroll query(String scrollId, MultiValueMap<String, String> searchParameters) {
return channelScrollService.search(scrollId, searchParameters);
public Scroll query(
String scrollId,
MultiValueMap<String, String> searchParameters,
Map<String, String> searchParamsBody) {
MultiValueMap<String, String> mergedParams =
SearchParameterMergerUtil.mergeParameters(searchParameters, searchParamsBody);
return channelScrollService.search(scrollId, mergedParams);
}
}
Loading
Loading