-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
webservice: refactoring: generalize Callback
A callback is something that pushes notifications, but which not necessarily does this by posting messages to a HTTP server.
- Loading branch information
Showing
5 changed files
with
150 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 0 additions & 122 deletions
122
webservice/src/main/java/org/daisy/pipeline/webservice/impl/Poster.java
This file was deleted.
Oops, something went wrong.
131 changes: 131 additions & 0 deletions
131
webservice/src/main/java/org/daisy/pipeline/webservice/impl/PosterCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package org.daisy.pipeline.webservice.impl; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.DataInputStream; | ||
import java.io.DataOutputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.IOException; | ||
import java.math.BigDecimal; | ||
import java.net.HttpURLConnection; | ||
import java.net.ProtocolException; | ||
import java.net.URI; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.daisy.common.messaging.Message; | ||
import org.daisy.pipeline.clients.Client; | ||
import org.daisy.pipeline.job.Job; | ||
import org.daisy.pipeline.job.Job.Status; | ||
import org.daisy.pipeline.webservice.Authenticator; | ||
import org.daisy.pipeline.webservice.Callback; | ||
import org.daisy.pipeline.webservice.xml.JobXmlWriter; | ||
import org.daisy.pipeline.webservice.xml.XmlUtils; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import org.w3c.dom.Document; | ||
|
||
/** | ||
* Callback that posts notification messages to a HTTP server. | ||
*/ | ||
public class PosterCallback extends Callback { | ||
|
||
private final URI url; | ||
private final Client client; | ||
private static Logger logger = LoggerFactory.getLogger(PosterCallback.class.getName()); | ||
|
||
public PosterCallback(Job job, CallbackType type, int frequency, URI url, Client client) { | ||
super(job, type, frequency); | ||
this.url = url; | ||
this.client = client; | ||
} | ||
|
||
public boolean postMessages(List<Message> messages, int newerThan, BigDecimal progress) { | ||
logger.debug("Posting messages to " + url); | ||
JobXmlWriter writer = new JobXmlWriter(getJob()); | ||
writer.withMessages(messages, newerThan); | ||
writer.withProgress(progress); | ||
Document doc = writer.getXmlDocument(); | ||
return postXml(doc, url, client); | ||
} | ||
|
||
public boolean postStatusUpdate(Status status) { | ||
logger.debug("Posting status '" + status + "' to " + url); | ||
JobXmlWriter writer = new JobXmlWriter(getJob()); | ||
writer.overwriteStatus(status); | ||
Document doc = writer.getXmlDocument(); | ||
return postXml(doc, url, client); | ||
} | ||
|
||
private static boolean postXml(Document doc, URI url, Client client) { | ||
URI requestUri = url; | ||
if (client != null) { | ||
requestUri = Authenticator.createUriWithCredentials(url.toString(), client); | ||
} | ||
|
||
// from http://code.geek.sh/2009/10/simple-post-in-java/ | ||
HttpURLConnection connection = null; | ||
try { | ||
connection = (HttpURLConnection) requestUri.toURL().openConnection(); | ||
} catch (IOException e) { | ||
logger.error(e.getMessage()); | ||
return false; | ||
} | ||
|
||
connection.setDoInput (true); | ||
connection.setDoOutput (true); | ||
connection.setUseCaches (false); | ||
|
||
try { | ||
connection.setRequestMethod("POST"); | ||
} catch (ProtocolException e) { | ||
logger.error(e.getMessage()); | ||
return false; | ||
} | ||
|
||
try { | ||
connection.connect(); | ||
} catch (IOException e) { | ||
logger.error(e.getMessage()); | ||
return false; | ||
} | ||
|
||
DataOutputStream output = null; | ||
|
||
try { | ||
output = new DataOutputStream(connection.getOutputStream()); | ||
} catch (IOException e) { | ||
logger.error(e.getMessage()); | ||
return false; | ||
} | ||
|
||
// Send the request data. | ||
try { | ||
logger.debug("Posting XML: "+XmlUtils.nodeToString(doc)); | ||
output.writeBytes(XmlUtils.nodeToString(doc)); | ||
output.flush(); | ||
output.close(); | ||
} catch (IOException e) { | ||
logger.error(e.getMessage()); | ||
return false; | ||
} | ||
|
||
// Get response data. We're not doing anything with it but if we don't retrieve it, the callback doesn't appear to work. | ||
try { | ||
logger.debug("Got response: " + connection.getResponseMessage() + " (" + connection.getResponseCode() + ")"); | ||
try { | ||
DataInputStream input = new DataInputStream(connection.getInputStream()); | ||
try { | ||
logger.debug((new BufferedReader(new InputStreamReader(input))).lines().collect(Collectors.joining("\n"))); | ||
} finally { input.close(); } | ||
} catch (Exception e) { | ||
} | ||
return true; | ||
} catch (IOException e) { | ||
logger.warn("No response"); | ||
logger.debug("No response", e); | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters