summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorCheng Chen2012-05-22 01:42:33 (EDT)
committer Cheng Chen2012-05-22 01:42:33 (EDT)
commitb2367076120f5d5d3271dd7e529398c60f04c964 (patch)
tree3db9e2d8debe0cf785c2417f0fdebda68487c778
parent0964802ee63c31e770423ac52792e30a4cfdc353 (diff)
downloadorg.eclipse.recommenders-b2367076120f5d5d3271dd7e529398c60f04c964.zip
org.eclipse.recommenders-b2367076120f5d5d3271dd7e529398c60f04c964.tar.gz
org.eclipse.recommenders-b2367076120f5d5d3271dd7e529398c60f04c964.tar.bz2
[snipmatch] switch to remote client for SubmitEffectThreadrefs/changes/69/6069/1
Change-Id: I72d99214d18febc3f8a3ab4fbd4e8efea490131b Signed-off-by: Cheng Chen <chengchendoc@gmail.com>
-rw-r--r--plugins/org.eclipse.recommenders.snipmatch.rcp/src/org/eclipse/recommenders/snipmatch/web/SubmitEffectThread.java215
1 files changed, 124 insertions, 91 deletions
diff --git a/plugins/org.eclipse.recommenders.snipmatch.rcp/src/org/eclipse/recommenders/snipmatch/web/SubmitEffectThread.java b/plugins/org.eclipse.recommenders.snipmatch.rcp/src/org/eclipse/recommenders/snipmatch/web/SubmitEffectThread.java
index d80df7e..8fd8804 100644
--- a/plugins/org.eclipse.recommenders.snipmatch.rcp/src/org/eclipse/recommenders/snipmatch/web/SubmitEffectThread.java
+++ b/plugins/org.eclipse.recommenders.snipmatch.rcp/src/org/eclipse/recommenders/snipmatch/web/SubmitEffectThread.java
@@ -4,7 +4,7 @@
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
-*/
+ */
package org.eclipse.recommenders.snipmatch.web;
@@ -22,97 +22,130 @@ import org.eclipse.recommenders.snipmatch.core.Effect;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
-
/**
* Sends a request asynchronously to submit a new effect to the SnipMatch repository.
*/
class SubmitEffectThread extends PostThread {
-
- private Effect effect;
- private boolean isPublic;
- private long waitTime;
- private ISubmitEffectListener listener;
-
- public SubmitEffectThread(RemoteMatchClient client, Effect effect, boolean isPublic,
- long waitTime, ISubmitEffectListener listener) {
-
- super(client, RemoteMatchClient.SUBMIT_URL);
- this.effect = effect;
- this.isPublic = isPublic;
- this.waitTime = waitTime;
- this.listener = listener;
- }
-
- @Override
- public void run() {
-
- if (!client.isLoggedIn()) {
- listener.submitEffectFailed("User not authenticated.");
- done = true;
- return;
- }
-
- try {
- sleep(waitTime);
- }
- catch (Exception e) {
- e.printStackTrace();
- listener.submitEffectFailed("Client thread error.");
- done = true;
- return;
- }
-
- String effectString = buildEffectString();
- //System.out.println(effectString);
-
- if (effectString == null) {
- listener.submitEffectFailed("Client XML error.");
- done = true;
- return;
- }
-
- addParameter("username", client.getUsername());
- addParameter("password", client.getPassword());
- addParameter("clientName", client.getName());
- addParameter("clientVersion", client.getVersion());
- addParameter("effectXML", effectString);
- addParameter("public", isPublic ? "true" : "false");
-
- if (done) return;
-
- done = true;
- }
-
- private String buildEffectString() {
-
- DocumentBuilderFactory dbf;
- DocumentBuilder db;
- Document doc;
-
- try {
- dbf = DocumentBuilderFactory.newInstance();
- db = dbf.newDocumentBuilder();
- doc = db.newDocument();
- }
- catch (Exception e) {
- e.printStackTrace();
- return null;
- }
-
- Element effectXml = MatchConverter.writeEffect(doc, effect);
- doc.appendChild(effectXml);
-
- StringWriter sw = new StringWriter();
-
- try {
- Transformer tr = TransformerFactory.newInstance().newTransformer();
- tr.transform(new DOMSource(doc), new StreamResult(sw));
- }
- catch (Exception e) {
- e.printStackTrace();
- return null;
- }
-
- return sw.toString();
- }
-}
+
+ private Effect effect;
+ private boolean isPublic;
+ private long waitTime;
+ private ISubmitEffectListener listener;
+
+ public SubmitEffectThread(RemoteMatchClient client, Effect effect, boolean isPublic, long waitTime,
+ ISubmitEffectListener listener) {
+
+ super(client, RemoteMatchClient.SUBMIT_URL);
+ this.effect = effect;
+ this.isPublic = isPublic;
+ this.waitTime = waitTime;
+ this.listener = listener;
+ }
+
+ @Override
+ public void run() {
+
+ if (!client.isLoggedIn()) {
+ listener.submitEffectFailed("User not authenticated.");
+ done = true;
+ return;
+ }
+
+ try {
+ sleep(waitTime);
+ } catch (Exception e) {
+ e.printStackTrace();
+ listener.submitEffectFailed("Client thread error.");
+ done = true;
+ return;
+ }
+
+ String effectString = buildEffectString();
+ // System.out.println(effectString);
+
+ if (effectString == null) {
+ listener.submitEffectFailed("Client XML error.");
+ done = true;
+ return;
+ }
+
+ addParameter("username", client.getUsername());
+ addParameter("password", client.getPassword());
+ addParameter("clientName", client.getName());
+ addParameter("clientVersion", client.getVersion());
+ addParameter("effectXML", effectString);
+ addParameter("public", isPublic ? "true" : "false");
+
+ if (done)
+ return;
+
+ InputStream response = post();
+
+ if (response == null) {
+ listener.submitEffectFailed("Connection error.");
+ done = true;
+ return;
+ }
+
+ if (done)
+ return;
+
+ DocumentBuilderFactory dbf;
+ DocumentBuilder db;
+ Document responseXml;
+
+ try {
+
+ dbf = DocumentBuilderFactory.newInstance();
+ db = dbf.newDocumentBuilder();
+ responseXml = db.parse(response);
+ } catch (Exception e) {
+
+ e.printStackTrace();
+ listener.submitEffectFailed("Bad response format.");
+ done = true;
+ return;
+ }
+
+ String msg = responseXml.getDocumentElement().getTextContent();
+
+ if (msg.equals("Effect inserted.")) {
+ listener.submitEffectSucceeded();
+ } else {
+ listener.submitEffectFailed(msg);
+ }
+
+ done = true;
+ }
+
+ private String buildEffectString() {
+
+ DocumentBuilderFactory dbf;
+ DocumentBuilder db;
+ Document doc;
+
+ try {
+ dbf = DocumentBuilderFactory.newInstance();
+ db = dbf.newDocumentBuilder();
+ doc = db.newDocument();
+ } catch (Exception e) {
+ e.printStackTrace();
+ return null;
+ }
+
+ Element effectXml = MatchConverter.writeEffect(doc, effect);
+ doc.appendChild(effectXml);
+
+ StringWriter sw = new StringWriter();
+
+ try {
+ Transformer tr = TransformerFactory.newInstance().newTransformer();
+ tr.transform(new DOMSource(doc), new StreamResult(sw));
+ } catch (Exception e) {
+ e.printStackTrace();
+ return null;
+ }
+
+ return sw.toString();
+ }
+} \ No newline at end of file