From 6ba55732dbae62a07ea1d631faceee13ad566bce Mon Sep 17 00:00:00 2001 From: jyotsna-tarento Date: Mon, 2 Apr 2018 11:46:53 +0530 Subject: [PATCH] Issue #130 static content exception handling --- .../registry/middleware/util/Constants.java | 19 +-------- .../registry/config/GenericConfiguration.java | 19 +++++++-- .../registry/controller/BaseController.java | 25 ++++++++++++ .../controller/RegistryController.java | 3 +- .../registry/exception/CustomException.java | 14 +++++++ .../exception/CustomExceptionHandler.java | 40 +++++++++++++++++++ .../1.0/vocab.jsonld.sample} | 0 7 files changed, 97 insertions(+), 23 deletions(-) create mode 100644 java/registry/src/main/java/io/opensaber/registry/controller/BaseController.java create mode 100644 java/registry/src/main/java/io/opensaber/registry/exception/CustomException.java create mode 100644 java/registry/src/main/java/io/opensaber/registry/exception/CustomExceptionHandler.java rename java/registry/src/main/resources/{vocabulary/vocabulary.owl.sample => vocab/1.0/vocab.jsonld.sample} (100%) diff --git a/java/middleware-commons/src/main/java/io/opensaber/registry/middleware/util/Constants.java b/java/middleware-commons/src/main/java/io/opensaber/registry/middleware/util/Constants.java index ff2ffbfec..efa4656df 100644 --- a/java/middleware-commons/src/main/java/io/opensaber/registry/middleware/util/Constants.java +++ b/java/middleware-commons/src/main/java/io/opensaber/registry/middleware/util/Constants.java @@ -36,6 +36,7 @@ public class Constants { public static final String JSONLD_PARSE_ERROR = "Unable to parse JSON-LD"; public static final String RDF_VALIDATION_ERROR = "Unable to validate RDF"; public static final String RDF_VALIDATION_MAPPING_ERROR = "Unable to map validations"; + public static final String CUSTOM_EXCEPTION_ERROR = "Something went wrong!! Please try again later"; public static final String OPENSABER_REGISTRY_API_NAME = "opensaber-registry-api"; public static final String SUNBIRD_ENCRYPTION_SERVICE_NAME = "sunbird.encryption.service"; @@ -57,23 +58,5 @@ public String getName() { return name; } } - - /*public enum ValidationMapper { - SCHOOL("school."), - ADDRESS("address."), - TEACHER("teacher."), - TEACHER_ROLE("teacher.role."); - - private String name; - - private ValidationMapper(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - }*/ } diff --git a/java/registry/src/main/java/io/opensaber/registry/config/GenericConfiguration.java b/java/registry/src/main/java/io/opensaber/registry/config/GenericConfiguration.java index d9b36cb81..af79272df 100644 --- a/java/registry/src/main/java/io/opensaber/registry/config/GenericConfiguration.java +++ b/java/registry/src/main/java/io/opensaber/registry/config/GenericConfiguration.java @@ -1,7 +1,6 @@ package io.opensaber.registry.config; import java.io.IOException; -import java.net.URISyntaxException; import java.util.HashMap; import java.util.Map; @@ -28,6 +27,7 @@ import com.google.gson.Gson; import io.opensaber.registry.authorization.AuthorizationFilter; +import io.opensaber.registry.exception.CustomExceptionHandler; import io.opensaber.registry.interceptor.AuthorizationInterceptor; import io.opensaber.registry.interceptor.RDFConversionInterceptor; import io.opensaber.registry.interceptor.RDFValidationInterceptor; @@ -40,6 +40,7 @@ import io.opensaber.registry.schema.config.SchemaConfigurator; import org.springframework.web.client.RestTemplate; +import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @@ -74,7 +75,7 @@ public ObjectMapper objectMapper() { public Gson gson(){ return new Gson(); } - + @Bean public RdfToJsonldConverter rdfToJsonldConverter(){ return new RdfToJsonldConverter(); @@ -146,7 +147,7 @@ public RDFValidationMapper rdfValidationMapper(){ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new AuthorizationInterceptor(authorizationFilter(), gson())) - .addPathPatterns("/**").excludePathPatterns("/health").order(1); + .addPathPatterns("/**").excludePathPatterns("/health","/error").order(1); registry.addInterceptor(new RDFConversionInterceptor(rdfConverter(), gson())) .addPathPatterns("/create", "/update/{id}").order(2); registry.addInterceptor(new RDFValidationMappingInterceptor(rdfValidationMapper(), gson())) @@ -157,11 +158,21 @@ public void addInterceptors(InterceptorRegistry registry) { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { + try{ registry.addResourceHandler("/resources/**") - .addResourceLocations("classpath:/BOOT-INF/classes/vocabulary/") + .addResourceLocations("classpath:vocab/1.0/") .setCachePeriod(3600) .resourceChain(true) .addResolver(new PathResourceResolver()); + }catch(Exception e){ + throw e; + } + } + @Bean + public HandlerExceptionResolver customExceptionHandler () { + return new CustomExceptionHandler(gson()); + } + } diff --git a/java/registry/src/main/java/io/opensaber/registry/controller/BaseController.java b/java/registry/src/main/java/io/opensaber/registry/controller/BaseController.java new file mode 100644 index 000000000..e6d023f63 --- /dev/null +++ b/java/registry/src/main/java/io/opensaber/registry/controller/BaseController.java @@ -0,0 +1,25 @@ +package io.opensaber.registry.controller; + +import org.springframework.boot.web.servlet.error.ErrorController; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import io.opensaber.registry.exception.CustomException; +import io.opensaber.registry.middleware.util.Constants; + +@RestController +public class BaseController implements ErrorController { + + private static final String PATH = "/error"; + + @RequestMapping(value = PATH) + public String error() throws Exception { + throw new CustomException(Constants.CUSTOM_EXCEPTION_ERROR); + } + + @Override + public String getErrorPath() { + return PATH; + } + +} diff --git a/java/registry/src/main/java/io/opensaber/registry/controller/RegistryController.java b/java/registry/src/main/java/io/opensaber/registry/controller/RegistryController.java index a1b2bd7d4..4e761eba8 100644 --- a/java/registry/src/main/java/io/opensaber/registry/controller/RegistryController.java +++ b/java/registry/src/main/java/io/opensaber/registry/controller/RegistryController.java @@ -33,7 +33,7 @@ public class RegistryController { private static Logger logger = LoggerFactory.getLogger(RegistryController.class); - + @Autowired private RegistryService registryService; @@ -142,4 +142,5 @@ public ResponseEntity health() { return new ResponseEntity<>(response, HttpStatus.OK); } + } diff --git a/java/registry/src/main/java/io/opensaber/registry/exception/CustomException.java b/java/registry/src/main/java/io/opensaber/registry/exception/CustomException.java new file mode 100644 index 000000000..1880b1dc8 --- /dev/null +++ b/java/registry/src/main/java/io/opensaber/registry/exception/CustomException.java @@ -0,0 +1,14 @@ +package io.opensaber.registry.exception; + +public class CustomException extends Exception { + + /** + * + */ + private static final long serialVersionUID = -4024691757405373470L; + + public CustomException(String message){ + super(message); + } + +} diff --git a/java/registry/src/main/java/io/opensaber/registry/exception/CustomExceptionHandler.java b/java/registry/src/main/java/io/opensaber/registry/exception/CustomExceptionHandler.java new file mode 100644 index 000000000..41bbcf5f1 --- /dev/null +++ b/java/registry/src/main/java/io/opensaber/registry/exception/CustomExceptionHandler.java @@ -0,0 +1,40 @@ +package io.opensaber.registry.exception; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.servlet.HandlerExceptionResolver; +import org.springframework.web.servlet.ModelAndView; + +import com.google.gson.Gson; + +import io.opensaber.registry.interceptor.handler.BaseResponseHandler; +import io.opensaber.registry.middleware.util.Constants; + +public class CustomExceptionHandler extends BaseResponseHandler implements HandlerExceptionResolver { + + private static Logger logger = LoggerFactory.getLogger(CustomExceptionHandler.class); + + private Gson gson; + + public CustomExceptionHandler(Gson gson){ + this.gson = gson; + } + + @Override + public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, + Exception ex) { + try{ + logger.info("Exception thrown-"+ex.getMessage()); + setResponse(response); + writeResponseObj(gson, Constants.CUSTOM_EXCEPTION_ERROR); + response = getResponse(); + }catch(Exception e){ + logger.error("Error in sending response"); + } + return null; + } + +} diff --git a/java/registry/src/main/resources/vocabulary/vocabulary.owl.sample b/java/registry/src/main/resources/vocab/1.0/vocab.jsonld.sample similarity index 100% rename from java/registry/src/main/resources/vocabulary/vocabulary.owl.sample rename to java/registry/src/main/resources/vocab/1.0/vocab.jsonld.sample