From d067f97df428c7f947059797a7bcd414e8c0a14c Mon Sep 17 00:00:00 2001 From: Mauricio Carneiro Date: Mon, 23 Jul 2012 23:50:24 -0400 Subject: [PATCH] Moving doclets to public --- .../sting/utils/help/ForumAPIUtils.java | 173 ++++++++++++++++++ .../sting/utils/help/ForumDiscussion.java | 83 +++++++++ 2 files changed, 256 insertions(+) create mode 100644 public/java/src/org/broadinstitute/sting/utils/help/ForumAPIUtils.java create mode 100644 public/java/src/org/broadinstitute/sting/utils/help/ForumDiscussion.java diff --git a/public/java/src/org/broadinstitute/sting/utils/help/ForumAPIUtils.java b/public/java/src/org/broadinstitute/sting/utils/help/ForumAPIUtils.java new file mode 100644 index 0000000000..388e7ce450 --- /dev/null +++ b/public/java/src/org/broadinstitute/sting/utils/help/ForumAPIUtils.java @@ -0,0 +1,173 @@ +/* + * Copyright (c) 2012, The Broad Institute + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +package org.broadinstitute.sting.utils.help; + +import com.google.gson.Gson; +import org.apache.commons.io.IOUtils; +import org.apache.http.HttpResponse; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.DefaultHttpClient; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.util.ArrayList; +import java.util.List; + +public class ForumAPIUtils { + /** + * How we post to the forum + */ + private final static String API_URL = "https://gatk.vanillaforums.com/"; + final private static String ACCESS_TOKEN = "access_token="; + + public static List getPostedTools(String forumKey) { + Gson gson = new Gson(); + List output = new ArrayList(); + + String text = httpGet(API_URL + "categories.json?CategoryIdentifier=tool-bulletin&page=1-100000&" + ACCESS_TOKEN + forumKey); + + APIQuery details = gson.fromJson(text, APIQuery.class); + ForumDiscussion[] discussions = details.Discussions; + + for (ForumDiscussion post : discussions) { + output.add(post.Name); + } + + /* + System.out.println(details.isJsonArray()); + System.out.println(details.isJsonNull()); + System.out.println(details.isJsonObject()); + System.out.println(details.isJsonPrimitive()); + + JsonArray posted = details.getAsJsonPrimitive().get("Discussions").getAsJsonArray(); + + for ( JsonElement post : posted ) { + output.add( post.getAsJsonObject().get("Name").getAsString()); + } + */ + return output; + } + + + private static String httpGet(String urlStr) { + String output = ""; + + try { + + DefaultHttpClient httpClient = new DefaultHttpClient(); + HttpGet getRequest = new HttpGet(urlStr); + getRequest.addHeader("accept", "application/json"); + + HttpResponse response = httpClient.execute(getRequest); + + if (response.getStatusLine().getStatusCode() != 200) { + throw new RuntimeException("Failed : HTTP error code : " + + response.getStatusLine().getStatusCode()); + } + + output = IOUtils.toString(response.getEntity().getContent()); + + httpClient.getConnectionManager().shutdown(); + + } catch (ClientProtocolException e) { + + e.printStackTrace(); + + } catch (IOException e) { + + e.printStackTrace(); + } + return output; + } + + private static String httpPost(String data, String URL) { + try { + + DefaultHttpClient httpClient = new DefaultHttpClient(); + HttpPost postRequest = new HttpPost(URL); + + StringEntity input = new StringEntity(data); + input.setContentType("application/json"); + postRequest.setEntity(input); + + HttpResponse response = httpClient.execute(postRequest); + + if (response.getStatusLine().getStatusCode() != 200) { + throw new RuntimeException("Failed : HTTP error code : " + + response.getStatusLine().getStatusCode()); + } + + BufferedReader br = new BufferedReader( + new InputStreamReader((response.getEntity().getContent()))); + + String output = ""; + String line; + System.out.println("Output from Server .... \n"); + while ((line = br.readLine()) != null) { + output += (line + '\n'); + System.out.println(line); + } + + httpClient.getConnectionManager().shutdown(); + return output; + + } catch (MalformedURLException e) { + + e.printStackTrace(); + + } catch (IOException e) { + + e.printStackTrace(); + + } + return null; + } + + public static void postToForum(GATKDocWorkUnit tool, final String forumKey) { + + + ForumDiscussion post = new ForumDiscussion(tool); + + Gson gson = new Gson(); + + String data = gson.toJson(post.getPostData()); + httpPost(data, API_URL + "post/discussion.json?" + ACCESS_TOKEN + forumKey); + + + } + + class APIQuery { + ForumDiscussion[] Discussions; + + public APIQuery() { + } + } + +} diff --git a/public/java/src/org/broadinstitute/sting/utils/help/ForumDiscussion.java b/public/java/src/org/broadinstitute/sting/utils/help/ForumDiscussion.java new file mode 100644 index 0000000000..ac5d73946d --- /dev/null +++ b/public/java/src/org/broadinstitute/sting/utils/help/ForumDiscussion.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2012, The Broad Institute + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +package org.broadinstitute.sting.utils.help; + +import java.util.HashMap; +import java.util.Map; + +class ForumDiscussion { + + final private static String POST_TEMPLATE = "

A new tool has been released!

Check out the documentation at %s.

"; + + final int Announce; + final String Body; + final String Category; + final int Closed; + final String Format; + final String Name; + final int Sink; + final String Tags; + final String Type; + + public ForumDiscussion(String name, String body, String format, String category, + String tagsCSV, String type, int closed, int announce, int sink) { + this.Name = name; + this.Body = body; + this.Format = format; + this.Category = category; + this.Tags = tagsCSV; + this.Type = type; + this.Closed = closed; + this.Announce = announce; + this.Sink = sink; + } + + public ForumDiscussion(GATKDocWorkUnit tool) { + this(tool.name, + String.format(POST_TEMPLATE, GATKDocUtils.URL_ROOT_FOR_RELEASE_GATKDOCS + tool.filename, tool.name), + "Html", "tool-bulletin", tool.name + "," + tool.group + ",gatkdocs", "Discussion", 0, -1, -1); + } + + public Map getPostData() { + Map output = new HashMap(); + + output.put("Name", Name); + output.put("Body", Body); + output.put("Format", Format); + output.put("Category", Category); + if (Tags != null) + output.put("Tags", Tags); + if (Type != null) + output.put("Type", Type); + if (Closed != -1) + output.put("Closed", Closed == 1 ? "1" : "0"); + if (Announce != -1) + output.put("Announce", Announce == 1 ? "1" : "0"); + if (Sink != -1) + output.put("Sink", Sink == 1 ? "1" : "0"); + + return output; + } +} \ No newline at end of file