Commit f08018cb authored by gmantele's avatar gmantele
Browse files

[UWS] Add PHASE, AFTER and LAST filters on a JobList.

- PHASE: list only jobs in the specified PHASE. If this parameter is repeated
         jobs matching any of the specified phases will be returned.
- AFTER: list jobs created after the specified ISO-8601 date (included).
         If this parameter is repeated, only the most recent date is retained.
- LAST: list the N-th most recently created jobs, ordered by descending
        creation time

These filter parameters are additive: their constraints are joint as with an
AND operator (except for PHASE parameters ; see above).

If no filter is specified, all jobs EXCEPT the ARCHIVED ones are listed. The
only way to list ARCHIVED jobs is to use PHASE=ARCHIVED (with or without other
filter parameters).

The filtering API has been made in a generic manner so that it is easily
possible to create and add new filters. See the interface JobFilter and the
class JobListRefined for more details.
parent f3954c71
Loading
Loading
Loading
Loading
+46 −5
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import uws.job.JobList;
import uws.job.Result;
import uws.job.UWSJob;
import uws.job.jobInfo.JobInfo;
import uws.job.serializer.filter.JobListRefiner;
import uws.job.user.JobOwner;
import uws.service.UWS;
import uws.service.UWSUrl;
@@ -37,7 +38,7 @@ import uws.service.UWSUrl;
 * Useful conversion functions from UWS to JSON.
 *
 * @author Grégory Mantelet (CDS;ARI)
 * @version 4.3 (09/2017)
 * @version 4.3 (10/2017)
 */
