Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve response when bundle does not exists #29825

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 was not deleted because it does not 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
Loading