/** * {@inheritDoc} * * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, * javax.servlet.FilterChain) */ @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; HttpServletResponse httpResponse = (HttpServletResponse) response; try { setCurrentJob(httpRequest, httpResponse); chain.doFilter(httpRequest, httpResponse); } finally { serviceRegistry.setCurrentJob(null); } }
/** * Sets the current job on the new thread * * @param httpRequest * the HTTP request * @param httpResponse * the HTTP response * @throws IOException * if the error response was not able to be sent */ private void setCurrentJob(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException { String currentJobId = httpRequest.getHeader(CURRENT_JOB_HEADER); try { if (StringUtils.isNotBlank(currentJobId)) { Job currentJob = serviceRegistry.getJob(Long.parseLong(currentJobId)); serviceRegistry.setCurrentJob(currentJob); } } catch (Exception e) { logger.error("Unable to set the current job {}: {}", currentJobId, e); httpResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Was not able to set the current job id {} to the service registry" + currentJobId); } }
@Override public Void call() throws Exception { final SecurityService securityService = getSecurityService(); final ServiceRegistry serviceRegistry = getServiceRegistry(); final Job jobBeforeProcessing = serviceRegistry.getJob(jobId); if (currentJobId.isSome()) serviceRegistry.setCurrentJob(serviceRegistry.getJob(currentJobId.get())); final Organization organization = getOrganizationDirectoryService() .getOrganization(jobBeforeProcessing.getOrganization()); securityService.setOrganization(organization); final User user = getUserDirectoryService().loadUser(jobBeforeProcessing.getCreator()); securityService.setUser(user); try { final String payload = process(jobBeforeProcessing); handleSuccessfulProcessing(payload); } catch (Throwable t) { handleFailedProcessing(t); } finally { serviceRegistry.setCurrentJob(null); securityService.setUser(null); securityService.setOrganization(null); } return null; }