public final class Json4Uws {

@@ -75,25 +76,65 @@ public final class Json4Uws {

	/**
	 * Gets the JSON representation of the given jobs list.
	 *
	 * @param jobsList	The jobs list to represent in JSON.
	 * @param owner				The user who asks to serialize the given jobs list. (MAY BE NULL)
	 * @param owner		The user who asks to serialize the given jobs list.
	 *             		(MAY BE NULL)
	 *
	 * @return	Its JSON representation.
	 * @throws JSONException	If there is an error while building the JSON object.
	 *
	 * @throws JSONException	If there is an error while building the JSON
	 *                      	object.
	 *
	 * @see #getJson(JobList, JobOwner, JobListRefiner)
	 */
	public final static JSONObject getJson(final JobList jobsList, final JobOwner owner) throws JSONException{
		return getJson(jobsList, owner, null);
	}

	/**
	 * Gets the JSON representation of the given jobs list by filtering by owner
	 * and some user-filters (e.g. on phase, creation time).
	 *
	 * @param jobsList		The jobs list to represent in JSON.
	 * @param owner			The user who asks to serialize the given jobs list.
	 *             			(MAY BE NULL)
	 * @param listRefiner	Represent all the specified job filters to apply ;
	 *                   	only the job that pass through this filter should be
	 *                   	displayed. If NULL, all jobs are displayed.
	 *
	 * @return	Its JSON representation.
	 *
	 * @throws JSONException	If there is an error while building the JSON
	 *                      	object.
	 *
	 * @since 4.3
	 */
	public final static JSONObject getJson(final JobList jobsList, final JobOwner owner, final JobListRefiner listRefiner) throws JSONException{
		JSONObject json = new JSONObject();
		if (jobsList != null){
			json.put("name", jobsList.getName());
			json.put("version", UWS.VERSION);
			JSONArray jsonJobs = new JSONArray();
			UWSUrl jobsListUrl = jobsList.getUrl();

			// Security filter: retrieve only the jobs of the specified owner:
			Iterator<UWSJob> it = jobsList.getJobs(owner);

			/* User filter: filter the jobs in function of filters specified by
			 * the user:  */
			if (listRefiner != null)
				it = listRefiner.refine(it);

			// Append the JSON serialization of all filtered jobs:
			JSONObject jsonObj = null;
			while(it.hasNext()){
				jsonObj = getJson(it.next(), jobsListUrl, true);
				if (jsonObj != null)
					jsonJobs.put(jsonObj);

			}

			json.put("jobs", jsonJobs);
		}
		return json;
+44 −1
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ package uws.job;
 *                       Astronomisches Rechen Institut (ARI)
 */

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
@@ -27,6 +28,8 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.ServletOutputStream;

import uws.UWSException;
import uws.UWSExceptionFactory;
import uws.UWSToolBox;
@@ -35,6 +38,7 @@ import uws.job.manager.DefaultExecutionManager;
import uws.job.manager.DestructionManager;
import uws.job.manager.ExecutionManager;
import uws.job.serializer.UWSSerializer;
import uws.job.serializer.filter.JobListRefiner;
import uws.job.user.JobOwner;
import uws.service.UWS;
import uws.service.UWSService;
@@ -171,7 +175,7 @@ import uws.service.log.UWSLog.LogLevel;
 * </i></p>
 *
 * @author Gr&eacute;gory Mantelet (CDS;ARI)
 * @version 4.3 (09/2017)
 * @version 4.3 (10/2017)
 *
 * @see UWSJob
 */
@@ -1000,6 +1004,45 @@ public class JobList extends SerializableUWSObject implements Iterable<UWSJob> {
		}
	}

	/**
	 * Serializes the while object in the given output stream,
	 * considering the given owner ID, the given job filters and thanks to the
	 * given serializer.
	 *
	 * @param output		The ouput stream in which this object must be
	 *              		serialized.
	 * @param serializer	The serializer to use.
	 * @param ownerId		The ID of the current ID.
	 * @param listRefiner	Special filter able to refine the list of jobs with
	 *                   	job filters specified by the user
	 *                   	(i.e. filter, sort and limit).
	 *
	 * @throws UWSException		If the owner is not allowed to see the content
	 *                     		of the serializable object.
	 * @throws IOException		If there is an error while writing in the given
	 *                    		stream.
	 * @throws Exception		If there is any other error during the
	 *                  		serialization.
	 *
	 * @see UWSSerializer#getJobList(JobList, JobOwner, JobListRefiner, boolean)
	 *
	 * @since 4.3
	 */
	public void serialize(ServletOutputStream output, UWSSerializer serializer, JobOwner owner, JobListRefiner listRefiner) throws UWSException, IOException, Exception{
		if (output == null)
			throw new NullPointerException("Missing serialization output stream!");

		if (owner != null && !owner.hasReadPermission(this))
			throw new UWSException(UWSException.PERMISSION_DENIED, UWSExceptionFactory.writePermissionDenied(owner, true, getName()));

		String serialization = serializer.getJobList(this, owner, listRefiner, true);
		if (serialization != null){
			output.print(serialization);
			output.flush();
		}else
			throw new UWSException(UWSException.INTERNAL_SERVER_ERROR, "Incorrect serialization value (=NULL) ! => impossible to serialize " + toString() + ".");
	}

	/* ***************** */
	/* INHERITED METHODS */
	/* ***************** */
+4 −3
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import uws.job.ErrorSummary;
import uws.job.JobList;
import uws.job.Result;
import uws.job.UWSJob;
import uws.job.serializer.filter.JobListRefiner;
import uws.job.user.JobOwner;
import uws.service.UWS;
import uws.service.UWSUrl;
@@ -36,7 +37,7 @@ import uws.service.UWSUrl;
 * Lets serializing any UWS resource in JSON.
 *
 * @author Gr&eacute;gory Mantelet (CDS;ARI)
 * @version 4.3 (09/2017)
 * @version 4.3 (10/2017)
 *
 * @see Json4Uws
 */
@@ -54,8 +55,8 @@ public class JSONSerializer extends UWSSerializer {
	}

	@Override
	public String getJobList(final JobList jobsList, final JobOwner owner, final boolean root) throws JSONException{
		return Json4Uws.getJson(jobsList, owner).toString();
	public String getJobList(final JobList jobsList, final JobOwner owner, final JobListRefiner listRefiner, final boolean root) throws JSONException{
		return Json4Uws.getJson(jobsList, owner, listRefiner).toString();
	}

	@Override
+42 −6
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import uws.job.ErrorSummary;
import uws.job.JobList;
import uws.job.Result;
import uws.job.UWSJob;
import uws.job.serializer.filter.JobListRefiner;
import uws.job.user.JobOwner;
import uws.service.UWS;
import uws.service.UWSUrl;
@@ -41,7 +42,7 @@ import uws.service.UWSUrl;
 * </ul>
 *
 * @author Gr&eacute;gory Mantelet (CDS;ARI)
 * @version 4.3 (09/2017)
 * @version 4.3 (10/2017)
 *
 * @see XMLSerializer
 * @see JSONSerializer
@@ -207,23 +208,58 @@ public abstract class UWSSerializer implements Serializable {
	 * @return				The serialization of the given jobs list.
	 *
	 * @throws Exception	If there is an error during the serialization.
	 *
	 * @see #getJobList(JobList, JobOwner, boolean)
	 */
	public String getJobList(final JobList jobsList, final boolean root) throws Exception{
		return getJobList(jobsList, null, root);
	}

	/**
	 * Serializes the given jobs list.
	 * Serializes the given jobs list, by filtering by user.
	 *
	 * @param jobsList		The jobs list to serialize.
	 * @param owner			The user which has asked the serialization of the given jobs list.
	 * @param root			<i>false</i> if the jobs list to serialize will be included
	 * 						in a top level serialization (for a jobs list: uws), <i>true</i> otherwise.
	 * @param owner			The user which has asked the serialization of the
	 *             			given jobs list. If NULL, all anonymous jobs are
	 *             			displayed.
	 * @param root			<code>false</code> if the jobs list to serialize
	 *            			will be included in a top level serialization (for a
	 *            			jobs list: uws),
	 *            			<code>true</code> otherwise.
	 *
	 * @return				The serialization of the given jobs list.
	 *
	 * @throws Exception	If there is an error during the serialization.
	 *
	 * @see #getJobList(JobList, JobOwner, JobListRefiner, boolean)
	 */
	public String getJobList(final JobList jobsList, JobOwner owner, final boolean root) throws Exception{
		return getJobList(jobsList, null, null, root);
	}

	/**
	 * Serializes the given jobs list, by filtering using user-specified
	 * filters.
	 *
	 * @param jobsList		The jobs list to serialize.
	 * @param owner			The user which has asked the serialization of the
	 *             			given jobs list. If NULL, all anonymous jobs are
	 *             			displayed.
	 * @param phaseFilters	Represent all the specified job filters to apply ;
	 *                    	only the job that pass through this filter should be
	 *                    	displayed. If NULL, all jobs are displayed.
	 * @param root			<code>false</code> if the jobs list to serialize
	 *            			will be included in a top level serialization (for a
	 *            			jobs list: uws),
	 *            			<code>true</code> otherwise.
	 *
	 * @return				The serialization of the given jobs list.
	 *
	 * @throws Exception	If there is an error during the serialization.
	 *
	 * @since 4.3
	 */
	public abstract String getJobList(final JobList jobsList, JobOwner owner, final boolean root) throws Exception;
	public abstract String getJobList(final JobList jobsList, JobOwner owner, final JobListRefiner listRefiner, final boolean root) throws Exception;

	/**
	 * Serializes the whole given job.
+12 −2
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import uws.job.JobList;
import uws.job.Result;
import uws.job.UWSJob;
import uws.job.jobInfo.JobInfo;
import uws.job.serializer.filter.JobListRefiner;
import uws.job.user.JobOwner;
import uws.service.UWS;
import uws.service.UWSUrl;
@@ -40,7 +41,7 @@ import uws.service.request.UploadFile;
 * Lets serializing any UWS resource in XML.
 *
 * @author Gr&eacute;gory Mantelet (CDS;ARI)
 * @version 4.3 (09/2017)
 * @version 4.3 (10/2017)
 */
public class XMLSerializer extends UWSSerializer {
	private static final long serialVersionUID = 1L;
@@ -178,7 +179,7 @@ public class XMLSerializer extends UWSSerializer {
	}

	@Override
	public String getJobList(final JobList jobsList, final JobOwner owner, final boolean root){
	public String getJobList(final JobList jobsList, final JobOwner owner, final JobListRefiner listRefiner, final boolean root) throws Exception{
		StringBuffer xml = new StringBuffer(getHeader());

		xml.append("<jobs version=\"").append(UWS.VERSION).append('"').append(getUWSNamespace(true));
@@ -190,7 +191,16 @@ public class XMLSerializer extends UWSSerializer {
		xml.append('>');

		UWSUrl jobsListUrl = jobsList.getUrl();

		// Security filter: retrieve only the jobs of the specified owner:
		Iterator<UWSJob> it = jobsList.getJobs(owner);

		/* User filter: filter the jobs in function of filters specified by the
		 * user: */
		if (listRefiner != null)
			it = listRefiner.refine(it);

		// Append the jobs' description:
		while(it.hasNext())
			xml.append("\n\t").append(getJobRef(it.next(), jobsListUrl));

Loading