From 37cf09ed474eed0ba17f8ac6c37de7338177be5e Mon Sep 17 00:00:00 2001 From: Bert Frees Date: Mon, 1 Aug 2022 14:18:09 +0200 Subject: [PATCH] webservice: Detect URL used to retreive resources and use it in href attributes This makes the href attributes actually usable (note that they are currently not used in the CLI and web UI client). Before, the org.daisy.pipeline.ws.host, org.daisy.pipeline.ws.port and org.daisy.pipeline.ws.path properties, which are used to configure the "listen address" of the web server, were also used in the href attributes, but this doesn't always make sense. Related issues: - https://github.com/daisy/pipeline-framework/issues/153 - https://github.com/daisy/pipeline-assembly/issues/184 --- .../org/daisy/pipeline/webservice/Routes.java | 7 ------ .../webservice/impl/ClientResource.java | 4 +-- .../webservice/impl/ClientsResource.java | 5 ++-- .../webservice/impl/DatatypesResource.java | 4 ++- .../webservice/impl/JobBatchResource.java | 2 +- .../pipeline/webservice/impl/JobResource.java | 2 +- .../webservice/impl/JobsResource.java | 7 +++--- .../webservice/impl/PosterCallback.java | 11 +++++--- .../webservice/impl/QueueMoveResource.java | 2 +- .../webservice/impl/QueueResource.java | 2 +- .../webservice/impl/ScriptResource.java | 2 +- .../webservice/impl/ScriptsResource.java | 2 +- .../webservice/impl/SizesResource.java | 3 ++- .../webservice/xml/ClientXmlWriter.java | 25 +++++++++++++------ .../webservice/xml/ClientsXmlWriter.java | 17 ++++++++++--- .../webservice/xml/DatatypesXmlWriter.java | 16 ++++++++---- .../pipeline/webservice/xml/JobXmlWriter.java | 22 ++++++++++------ .../webservice/xml/JobsSizeXmlWriter.java | 21 ++++++++++++---- .../webservice/xml/JobsXmlWriter.java | 20 ++++++++++----- .../webservice/xml/QueueXmlWriter.java | 21 ++++++++++------ .../webservice/xml/ScriptXmlWriter.java | 15 ++++++++--- .../webservice/xml/ScriptsXmlWriter.java | 19 ++++++++++---- 22 files changed, 154 insertions(+), 75 deletions(-) diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/Routes.java b/webservice/src/main/java/org/daisy/pipeline/webservice/Routes.java index 7a9ca835..31131bf9 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/Routes.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/Routes.java @@ -37,7 +37,6 @@ public class Routes { private static final int PORT=8181; private int portNumber = 0; private String host = "localhost"; - private String proto="http://"; public Routes() { readOptions(); @@ -52,9 +51,6 @@ public String getPath() { public int getPort() { return portNumber; } - public String getBaseUri() { - return proto+host + ":" + String.valueOf(portNumber) + path; - } private void readOptions() { String path = Properties.PATH.get(); @@ -69,9 +65,6 @@ private void readOptions() { if (hostname != null) { host = hostname; } - if (Properties.SSL.get()!=null&&Properties.SSL.get().equalsIgnoreCase("true")){ - proto="https://"; - } String port = Properties.PORT.get(); if (port != null) { diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/ClientResource.java b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/ClientResource.java index 7df98182..8501b33a 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/ClientResource.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/ClientResource.java @@ -67,7 +67,7 @@ public Representation getResource() { } setStatus(Status.SUCCESS_OK); - ClientXmlWriter writer = new ClientXmlWriter(client.get()); + ClientXmlWriter writer = new ClientXmlWriter(client.get(), getRequest().getRootRef().toString()); DomRepresentation dom = new DomRepresentation(MediaType.APPLICATION_XML, writer.getXmlDocument()); logResponse(dom); @@ -167,7 +167,7 @@ public Representation putResource(Representation representation) { } setStatus(Status.SUCCESS_OK); - ClientXmlWriter writer = new ClientXmlWriter(updated.get()); + ClientXmlWriter writer = new ClientXmlWriter(updated.get(), getRequest().getRootRef().toString()); DomRepresentation dom = new DomRepresentation(MediaType.APPLICATION_XML, writer.getXmlDocument()); logResponse(dom); diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/ClientsResource.java b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/ClientsResource.java index 3ac781c1..c3d607fd 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/ClientsResource.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/ClientsResource.java @@ -55,7 +55,8 @@ public Representation getResource() { } setStatus(Status.SUCCESS_OK); - ClientsXmlWriter writer = new ClientsXmlWriter(webservice().getStorage().getClientStorage().getAll()); + ClientsXmlWriter writer = new ClientsXmlWriter(webservice().getStorage().getClientStorage().getAll(), + getRequest().getRootRef().toString()); DomRepresentation dom = new DomRepresentation(MediaType.APPLICATION_XML, writer.getXmlDocument()); @@ -122,7 +123,7 @@ public Representation createResource(Representation representation) { } setStatus(Status.SUCCESS_CREATED); - ClientXmlWriter writer = new ClientXmlWriter(newClient.get()); + ClientXmlWriter writer = new ClientXmlWriter(newClient.get(), getRequest().getRootRef().toString()); DomRepresentation dom = new DomRepresentation(MediaType.APPLICATION_XML, writer.getXmlDocument()); logResponse(dom); return dom; diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/DatatypesResource.java b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/DatatypesResource.java index f4895655..c5fde970 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/DatatypesResource.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/DatatypesResource.java @@ -43,7 +43,9 @@ public Representation getResource() { Iterable datatypes = webservice().getDatatypeRegistry().getDatatypes(); try { dom = new DomRepresentation(MediaType.APPLICATION_XML, - new DatatypesXmlWriter(datatypes).getXmlDocument()); + new DatatypesXmlWriter(datatypes, + getRequest().getRootRef().toString()) + .getXmlDocument()); } catch (Exception e) { setStatus(Status.SERVER_ERROR_INTERNAL); diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/JobBatchResource.java b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/JobBatchResource.java index e59ca2cb..75c793ec 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/JobBatchResource.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/JobBatchResource.java @@ -38,7 +38,7 @@ public Representation getResource() { return null; } JobManager jobMan = webservice().getJobManager(this.getClient(),this.batchId); - JobsXmlWriter writer = new JobsXmlWriter(jobMan.getJobs()); + JobsXmlWriter writer = new JobsXmlWriter(jobMan.getJobs(), getRequest().getRootRef().toString()); if(this.webservice().getConfiguration().isLocalFS()){ writer.withLocalPaths(); } diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/JobResource.java b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/JobResource.java index c24200bd..00ef8964 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/JobResource.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/JobResource.java @@ -82,7 +82,7 @@ public Representation getResource() { } setStatus(Status.SUCCESS_OK); - JobXmlWriter writer = new JobXmlWriter(job.get()); + JobXmlWriter writer = new JobXmlWriter(job.get(), getRequest().getRootRef().toString()); writer.withFullResults(true); if(this.webservice().getConfiguration().isLocalFS()){ diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/JobsResource.java b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/JobsResource.java index a5b4fa45..7d02e817 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/JobsResource.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/JobsResource.java @@ -95,7 +95,7 @@ public Representation getResource() { return null; } JobManager jobMan = webservice().getJobManager(this.getClient()); - JobsXmlWriter writer = new JobsXmlWriter(jobMan.getJobs()); + JobsXmlWriter writer = new JobsXmlWriter(jobMan.getJobs(), getRequest().getRootRef().toString()); if(this.webservice().getConfiguration().isLocalFS()){ writer.withLocalPaths(); } @@ -195,7 +195,7 @@ public Representation createResource(Representation representation) { webservice().getStorage().getJobConfigurationStorage() .add(job.get().getId(), XmlUtils.nodeToString(doc)); - JobXmlWriter writer = new JobXmlWriter(job.get()); + JobXmlWriter writer = new JobXmlWriter(job.get(), getRequest().getRootRef().toString()); Document jobXml = writer.withAllMessages().withScriptDetails().getXmlDocument(); DomRepresentation dom = new DomRepresentation(MediaType.APPLICATION_XML, jobXml); setStatus(Status.SUCCESS_CREATED); @@ -410,7 +410,8 @@ private Optional createJob(Document doc, ZipFile zip) if (handler == null) { throw new RuntimeException("No push notifier"); } - handler.addCallback(new PosterCallback(newJob.get(), type, freq, href, getClient())); + handler.addCallback(new PosterCallback(newJob.get(), type, freq, href, getClient(), + getRequest().getRootRef().toString())); } catch (URISyntaxException e) { logger.warn("Cannot create callback: " + e.getMessage()); } diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/PosterCallback.java b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/PosterCallback.java index db7dab42..ad9e5b52 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/PosterCallback.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/PosterCallback.java @@ -33,17 +33,22 @@ public class PosterCallback extends Callback { private final URI url; private final Client client; + private final String requestRootUrl; private static Logger logger = LoggerFactory.getLogger(PosterCallback.class.getName()); - public PosterCallback(Job job, CallbackType type, int frequency, URI url, Client client) { + /** + * @param requestRootUrl Root URL of the /jobs POST request that declared the callback. + */ + public PosterCallback(Job job, CallbackType type, int frequency, URI url, Client client, String requestRootUrl) { super(job, type, frequency); this.url = url; this.client = client; + this.requestRootUrl = requestRootUrl; } public boolean postMessages(List messages, int newerThan, BigDecimal progress) { logger.debug("Posting messages to " + url); - JobXmlWriter writer = new JobXmlWriter(getJob()); + JobXmlWriter writer = new JobXmlWriter(getJob(), requestRootUrl); writer.withMessages(messages, newerThan); writer.withProgress(progress); Document doc = writer.getXmlDocument(); @@ -52,7 +57,7 @@ public boolean postMessages(List messages, int newerThan, BigDecimal pr public boolean postStatusUpdate(Status status) { logger.debug("Posting status '" + status + "' to " + url); - JobXmlWriter writer = new JobXmlWriter(getJob()); + JobXmlWriter writer = new JobXmlWriter(getJob(), requestRootUrl); writer.overwriteStatus(status); Document doc = writer.getXmlDocument(); return postXml(doc, url, client); diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/QueueMoveResource.java b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/QueueMoveResource.java index d45b9936..f539d77b 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/QueueMoveResource.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/QueueMoveResource.java @@ -57,7 +57,7 @@ public Representation getResource() { setStatus(Status.SUCCESS_OK); this.move(this.queue,this.job.get().getId()); Collection> jobs=this.queue.asCollection(); - QueueXmlWriter writer = new QueueXmlWriter(jobs); + QueueXmlWriter writer = new QueueXmlWriter(jobs, getRequest().getRootRef().toString()); DomRepresentation dom = new DomRepresentation(MediaType.APPLICATION_XML, writer.getXmlDocument()); logResponse(dom); diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/QueueResource.java b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/QueueResource.java index 3e6d1393..c65cd8df 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/QueueResource.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/QueueResource.java @@ -38,7 +38,7 @@ public Representation getResource() { setStatus(Status.SUCCESS_OK); Collection> jobs=webservice().getJobManager(this.getClient()).getExecutionQueue().asCollection(); - QueueXmlWriter writer = new QueueXmlWriter(jobs); + QueueXmlWriter writer = new QueueXmlWriter(jobs, getRequest().getRootRef().toString()); DomRepresentation dom = new DomRepresentation(MediaType.APPLICATION_XML, writer.getXmlDocument()); logResponse(dom); diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/ScriptResource.java b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/ScriptResource.java index a81530b7..16dfd149 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/ScriptResource.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/ScriptResource.java @@ -68,7 +68,7 @@ public Representation getResource() { return this.getErrorRepresentation("Script not found"); } setStatus(Status.SUCCESS_OK); - ScriptXmlWriter writer = new ScriptXmlWriter(script); + ScriptXmlWriter writer = new ScriptXmlWriter(script, getRequest().getRootRef().toString()); DomRepresentation dom = new DomRepresentation( MediaType.APPLICATION_XML, writer.withDetails().getXmlDocument()); diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/ScriptsResource.java b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/ScriptsResource.java index 6130422a..ed58642c 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/ScriptsResource.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/ScriptsResource.java @@ -56,7 +56,7 @@ public Representation getResource() { } this.setStatus(Status.SUCCESS_OK); - ScriptsXmlWriter writer = new ScriptsXmlWriter(scripts); + ScriptsXmlWriter writer = new ScriptsXmlWriter(scripts, getRequest().getRootRef().toString()); DomRepresentation dom = new DomRepresentation(MediaType.APPLICATION_XML, writer.getXmlDocument()); logResponse(dom); diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/SizesResource.java b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/SizesResource.java index 80504e89..82b9fcaf 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/impl/SizesResource.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/impl/SizesResource.java @@ -34,7 +34,8 @@ public Representation getResource() { setStatus(Status.SUCCESS_OK); - JobsSizeXmlWriter writer = new JobsSizeXmlWriter(JobSize.getSizes(webservice().getJobManager(this.getClient()).getJobs())); + JobsSizeXmlWriter writer = new JobsSizeXmlWriter(JobSize.getSizes(webservice().getJobManager(this.getClient()).getJobs()), + getRequest().getRootRef().toString()); DomRepresentation dom = new DomRepresentation(MediaType.APPLICATION_XML, writer.getXmlDocument()); logResponse(dom); diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ClientXmlWriter.java b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ClientXmlWriter.java index 14b02dd0..effb226a 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ClientXmlWriter.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ClientXmlWriter.java @@ -3,6 +3,8 @@ import org.daisy.pipeline.clients.Client; import org.daisy.pipeline.webservice.Routes; +import org.restlet.Request; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -11,11 +13,19 @@ public class ClientXmlWriter { + private final String baseUrl; private Client client = null; private static Logger logger = LoggerFactory.getLogger(ClientXmlWriter.class); - public ClientXmlWriter(Client client) { + /** + * @param baseUrl Prefix to be included at the beginning of href + * attributes (the resource paths). Set this to {@link Request#getRootRef()} + * to get fully qualified URLs. Set this to {@link Routes#getPath()} to get + * absolute paths relative to the domain name. + */ + public ClientXmlWriter(Client client, String baseUrl) { this.client = client; + this.baseUrl = baseUrl; } public Document getXmlDocument() { @@ -23,21 +33,21 @@ public Document getXmlDocument() { logger.warn("Could not generate XML for null client"); return null; } - return clientToXmlDocument(client); + return clientToXmlDocument(client, baseUrl); } // instead of getting a document representation, add an element representation to an existing document public void addAsElementChild(Element parent) { Document doc = parent.getOwnerDocument(); Element clientElm = doc.createElementNS(XmlUtils.NS_PIPELINE_DATA, "client"); - addElementData(client, clientElm); + addElementData(client, baseUrl, clientElm); parent.appendChild(clientElm); } - private static Document clientToXmlDocument(Client client) { + private static Document clientToXmlDocument(Client client, String baseUrl) { Document doc = XmlUtils.createDom("client"); Element rootElm = doc.getDocumentElement(); - addElementData(client, rootElm); + addElementData(client, baseUrl, rootElm); // for debugging only if (!XmlValidator.validate(doc, XmlValidator.CLIENT_SCHEMA_URL)) { @@ -46,9 +56,8 @@ private static Document clientToXmlDocument(Client client) { return doc; } - private static void addElementData(Client client, Element element) { - String baseUri = new Routes().getBaseUri(); - String clientHref = baseUri + Routes.CLIENT_ROUTE.replaceFirst("\\{id\\}", client.getId()); + private static void addElementData(Client client, String baseUrl, Element element) { + String clientHref = baseUrl + Routes.CLIENT_ROUTE.replaceFirst("\\{id\\}", client.getId()); element.setAttribute("id", client.getId()); element.setAttribute("href", clientHref); diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ClientsXmlWriter.java b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ClientsXmlWriter.java index d2f45750..ebee586d 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ClientsXmlWriter.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ClientsXmlWriter.java @@ -5,6 +5,8 @@ import org.daisy.pipeline.clients.Client; import org.daisy.pipeline.webservice.Routes; +import org.restlet.Request; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -13,11 +15,19 @@ public class ClientsXmlWriter { + private final String baseUrl; private List clients = null; private static Logger logger = LoggerFactory.getLogger(ClientsXmlWriter.class.getName()); - public ClientsXmlWriter(List clients) { + /** + * @param baseUrl Prefix to be included at the beginning of href + * attributes (the resource paths). Set this to {@link Request#getRootRef()} + * to get fully qualified URLs. Set this to {@link Routes#getPath()} to get + * absolute paths relative to the domain name. + */ + public ClientsXmlWriter(List clients, String baseUrl) { this.clients = clients; + this.baseUrl = baseUrl; } public Document getXmlDocument() { @@ -29,12 +39,11 @@ public Document getXmlDocument() { } private Document clientsToXmlDoc() { - String baseUri = new Routes().getBaseUri(); Document doc = XmlUtils.createDom("clients"); Element clientsElm = doc.getDocumentElement(); - clientsElm.setAttribute("href", baseUri + Routes.CLIENTS_ROUTE); + clientsElm.setAttribute("href", baseUrl + Routes.CLIENTS_ROUTE); for (Client client : clients) { - ClientXmlWriter writer = new ClientXmlWriter(client); + ClientXmlWriter writer = new ClientXmlWriter(client, baseUrl); writer.addAsElementChild(clientsElm); } // for debugging only diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/DatatypesXmlWriter.java b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/DatatypesXmlWriter.java index 305fa852..ffaf83f1 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/DatatypesXmlWriter.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/DatatypesXmlWriter.java @@ -3,6 +3,8 @@ import org.daisy.pipeline.datatypes.DatatypeService; import org.daisy.pipeline.webservice.Routes; +import org.restlet.Request; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -10,14 +12,19 @@ import org.w3c.dom.Element; public class DatatypesXmlWriter { + private final String baseUrl; private Iterable datatypes; private static final Logger logger = LoggerFactory.getLogger(DatatypesXmlWriter.class); /** - * @param datatypes + * @param baseUrl Prefix to be included at the beginning of href + * attributes (the resource paths). Set this to {@link Request#getRootRef()} + * to get fully qualified URLs. Set this to {@link Routes#getPath()} to get + * absolute paths relative to the domain name. */ - public DatatypesXmlWriter(Iterable datatypes) { + public DatatypesXmlWriter(Iterable datatypes, String baseUrl) { this.datatypes = datatypes; + this.baseUrl = baseUrl; } public Document getXmlDocument(){ @@ -29,14 +36,13 @@ public Document getXmlDocument(){ } private Document buildXml(){ - String baseUri = new Routes().getBaseUri(); Document doc = XmlUtils.createDom("datatypes"); Element datatypesElem= doc.getDocumentElement(); - datatypesElem.setAttribute("href", baseUri + Routes.DATATYPES_ROUTE); + datatypesElem.setAttribute("href", baseUrl + Routes.DATATYPES_ROUTE); for (DatatypeService ds : this.datatypes) { Element dsElem=doc.createElementNS(XmlUtils.NS_PIPELINE_DATA,"datatype"); dsElem.setAttribute("id",ds.getId()); - dsElem.setAttribute("href",String.format("%s%s/%s",baseUri,Routes.DATATYPES_ROUTE,ds.getId())); + dsElem.setAttribute("href",String.format("%s%s/%s", baseUrl, Routes.DATATYPES_ROUTE,ds.getId())); datatypesElem.appendChild(dsElem); } diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobXmlWriter.java b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobXmlWriter.java index 31642413..2e364f04 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobXmlWriter.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobXmlWriter.java @@ -17,6 +17,8 @@ import org.daisy.pipeline.script.XProcScript; import org.daisy.pipeline.webservice.Routes; +import org.restlet.Request; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -25,6 +27,7 @@ public class JobXmlWriter { + private final String baseUrl; private Job job = null; private List messages = null; private long messagesNewerThan = -1; @@ -48,8 +51,15 @@ public class JobXmlWriter { MSG_LEVELS.add(Level.DEBUG); } - public JobXmlWriter(Job job) { + /** + * @param baseUrl Prefix to be included at the beginning of href + * attributes (the resource paths). Set this to {@link Request#getRootRef()} + * to get fully qualified URLs. Set this to {@link Routes#getPath()} to get + * absolute paths relative to the domain name. + */ + public JobXmlWriter(Job job, String baseUrl) { this.job = job; + this.baseUrl = baseUrl; } public Document getXmlDocument() { @@ -134,9 +144,8 @@ private Document jobToXmlDocument() { private void addElementData(Job job, Element element) { Document doc = element.getOwnerDocument(); - String baseUri = new Routes().getBaseUri(); Job.Status status = (this.statusOverWrite==null)?job.getStatus():this.statusOverWrite; - String jobHref = baseUri + Routes.JOB_ROUTE.replaceFirst("\\{id\\}", job.getId().toString()); + String jobHref = baseUrl + Routes.JOB_ROUTE.replaceFirst("\\{id\\}", job.getId().toString()); element.setAttribute("id", job.getId().toString()); element.setAttribute("href", jobHref); @@ -161,7 +170,7 @@ private void addElementData(Job job, Element element) { XProcScript script=job.getContext().getScript(); //return if no script was loadeded if(script.getDescriptor()!=null){ - ScriptXmlWriter writer = new ScriptXmlWriter(script); + ScriptXmlWriter writer = new ScriptXmlWriter(script, baseUrl); writer.addAsElementChild(element); } } @@ -185,7 +194,7 @@ private void addElementData(Job job, Element element) { if (job.getStatus() == Job.Status.SUCCESS || job.getStatus() == Job.Status.FAIL) { Element logElm = doc.createElementNS(XmlUtils.NS_PIPELINE_DATA, "log"); - String logHref = baseUri + Routes.LOG_ROUTE.replaceFirst("\\{id\\}", job.getId().toString()); + String logHref = baseUrl + Routes.LOG_ROUTE.replaceFirst("\\{id\\}", job.getId().toString()); logElm.setAttribute("href", logHref); element.appendChild(logElm); if(this.fullResult) @@ -224,9 +233,8 @@ private void addResults(Element jobElem) { return; } Document doc = jobElem.getOwnerDocument(); - String baseUri = new Routes().getBaseUri(); Element resultsElm = doc.createElementNS(XmlUtils.NS_PIPELINE_DATA, "results"); - String resultHref = baseUri + Routes.RESULT_ROUTE.replaceFirst("\\{id\\}", job.getId().toString()); + String resultHref = baseUrl + Routes.RESULT_ROUTE.replaceFirst("\\{id\\}", job.getId().toString()); resultsElm.setAttribute("href", resultHref); resultsElm.setAttribute("mime-type", "application/zip"); jobElem.appendChild(resultsElm); diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobsSizeXmlWriter.java b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobsSizeXmlWriter.java index b677d163..be5a81f5 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobsSizeXmlWriter.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobsSizeXmlWriter.java @@ -3,6 +3,8 @@ import org.daisy.pipeline.job.JobSize; import org.daisy.pipeline.webservice.Routes; +import org.restlet.Request; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -10,10 +12,20 @@ import org.w3c.dom.Element; public class JobsSizeXmlWriter{ + + private final String baseUrl; Iterable sizes; private static final Logger logger = LoggerFactory.getLogger(JobsSizeXmlWriter.class); - public JobsSizeXmlWriter(Iterable sizes) { + + /** + * @param baseUrl Prefix to be included at the beginning of href + * attributes (the resource paths). Set this to {@link Request#getRootRef()} + * to get fully qualified URLs. Set this to {@link Routes#getPath()} to get + * absolute paths relative to the domain name. + */ + public JobsSizeXmlWriter(Iterable sizes, String baseUrl) { this.sizes = sizes; + this.baseUrl = baseUrl; } public Document getXmlDocument() { @@ -21,14 +33,13 @@ public Document getXmlDocument() { logger.warn("Could not create XML for null jobs"); return null; } - return sizesToXml(this.sizes); + return sizesToXml(this.sizes, baseUrl); } - private static Document sizesToXml(Iterable sizes) { - String baseUri = new Routes().getBaseUri(); + private static Document sizesToXml(Iterable sizes, String baseUrl) { Document doc = XmlUtils.createDom("jobSizes"); Element sizesElm = doc.getDocumentElement(); - sizesElm.setAttribute("href", baseUri + Routes.SIZES_ROUTE); + sizesElm.setAttribute("href", baseUrl + Routes.SIZES_ROUTE); sizesElm.setAttribute("total", String.valueOf(JobSize.getTotal(sizes))); for (JobSize size : sizes) { diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobsXmlWriter.java b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobsXmlWriter.java index 7d9241f5..dc1ff711 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobsXmlWriter.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/JobsXmlWriter.java @@ -3,6 +3,8 @@ import org.daisy.pipeline.job.Job; import org.daisy.pipeline.webservice.Routes; +import org.restlet.Request; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -11,16 +13,23 @@ public class JobsXmlWriter { + private final String baseUrl; Iterable jobs = null; private static Logger logger = LoggerFactory.getLogger(JobsXmlWriter.class.getName()); private boolean localPaths=false; - - public JobsXmlWriter(Iterable jobs) { + + /** + * @param baseUrl Prefix to be included at the beginning of href + * attributes (the resource paths). Set this to {@link Request#getRootRef()} + * to get fully qualified URLs. Set this to {@link Routes#getPath()} to get + * absolute paths relative to the domain name. + */ + public JobsXmlWriter(Iterable jobs, String baseUrl) { this.jobs = jobs; + this.baseUrl = baseUrl; } - public Document getXmlDocument() { if (jobs == null) { logger.warn("Could not create XML for null jobs"); @@ -30,13 +39,12 @@ public Document getXmlDocument() { } private Document jobsToXml(Iterable jobs) { - String baseUri = new Routes().getBaseUri(); Document doc = XmlUtils.createDom("jobs"); Element jobsElm = doc.getDocumentElement(); - jobsElm.setAttribute("href", baseUri + Routes.JOBS_ROUTE); + jobsElm.setAttribute("href", baseUrl + Routes.JOBS_ROUTE); for (Job job : jobs) { - JobXmlWriter writer = new JobXmlWriter(job); + JobXmlWriter writer = new JobXmlWriter(job, baseUrl); writer.withFullResults(true); writer.withOnlyPrimaries(true); if(this.localPaths){ diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/QueueXmlWriter.java b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/QueueXmlWriter.java index c746311e..b86e2aad 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/QueueXmlWriter.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/QueueXmlWriter.java @@ -4,6 +4,8 @@ import org.daisy.pipeline.job.Job; import org.daisy.pipeline.webservice.Routes; +import org.restlet.Request; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -12,19 +14,24 @@ public class QueueXmlWriter{ private static final Logger logger = LoggerFactory.getLogger(QueueXmlWriter.class); + private final String baseUrl; Iterable> jobs; + /** - * @param size + * @param baseUrl Prefix to be included at the beginning of href + * attributes (the resource paths). Set this to {@link Request#getRootRef()} + * to get fully qualified URLs. Set this to {@link Routes#getPath()} to get + * absolute paths relative to the domain name. */ - public QueueXmlWriter(Iterable> jobs) { + public QueueXmlWriter(Iterable> jobs, String baseUrl) { this.jobs= jobs; + this.baseUrl = baseUrl; } public Document getXmlDocument(){ Document doc = XmlUtils.createDom("queue"); - String baseUri = new Routes().getBaseUri(); Element queueElem= doc.getDocumentElement(); - queueElem.setAttribute("href", baseUri + Routes.QUEUE_ROUTE); + queueElem.setAttribute("href", baseUrl + Routes.QUEUE_ROUTE); for (Prioritizable job: this.jobs){ addElementData(job, queueElem); @@ -41,14 +48,14 @@ public Document getXmlDocument(){ private void addElementData(Prioritizable job, Element parent) { Element element= parent.getOwnerDocument().createElementNS(XmlUtils.NS_PIPELINE_DATA, "job"); element.setAttribute("id",job.prioritySource().getId().toString()); - element.setAttribute("href",new Routes().getBaseUri()+"/jobs/"+job.prioritySource().getId().toString()); + element.setAttribute("href", baseUrl + "/jobs/" + job.prioritySource().getId().toString()); element.setAttribute("computedPriority",String.valueOf(job.getPriority())); element.setAttribute("jobPriority",String.valueOf(job.prioritySource().getPriority()).toLowerCase()); element.setAttribute("clientPriority",String.valueOf(job.prioritySource().getContext().getClient().getPriority()).toLowerCase()); element.setAttribute("relativeTime",String.valueOf(job.getRelativeWaitingTime())); element.setAttribute("timestamp",String.valueOf(job.getTimestamp())); - element.setAttribute("moveUp",new Routes().getBaseUri()+"/queue/up/"+job.prioritySource().getId().toString()); - element.setAttribute("moveDown",new Routes().getBaseUri()+"/queue/down/"+job.prioritySource().getId().toString()); + element.setAttribute("moveUp", baseUrl + "/queue/up/" + job.prioritySource().getId().toString()); + element.setAttribute("moveDown", baseUrl + "/queue/down/" + job.prioritySource().getId().toString()); parent.appendChild(element); diff --git a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ScriptXmlWriter.java b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ScriptXmlWriter.java index 798a23cc..80466161 100644 --- a/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ScriptXmlWriter.java +++ b/webservice/src/main/java/org/daisy/pipeline/webservice/xml/ScriptXmlWriter.java @@ -8,6 +8,8 @@ import org.daisy.pipeline.script.XProcScript; import org.daisy.pipeline.webservice.Routes; +import org.restlet.Request; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -20,13 +22,21 @@ public class ScriptXmlWriter { + private final String baseUrl; XProcScript script = null; boolean details = false; private static Logger logger = LoggerFactory.getLogger(ScriptXmlWriter.class.getName()); - public ScriptXmlWriter(XProcScript script) { + /** + * @param baseUrl Prefix to be included at the beginning of href + * attributes (the resource paths). Set this to {@link Request#getRootRef()} + * to get fully qualified URLs. Set this to {@link Routes#getPath()} to get + * absolute paths relative to the domain name. + */ + public ScriptXmlWriter(XProcScript script, String baseUrl) { this.script = script; + this.baseUrl = baseUrl; } public ScriptXmlWriter withDetails() { @@ -66,8 +76,7 @@ private Document scriptToXmlDocument(XProcScript script) { // element is