Skip to content

Commit

Permalink
Merge pull request #669 from opencb/TASK-4976-develop
Browse files Browse the repository at this point in the history
TASK-4976 Fix error handling when an exception raises in the CellBase Rest server constructors
  • Loading branch information
jtarraga authored Oct 2, 2023
2 parents f785365 + bb2a80d commit 81276f6
Show file tree
Hide file tree
Showing 23 changed files with 235 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public void checkQuota(String apiKey, ApiKeyJwtPayload payload) throws CellBaseE
numQueries = quotaResult.first().getNumQueries();
}
if (numQueries >= payload.getQuota().getMaxNumQueries()) {
throw new CellBaseException("Exceeded the maximum number of queries");
throw new CellBaseException("Maximum query limit reached: Your current API key has a quota of "
+ payload.getQuota().getMaxNumQueries() + " queries");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2015-2020 OpenCB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.opencb.cellbase.server.exception;

import javax.ws.rs.WebApplicationException;

public class CellBaseServerException extends WebApplicationException {
public CellBaseServerException(String msg) {
super(msg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import org.opencb.cellbase.core.ParamConstants;
import org.opencb.cellbase.core.api.FileQuery;
import org.opencb.cellbase.core.api.query.QueryException;
import org.opencb.cellbase.core.exception.CellBaseException;
import org.opencb.cellbase.core.result.CellBaseDataResult;
import org.opencb.cellbase.lib.managers.FileManager;
import org.opencb.cellbase.server.exception.CellBaseServerException;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
Expand All @@ -48,9 +48,13 @@ public FileWSServer(@PathParam("apiVersion")
@ApiParam(name = "apiVersion", value = ParamConstants.VERSION_DESCRIPTION,
defaultValue = ParamConstants.DEFAULT_VERSION) String apiVersion,
@Context UriInfo uriInfo, @Context HttpServletRequest hsr)
throws QueryException, IOException, CellBaseException {
throws CellBaseServerException {
super(apiVersion, uriInfo, hsr);
fileManager = cellBaseManagerFactory.getFileManager();
try {
fileManager = cellBaseManagerFactory.getFileManager();
} catch (Exception e) {
throw new CellBaseServerException(e.getMessage());
}
}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.opencb.cellbase.core.ParamConstants;
import org.opencb.cellbase.core.api.key.ApiKeyJwtPayload;
import org.opencb.cellbase.core.api.key.ApiKeyManager;
import org.opencb.cellbase.core.api.query.QueryException;
import org.opencb.cellbase.core.config.CellBaseConfiguration;
import org.opencb.cellbase.core.exception.CellBaseException;
import org.opencb.cellbase.core.result.CellBaseDataResponse;
Expand All @@ -40,6 +39,7 @@
import org.opencb.cellbase.lib.managers.DataReleaseManager;
import org.opencb.cellbase.lib.managers.MetaManager;
import org.opencb.cellbase.lib.monitor.Monitor;
import org.opencb.cellbase.server.exception.CellBaseServerException;
import org.opencb.commons.datastore.core.Event;
import org.opencb.commons.datastore.core.ObjectMap;
import org.opencb.commons.datastore.core.Query;
Expand All @@ -60,7 +60,8 @@
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.opencb.cellbase.core.ParamConstants.*;
import static org.opencb.cellbase.core.ParamConstants.API_KEY_PARAM;
import static org.opencb.cellbase.core.ParamConstants.DATA_RELEASE_PARAM;

@Path("/{version}/{species}")
@Produces("text/plain")
Expand Down Expand Up @@ -101,21 +102,25 @@ public class GenericRestWSServer implements IWSServer {
protected static ApiKeyManager apiKeyManager;

public GenericRestWSServer(@PathParam("version") String version, @Context UriInfo uriInfo, @Context HttpServletRequest hsr)
throws QueryException, IOException, CellBaseException {
throws CellBaseServerException {
this(version, DONT_CHECK_SPECIES, uriInfo, hsr);
}

public GenericRestWSServer(@PathParam("version") String version, @PathParam("species") String species, @Context UriInfo uriInfo,
@Context HttpServletRequest hsr)
throws QueryException, IOException, CellBaseException {
throws CellBaseServerException {

this.version = version;
this.uriInfo = uriInfo;
this.httpServletRequest = hsr;
this.species = species;

init();
initQuery();
try {
init();
initQuery();
} catch (Exception e) {
throw new CellBaseServerException(e.getMessage());
}
}

private void init() throws IOException, CellBaseException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.opencb.cellbase.core.ParamConstants;
import org.opencb.cellbase.core.api.query.QueryException;
import org.opencb.cellbase.core.api.key.ApiKeyJwtPayload;
import org.opencb.cellbase.core.api.key.ApiKeyManager;
import org.opencb.cellbase.core.common.GitRepositoryState;
import org.opencb.cellbase.core.config.DownloadProperties;
import org.opencb.cellbase.core.config.SpeciesConfiguration;
import org.opencb.cellbase.core.config.SpeciesProperties;
import org.opencb.cellbase.core.exception.CellBaseException;
import org.opencb.cellbase.core.models.DataRelease;
import org.opencb.cellbase.core.result.CellBaseDataResult;
import org.opencb.cellbase.core.api.key.ApiKeyManager;
import org.opencb.cellbase.core.api.key.ApiKeyJwtPayload;
import org.opencb.cellbase.core.utils.SpeciesUtils;
import org.opencb.cellbase.lib.managers.DataReleaseManager;
import org.opencb.cellbase.lib.managers.MetaManager;
import org.opencb.cellbase.server.exception.CellBaseServerException;
import org.opencb.cellbase.server.rest.clinical.ClinicalWSServer;
import org.opencb.cellbase.server.rest.feature.GeneWSServer;
import org.opencb.cellbase.server.rest.feature.IdWSServer;
Expand All @@ -50,7 +50,6 @@
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.text.DateFormat;
Expand All @@ -73,9 +72,13 @@ public MetaWSServer(@PathParam("apiVersion")
@ApiParam(name = "apiVersion", value = ParamConstants.VERSION_DESCRIPTION,
defaultValue = ParamConstants.DEFAULT_VERSION) String apiVersion,
@Context UriInfo uriInfo, @Context HttpServletRequest hsr)
throws QueryException, IOException, CellBaseException {
throws CellBaseServerException {
super(apiVersion, uriInfo, hsr);
metaManager = cellBaseManagerFactory.getMetaManager();
try {
metaManager = cellBaseManagerFactory.getMetaManager();
} catch (Exception e) {
throw new CellBaseServerException(e.getMessage());
}
}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,17 @@
import org.apache.commons.lang3.StringUtils;
import org.opencb.biodata.models.core.OntologyTerm;
import org.opencb.cellbase.core.api.OntologyQuery;
import org.opencb.cellbase.core.api.query.QueryException;
import org.opencb.cellbase.core.exception.CellBaseException;
import org.opencb.cellbase.core.result.CellBaseDataResult;
import org.opencb.cellbase.core.utils.SpeciesUtils;
import org.opencb.cellbase.lib.managers.OntologyManager;
import org.opencb.cellbase.server.exception.CellBaseServerException;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand All @@ -55,17 +53,21 @@ public OntologyWSServer(@PathParam("apiVersion") @ApiParam(name = "apiVersion",
int dataRelease,
@ApiParam(name = "apiKey", value = API_KEY_DESCRIPTION) @DefaultValue("") @QueryParam("apiKey") String apiKey,
@Context UriInfo uriInfo, @Context HttpServletRequest hsr)
throws QueryException, IOException, CellBaseException {
throws CellBaseServerException {
super(apiVersion, species, uriInfo, hsr);
List<String> assemblies = uriInfo.getQueryParameters().get("assembly");
if (CollectionUtils.isNotEmpty(assemblies)) {
assembly = assemblies.get(0);
}
if (StringUtils.isEmpty(assembly)) {
assembly = SpeciesUtils.getDefaultAssembly(cellBaseConfiguration, species).getName();
}
try {
List<String> assemblies = uriInfo.getQueryParameters().get("assembly");
if (CollectionUtils.isNotEmpty(assemblies)) {
assembly = assemblies.get(0);
}
if (StringUtils.isEmpty(assembly)) {
assembly = SpeciesUtils.getDefaultAssembly(cellBaseConfiguration, species).getName();
}

ontologyManager = cellBaseManagerFactory.getOntologyManager(species, assembly);
ontologyManager = cellBaseManagerFactory.getOntologyManager(species, assembly);
} catch (Exception e) {
throw new CellBaseServerException(e.getMessage());
}
}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@
import org.opencb.biodata.formats.pubmed.v233jaxb.PubmedArticle;
import org.opencb.cellbase.core.ParamConstants;
import org.opencb.cellbase.core.api.PublicationQuery;
import org.opencb.cellbase.core.api.query.QueryException;
import org.opencb.cellbase.core.exception.CellBaseException;
import org.opencb.cellbase.core.result.CellBaseDataResult;
import org.opencb.cellbase.lib.managers.PublicationManager;
import org.opencb.cellbase.server.exception.CellBaseServerException;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.io.IOException;
import java.util.Map;

import static org.opencb.cellbase.core.ParamConstants.*;
Expand All @@ -55,9 +53,13 @@ public PublicationWSServer(@PathParam("apiVersion")
@ApiParam(name = "apiKey", value = API_KEY_DESCRIPTION) @DefaultValue("") @QueryParam("apiKey")
String apiKey,
@Context UriInfo uriInfo, @Context HttpServletRequest hsr)
throws QueryException, IOException, CellBaseException {
throws CellBaseServerException {
super(apiVersion, uriInfo, hsr);
publicationManager = cellBaseManagerFactory.getPublicationManager();
try {
publicationManager = cellBaseManagerFactory.getPublicationManager();
} catch (Exception e) {
throw new CellBaseServerException(e.getMessage());
}
}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,16 @@
import io.swagger.annotations.*;
import org.opencb.biodata.models.core.Chromosome;
import org.opencb.cellbase.core.api.GenomeQuery;
import org.opencb.cellbase.core.api.query.QueryException;
import org.opencb.cellbase.core.exception.CellBaseException;
import org.opencb.cellbase.core.result.CellBaseDataResult;
import org.opencb.cellbase.lib.managers.GenomeManager;
import org.opencb.cellbase.server.exception.CellBaseServerException;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.io.IOException;

import static org.opencb.cellbase.core.ParamConstants.*;

Expand All @@ -53,10 +51,13 @@ public SpeciesWSServer(@PathParam("apiVersion") @ApiParam(name = "apiVersion", v
int dataRelease,
@ApiParam(name = "apiKey", value = API_KEY_DESCRIPTION) @DefaultValue("") @QueryParam("apiKey") String apiKey,
@Context UriInfo uriInfo,
@Context HttpServletRequest hsr) throws QueryException, IOException, CellBaseException {
@Context HttpServletRequest hsr) throws CellBaseServerException {
super(apiVersion, species, uriInfo, hsr);

genomeManager = cellBaseManagerFactory.getGenomeManager(species, assembly);
try {
genomeManager = cellBaseManagerFactory.getGenomeManager(species, assembly);
} catch (Exception e) {
throw new CellBaseServerException(e.getMessage());
}
}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
import io.swagger.annotations.*;
import org.opencb.biodata.models.variant.Variant;
import org.opencb.cellbase.core.api.ClinicalVariantQuery;
import org.opencb.cellbase.core.api.query.QueryException;
import org.opencb.cellbase.core.exception.CellBaseException;
import org.opencb.cellbase.core.result.CellBaseDataResult;
import org.opencb.cellbase.core.utils.SpeciesUtils;
import org.opencb.cellbase.lib.managers.ClinicalManager;
import org.opencb.cellbase.server.exception.CellBaseServerException;
import org.opencb.cellbase.server.rest.GenericRestWSServer;

import javax.servlet.http.HttpServletRequest;
Expand All @@ -32,7 +31,6 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.io.IOException;

import static org.opencb.cellbase.core.ParamConstants.*;

Expand All @@ -53,13 +51,17 @@ public ClinicalWSServer(@PathParam("apiVersion") @ApiParam(name = "apiVersion",
int dataRelease,
@ApiParam(name = "apiKey", value = API_KEY_DESCRIPTION) @DefaultValue("") @QueryParam("apiKey") String apiKey,
@Context UriInfo uriInfo, @Context HttpServletRequest hsr)
throws QueryException, IOException, CellBaseException {
throws CellBaseServerException {
super(apiVersion, species, uriInfo, hsr);
if (assembly == null) {
assembly = SpeciesUtils.getDefaultAssembly(cellBaseConfiguration, species).getName();
}
try {
if (assembly == null) {
assembly = SpeciesUtils.getDefaultAssembly(cellBaseConfiguration, species).getName();
}

clinicalManager = cellBaseManagerFactory.getClinicalManager(species, assembly);
clinicalManager = cellBaseManagerFactory.getClinicalManager(species, assembly);
} catch (Exception e) {
throw new CellBaseServerException(e.getMessage());
}
}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
import io.swagger.annotations.*;
import org.opencb.biodata.models.pharma.PharmaChemical;
import org.opencb.cellbase.core.api.PharmaChemicalQuery;
import org.opencb.cellbase.core.api.query.QueryException;
import org.opencb.cellbase.core.exception.CellBaseException;
import org.opencb.cellbase.core.result.CellBaseDataResult;
import org.opencb.cellbase.core.utils.SpeciesUtils;
import org.opencb.cellbase.lib.managers.ClinicalManager;
import org.opencb.cellbase.lib.managers.PharmacogenomicsManager;
import org.opencb.cellbase.server.exception.CellBaseServerException;
import org.opencb.cellbase.server.rest.GenericRestWSServer;

import javax.servlet.http.HttpServletRequest;
Expand All @@ -33,7 +32,6 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

Expand All @@ -58,14 +56,18 @@ public PharmacogenomicsWSServer(@PathParam("apiVersion") @ApiParam(name = "apiVe
@ApiParam(name = "apiKey", value = API_KEY_DESCRIPTION) @DefaultValue("") @QueryParam("apiKey")
String apiKey,
@Context UriInfo uriInfo, @Context HttpServletRequest hsr)
throws QueryException, IOException, CellBaseException {
throws CellBaseServerException {
super(apiVersion, species, uriInfo, hsr);
if (assembly == null) {
assembly = SpeciesUtils.getDefaultAssembly(cellBaseConfiguration, species).getName();
}
try {
if (assembly == null) {
assembly = SpeciesUtils.getDefaultAssembly(cellBaseConfiguration, species).getName();
}

clinicalManager = cellBaseManagerFactory.getClinicalManager(species, assembly);
pharmacogenomicsManager = cellBaseManagerFactory.getPharmacogenomicsManager(species, assembly);
clinicalManager = cellBaseManagerFactory.getClinicalManager(species, assembly);
pharmacogenomicsManager = cellBaseManagerFactory.getPharmacogenomicsManager(species, assembly);
} catch (Exception e) {
throw new CellBaseServerException(e.getMessage());
}
}

@GET
Expand Down
Loading

0 comments on commit 81276f6

Please sign in to comment.