Skip to content
Snippets Groups Projects
Commit 7a382421 authored by kfilippopolitis's avatar kfilippopolitis Committed by ThanKarab
Browse files

Implemented exteption/error handling and added java docs on service layer.

parent 5e34a236
No related branches found
No related tags found
1 merge request!19Feat/186 experiment refactor
Showing
with 423 additions and 125 deletions
...@@ -3,14 +3,18 @@ package eu.hbp.mip.controllers; ...@@ -3,14 +3,18 @@ package eu.hbp.mip.controllers;
import eu.hbp.mip.model.DTOs.ExperimentDTO; import eu.hbp.mip.model.DTOs.ExperimentDTO;
import eu.hbp.mip.model.UserInfo; import eu.hbp.mip.model.UserInfo;
import eu.hbp.mip.services.ExperimentService; import eu.hbp.mip.services.ExperimentService;
import eu.hbp.mip.utils.JsonConverters;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.Map;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
/* /*
...@@ -39,32 +43,50 @@ public class ExperimentApi { ...@@ -39,32 +43,50 @@ public class ExperimentApi {
@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "3") int size @RequestParam(defaultValue = "3") int size
) { ) {
return experimentService.getExperiments(name, algorithm, shared, viewed, page, size, "(GET) /experiments"); Map experiments = experimentService.getExperiments(name, algorithm, shared, viewed, page, size, "(GET) /experiments");
return new ResponseEntity(experiments, HttpStatus.OK);
} }
@ApiOperation(value = "Get an experiment", response = ExperimentDTO.class) @ApiOperation(value = "Get an experiment", response = ExperimentDTO.class)
@RequestMapping(value = "/{uuid}", method = RequestMethod.GET) @RequestMapping(value = "/{uuid}", method = RequestMethod.GET)
public ResponseEntity<String> getExperiment( public ResponseEntity<String> getExperiment(
@ApiParam(value = "uuid", required = true) @PathVariable("uuid") String uuid) { @ApiParam(value = "uuid", required = true) @PathVariable("uuid") String uuid) {
return experimentService.getExperiment(uuid, "(GET) /experiments/{uuid}"); ExperimentDTO experimentDTO = experimentService.getExperiment(uuid, "(GET) /experiments/{uuid}");
return new ResponseEntity<>(JsonConverters.convertObjectToJsonString(experimentDTO), HttpStatus.OK);
} }
@ApiOperation(value = "Create an experiment", response = ExperimentDTO.class) @ApiOperation(value = "Create an experiment", response = ExperimentDTO.class)
@RequestMapping(method = RequestMethod.POST) @RequestMapping(method = RequestMethod.POST)
public ResponseEntity<String> createExperiment(Authentication authentication, @RequestBody ExperimentDTO experimentDTO) { public ResponseEntity<String> createExperiment(Authentication authentication, @RequestBody ExperimentDTO experimentDTO) {
return experimentService.createExperiment(authentication, experimentDTO, "(POST) /experiments"); experimentDTO = experimentService.createExperiment(authentication, experimentDTO, "(POST) /experiments");
return new ResponseEntity<>(JsonConverters.convertObjectToJsonString(experimentDTO), HttpStatus.CREATED);
} }
@ApiOperation(value = "Create a transient experiment", response = ExperimentDTO.class)
@RequestMapping(value = "/{transient}",method = RequestMethod.POST)
public ResponseEntity<String> createTransientExperiment(Authentication authentication, @RequestBody ExperimentDTO experimentDTO) {
experimentDTO = experimentService.createTransientExperiment(authentication, experimentDTO, "(POST) /experiments/transient");
return new ResponseEntity<>(JsonConverters.convertObjectToJsonString(experimentDTO), HttpStatus.OK);
}
@ApiOperation(value = "Update an experiment", response = ExperimentDTO.class) @ApiOperation(value = "Update an experiment", response = ExperimentDTO.class)
@RequestMapping(value = "/{uuid}", method = RequestMethod.PATCH) @RequestMapping(value = "/{uuid}", method = RequestMethod.PATCH)
public ResponseEntity<String> updateExperiment(@RequestBody ExperimentDTO experimentDTO,@ApiParam(value = "uuid", required = true) @PathVariable("uuid") String uuid) { public ResponseEntity<String> updateExperiment(@RequestBody ExperimentDTO experimentDTO,@ApiParam(value = "uuid", required = true) @PathVariable("uuid") String uuid) {
return experimentService.updateExperiment(uuid, experimentDTO, "(PATCH) /experiments/{uuid}"); experimentDTO = experimentService.updateExperiment(uuid, experimentDTO, "(PATCH) /experiments/{uuid}");
return new ResponseEntity<>(JsonConverters.convertObjectToJsonString(experimentDTO), HttpStatus.OK);
} }
@ApiOperation(value = "Delete an experiment", response = boolean.class) @ApiOperation(value = "Delete an experiment", response = boolean.class)
@RequestMapping(value = "/{uuid}", method = RequestMethod.DELETE) @RequestMapping(value = "/{uuid}", method = RequestMethod.DELETE)
public ResponseEntity<String> deleteExperiment( public ResponseEntity<String> deleteExperiment(
@ApiParam(value = "uuid", required = true) @PathVariable("uuid") String uuid) { @ApiParam(value = "uuid", required = true) @PathVariable("uuid") String uuid) {
return experimentService.deleteExperiment(uuid, "(DELETE) /experiments/{uuid}"); experimentService.deleteExperiment(uuid, "(DELETE) /experiments/{uuid}");
return new ResponseEntity<>(HttpStatus.OK);
} }
} }
\ No newline at end of file
...@@ -10,6 +10,7 @@ import eu.hbp.mip.model.DTOs.PathologyDTO; ...@@ -10,6 +10,7 @@ import eu.hbp.mip.model.DTOs.PathologyDTO;
import eu.hbp.mip.model.UserInfo; import eu.hbp.mip.model.UserInfo;
import eu.hbp.mip.utils.ClaimUtils; import eu.hbp.mip.utils.ClaimUtils;
import eu.hbp.mip.utils.CustomResourceLoader; import eu.hbp.mip.utils.CustomResourceLoader;
import eu.hbp.mip.utils.Exceptions.BadRequestException;
import eu.hbp.mip.utils.InputStreamConverter; import eu.hbp.mip.utils.InputStreamConverter;
import eu.hbp.mip.utils.Logging; import eu.hbp.mip.utils.Logging;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
...@@ -56,7 +57,7 @@ public class PathologiesApi { ...@@ -56,7 +57,7 @@ public class PathologiesApi {
}.getType()); }.getType());
} catch (IOException e) { } catch (IOException e) {
Logging.LogUserAction(userInfo.getUser().getUsername(), "(GET) /pathologies", "Unable to load pathologies"); Logging.LogUserAction(userInfo.getUser().getUsername(), "(GET) /pathologies", "Unable to load pathologies");
return ResponseEntity.badRequest().body("The pathologies could not be loaded."); throw new BadRequestException("The pathologies could not be loaded.");
} }
// If authentication is disabled return everything // If authentication is disabled return everything
......
...@@ -120,7 +120,7 @@ public class ExperimentDAO { ...@@ -120,7 +120,7 @@ public class ExperimentDAO {
experimentDTO.setName(this.name); experimentDTO.setName(this.name);
experimentDTO.setResult(JsonConverters.convertJsonStringToObject(this.result, ExperimentDTO.ResultDTO.class)); experimentDTO.setResult(JsonConverters.convertJsonStringToObject(this.result, ExperimentDTO.ResultDTO.class));
experimentDTO.setShared(this.shared); experimentDTO.setShared(this.shared);
experimentDTO.setUuid(this.uuid.toString()); experimentDTO.setUuid(this.uuid);
experimentDTO.setViewed(this.viewed); experimentDTO.setViewed(this.viewed);
return experimentDTO; return experimentDTO;
} }
......
...@@ -4,10 +4,11 @@ import eu.hbp.mip.model.DAOs.ExperimentDAO; ...@@ -4,10 +4,11 @@ import eu.hbp.mip.model.DAOs.ExperimentDAO;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.UUID;
public class ExperimentDTO { public class ExperimentDTO {
private String uuid; private UUID uuid;
private String name; private String name;
private String createdBy; private String createdBy;
private Date created; private Date created;
...@@ -39,11 +40,11 @@ public class ExperimentDTO { ...@@ -39,11 +40,11 @@ public class ExperimentDTO {
this.algorithm = algorithmDetails.getName(); this.algorithm = algorithmDetails.getName();
} }
public String getUuid() { public UUID getUuid() {
return uuid; return uuid;
} }
public void setUuid(String uuid) { public void setUuid(UUID uuid) {
this.uuid = uuid; this.uuid = uuid;
} }
......
This diff is collapsed.
...@@ -16,7 +16,7 @@ public class ExperimentSpecifications { ...@@ -16,7 +16,7 @@ public class ExperimentSpecifications {
public ExperimentWithName(String name){ public ExperimentWithName(String name){
this.name = name; this.name = name;
this.regExp = "%"+name+"%"; this.regExp = name;
} }
public Predicate toPredicate(Root<ExperimentDAO> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) public Predicate toPredicate(Root<ExperimentDAO> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb)
...@@ -24,8 +24,11 @@ public class ExperimentSpecifications { ...@@ -24,8 +24,11 @@ public class ExperimentSpecifications {
if (name == null) { if (name == null) {
return cb.isTrue(cb.literal(true)); return cb.isTrue(cb.literal(true));
} }
else {
regExp = (name.contains("%")?name:name+"%");
}
return cb.like( root.get( "name" ), regExp ); return cb.like( root.get( "name" ), this.regExp );
} }
} }
public static class ExperimentWithAlgorithm implements Specification<ExperimentDAO> { public static class ExperimentWithAlgorithm implements Specification<ExperimentDAO> {
......
package eu.hbp.mip.utils; package eu.hbp.mip.utils;
import eu.hbp.mip.utils.Exceptions.*;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest; import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import java.util.Date; import java.util.Date;
@ControllerAdvice @ControllerAdvice
public class ControllerExceptionHandler { public class ControllerExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(ExperimentNotFoundException.class)
public ResponseEntity<Object> handleExperimentNotFoundException(ExperimentNotFoundException ex, WebRequest request) {
@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<ErrorMessage> resourceNotFoundException(ResourceNotFoundException ex, WebRequest request) {
ErrorMessage message = new ErrorMessage( ErrorMessage message = new ErrorMessage(
2, HttpStatus.NOT_FOUND.value(),
new Date(), new Date(),
ex.getMessage(), ex.getMessage(),
request.getDescription(false)); request.getDescription(false));
return new ResponseEntity<>(message, HttpStatus.NOT_FOUND); return new ResponseEntity<>(message, HttpStatus.NOT_FOUND);
} }
@ExceptionHandler(BadRequestException.class)
public ResponseEntity<Object> handleBadRequestException(BadRequestException ex, WebRequest request) {
ErrorMessage message = new ErrorMessage(
HttpStatus.BAD_REQUEST.value(),
new Date(),
ex.getMessage(),
request.getDescription(false));
return new ResponseEntity<>(message, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(UnauthorizedException.class)
public ResponseEntity<Object> handleUnauthorizedException(UnauthorizedException ex, WebRequest request) {
ErrorMessage message = new ErrorMessage(
HttpStatus.UNAUTHORIZED.value(),
new Date(),
ex.getMessage(),
request.getDescription(false));
return new ResponseEntity<>(message, HttpStatus.UNAUTHORIZED);
}
@ExceptionHandler(IllegalArgumentException.class)
public Object handleIllegalArgumentException(IllegalArgumentException ex, WebRequest request) {
return null;
}
@ExceptionHandler(InternalServerError.class)
public ResponseEntity<ErrorMessage> handleInternalServerError(InternalServerError er, WebRequest request) {
ErrorMessage message = new ErrorMessage(
HttpStatus.INTERNAL_SERVER_ERROR.value(),
new Date(),
er.getMessage(),
request.getDescription(false));
return new ResponseEntity<>(message, HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler(NoContent.class)
public ResponseEntity<ErrorMessage> handleNoContent(NoContent nc, WebRequest request) {
ErrorMessage message = new ErrorMessage(
HttpStatus.NO_CONTENT.value(),
new Date(),
nc.getMessage(),
request.getDescription(false));
return new ResponseEntity<>(message, HttpStatus.NO_CONTENT);
}
@ExceptionHandler(Exception.class) @ExceptionHandler(Exception.class)
public ResponseEntity<ErrorMessage> globalExceptionHandler(Exception ex, WebRequest request) { public ResponseEntity<ErrorMessage> globalExceptionHandler(Exception ex, WebRequest request) {
......
package eu.hbp.mip.utils.Exceptions;
public class BadRequestException extends RuntimeException {
public BadRequestException(String msg) {
super(msg);
}
}
\ No newline at end of file
package eu.hbp.mip.utils.Exceptions;
public class ExperimentNotFoundException extends RuntimeException {
public ExperimentNotFoundException(String msg) {
super(msg);
}
}
\ No newline at end of file
package eu.hbp.mip.utils.Exceptions;
public class InternalServerError extends RuntimeException {
public InternalServerError(String msg) {
super(msg);
}
}
\ No newline at end of file
package eu.hbp.mip.utils.Exceptions;
public class NoContent extends RuntimeException {
public NoContent(String msg) {
super(msg);
}
}
\ No newline at end of file
package eu.hbp.mip.utils.Exceptions;
public class UnauthorizedException extends RuntimeException {
public UnauthorizedException(String msg) {
super(msg);
}
}
\ No newline at end of file
package eu.hbp.mip.utils;
public class ResourceNotFoundException extends RuntimeException {
private static final long serialVersionUID = 1L;
public ResourceNotFoundException(String msg) {
super(msg);
}
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment