Skip to content

Commit

Permalink
feat(maintenance mode): REST API respond with 503 in maintenance (#2718)
Browse files Browse the repository at this point in the history
* catch TenantStatusException to throw 503 for APIs that are not available in maintenance
* catch InvalidSessionException to throw 401 ad a single filter level
* stop catching all exception when calling engine java API to be able to catch and handle runtime exceptions at filter leve
* clean useless tests of wrong methods usage
* clean useless exceptions

Covers [DEV-484](https://bonitasoft.atlassian.net/browse/DEV-484)
  • Loading branch information
abirembaut authored Sep 20, 2023
1 parent 8c96d2f commit 9c679cd
Show file tree
Hide file tree
Showing 62 changed files with 287 additions and 400 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
import java.util.Map;

import org.bonitasoft.engine.api.TenantAPIAccessor;
import org.bonitasoft.engine.exception.BonitaException;
import org.bonitasoft.engine.session.APISession;
import org.bonitasoft.engine.session.InvalidSessionException;
import org.bonitasoft.web.toolkit.client.common.exception.api.APIException;
import org.bonitasoft.web.toolkit.client.common.exception.api.APISessionInvalidException;

/**
* @author Vincent Elcrin
Expand Down Expand Up @@ -51,9 +50,7 @@ public CommandCaller addParameter(final String key, final Serializable value) {
public Serializable run() {
try {
return TenantAPIAccessor.getCommandAPI(this.session).execute(this.command, this.parameters);
} catch (InvalidSessionException e) {
throw new APISessionInvalidException(e);
} catch (final Exception e) {
} catch (final BonitaException e) {
throw new APIException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,12 @@
import org.bonitasoft.console.common.server.utils.SessionUtil;
import org.bonitasoft.engine.api.TenantAPIAccessor;
import org.bonitasoft.engine.api.permission.APICallContext;
import org.bonitasoft.engine.exception.BonitaException;
import org.bonitasoft.engine.exception.BonitaHomeNotSetException;
import org.bonitasoft.engine.exception.ExecutionException;
import org.bonitasoft.engine.exception.ServerAPIException;
import org.bonitasoft.engine.exception.UnknownAPITypeException;
import org.bonitasoft.engine.exception.*;
import org.bonitasoft.engine.session.APISession;
import org.bonitasoft.engine.session.InvalidSessionException;
import org.bonitasoft.engine.session.PlatformSession;
import org.bonitasoft.web.rest.server.framework.utils.RestRequestParser;
import org.bonitasoft.web.toolkit.client.common.exception.api.APIException;
import org.bonitasoft.web.toolkit.client.common.i18n.model.I18nLocaleDefinition;
import org.bonitasoft.web.toolkit.client.common.session.SessionDefinition;
import org.bonitasoft.web.toolkit.client.data.APIID;
Expand All @@ -67,8 +64,8 @@ public class RestAPIAuthorizationFilter extends ExcludingPatternFilter {
@Override
public void proceedWithFiltering(ServletRequest request, ServletResponse response, FilterChain chain)
throws ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
try {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
// body of multi-parts requests is not needed when checking permissions
if (!ServletFileUpload.isMultipartContent(httpServletRequest)) {
//we need to use a MultiReadHttpServletRequest wrapper in order to be able to get the input stream twice (in the filter and in the API servlet)
Expand All @@ -86,11 +83,27 @@ public void proceedWithFiltering(ServletRequest request, ServletResponse respons
if (isAuthorized) {
chain.doFilter(httpServletRequest, response);
}
} catch (final InvalidSessionException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Invalid Bonita engine session.", e.getMessage());
}
SessionUtil.sessionLogout(httpServletRequest.getSession());
((HttpServletResponse) response).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
} catch (final TenantStatusException e) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Platform is probably under Maintenance : " + e.getMessage());
}
((HttpServletResponse) response).setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
} catch (final Exception e) {
if (LOGGER.isErrorEnabled()) {
LOGGER.error(e.getMessage(), e);
}
throw new ServletException(e);
if (e instanceof APIException) {
throw new ServletException(e);
} else {
//wrap exception in APIException to avoid disclose too much information
throw new ServletException(new APIException(e));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import org.bonitasoft.engine.exception.BonitaException;
import org.bonitasoft.engine.io.FileContent;
import org.bonitasoft.engine.session.APISession;
import org.bonitasoft.engine.session.InvalidSessionException;
import org.bonitasoft.web.toolkit.client.common.exception.api.APISessionInvalidException;
import org.bonitasoft.web.toolkit.server.ServiceException;

/**
Expand All @@ -39,16 +37,12 @@ public Object run() {
final BonitaHomeFolderAccessor tenantFolder = new BonitaHomeFolderAccessor();
try {
final FileContent xmlFile = tenantFolder.retrieveUploadedTempContent(getFileUploadParameter());

final APISession apiSession = getSession();
final ProcessAPI processAPI = TenantAPIAccessor.getProcessAPI(apiSession);
try (InputStream xmlStream = xmlFile.getInputStream()) {
final byte[] actorsXmlContent = IOUtils.toByteArray(xmlStream);
processAPI.importActorMapping(Long.valueOf(getParameter("process_id")), actorsXmlContent);
}

} catch (final InvalidSessionException e) {
throw new APISessionInvalidException(e);
} catch (final BonitaException | IOException e) {
throw new ServiceException(TOKEN, e.getMessage());
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.bonitasoft.console.common.server.login.HttpServletRequestAccessor;
import org.bonitasoft.console.common.server.utils.SessionUtil;
import org.bonitasoft.engine.exception.NotFoundException;
import org.bonitasoft.engine.exception.TenantStatusException;
import org.bonitasoft.engine.session.InvalidSessionException;
import org.bonitasoft.web.rest.model.ModelFactory;
import org.bonitasoft.web.rest.server.datastore.bpm.flownode.FlowNodeConverter;
Expand Down Expand Up @@ -72,6 +73,11 @@ protected void catchAllExceptions(final Throwable exception, final HttpServletRe
SessionUtil.sessionLogout(requestAccessor.getHttpSession());
} else if (exception.getCause() instanceof NotFoundException) {
outputException(null, req, resp, HttpServletResponse.SC_NOT_FOUND);
} else if (exception instanceof TenantStatusException) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Platform is probably under Maintenance : " + exception.getMessage());
}
outputException(null, req, resp, HttpServletResponse.SC_SERVICE_UNAVAILABLE);
} else {
super.catchAllExceptions(exception, req, resp);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.bonitasoft.engine.api.ProcessAPI;
import org.bonitasoft.engine.api.TenantAPIAccessor;
import org.bonitasoft.engine.api.TenantAdministrationAPI;
import org.bonitasoft.engine.exception.BonitaException;
import org.bonitasoft.engine.session.APISession;
import org.bonitasoft.web.toolkit.client.common.exception.api.APIException;
import org.restlet.Request;
Expand All @@ -40,7 +41,7 @@ protected CommandAPI getCommandAPI(final Request request) {
final APISession apiSession = getAPISession(request);
try {
return TenantAPIAccessor.getCommandAPI(apiSession);
} catch (final Exception e) {
} catch (final BonitaException e) {
throw new APIException(e);
}
}
Expand All @@ -49,7 +50,7 @@ protected ProcessAPI getProcessAPI(final Request request) {
final APISession apiSession = getAPISession(request);
try {
return TenantAPIAccessor.getProcessAPI(apiSession);
} catch (final Exception e) {
} catch (final BonitaException e) {
throw new APIException(e);
}
}
Expand All @@ -58,7 +59,7 @@ protected BusinessDataAPI getBdmAPI(final Request request) {
final APISession apiSession = getAPISession(request);
try {
return TenantAPIAccessor.getBusinessDataAPI(apiSession);
} catch (final Exception e) {
} catch (final BonitaException e) {
throw new APIException(e);
}
}
Expand All @@ -67,7 +68,7 @@ protected TenantAdministrationAPI getTenantAdministrationAPI(final Request reque
final APISession apiSession = getAPISession(request);
try {
return TenantAPIAccessor.getTenantAdministrationAPI(apiSession);
} catch (final Exception e) {
} catch (final BonitaException e) {
throw new APIException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import org.bonitasoft.engine.business.data.BusinessDataRepositoryDeploymentException;
import org.bonitasoft.engine.business.data.InvalidBusinessDataModelException;
import org.bonitasoft.engine.exception.BonitaException;
import org.bonitasoft.engine.exception.TenantStatusException;
import org.bonitasoft.engine.exception.UnavailableLockException;
import org.bonitasoft.engine.io.FileContent;
import org.bonitasoft.engine.session.InvalidSessionException;
import org.bonitasoft.web.rest.model.bdm.BusinessDataModelItem;
import org.bonitasoft.web.rest.server.api.resource.CommonResource;
import org.bonitasoft.web.rest.server.api.tenant.TenantResourceItem;
Expand Down Expand Up @@ -69,6 +71,8 @@ public TenantResourceItem addBDM(final BusinessDataModelItem businessDataModelIt
return null;
} catch (final BusinessDataRepositoryDeploymentException e) {
throw new APIException("An error has occurred when deploying Business Data Model.", e);
} catch (final TenantStatusException | InvalidSessionException e) {
throw e; //handled by REST API Authorization filter
} catch (Exception e) {
Throwable cause = e.getCause();
if (cause instanceof UnavailableLockException) {
Expand All @@ -88,6 +92,8 @@ public TenantResourceItem addBDM(final BusinessDataModelItem businessDataModelIt
public TenantResourceItem getBDM() {
try {
return new TenantResourceItem(tenantAdministrationAPI.getBusinessDataModelResource());
} catch (final TenantStatusException | InvalidSessionException e) {
throw e; //handled by REST API Authorization filter
} catch (final Exception e) {
throw new APIException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.bonitasoft.console.common.server.preferences.constants.WebBonitaConstantsUtils;
import org.bonitasoft.engine.api.ProcessAPI;
import org.bonitasoft.engine.api.TenantAPIAccessor;
import org.bonitasoft.engine.exception.BonitaException;
import org.bonitasoft.web.rest.model.bpm.cases.ArchivedCaseDocumentDefinition;
import org.bonitasoft.web.rest.model.bpm.cases.ArchivedCaseDocumentItem;
import org.bonitasoft.web.rest.server.api.ConsoleAPI;
Expand Down Expand Up @@ -82,7 +83,7 @@ protected ArchivedCaseDocumentDatastore getArchivedCaseDocumentDatastore() {
ProcessAPI processAPI;
try {
processAPI = TenantAPIAccessor.getProcessAPI(getEngineSession());
} catch (final Exception e) {
} catch (final BonitaException e) {
throw new APIException(e);
}
final WebBonitaConstantsUtils constants = WebBonitaConstantsUtils.getTenantInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.bonitasoft.console.common.server.utils.BonitaHomeFolderAccessor;
import org.bonitasoft.engine.api.ProcessAPI;
import org.bonitasoft.engine.api.TenantAPIAccessor;
import org.bonitasoft.engine.exception.BonitaException;
import org.bonitasoft.web.rest.model.bpm.cases.CaseDocumentDefinition;
import org.bonitasoft.web.rest.model.bpm.cases.CaseDocumentItem;
import org.bonitasoft.web.rest.server.api.ConsoleAPI;
Expand Down Expand Up @@ -93,7 +94,7 @@ protected CaseDocumentDatastore getCaseDocumentDatastore() {
ProcessAPI processAPI;
try {
processAPI = TenantAPIAccessor.getProcessAPI(getEngineSession());
} catch (final Exception e) {
} catch (final BonitaException e) {
throw new APIException(e);
}
final WebBonitaConstantsUtils constants = WebBonitaConstantsUtils.getTenantInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
import org.bonitasoft.engine.api.ProcessAPI;
import org.bonitasoft.engine.bpm.data.DataInstance;
import org.bonitasoft.engine.bpm.data.DataNotFoundException;
import org.bonitasoft.engine.exception.BonitaException;
import org.bonitasoft.web.rest.server.api.resource.CommonResource;
import org.bonitasoft.web.toolkit.client.common.exception.api.APIException;
import org.bonitasoft.web.toolkit.client.common.exception.api.APIItemIdMalformedException;
import org.bonitasoft.web.toolkit.client.common.exception.api.APIMalformedUrlException;
import org.restlet.resource.Get;

public class ActivityVariableResource extends CommonResource {
Expand All @@ -36,12 +39,23 @@ public DataInstance getTaskVariable() {
try {
final String taskId = getAttribute(ACTIVITYDATA_ACTIVITY_ID);
final String dataName = getAttribute(ACTIVITYDATA_DATA_NAME);
return getTaskVariableInstance(dataName, Long.valueOf(taskId));
} catch (final Exception e) {
if (taskId == null || dataName == null) {
throw new APIMalformedUrlException("missing activity Id and or variable name");
}
return getTaskVariableInstance(dataName, getActivityInstanceId(taskId));
} catch (final BonitaException e) {
throw new APIException(e);
}
}

private Long getActivityInstanceId(String taskId) {
try {
return Long.valueOf(taskId);
} catch (NumberFormatException e) {
throw new APIItemIdMalformedException("Long", "long value expected for activity Id");
}
}

private DataInstance getTaskVariableInstance(final String dataName, final Long activityInstanceId)
throws DataNotFoundException {
return processAPI.getActivityDataInstance(dataName, activityInstanceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
**/
package org.bonitasoft.web.rest.server.api.bpm.flownode;

import java.io.IOException;
import java.util.Date;

import org.bonitasoft.engine.api.ProcessAPI;
import org.bonitasoft.engine.bpm.flownode.TimerEventTriggerInstance;
import org.bonitasoft.engine.exception.BonitaException;
import org.bonitasoft.engine.search.SearchResult;
import org.bonitasoft.web.rest.server.api.resource.CommonResource;
import org.bonitasoft.web.toolkit.client.common.exception.api.APIException;
Expand Down Expand Up @@ -55,7 +57,7 @@ public void searchTimerEventTriggers() {
representation.setCharacterSet(CharacterSet.UTF_8);
getResponse().setEntity(representation);
setContentRange(searchResult);
} catch (final Exception e) {
} catch (final BonitaException | IOException e) {
throw new APIException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.bonitasoft.engine.bpm.parameter.ParameterInstance;
import org.bonitasoft.engine.bpm.process.ProcessDefinition;
import org.bonitasoft.engine.bpm.process.ProcessDeploymentInfo;
import org.bonitasoft.engine.exception.BonitaException;
import org.bonitasoft.engine.exception.BonitaHomeNotSetException;
import org.bonitasoft.engine.exception.ServerAPIException;
import org.bonitasoft.engine.exception.UnknownAPITypeException;
Expand Down Expand Up @@ -83,7 +84,7 @@ public ProcessParameterItem get(final APIID id) {
return new ProcessParameterItem(processId, parameterInstance.getName(), parameterInstance.getType(),
paramValue, parameterInstance.getDescription(), processDeploy.getDisplayName(),
processDef.getVersion());
} catch (final Exception e) {
} catch (final BonitaException e) {
throw new APIException(e);
}
}
Expand Down Expand Up @@ -121,7 +122,7 @@ public ItemSearchResult<ProcessParameterItem> search(final int page, final int r
p.getDescription(), "", ""));
}
return new ItemSearchResult<>(page, resultsByPage, parametersCount, items);
} catch (final Exception e) {
} catch (final BonitaException e) {
throw new APIException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.bonitasoft.engine.api.TenantAPIAccessor;
import org.bonitasoft.engine.bpm.document.ArchivedDocument;
import org.bonitasoft.engine.bpm.document.ArchivedDocumentsSearchDescriptor;
import org.bonitasoft.engine.exception.BonitaException;
import org.bonitasoft.engine.search.SearchOptionsBuilder;
import org.bonitasoft.engine.search.SearchResult;
import org.bonitasoft.engine.session.APISession;
Expand Down Expand Up @@ -55,7 +56,7 @@ public ArchivedDocumentItem get(final APIID id) {
final DocumentDatastore dataStore = new DocumentDatastore(apiSession);
final ArchivedDocument document = processAPI.getArchivedProcessDocument(id.toLong());
item = dataStore.mapToArchivedDocumentItem(document);
} catch (final Exception e) {
} catch (final BonitaException e) {
throw new APIException(e);
}
return item;
Expand Down Expand Up @@ -117,7 +118,7 @@ public ItemSearchResult<ArchivedDocumentItem> search(final int page, final int r
items.add(dataStore.mapToArchivedDocumentItem(document));
}
}
} catch (final Exception e) {
} catch (final BonitaException | IllegalArgumentException e) {
throw new APIException(e);
}
return new ItemSearchResult<>(page, resultsByPage, nbOfDocument, items);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
**/
package org.bonitasoft.web.rest.server.api.document;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -22,6 +23,7 @@
import org.bonitasoft.engine.api.TenantAPIAccessor;
import org.bonitasoft.engine.bpm.document.ArchivedDocumentsSearchDescriptor;
import org.bonitasoft.engine.bpm.document.Document;
import org.bonitasoft.engine.exception.BonitaException;
import org.bonitasoft.engine.search.SearchOptionsBuilder;
import org.bonitasoft.engine.search.SearchResult;
import org.bonitasoft.engine.session.APISession;
Expand Down Expand Up @@ -59,7 +61,7 @@ public DocumentItem get(final APIID id) {
final ProcessAPI processAPI = TenantAPIAccessor.getProcessAPI(apiSession);
final Document document = processAPI.getDocument(id.toLong());
item = getDataStore().mapToDocumentItem(document);
} catch (final Exception e) {
} catch (final BonitaException e) {
throw new APIException(e);
}

Expand Down Expand Up @@ -123,7 +125,7 @@ public ItemSearchResult<DocumentItem> search(final int page, final int resultsBy
items.add(getDataStore().mapToDocumentItem(document));
}
}
} catch (final Exception e) {
} catch (final BonitaException | IllegalArgumentException e) {
throw new APIException(e);
}
return new ItemSearchResult<>(page, resultsByPage, nbOfDocument, items);
Expand Down Expand Up @@ -151,7 +153,7 @@ public DocumentItem add(final DocumentItem item) {
} else {
throw new APIException("Error while attaching a new document. Request with bad param value.");
}
} catch (final Exception e) {
} catch (final BonitaException | IOException e) {
throw new APIException(e);
}
}
Expand Down
Loading

0 comments on commit 9c679cd

Please sign in to comment.