This is an automated email from the git hooks/post-receive script. New commit to branch feature/sync in repository jtimer. See https://gitlab.nuiton.org/chorem/jtimer.git commit 325be3227e7281d7751b1e32603767fa8ae05b39 Author: servantie <servantie.c@gmail.com> Date: Thu Jun 9 14:43:41 2016 +0200 cleaned up synchronizeTaskOnURL method --- .../chorem/jtimer/io/TimerTaskSynchronizer.java | 63 ++++++++----------- .../jtimer/ui/report/TimerTaskUpdaterView.java | 71 +++++++++------------- 2 files changed, 55 insertions(+), 79 deletions(-) diff --git a/src/main/java/org/chorem/jtimer/io/TimerTaskSynchronizer.java b/src/main/java/org/chorem/jtimer/io/TimerTaskSynchronizer.java index 694c43e..b0ea2d0 100644 --- a/src/main/java/org/chorem/jtimer/io/TimerTaskSynchronizer.java +++ b/src/main/java/org/chorem/jtimer/io/TimerTaskSynchronizer.java @@ -133,59 +133,46 @@ public class TimerTaskSynchronizer implements DataEventListener { * @return a boolean indicating failure or success */ public boolean synchronizeTaskOnURL(JsonObject object) { - String updateJsonString = object.toString(); String syncURL = object.get("URL").getAsString(); String charset = "UTF-8"; HttpURLConnection connection; URL url; + byte[] postDataBytes; + boolean hasUpdated = false; try { url = new URL(syncURL); + connection = (HttpURLConnection) url.openConnection(); + connection.setUseCaches(false); + connection.setDoInput(true); + connection.setDoOutput(true); + connection.setRequestProperty("Content-Length", "" + Integer.toString(updateJsonString.length())); + connection.setRequestProperty("Accept-Charset", charset); + connection.setRequestProperty("Content-Type", "application/json"); + connection.setRequestMethod("POST"); + postDataBytes = updateJsonString.getBytes("UTF-8"); + connection.getOutputStream().write(postDataBytes); + //get the header fields + Map<String, List<String>> map = connection.getHeaderFields(); + //check for answer from server + for (Map.Entry<String, List<String>> entry : map.entrySet()) { + for (String s : entry.getValue()) { + //positive answer, synchronization accepted + if ("HTTP/1.1 200 OK".equals(s)) { + hasUpdated = true; + } + } + } } catch (MalformedURLException e) { log.error("URL malformed"); //abort synchronization of this task if the url is wrong - return false; - } - try { - connection = (HttpURLConnection) url.openConnection(); - } catch (IOException e) { - log.error("couldn't open connection"); - //abort synchronization of this task if connection impossible - return false; - } - connection.setUseCaches(false); - connection.setDoInput(true); - connection.setDoOutput(true); - connection.setRequestProperty("Content-Length", "" + Integer.toString(updateJsonString.length())); - connection.setRequestProperty("Accept-Charset", charset); - connection.setRequestProperty("Content-Type", "application/json"); - try { - connection.setRequestMethod("POST"); } catch (ProtocolException e) { log.error("Protocol error."); - } - byte[] postDataBytes = new byte[0]; - try { - postDataBytes = updateJsonString.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { log.error("Problem with encoding"); - } - try { - connection.getOutputStream().write(postDataBytes); } catch (IOException e) { - log.error("Problem with the Outputstream"); - } - //get the header fields - Map<String, List<String>> map = connection.getHeaderFields(); - boolean hasUpdated = false; - //check for answer from server - for (Map.Entry<String, List<String>> entry : map.entrySet()) { - for (String s : entry.getValue()) { - //positive answer, synchronization accepted - if ("HTTP/1.1 200 OK".equals(s)) { - hasUpdated = true; - } - } + log.error("Problem with the connection"); + //abort synchronization of this task if connection impossible } return hasUpdated; } diff --git a/src/main/java/org/chorem/jtimer/ui/report/TimerTaskUpdaterView.java b/src/main/java/org/chorem/jtimer/ui/report/TimerTaskUpdaterView.java index 4d7ea5f..26a48ac 100644 --- a/src/main/java/org/chorem/jtimer/ui/report/TimerTaskUpdaterView.java +++ b/src/main/java/org/chorem/jtimer/ui/report/TimerTaskUpdaterView.java @@ -418,64 +418,53 @@ public class TimerTaskUpdaterView extends FrameView implements DocumentListener String updateJsonString = object.toString(); String syncURL = object.get("URL").getAsString(); String charset = "UTF-8"; - if (syncURL != null) { - HttpURLConnection connection= null; - URL url = null; - try { - url = new URL(syncURL); - } catch (MalformedURLException e) { - log.error("Malformed URL"); - } - try { - connection = (HttpURLConnection) url.openConnection(); - } catch (IOException e) { - log.error("Connection error"); - } - + HttpURLConnection connection; + URL url; + byte[] postDataBytes; + boolean hasUpdated = false; + try { + url = new URL(syncURL); + connection = (HttpURLConnection) url.openConnection(); connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestProperty("Content-Length", "" + Integer.toString(updateJsonString.length())); connection.setRequestProperty("Accept-Charset", charset); connection.setRequestProperty("Content-Type", "application/json"); - try { - connection.setRequestMethod("POST"); - } catch (ProtocolException e) { - log.error("Protocol Error"); - } - byte[] postDataBytes = new byte[0]; - try { - postDataBytes = object.toString().getBytes("UTF-8"); - } catch (UnsupportedEncodingException e) { - log.error("Unsupported Encoding"); - } - try { - connection.getOutputStream().write(postDataBytes); - } catch (IOException e) { - log.error("OutputStream error"); - } - + connection.setRequestMethod("POST"); + postDataBytes = updateJsonString.getBytes("UTF-8"); + connection.getOutputStream().write(postDataBytes); //get the header fields Map<String, List<String>> map = connection.getHeaderFields(); - boolean hasUpdated = false; - //check for positive answer from server + //check for answer from server for (Map.Entry<String, List<String>> entry : map.entrySet()) { for (String s : entry.getValue()) { + //positive answer, synchronization accepted if ("HTTP/1.1 200 OK".equals(s)) { hasUpdated = true; } } } - if (hasUpdated) { + } catch (MalformedURLException e) { + log.error("URL malformed"); + //abort synchronization of this task if the url is wrong + } catch (ProtocolException e) { + log.error("Protocol error."); + } catch (UnsupportedEncodingException e) { + log.error("Problem with encoding"); + } catch (IOException e) { + log.error("Problem with the connection"); + //abort synchronization of this task if connection impossible + } + if (hasUpdated) { core.getData().editTaskLastSync(task, LocalDateTime.now()); - } else { - log.error("Update error, wrong URL?"); - String message = getResourceMap().getString("action.updateError"); - String title = getResourceMap().getString("action.updateErrorTitle"); - JOptionPane.showMessageDialog(this.getMainComponent(), message, title, - JOptionPane.ERROR_MESSAGE); + } else { + log.error("Update error, wrong URL?"); + String message = getResourceMap().getString("action.updateError"); + String title = getResourceMap().getString("action.updateErrorTitle"); + JOptionPane.showMessageDialog(this.getMainComponent(), message, title, + JOptionPane.ERROR_MESSAGE); - } } } -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.