Skip to content

Commit

Permalink
Issue #130 static content exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jyotsnaraveendran committed Apr 2, 2018
1 parent 909c6a4 commit 6ba5573
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
}
}*/

}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -74,7 +75,7 @@ public ObjectMapper objectMapper() {
public Gson gson(){
return new Gson();
}

@Bean
public RdfToJsonldConverter rdfToJsonldConverter(){
return new RdfToJsonldConverter();
Expand Down Expand Up @@ -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()))
Expand All @@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
public class RegistryController {

private static Logger logger = LoggerFactory.getLogger(RegistryController.class);

@Autowired
private RegistryService registryService;

Expand Down Expand Up @@ -142,4 +142,5 @@ public ResponseEntity<Response> health() {
return new ResponseEntity<>(response, HttpStatus.OK);
}


}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit 6ba5573

Please sign in to comment.