Skip to content

Commit

Permalink
server: the exception mapper handles the CellBaseException, #TASK-4976
Browse files Browse the repository at this point in the history
  • Loading branch information
jtarraga committed Sep 12, 2023
1 parent 41d9e96 commit ad3ae74
Show file tree
Hide file tree
Showing 22 changed files with 48 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@
* limitations under the License.
*/

package org.opencb.cellbase.server.rest;
package org.opencb.cellbase.server.exception;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import org.opencb.cellbase.core.exception.CellBaseException;
import org.opencb.cellbase.core.result.CellBaseDataResponse;
import org.opencb.cellbase.core.result.CellBaseDataResult;
import org.opencb.cellbase.server.rest.GenericRestWSServer;
import org.opencb.commons.datastore.core.Event;
import org.opencb.commons.datastore.core.ObjectMap;
import org.opencb.commons.datastore.core.QueryOptions;
Expand All @@ -43,7 +45,7 @@
* Created by imedina on 02/03/16.
*/
@Provider
public class CellBaseExceptionMapper implements ExceptionMapper<Exception> {
public class CellBaseExceptionMapper implements ExceptionMapper<CellBaseException> {

@Context
private UriInfo uriInfo;
Expand All @@ -64,7 +66,7 @@ public class CellBaseExceptionMapper implements ExceptionMapper<Exception> {
}

@Override
public Response toResponse(Exception e) {
public Response toResponse(CellBaseException e) {
// First we print the exception in Server logs
logger.error("Catch error: " + e.getMessage(), e);

Expand All @@ -88,7 +90,7 @@ public Response toResponse(Exception e) {
jsonObjectWriter.writeValueAsString(queryOptions),
ERROR);

return Response.ok(GenericRestWSServer.jsonObjectWriter.writeValueAsString(queryResponse), MediaType.APPLICATION_JSON_TYPE)
return Response.ok(GenericRestWSServer.getJsonObjectWriter().writeValueAsString(queryResponse), MediaType.APPLICATION_JSON_TYPE)
.status(Response.Status.INTERNAL_SERVER_ERROR)
.build();
} catch (JsonProcessingException e1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ 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 CellBaseException {
super(apiVersion, uriInfo, hsr);
fileManager = cellBaseManagerFactory.getFileManager();
}
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 Down Expand Up @@ -60,7 +59,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 +101,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 CellBaseException {
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 CellBaseException {

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

init();
initQuery();
try {
init();
initQuery();
} catch (IOException e) {
throw new CellBaseException(e.getMessage());
}
}

private void init() throws IOException, CellBaseException {
Expand Down Expand Up @@ -467,4 +471,8 @@ private Response buildResponse(ResponseBuilder responseBuilder) {
.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
.build();
}

public static ObjectWriter getJsonObjectWriter() {
return jsonObjectWriter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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.common.GitRepositoryState;
import org.opencb.cellbase.core.config.DownloadProperties;
import org.opencb.cellbase.core.config.SpeciesConfiguration;
Expand Down Expand Up @@ -50,7 +49,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,7 +71,7 @@ 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 CellBaseException {
super(apiVersion, uriInfo, hsr);
metaManager = cellBaseManagerFactory.getMetaManager();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
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;
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;
import java.util.Map;
Expand All @@ -55,7 +53,7 @@ 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 CellBaseException {
super(apiVersion, species, uriInfo, hsr);
List<String> assemblies = uriInfo.getQueryParameters().get("assembly");
if (CollectionUtils.isNotEmpty(assemblies)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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;
Expand All @@ -30,7 +29,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.util.Map;

import static org.opencb.cellbase.core.ParamConstants.*;
Expand All @@ -55,7 +53,7 @@ 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 CellBaseException {
super(apiVersion, uriInfo, hsr);
publicationManager = cellBaseManagerFactory.getPublicationManager();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
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;
Expand All @@ -30,7 +29,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,7 +51,8 @@ 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 CellBaseException {
super(apiVersion, species, uriInfo, hsr);

genomeManager = cellBaseManagerFactory.getGenomeManager(species, assembly);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
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;
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,7 +51,7 @@ 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 CellBaseException {
super(apiVersion, species, uriInfo, hsr);
if (assembly == null) {
assembly = SpeciesUtils.getDefaultAssembly(cellBaseConfiguration, species).getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
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;
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,7 +56,7 @@ 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 CellBaseException {
super(apiVersion, species, uriInfo, hsr);
if (assembly == null) {
assembly = SpeciesUtils.getDefaultAssembly(cellBaseConfiguration, species).getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.opencb.cellbase.core.api.TranscriptQuery;
import org.opencb.cellbase.core.api.VariantQuery;
import org.opencb.cellbase.core.api.query.LogicalList;
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;
Expand All @@ -43,7 +42,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.security.InvalidParameterException;
import java.util.*;

Expand All @@ -70,8 +68,8 @@ public GeneWSServer(@PathParam("apiVersion") @ApiParam(name = "apiVersion", valu
@ApiParam(name = "dataRelease", value = DATA_RELEASE_DESCRIPTION) @DefaultValue("0") @QueryParam("dataRelease")
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 UriInfo uriInfo, @Context HttpServletRequest hsr)
throws CellBaseException {
super(apiVersion, species, uriInfo, hsr);
List<String> assemblies = uriInfo.getQueryParameters().get("assembly");
if (CollectionUtils.isNotEmpty(assemblies)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.opencb.biodata.models.core.Xref;
import org.opencb.cellbase.core.api.GeneQuery;
import org.opencb.cellbase.core.api.XrefQuery;
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;
Expand All @@ -35,7 +34,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;
import java.util.Map;
Expand All @@ -60,7 +58,8 @@ public IdWSServer(@PathParam("apiVersion") @ApiParam(name = "apiVersion", value
@ApiParam(name = "dataRelease", value = DATA_RELEASE_DESCRIPTION) @DefaultValue("0") @QueryParam("dataRelease")
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 UriInfo uriInfo, @Context HttpServletRequest hsr)
throws CellBaseException {
super(apiVersion, species, uriInfo, hsr);
if (assembly == null) {
assembly = SpeciesUtils.getDefaultAssembly(cellBaseConfiguration, species).getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,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;
import java.util.Map;
Expand All @@ -56,7 +55,7 @@ public ProteinWSServer(@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 {
throws CellBaseException {
super(apiVersion, species, uriInfo, hsr);
if (assembly == null) {
assembly = SpeciesUtils.getDefaultAssembly(cellBaseConfiguration, species).getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.opencb.cellbase.core.api.GeneQuery;
import org.opencb.cellbase.core.api.ProteinQuery;
import org.opencb.cellbase.core.api.TranscriptQuery;
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;
Expand All @@ -38,7 +37,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.*;

import static org.opencb.cellbase.core.ParamConstants.*;
Expand All @@ -64,7 +62,7 @@ public TranscriptWSServer(@PathParam("apiVersion") @ApiParam(name = "apiVersion"
@QueryParam("dataRelease") 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 CellBaseException {
super(apiVersion, species, uriInfo, hsr);
if (assembly == null) {
assembly = SpeciesUtils.getDefaultAssembly(cellBaseConfiguration, species).getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.apache.commons.lang3.StringUtils;
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.core.utils.SpeciesUtils;
Expand All @@ -34,7 +33,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.List;

import static org.opencb.cellbase.core.ParamConstants.*;
Expand All @@ -58,7 +56,7 @@ public ChromosomeWSServer(@PathParam("apiVersion") @ApiParam(name = "apiVersion"
@QueryParam("dataRelease") 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 CellBaseException {
super(apiVersion, species, uriInfo, hsr);
List<String> assemblies = uriInfo.getQueryParameters().get("assembly");
if (CollectionUtils.isNotEmpty(assemblies)) {
Expand Down
Loading

0 comments on commit ad3ae74

Please sign in to comment.