diff options
Diffstat (limited to 'org.eclipse.egit.github.core/src/org/eclipse/egit')
5 files changed, 382 insertions, 12 deletions
diff --git a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/RepositoryContents.java b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/RepositoryContents.java new file mode 100644 index 00000000..f5527fe7 --- /dev/null +++ b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/RepositoryContents.java @@ -0,0 +1,156 @@ +/****************************************************************************** + * 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; + +import java.io.Serializable; + +/** + * Contents of a path in a repository + */ +public class RepositoryContents implements Serializable { + + private static final long serialVersionUID = -70974727412738287L; + + /** ENCODING_BASE64 */ + public static final String ENCODING_BASE64 = "base64"; //$NON-NLS-1$ + + /** TYPE_FILE */ + public static final String TYPE_FILE = "file"; //$NON-NLS-1$ + + /** TYPE_DIR */ + public static final String TYPE_DIR = "dir"; //$NON-NLS-1$ + + private long size; + + private String content; + + private String encoding; + + private String name; + + private String path; + + private String sha; + + private String type; + + /** + * @return size + */ + public long getSize() { + return size; + } + + /** + * @param size + * @return this contents + */ + public RepositoryContents setSize(long size) { + this.size = size; + return this; + } + + /** + * @return content + */ + public String getContent() { + return content; + } + + /** + * @param content + * @return this contents + */ + public RepositoryContents setContent(String content) { + this.content = content; + return this; + } + + /** + * @return encoding + */ + public String getEncoding() { + return encoding; + } + + /** + * @param encoding + * @return this contents + */ + public RepositoryContents setEncoding(String encoding) { + this.encoding = encoding; + return this; + } + + /** + * @return name + */ + public String getName() { + return name; + } + + /** + * @param name + * @return this contents + */ + public RepositoryContents setName(String name) { + this.name = name; + return this; + } + + /** + * @return path + */ + public String getPath() { + return path; + } + + /** + * @param path + * @return this contents + */ + public RepositoryContents setPath(String path) { + this.path = path; + return this; + } + + /** + * @return type + */ + public String getType() { + return type; + } + + /** + * @param type + * @return this contents + */ + public RepositoryContents setType(String type) { + this.type = type; + return this; + } + + /** + * @return sha + */ + public String getSha() { + return sha; + } + + /** + * @param sha + * @return this contents + */ + public RepositoryContents setSha(String sha) { + this.sha = sha; + return this; + } +} 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 ea8359e5..a5c1fe48 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 @@ -11,6 +11,7 @@ *******************************************************************************/ package org.eclipse.egit.github.core.client; +import static com.google.gson.stream.JsonToken.BEGIN_ARRAY; import static java.net.HttpURLConnection.HTTP_ACCEPTED; import static java.net.HttpURLConnection.HTTP_BAD_REQUEST; import static java.net.HttpURLConnection.HTTP_CONFLICT; @@ -33,6 +34,7 @@ import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_V3_AP import com.google.gson.Gson; import com.google.gson.JsonParseException; +import com.google.gson.stream.JsonReader; import java.io.BufferedOutputStream; import java.io.BufferedReader; @@ -398,20 +400,56 @@ public class GitHubClient { * @throws IOException */ protected <V> V parseJson(InputStream stream, Type type) throws IOException { + return parseJson(stream, type, null); + } + + /** + * Parse JSON to specified type + * + * @param <V> + * @param stream + * @param type + * @param listType + * @return parsed type + * @throws IOException + */ + protected <V> V parseJson(InputStream stream, Type type, Type listType) + throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader( stream, CHARSET_UTF8), bufferSize); - try { - return gson.fromJson(reader, type); - } catch (JsonParseException jpe) { - IOException ioe = new IOException( - "Parse exception converting JSON to object"); //$NON-NLS-1$ - ioe.initCause(jpe); - throw ioe; - } finally { + if (listType == null) try { - reader.close(); - } catch (IOException ignored) { - // Ignored + return gson.fromJson(reader, type); + } catch (JsonParseException jpe) { + IOException ioe = new IOException( + "Parse exception converting JSON to object"); //$NON-NLS-1$ + ioe.initCause(jpe); + throw ioe; + } finally { + try { + reader.close(); + } catch (IOException ignored) { + // Ignored + } + } + else { + JsonReader jsonReader = new JsonReader(reader); + try { + if (jsonReader.peek() == BEGIN_ARRAY) + return gson.fromJson(jsonReader, listType); + else + return gson.fromJson(jsonReader, type); + } catch (JsonParseException jpe) { + IOException ioe = new IOException( + "Parse exception converting JSON to object"); //$NON-NLS-1$ + ioe.initCause(jpe); + throw ioe; + } finally { + try { + jsonReader.close(); + } catch (IOException ignored) { + // Ignored + } } } } @@ -488,7 +526,7 @@ public class GitHubClient { throws IOException { Type type = request.getType(); if (type != null) - return parseJson(stream, type); + return parseJson(stream, type, request.getArrayType()); else return null; } diff --git a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GitHubRequest.java b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GitHubRequest.java index e4edca46..23502747 100644 --- a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GitHubRequest.java +++ b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GitHubRequest.java @@ -32,6 +32,8 @@ public class GitHubRequest { private String responseContentType; + private Type arrayType; + /** * Create empty request */ @@ -40,6 +42,24 @@ public class GitHubRequest { } /** + * Set type to expect if first token is a beginning of an array + * + * @param arrayType + * @return this request + */ + public GitHubRequest setArrayType(Type arrayType) { + this.arrayType = arrayType; + return this; + } + + /** + * @return arrayType + */ + public Type getArrayType() { + return arrayType; + } + + /** * @return uri */ public String getUri() { 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 8ff5a476..83dc0206 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 @@ -83,6 +83,8 @@ public interface IGitHubConstants { /** */ String SEGMENT_COMMENTS = "/comments"; //$NON-NLS-1$ /** */ + String SEGMENT_CONTENTS= "/contents"; //$NON-NLS-1$ + /** */ String SEGMENT_CONTRIBUTORS = "/contributors"; //$NON-NLS-1$ /** */ String SEGMENT_COMMITS = "/commits"; //$NON-NLS-1$ @@ -143,6 +145,8 @@ public interface IGitHubConstants { /** */ String SEGMENT_PULLS = "/pulls"; //$NON-NLS-1$ /** */ + String SEGMENT_README = "/readme"; //$NON-NLS-1$ + /** */ String SEGMENT_RECEIVED_EVENTS = "/received_events"; //$NON-NLS-1$ /** */ String SEGMENT_REFS = "/refs"; //$NON-NLS-1$ diff --git a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/service/ContentsService.java b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/service/ContentsService.java new file mode 100644 index 00000000..02326f5f --- /dev/null +++ b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/service/ContentsService.java @@ -0,0 +1,152 @@ +/****************************************************************************** + * Copyright (c) 2011 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.SEGMENT_CONTENTS; +import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_README; +import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_REPOS; + +import com.google.gson.reflect.TypeToken; + +import java.io.IOException; +import java.util.Collections; +import java.util.List; + +import org.eclipse.egit.github.core.IRepositoryIdProvider; +import org.eclipse.egit.github.core.RepositoryContents; +import org.eclipse.egit.github.core.client.GitHubClient; +import org.eclipse.egit.github.core.client.GitHubRequest; + +/** + * Service for accessing repository contents + * + * @see <a href="http://developer.github.com/v3/repos/contents">GitHub contents + * API documentation</a> + */ +public class ContentsService extends GitHubService { + + /** + * Create contents service + */ + public ContentsService() { + super(); + } + + /** + * Create contents service + * + * @param client + */ + public ContentsService(final GitHubClient client) { + super(client); + } + + /** + * Get repository README + * + * @param repository + * @return README + * @throws Exception + */ + public RepositoryContents getReadme(IRepositoryIdProvider repository) + throws Exception { + return getReadme(repository, null); + } + + /** + * Get repository README + * + * @param repository + * @param ref + * @return README + * @throws IOException + */ + public RepositoryContents getReadme(IRepositoryIdProvider repository, + String ref) throws IOException { + String id = getId(repository); + + StringBuilder uri = new StringBuilder(SEGMENT_REPOS); + uri.append('/').append(id); + uri.append(SEGMENT_README); + GitHubRequest request = createRequest(); + request.setUri(uri); + if (ref != null && ref.length() > 0) + request.setParams(Collections.singletonMap("ref", ref)); + request.setType(RepositoryContents.class); + return (RepositoryContents) client.get(request).getBody(); + } + + /** + * Get contents at the root of the given repository on master branch + * + * @param repository + * @return list of contents at root + * @throws IOException + */ + public List<RepositoryContents> getContents(IRepositoryIdProvider repository) + throws IOException { + return getContents(repository, null); + } + + /** + * Get contents at path in the given repository on master branch + * + * @param repository + * @param path + * @return list of contents at path + * @throws IOException + */ + public List<RepositoryContents> getContents( + IRepositoryIdProvider repository, String path) throws IOException { + return getContents(repository, path, null); + } + + /** + * Get contents of path at reference in given repository + * <p> + * For file paths this will return a list with one entry corresponding to + * the file contents at the given path + * + * @param repository + * @param path + * @param ref + * @return list of contents at path + * @throws IOException + */ + @SuppressWarnings("unchecked") + public List<RepositoryContents> getContents( + IRepositoryIdProvider repository, String path, String ref) + throws IOException { + String id = getId(repository); + + StringBuilder uri = new StringBuilder(SEGMENT_REPOS); + uri.append('/').append(id); + uri.append(SEGMENT_CONTENTS); + if (path != null && path.length() > 0) { + if (path.charAt(0) != '/') + uri.append('/'); + uri.append(path); + } + GitHubRequest request = createRequest(); + request.setUri(uri); + request.setType(RepositoryContents.class); + request.setArrayType(new TypeToken<List<RepositoryContents>>() { + }.getType()); + if (ref != null && ref.length() > 0) + request.setParams(Collections.singletonMap("ref", ref)); + + Object body = client.get(request).getBody(); + if (body instanceof RepositoryContents) + return Collections.singletonList((RepositoryContents) body); + else + return (List<RepositoryContents>) body; + } +} |