Skip to content

Commit

Permalink
Improve response when bundle does not exists (#29825)
Browse files Browse the repository at this point in the history
### Proposed Changes
* Improve the BundleResource response when the bundle does not exists
  • Loading branch information
freddyDOTCMS authored Aug 31, 2024
1 parent f9eb9a3 commit f21795e
Show file tree
Hide file tree
Showing 2 changed files with 814 additions and 145 deletions.
56 changes: 46 additions & 10 deletions dotCMS/src/main/java/com/dotcms/rest/BundleResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@
import io.vavr.Tuple2;
import io.vavr.control.Try;
import java.io.Reader;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.StreamingOutput;
Expand Down Expand Up @@ -85,11 +84,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import static com.dotcms.publisher.business.PublishAuditAPIImpl.NO_LIMIT_ASSETS;
import static com.dotcms.publisher.business.PublishAuditStatus.Status.FAILED_TO_BUNDLE;
Expand Down Expand Up @@ -374,7 +368,7 @@ public Response deleteEnvironmentPushHistory(@Context HttpServletRequest request
@Produces("application/json")
public Response deleteBundlesByIdentifiers(@Context final HttpServletRequest request,
@Context final HttpServletResponse response,
final DeleteBundlesByIdentifierForm deleteBundlesByIdentifierForm) {
final DeleteBundlesByIdentifierForm deleteBundlesByIdentifierForm) throws DotDataException {

final InitDataObject initData = new WebResource.InitBuilder(webResource)
.requiredBackendUser(true)
Expand All @@ -383,6 +377,10 @@ public Response deleteBundlesByIdentifiers(@Context final HttpServletRequest r
.rejectWhenNoUser(true)
.init();

if (!UtilMethods.isSet(deleteBundlesByIdentifierForm) || !UtilMethods.isSet(deleteBundlesByIdentifierForm.getIdentifiers())) {
throw new IllegalArgumentException("List of body to delete is mandatory");
}

Logger.info(this, "Deleting the bundles: " + deleteBundlesByIdentifierForm.getIdentifiers()
+ " by the user: " + initData.getUser().getUserId());

Expand Down Expand Up @@ -413,10 +411,48 @@ public Response deleteBundlesByIdentifiers(@Context final HttpServletRequest r
}
});

return Response.ok(new ResponseEntityView(
"Removing bundles in a separated process, the result of the operation will be notified")).build();
return buildResponseToDeleteEndpoint(deleteBundlesByIdentifierForm);
} // deleteBundlesByIdentifiers.

private static Response buildResponseToDeleteEndpoint(final DeleteBundlesByIdentifierForm deleteBundlesByIdentifierForm)
throws DotDataException {

final List<String> existingBundles = new ArrayList<>();
final List<String> notExistingBundles = new ArrayList<>();

for (String identifier : deleteBundlesByIdentifierForm.getIdentifiers()) {
final Bundle bundleById = APILocator.getBundleAPI().getBundleById(identifier);

if (UtilMethods.isSet(bundleById)) {
existingBundles.add(identifier);
} else {
notExistingBundles.add(identifier);
}
}

final StringBuffer message = new StringBuffer();

if (!existingBundles.isEmpty()) {
message.append(String.format(
"Removing the follow bundles in a separated process, the result of the operation will be notified: %s",
existingBundles.stream().collect(Collectors.joining(", ")))
);
}

if (!existingBundles.isEmpty() && !notExistingBundles.isEmpty()) {
message.append(", ");
}

if (!notExistingBundles.isEmpty()) {
message.append(String.format(
"The following bundles were not deleted because they don't exist: %s",
notExistingBundles.stream().collect(Collectors.joining(", ")))
);
}

return Response.ok(new ResponseEntityView(message)).build();
}

private void sendErrorDeleteBundleMessage(final InitDataObject initData,
final Locale locale,
final Exception e) {
Expand Down
Loading

0 comments on commit f21795e

Please sign in to comment.