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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public <S extends Channel> Iterable<S> saveAll(Iterable<S> channels) {
}
BulkResponse result;
try {
result = client.bulk(br.refresh(Refresh.True).build());
result = client.bulk(br.refresh(Refresh.WaitFor).build());
// Log errors, if any
if (result.errors()) {
logger.log(Level.SEVERE, TextUtil.BULK_HAD_ERRORS);
Expand Down Expand Up @@ -806,8 +806,39 @@ public Scroll scroll(String scrollId, MultiValueMap<String, String> searchParame

@Override
public void deleteAllById(Iterable<? extends String> ids) {
// TODO Auto-generated method stub
List<String> idList =
StreamSupport.stream(ids.spliterator(), false)
.filter(id -> id != null && !id.isBlank())
.map(String::valueOf)
.distinct()
.toList();

for (int i = 0; i < idList.size(); i += chunkSize) {
List<String> chunk = idList.stream().skip(i).limit(chunkSize).toList();
BulkRequest.Builder br = new BulkRequest.Builder();
for (String id : chunk) {
br.operations(op -> op.delete(del -> del.index(esService.getES_CHANNEL_INDEX()).id(id)));
}
br.refresh(Refresh.True);

try {
BulkResponse result = client.bulk(br.build());
if (result.errors()) {
String message = MessageFormat.format(TextUtil.FAILED_TO_DELETE_CHANNEL, chunk);
logger.log(Level.SEVERE, TextUtil.BULK_HAD_ERRORS);
for (BulkResponseItem item : result.items()) {
if (item.error() != null) {
logger.log(Level.SEVERE, () -> item.error().reason());
}
}
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, message, null);
}
} catch (IOException e) {
String message = MessageFormat.format(TextUtil.FAILED_TO_DELETE_CHANNEL, chunk);
logger.log(Level.SEVERE, message, e);
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, message, e);
}
}
}

@PreDestroy
Expand Down
73 changes: 54 additions & 19 deletions src/main/java/org/phoebus/channelfinder/service/ChannelService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.collect.Lists;
import java.text.MessageFormat;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -92,16 +93,12 @@
}

public Iterable<Channel> create(Iterable<Channel> channels) {
requireRole(ROLES.CF_CHANNEL, "channels batch");

Check failure on line 96 in src/main/java/org/phoebus/channelfinder/service/ChannelService.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "channels batch" 3 times.

See more on https://sonarcloud.io/project/issues?id=ChannelFinder_ChannelFinderService&issues=AZ30CVEtT5I8PLC_FC-H&open=AZ30CVEtT5I8PLC_FC-H&pullRequest=214

Map<String, Channel> existing =
channelRepository
.findAllById(
StreamSupport.stream(channels.spliterator(), true).map(Channel::getName).toList())
.stream()
.collect(Collectors.toMap(Channel::getName, c -> c));
List<Channel> channelList = Lists.newArrayList(channels);
Map<String, Channel> existing = findExistingChannels(channelList);

for (Channel channel : channels) {
for (Channel channel : channelList) {
if (existing.containsKey(channel.getName())) {
requireOwner(existing.get(channel.getName()));
channel.setOwner(existing.get(channel.getName()).getOwner());
Expand All @@ -110,11 +107,11 @@
}
}

validateChannels(channels);
channelRepository.deleteAll(channels);
resetOwnersToExisting(channels);
validateChannels(channelList);
channelRepository.deleteAll(channelList);
resetOwnersToExisting(channelList);

List<Channel> created = channelRepository.indexAll(Lists.newArrayList(channels));
List<Channel> created = channelRepository.indexAll(channelList);
channelProcessorService.sendToProcessors(created);
return created;
}
Expand Down Expand Up @@ -151,20 +148,22 @@
public Iterable<Channel> update(Iterable<Channel> channels) {
requireRole(ROLES.CF_CHANNEL, "channels batch");

for (Channel channel : channels) {
Optional<Channel> existing = channelRepository.findById(channel.getName());
if (existing.isPresent()) {
requireOwner(existing.get());
channel.setOwner(existing.get().getOwner());
List<Channel> channelList = Lists.newArrayList(channels);
Map<String, Channel> existing = findExistingChannels(channelList);

for (Channel channel : channelList) {
if (existing.containsKey(channel.getName())) {
requireOwner(existing.get(channel.getName()));
channel.setOwner(existing.get(channel.getName()).getOwner());
} else {
requireOwner(channel);
}
}

validateChannels(channels);
resetOwnersToExisting(channels);
validateChannels(channelList);
resetOwnersToExisting(channelList);

List<Channel> updated = Lists.newArrayList(channelRepository.saveAll(channels));
List<Channel> updated = Lists.newArrayList(channelRepository.saveAll(channelList));
channelProcessorService.sendToProcessors(updated);
return updated;
}
Expand All @@ -181,6 +180,42 @@
channelRepository.deleteById(channelName);
}

public long remove(Iterable<String> channelNames) {
requireRole(ROLES.CF_CHANNEL, "channels batch");

List<String> distinctChannelNames =
StreamSupport.stream(channelNames.spliterator(), false)
.filter(name -> name != null && !name.isBlank())
.collect(Collectors.toCollection(LinkedHashSet::new))
.stream()
.toList();

if (distinctChannelNames.isEmpty()) {
return 0;
}

Map<String, Channel> existingChannels =
channelRepository.findAllById(distinctChannelNames).stream()
.collect(Collectors.toMap(Channel::getName, c -> c));

for (String channelName : distinctChannelNames) {
Channel existing = existingChannels.get(channelName);
if (existing == null) {
throw new ChannelNotFoundException(channelName);
}
requireOwner(existing);
audit.log(Level.INFO, () -> MessageFormat.format(TextUtil.DELETE_CHANNEL, channelName));
}

channelRepository.deleteAllById(distinctChannelNames);
return distinctChannelNames.size();
}

private Map<String, Channel> findExistingChannels(List<Channel> channels) {
return channelRepository.findAllById(channels.stream().map(Channel::getName).toList()).stream()
.collect(Collectors.toMap(Channel::getName, c -> c));
}

private void validateChannel(Channel channel) {
if (channel.getName() == null || channel.getName().isEmpty()) {
throw new ChannelValidationException(
Expand Down
77 changes: 71 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,
@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 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 Expand Up @@ -269,4 +310,28 @@ long queryCount(
})
@DeleteMapping("/{channelName}")
void remove(@PathVariable("channelName") String channelName);

@Operation(
summary = "Delete multiple channels",
description = "Delete multiple channel instances identified by a request-body list of names.",
operationId = "deleteChannels",
tags = {"Channel"})
@ApiResponses(
value = {
@ApiResponse(responseCode = "200", description = "Number of channels deleted"),
@ApiResponse(
responseCode = "401",
description = "Unauthorized",
content = @Content(schema = @Schema(implementation = ResponseStatusException.class))),
@ApiResponse(
responseCode = "404",
description = "Channel not found",
content = @Content(schema = @Schema(implementation = ResponseStatusException.class))),
@ApiResponse(
responseCode = "500",
description = "Error while trying to delete channels",
content = @Content(schema = @Schema(implementation = ResponseStatusException.class)))
})
@DeleteMapping
long remove(@RequestBody List<String> channelNames);
}
Loading
Loading