agave-jobs/jobs-api/src/main/java/org/iplantc/service/jobs/resources/JobMacroCallbackResource.java
/**
	 * This method represents the HTTP GET action. It provides a simple,
	 * convenient way for jobs to update their own status information while
	 * running. If the job id, token, or status included in the URL are invalid
	 * or the form data is invalid, a HTTP
	 * {@link org.restlet.data.Status#CLIENT_ERROR_BAD_REQUEST 400} code is
	 * sent. If a problem occurs looking up the job a HTTP
	 * {@link org.restlet.data.Status#SERVER_ERROR_INTERNAL 500} code is sent.
	 */
This method represents the HTTP GET action. It provides a simple,
convenient way for jobs to update their own status information while
running. If the job id, token, or status included in the URL are invalid
or the form data is invalid, a HTTP
org.restlet.data.Status#CLIENT_ERROR_BAD_REQUEST 400 code is
sent. If a problem occurs looking up the job a HTTP
org.restlet.data.Status#SERVER_ERROR_INTERNAL 500 code is sent.
This method represents the HTTP GET action . It provides a simple convenient way for jobs to update their own status information while running . If the job id token or status included in the URL are invalid or the form data is invalid a HTTP org . restlet . data . Status#CLIENT_ERROR_BAD_REQUEST 400 code is sent . If a problem occurs looking up the job a HTTP org . restlet . data . Status#SERVER_ERROR_INTERNAL 500 code is sent .
@Override
	public Representation represent(Variant variant) throws ResourceException
	{
		try {
			if (!StringUtils.isEmpty(jobUuid)) {
				getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
				return new IplantErrorRepresentation("Invalid job id.");
			}
			if (!StringUtils.isEmpty(token)) {
				getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
				return new IplantErrorRepresentation("Invalid token.");
			}
			JobMacroType macro;
			try {
				macro = JobMacroType.valueOf(sMacro.toUpperCase());
				if (macro == null) {
					getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
					return new IplantErrorRepresentation("Invalid callback macro value " + sMacro);
				}
			} catch (Exception e) {
				getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
				return new IplantErrorRepresentation("Invalid callback macro value " + sMacro);
			}
			try {
				Job job = JobDao.getByUuid(jobUuid);
				if (job == null) {
					String msg = "No job found with job id " + jobUuid + ".";
					log.error(msg);
					throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, msg);
				} else if (job.getUpdateToken().equals(token)) {
					TenancyHelper.setCurrentEndUser(job.getOwner());
					TenancyHelper.setCurrentTenantId(job.getTenantId());
					if (!job.isRunning()) {
						throw new ResourceException(
								Status.CLIENT_ERROR_BAD_REQUEST, "Job " + jobUuid + " is not running.");
					}
					job.setLastUpdated(new Date());
					job.addEvent(new JobEvent(macro.name(), macro.getDescription(), job.getOwner()));
					JobDao.persist(job);
					getResponse().setStatus(Status.SUCCESS_ACCEPTED);
					return new IplantSuccessRepresentation();
				} else {
					throw new ResourceException(Status.CLIENT_ERROR_UNAUTHORIZED,
							"Invalid job id or key");
				}
			} catch (ResourceException e) {
				log.debug("Callback failed: " + e.getMessage());
				getResponse().setStatus(e.getStatus());
				return new IplantErrorRepresentation(e.getMessage());
			} catch (Exception e) {
				log.error("Failed to process callback macro " + sMacro + " for job " + jobUuid, e);
				return new IplantErrorRepresentation("Failed to process callback macro " + sMacro + " for job " + jobUuid);
			}
		}
		finally {
			try { HibernateUtil.closeSession(); } catch (Throwable ignored) {}
		}
	}
@ Override public Representation represent ( Variant variant ) throws ResourceException { try { if ( ! StringUtils . isEmpty ( jobUuid ) ) { getResponse ( ) . setStatus ( Status . CLIENT_ERROR_BAD_REQUEST ) ; return new IplantErrorRepresentation ( "Invalid job id." ) ; } if ( ! StringUtils . isEmpty ( token ) ) { getResponse ( ) . setStatus ( Status . CLIENT_ERROR_BAD_REQUEST ) ; return new IplantErrorRepresentation ( "Invalid token." ) ; } JobMacroType macro ; try { macro = JobMacroType . valueOf ( sMacro . toUpperCase ( ) ) ; if ( macro == null ) { getResponse ( ) . setStatus ( Status . CLIENT_ERROR_BAD_REQUEST ) ; return new IplantErrorRepresentation ( "Invalid callback macro value " + sMacro ) ; } } catch ( Exception e ) { getResponse ( ) . setStatus ( Status . CLIENT_ERROR_BAD_REQUEST ) ; return new IplantErrorRepresentation ( "Invalid callback macro value " + sMacro ) ; } try { Job job = JobDao . getByUuid ( jobUuid ) ; if ( job == null ) { String msg = "No job found with job id " + jobUuid + "." ; log . error ( msg ) ; throw new ResourceException ( Status . CLIENT_ERROR_NOT_FOUND , msg ) ; } else if ( job . getUpdateToken ( ) . equals ( token ) ) { TenancyHelper . setCurrentEndUser ( job . getOwner ( ) ) ; TenancyHelper . setCurrentTenantId ( job . getTenantId ( ) ) ; if ( ! job . isRunning ( ) ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "Job " + jobUuid + " is not running." ) ; } job . setLastUpdated ( new Date ( ) ) ; job . addEvent ( new JobEvent ( macro . name ( ) , macro . getDescription ( ) , job . getOwner ( ) ) ) ; JobDao . persist ( job ) ; getResponse ( ) . setStatus ( Status . SUCCESS_ACCEPTED ) ; return new IplantSuccessRepresentation ( ) ; } else { throw new ResourceException ( Status . CLIENT_ERROR_UNAUTHORIZED , "Invalid job id or key" ) ; } } catch ( ResourceException e ) { log . debug ( "Callback failed: " + e . getMessage ( ) ) ; getResponse ( ) . setStatus ( e . getStatus ( ) ) ; return new IplantErrorRepresentation ( e . getMessage ( ) ) ; } catch ( Exception e ) { log . error ( "Failed to process callback macro " + sMacro + " for job " + jobUuid , e ) ; return new IplantErrorRepresentation ( "Failed to process callback macro " + sMacro + " for job " + jobUuid ) ; } } finally { try { HibernateUtil . closeSession ( ) ; } catch ( Throwable ignored ) { } } }
This method represents the HTTP GET action.
This method represents the HTTP GET action .
//		Long jobId = null; // update the job status if the tokens match // can't set a stopped job back to running. Bad request //				// the HEARTBEAT status is used to update the job timestamp and is used //				// by app developers just to keep aware of a job being alive. //				if (macro.equals(JobMacroType.HEARTBEAT))  //				{ //					// nothing to do. we update the job.lastUpdated timestamp on every macro call //				} //				else if (macro.equals(JobMacroType.NOTIFICATION))  //				{ // notification will be sent on status update //					NotificationManager.process(job.getUuid(), job.getStatus().name(), job.getOwner()); //				}  // can't set a stopped job back to running. Bad request