Search in sources :

Example 1 with JobDataWriteListener

use of org.haiku.haikudepotserver.support.web.JobDataWriteListener in project haikudepotserver by haiku.

the class JobController method downloadGeneratedData.

/**
 * <p>This URL can be used to download job data that has resulted from a job being run.</p>
 */
@RequestMapping(value = "/" + SEGMENT_JOBDATA + "/{" + KEY_GUID + "}/" + SEGMENT_DOWNLOAD, method = RequestMethod.GET)
public void downloadGeneratedData(HttpServletRequest request, HttpServletResponse response, @PathVariable(value = KEY_GUID) String guid) throws IOException {
    Preconditions.checkArgument(PATTERN_GUID.matcher(guid).matches(), "the supplied guid does not match the required pattern");
    ObjectContext context = serverRuntime.newContext();
    JobSnapshot job = jobService.tryGetJobForData(guid).orElseThrow(() -> {
        LOGGER.warn("attempt to access job data {} for which no job exists", guid);
        return new JobDataAuthorizationFailure();
    });
    if (!Strings.isNullOrEmpty(job.getOwnerUserNickname())) {
        User user = tryObtainAuthenticatedUser(context).orElseThrow(() -> {
            LOGGER.warn("attempt to obtain job data {} with no authenticated user", guid);
            return new JobDataAuthorizationFailure();
        });
        User ownerUser = User.tryGetByNickname(context, job.getOwnerUserNickname()).orElseThrow(() -> {
            LOGGER.warn("owner of job does not seem to exist; {}", job.getOwnerUserNickname());
            return new JobDataAuthorizationFailure();
        });
        if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), ownerUser, Permission.USER_VIEWJOBS)) {
            LOGGER.warn("attempt to access jobs view for; {}", job.toString());
            throw new JobDataAuthorizationFailure();
        }
    } else {
        LOGGER.debug("access to job [{}] allowed for unauthenticated access", job.toString());
    }
    JobDataWithByteSource jobDataWithByteSink = jobService.tryObtainData(guid).orElseThrow(() -> {
        LOGGER.warn("requested job data {} not found", guid);
        return new JobDataAuthorizationFailure();
    });
    // finally access has been checked and the logic can move onto actual
    // delivery of the material.
    JobData jobData = jobDataWithByteSink.getJobData();
    if (!Strings.isNullOrEmpty(jobData.getMediaTypeCode())) {
        response.setContentType(jobData.getMediaTypeCode());
    } else {
        response.setContentType(MediaType.OCTET_STREAM.toString());
    }
    response.setContentType(MediaType.CSV_UTF_8.toString());
    response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + jobService.deriveDataFilename(guid));
    response.setDateHeader(HttpHeaders.EXPIRES, 0);
    response.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache");
    // now switch to async for the delivery of the data.
    AsyncContext async = request.startAsync();
    async.setTimeout(TIMEOUT_DOWNLOAD_MILLIS);
    ServletOutputStream outputStream = response.getOutputStream();
    outputStream.setWriteListener(new JobDataWriteListener(guid, jobService, async, outputStream));
    LOGGER.info("did start async stream job data; {}", guid);
}
Also used : User(org.haiku.haikudepotserver.dataobjects.User) JobDataWriteListener(org.haiku.haikudepotserver.support.web.JobDataWriteListener) ServletOutputStream(javax.servlet.ServletOutputStream) AsyncContext(javax.servlet.AsyncContext) ObjectContext(org.apache.cayenne.ObjectContext)

Aggregations

AsyncContext (javax.servlet.AsyncContext)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 ObjectContext (org.apache.cayenne.ObjectContext)1 User (org.haiku.haikudepotserver.dataobjects.User)1 JobDataWriteListener (org.haiku.haikudepotserver.support.web.JobDataWriteListener)1