Skip to content

Commit

Permalink
webservice: Detect URL used to retreive resources and use it in href …
Browse files Browse the repository at this point in the history
…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:
- #153
- daisy/pipeline-assembly#184
  • Loading branch information
bertfrees committed Jan 20, 2023
1 parent 75903c8 commit 37cf09e
Show file tree
Hide file tree
Showing 22 changed files with 154 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public Representation getResource() {
Iterable<DatatypeService> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -410,7 +410,8 @@ private Optional<Job> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Message> 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();
Expand All @@ -52,7 +57,7 @@ public boolean postMessages(List<Message> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Representation getResource() {
setStatus(Status.SUCCESS_OK);
this.move(this.queue,this.job.get().getId());
Collection<? extends Prioritizable< Job>> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Representation getResource() {

setStatus(Status.SUCCESS_OK);
Collection<? extends Prioritizable<Job>> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -11,33 +13,41 @@

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 <code>href</code>
* 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() {
if (client == null) {
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)) {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -13,11 +15,19 @@

public class ClientsXmlWriter {

private final String baseUrl;
private List<? extends Client> clients = null;
private static Logger logger = LoggerFactory.getLogger(ClientsXmlWriter.class.getName());

public ClientsXmlWriter(List<? extends Client> clients) {
/**
* @param baseUrl Prefix to be included at the beginning of <code>href</code>
* 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<? extends Client> clients, String baseUrl) {
this.clients = clients;
this.baseUrl = baseUrl;
}

public Document getXmlDocument() {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@
import org.daisy.pipeline.datatypes.DatatypeService;
import org.daisy.pipeline.webservice.Routes;

import org.restlet.Request;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class DatatypesXmlWriter {
private final String baseUrl;
private Iterable<DatatypeService> datatypes;
private static final Logger logger = LoggerFactory.getLogger(DatatypesXmlWriter.class);

/**
* @param datatypes
* @param baseUrl Prefix to be included at the beginning of <code>href</code>
* 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<DatatypeService> datatypes) {
public DatatypesXmlWriter(Iterable<DatatypeService> datatypes, String baseUrl) {
this.datatypes = datatypes;
this.baseUrl = baseUrl;
}

public Document getXmlDocument(){
Expand All @@ -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);
}

Expand Down
Loading

0 comments on commit 37cf09e

Please sign in to comment.