diff options
author | Kevin Sawicki | 2012-09-22 17:26:39 +0000 |
---|---|---|
committer | Kevin Sawicki | 2012-09-22 17:26:39 +0000 |
commit | 6e8dd15c95ba0c3d7fdea8fad177dc7389609357 (patch) | |
tree | 71b7e0777369e15315f9a5eaac3154b77865ed79 /org.eclipse.egit.github.core | |
parent | fe350995bfaf999334789c34baf91241bd88b478 (diff) | |
download | egit-github-6e8dd15c95ba0c3d7fdea8fad177dc7389609357.tar.gz egit-github-6e8dd15c95ba0c3d7fdea8fad177dc7389609357.tar.xz egit-github-6e8dd15c95ba0c3d7fdea8fad177dc7389609357.zip |
Add service support for Markdown API
Change-Id: Ief3262ab239f10be11ecca0a2c3f043ef673eaee
Diffstat (limited to 'org.eclipse.egit.github.core')
3 files changed, 195 insertions, 5 deletions
diff --git a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GitHubClient.java b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GitHubClient.java index ec8382cf..1c7fe04f 100644 --- a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GitHubClient.java +++ b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GitHubClient.java @@ -512,7 +512,12 @@ public class GitHubClient { } if (error != null) return new RequestException(error, code); - } + } else + try { + response.close(); + } catch (IOException ignored) { + // Ignored + } String message; if (status != null && status.length() > 0) message = status + " (" + code + ')'; //$NON-NLS-1$ @@ -612,15 +617,50 @@ public class GitHubClient { } /** - * Get response stream from URI. It is the responsibility of the calling - * method to close the returned stream. + * Get response stream from GET to URI. It is the responsibility of the + * calling method to close the returned stream. * * @param request * @return stream * @throws IOException */ - public InputStream getStream(GitHubRequest request) throws IOException { - return getStream(createGet(request.generateUri())); + public InputStream getStream(final GitHubRequest request) + throws IOException { + return getResponseStream(createGet(request.generateUri())); + } + + /** + * Get response stream from POST to URI. It is the responsibility of the + * calling method to close the returned stream. + * + * @param uri + * @param params + * @return stream + * @throws IOException + */ + public InputStream postStream(final String uri, final Object params) + throws IOException { + HttpURLConnection connection = createPost(uri); + sendParams(connection, params); + updateRateLimits(connection); + return getResponseStream(connection); + } + + /** + * Get response stream for request + * + * @param request + * @return stream + * @throws IOException + */ + protected InputStream getResponseStream(final HttpURLConnection request) + throws IOException { + InputStream stream = getStream(request); + int code = request.getResponseCode(); + if (isOk(code)) + return stream; + else + throw createException(stream, code, request.getResponseMessage()); } /** diff --git a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/IGitHubConstants.java b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/IGitHubConstants.java index 6f96923e..8ff5a476 100644 --- a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/IGitHubConstants.java +++ b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/IGitHubConstants.java @@ -123,6 +123,8 @@ public interface IGitHubConstants { /** */ String SEGMENT_LANGUAGES = "/languages"; //$NON-NLS-1$ /** */ + String SEGMENT_MARKDOWN = "/markdown"; //$NON-NLS-1$ + /** */ String SEGMENT_MEMBERS = "/members"; //$NON-NLS-1$ /** */ String SEGMENT_MERGE = "/merge"; //$NON-NLS-1$ diff --git a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/service/MarkdownService.java b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/service/MarkdownService.java new file mode 100644 index 00000000..788585f8 --- /dev/null +++ b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/service/MarkdownService.java @@ -0,0 +1,148 @@ +/****************************************************************************** + * Copyright (c) 2012 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * 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 + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *****************************************************************************/ +package org.eclipse.egit.github.core.service; + +import static org.eclipse.egit.github.core.client.IGitHubConstants.CHARSET_UTF8; +import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_MARKDOWN; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.egit.github.core.IRepositoryIdProvider; +import org.eclipse.egit.github.core.client.GitHubClient; + +/** + * Service to request Markdown text to be rendered as HTML + * + * @see <a href="http://developer.github.com/v3/markdown/">GitHub Markdown API + * documentation</a> + */ +public class MarkdownService extends GitHubService { + + /** + * GitHub-flavored Markdown mode + */ + public static final String MODE_GFM = "gfm"; + + /** + * Default Markdown mode + */ + public static final String MODE_MARKDOWN = "markdown"; + + /** + * Create Markdown service + */ + public MarkdownService() { + super(); + } + + /** + * Create Markdown service for client + * + * @param client + */ + public MarkdownService(final GitHubClient client) { + super(client); + } + + private String readStream(final InputStream stream) throws IOException { + BufferedReader reader = new BufferedReader(new InputStreamReader( + stream, CHARSET_UTF8)); + try { + StringBuilder output = new StringBuilder(); + char[] buffer = new char[8192]; + int read; + while ((read = reader.read(buffer)) != -1) + output.append(buffer, 0, read); + return output.toString(); + } finally { + try { + reader.close(); + } catch (IOException ignored) { + // Ignored + } + } + } + + /** + * Get stream of HTML for given Markdown text scoped to given repository + * context + * + * @param repo + * @param text + * @return stream of HTML + * @throws IOException + */ + public InputStream getRepositoryStream(final IRepositoryIdProvider repo, + final String text) throws IOException { + String context = getId(repo); + + Map<String, String> params = new HashMap<String, String>(3, 1); + params.put("context", context); + params.put("text", text); + params.put("mode", MODE_GFM); + + return client.postStream(SEGMENT_MARKDOWN, params); + } + + /** + * Get HTML for given Markdown text scoped to given repository context + * + * @param repo + * @param text + * @return HTML + * @throws IOException + */ + public String getRepositoryHtml(final IRepositoryIdProvider repo, + final String text) throws IOException { + return readStream(getRepositoryStream(repo, text)); + } + + /** + * Get stream of HTML for given Markdown text + * <p> + * Use {@link #getRepositoryStream(IRepositoryIdProvider, String)} if you + * want the Markdown scoped to a specific repository. + * + * @param text + * @param mode + * @return stream of HTML + * @throws IOException + */ + public InputStream getStream(final String text, final String mode) + throws IOException { + Map<String, String> params = new HashMap<String, String>(2, 1); + params.put("text", text); + params.put("mode", mode); + + return client.postStream(SEGMENT_MARKDOWN, params); + } + + /** + * Get HTML for given Markdown text + * <p> + * Use {@link #getRepositoryHtml(IRepositoryIdProvider, String)} if you want + * the Markdown scoped to a specific repository. + * + * @param text + * @param mode + * @return HTML + * @throws IOException + */ + public String getHtml(final String text, final String mode) + throws IOException { + return readStream(getStream(text, mode)); + } +} |