I am running a Tomcat 7 server for a past year or so, with multiple jersey projects on it.
One of the API projects which used to work fine before is taking an usually long time to serve the pre-flight "OPTIONS" request. It does respond, but it has a very random behaviour, sometimes it takes 5 - 10 seconds, others it serves it in milliseconds.
Problem shown in Chrome Dev tools - 1
Problem shown in Chrome Dev tools - 2
The server receives request after a a very long time (5 - 10 seconds) to serve the OPTIONS request (verified by adding a debug pointer to the first piece of code that is received by the server.). The issue is that it blocks the entire server, and the server is unable to process any additional request till the preflight is cleared.
Note: This problem doesn't occur if the request made from POSTMAN, only external and cross origin tools cause this issue.
Things I have tried:
- Deleting all maven dependencies.
- Cleaning projects, re-importing them in IDE, recloing repo.
- Ensuring there is no deadlock or infinite loop.
Filter code:
@PreMatching
@Priority(value = 1)
@Provider
public class EnterpriseAPIRequestFilter implements ContainerRequestFilter {
@Context
private HttpServletRequest sr;
private Logger logger = Logger.getLogger(EnterpriseAPIRequestFilter.class);
public void filter(ContainerRequestContext ctx) throws IOException {
logger.info("Request Received for :" + sr.getPathInfo());
logger.info("Remote IP :" + sr.getRemoteAddr());
logger.info("Remote Host :" + sr.getLocalAddr());
Authenticator authenticator = new Authenticator();
MultivaluedMap<String, String> headers = ctx.getHeaders();
String role = headers.getFirst("channelRole");
String locale = headers.getFirst("locale");
if (sr.getMethod().equalsIgnoreCase("OPTIONS"))
return;
//MORE CODE EXISTS HERE.
}
@Provider
public class CORSResponseFilter
implements ContainerResponseFilter {
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)throws IOException {
MultivaluedMap<String, Object> headers = responseContext.getHeaders();
headers.add("Access-Control-Allow-Origin", "*");
headers.add("Access-Control-Allow-Methods", "OPTIONS, GET, POST, DELETE, PUT");
headers.add("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, X-Codingpedia, channelRole, channelauthkey,channeltype,channelname,channelidentifier,orgid,id,channelOrgId,key,employeeId,subRole");
}
}