Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add try/catch, enhance error nmessages and remove unneeded dependencies. #43

Merged
merged 3 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 24 additions & 58 deletions src/main/java/com/deloitte/elrr/ElrrApplication.java
Original file line number Diff line number Diff line change
@@ -1,76 +1,42 @@
package com.deloitte.elrr;

import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.web.client.RestTemplate;

import com.deloitte.elrr.repository.CompetencyRepository;
import com.deloitte.elrr.repository.CourseRepository;
import com.deloitte.elrr.repository.EmploymentRepository;
import com.deloitte.elrr.repository.OrganizationRepository;
import com.deloitte.elrr.repository.PersonalRepository;
import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;

@SpringBootApplication
@Import({ JacksonAutoConfiguration.class })
@Import({JacksonAutoConfiguration.class})
@EnableEncryptableProperties
public class ElrrApplication {
public class ElrrApplication {

/**
*
* @param args
*/
public static void main(final String[] args) {
SpringApplication.run(ElrrApplication.class, args);
}
/**
*
*/
@Autowired
private CourseRepository courseRepository;
/**
*
*/
@Autowired
private CompetencyRepository competencyListRepository;
/**
*
*/
@Autowired
private PersonalRepository personalRepository;
/**
*
*/
@Autowired
private EmploymentRepository employmentRepository;
/**
*
*/
@Autowired
private OrganizationRepository organizationRepository;
/**
* @param args
*/
public static void main(final String[] args) {
SpringApplication.run(ElrrApplication.class, args);
}

/**
*
* @return RestTemplate
*/
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
/**
*
* @return ModelMapper
*/
@Bean
public ModelMapper modelMapper() {
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setSkipNullEnabled(true);
return modelMapper;
}
/**
* @return RestTemplate
*/
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}

/**
* @return ModelMapper
*/
@Bean
public ModelMapper modelMapper() {
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setSkipNullEnabled(true);
return modelMapper;
}
}
22 changes: 15 additions & 7 deletions src/main/java/com/deloitte/elrr/HeaderFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

// import com.deloitte.elrr.elrrconsolidate.HeaderFilter;

import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
Expand All @@ -27,16 +29,22 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha

HttpServletRequest httpServletRequest = (HttpServletRequest) request;

if (checkHttpHeader == false) {
chain.doFilter(request, response);
} else {
if ("https".equalsIgnoreCase(httpServletRequest.getHeader("X-Forwarded-Proto"))) {
try {
if (checkHttpHeader == false) {
chain.doFilter(request, response);
} else {
log.error("Not a HTTPS request.");
((HttpServletResponse) response)
.sendError(HttpServletResponse.SC_BAD_REQUEST, "Not a HTTPS request.");
if ("https".equalsIgnoreCase(httpServletRequest.getHeader("X-Forwarded-Proto"))) {
chain.doFilter(request, response);
} else {
log.error("Not a HTTPS request.");
((HttpServletResponse) response)
.sendError(HttpServletResponse.SC_BAD_REQUEST, "Not a HTTPS request.");
}
}
} catch (IOException | ServletException e) {
log.error("Error: " + e.getMessage());
e.printStackTrace();
return;
}
}
}
12 changes: 6 additions & 6 deletions src/main/java/com/deloitte/elrr/InputSanatizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

public class InputSanatizer {

private static final String CHAR_WHITE_LIST_REGEX = "^[\\x09\\x0A\\x0D\\x20-\\x7E | \\xC2-\\xDF | \\xE0\\xA0-\\xBF | [\\xE1-\\xEC\\xEE\\xEF]{2} | \\xED\\x80-\\x9F | [\\xF0\\\\x90-\\xBF]{2} | [\\xF1-\\xF3]{3} | [\\xF4\\x80-\\x8F]{2}]*$";
public static boolean isValidInput(String input) {
return GenericValidator.matchRegexp(input, CHAR_WHITE_LIST_REGEX);
}
private static final String CHAR_WHITE_LIST_REGEX =
"^[\\x09\\x0A\\x0D\\x20-\\x7E | \\xC2-\\xDF | \\xE0\\xA0-\\xBF | [\\xE1-\\xEC\\xEE\\xEF]{2} | \\xED\\x80-\\x9F | [\\xF0\\\\x90-\\xBF]{2} | [\\xF1-\\xF3]{3} | [\\xF4\\x80-\\x8F]{2}]*$";

public static boolean isValidInput(String input) {
return GenericValidator.matchRegexp(input, CHAR_WHITE_LIST_REGEX);
}
}
24 changes: 16 additions & 8 deletions src/main/java/com/deloitte/elrr/JSONRequestSizeLimitFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

// import com.deloitte.elrr.elrrconsolidate.JSONRequestSizeLimitFilter;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class JSONRequestSizeLimitFilter extends OncePerRequestFilter {

@Value("${json.max.size.limit}")
private long maxSizeLimit;

private long MAX_SIZE_LIMIT = maxSizeLimit;
private long MAX_SIZE_LIMIT;

@Value("${check.media.type.json}")
private boolean checkMediaTypeJson;
Expand All @@ -28,11 +30,17 @@ public class JSONRequestSizeLimitFilter extends OncePerRequestFilter {
protected void doFilterInternal(
HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
if (isApplicationJson(request) && request.getContentLengthLong() < MAX_SIZE_LIMIT) {
filterChain.doFilter(request, response);
} else {
((HttpServletResponse) response)
.sendError(HttpServletResponse.SC_BAD_REQUEST, "Request size exceeds the limit.");
try {
if (isApplicationJson(request) && request.getContentLengthLong() < MAX_SIZE_LIMIT) {
filterChain.doFilter(request, response);
} else {
log.error("Request size exceeds the limit.");
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Request size exceeds the limit.");
}
} catch (IOException | ServletException e) {
log.error("Error: " + e.getMessage());
e.printStackTrace();
return;
}
}

Expand Down
Loading
Loading