public boolean isFirstCallToSetupMC(ContainerRequest request) { MissionControlProperties missionControlProperties = ContextHelper.get().beanForType(MissionControlProperties.class); String token = missionControlProperties.getToken(); String url = missionControlProperties.getUrl(); if ((isBlank(url) || isBlank(token)) && request.getPath().endsWith("setupmc")) { return true; } return false; }
@Override public ContainerRequest filter(ContainerRequest request) { String path = request.getPath(); log.info("Filtering request path: " + path); // IMPORTANT!!! First, Acknowledge any pre-flight test from browsers for // this case before validating the headers (CORS stuff) if (request.getMethod().equals("OPTIONS")) { log.info("en Options?"); ResponseBuilder builder = null; String response = "OK"; builder = Response.status(Response.Status.OK).entity(response); throw new WebApplicationException(builder.build()); } // Then check is the service key exists and is valid. Authenticator demoAuthenticator = Authenticator.getInstance(); String serviceKey = request.getHeaderValue(HttpHeaderNames.SERVICE_KEY); if (!demoAuthenticator.isServiceKeyValid(serviceKey)) { ResponseBuilder builder = null; String response = "Invalid Service Key"; builder = Response.status(Response.Status.UNAUTHORIZED).entity(response); throw new WebApplicationException(builder.build()); } // For any pther methods besides login, the authToken must be verified if (!path.startsWith("auth/login")) { String authToken = request.getHeaderValue(HttpHeaderNames.AUTH_TOKEN); // if it isn't valid, just kick them out. if (!demoAuthenticator.isAuthTokenValid(serviceKey, authToken)) { ResponseBuilder builder = null; String response = "Authentication is need"; builder = Response.status(Response.Status.UNAUTHORIZED).entity(response); throw new WebApplicationException(builder.build()); } } // read(request); return request; }
@Override public ContainerResponse filter(ContainerRequest req, ContainerResponse contResp) { LOGGER.info("Enter CORS filter"); LOGGER.info("Request= { path:" + req.getPath() + ", method:" + req.getMethod() + " }"); ResponseBuilder resp = Response.fromResponse(contResp.getResponse()); resp.header("Access-Control-Allow-Origin", "*"); resp.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); String reqHead = req.getHeaderValue("Access-Control-Request-Headers"); if (null != reqHead && !reqHead.equals(null)) { resp.header("Access-Control-Allow-Headers", reqHead); } contResp.setResponse(resp.build()); LOGGER.info("Exit CORS filter"); return contResp; }