aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Sawicki2011-05-02 19:40:50 (EDT)
committerChris Aniszczyk2011-05-03 10:06:56 (EDT)
commitf6a43aac89499faa78a2c30fb2d0415dc37b800a (patch)
treeab70f672d7f761ac359b5543b90f6feed38c1b15
parent6180fab51874473a6dc4f423f1d278d528dd5978 (diff)
downloadegit-github-f6a43aac89499faa78a2c30fb2d0415dc37b800a.zip
egit-github-f6a43aac89499faa78a2c30fb2d0415dc37b800a.tar.gz
egit-github-f6a43aac89499faa78a2c30fb2d0415dc37b800a.tar.bz2
Add response class and link header supportrefs/changes/21/3321/2
Responses that contain paginated results contain a Link header to request uris relative to the current response. Change-Id: I372bb065b6fa2937c0039491851a03547f7a0b90 Signed-off-by: Kevin Sawicki <kevin@github.com> Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
-rw-r--r--org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubResponse.java80
-rw-r--r--org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/PageLinks.java112
-rw-r--r--org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/PagedRequest.java42
3 files changed, 234 insertions, 0 deletions
diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubResponse.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubResponse.java
new file mode 100644
index 0000000..e10d39a
--- /dev/null
+++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubResponse.java
@@ -0,0 +1,80 @@
+/*******************************************************************************
+ * 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.mylyn.github.internal;
+
+import org.apache.commons.httpclient.HttpMethod;
+
+/**
+ * GitHub API response class.
+ */
+public class GitHubResponse {
+
+ private PageLinks links;
+
+ private Object body;
+
+ /**
+ * Create response
+ *
+ * @param method
+ * @param body
+ */
+ public GitHubResponse(HttpMethod method, Object body) {
+ links = new PageLinks(method);
+ this.body = body;
+ }
+
+ /**
+ * Get link uri to first page
+ *
+ * @return possibly null uri
+ */
+ public String getFirst() {
+ return this.links.getFirst();
+ }
+
+ /**
+ * Get link uri to previous page
+ *
+ * @return possibly null uri
+ */
+ public String getPrevious() {
+ return this.links.getPrev();
+ }
+
+ /**
+ * Get link uri to next page
+ *
+ * @return possibly null uri
+ */
+ public String getNext() {
+ return this.links.getNext();
+ }
+
+ /**
+ * Get link uri to last page
+ *
+ * @return possibly null uri
+ */
+ public String getLast() {
+ return this.links.getLast();
+ }
+
+ /**
+ * Parsed response body
+ *
+ * @return body
+ */
+ public Object getBody() {
+ return this.body;
+ }
+
+}
diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/PageLinks.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/PageLinks.java
new file mode 100644
index 0000000..0d5a030
--- /dev/null
+++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/PageLinks.java
@@ -0,0 +1,112 @@
+/*******************************************************************************
+ * 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.mylyn.github.internal;
+
+import org.apache.commons.httpclient.Header;
+import org.apache.commons.httpclient.HttpMethod;
+
+/**
+ * Page link class to be used to determine the links to other pages of request
+ * responses encoded in the current response. These will be present if the
+ * result set size exceeds the per page limit.
+ */
+public class PageLinks {
+
+ private static final String DELIM_LINKS = ","; //$NON-NLS-1$
+
+ private static final String DELIM_LINK_PARAM = ";"; //$NON-NLS-1;$
+
+ private String first;
+ private String last;
+ private String next;
+ private String prev;
+
+ /**
+ * Parse links from executed method
+ *
+ * @param method
+ */
+ public PageLinks(HttpMethod method) {
+ Header[] linkHeaders = method
+ .getResponseHeaders(IGitHubConstants.HEADER_LINK);
+ if (linkHeaders.length > 0) {
+ String[] links = linkHeaders[0].getValue().split(DELIM_LINKS);
+ for (String link : links) {
+ String[] segments = link.split(DELIM_LINK_PARAM);
+ if (segments.length < 2)
+ continue;
+
+ String linkPart = segments[0].trim();
+ if (!linkPart.startsWith("<") || !linkPart.endsWith(">")) //$NON-NLS-1$ //$NON-NLS-2$
+ continue;
+ linkPart = linkPart.substring(1, linkPart.length() - 1);
+
+ for (int i = 1; i < segments.length; i++) {
+ String[] rel = segments[i].trim().split("="); //$NON-NLS-1$
+ if (rel.length < 2
+ || !IGitHubConstants.META_REL.equals(rel[0]))
+ continue;
+
+ String relValue = rel[1];
+ if (relValue.startsWith("\"") && relValue.endsWith("\"")) //$NON-NLS-1$ //$NON-NLS-2$
+ relValue = relValue.substring(1, relValue.length() - 1);
+
+ if (IGitHubConstants.META_FIRST.equals(relValue))
+ first = linkPart;
+ else if (IGitHubConstants.META_LAST.equals(relValue))
+ last = linkPart;
+ else if (IGitHubConstants.META_NEXT.equals(relValue))
+ next = linkPart;
+ else if (IGitHubConstants.META_PREV.equals(relValue))
+ prev = linkPart;
+ }
+ }
+ } else {
+ Header[] nextHeaders = method
+ .getResponseHeaders(IGitHubConstants.HEADER_NEXT);
+ if (nextHeaders.length > 0)
+ next = nextHeaders[0].getValue();
+
+ Header[] lastHeaders = method
+ .getResponseHeaders(IGitHubConstants.HEADER_LAST);
+ if (lastHeaders.length > 0)
+ last = lastHeaders[0].getValue();
+ }
+ }
+
+ /**
+ * @return first
+ */
+ public String getFirst() {
+ return this.first;
+ }
+
+ /**
+ * @return last
+ */
+ public String getLast() {
+ return this.last;
+ }
+
+ /**
+ * @return next
+ */
+ public String getNext() {
+ return this.next;
+ }
+
+ /**
+ * @return prev
+ */
+ public String getPrev() {
+ return this.prev;
+ }
+}
diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/PagedRequest.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/PagedRequest.java
new file mode 100644
index 0000000..b61f0e0
--- /dev/null
+++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/PagedRequest.java
@@ -0,0 +1,42 @@
+/*******************************************************************************
+ * 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.mylyn.github.internal;
+
+import org.eclipse.core.runtime.Assert;
+
+/**
+ * Pages request class that contains a collector for accept resources page by
+ * page.
+ *
+ * @param <V>
+ */
+public class PagedRequest<V> extends GitHubRequest {
+
+ private IResourceCollector<V> collector;
+
+ /**
+ * Create paged request with non-null collector
+ *
+ * @param collector
+ */
+ public PagedRequest(IResourceCollector<V> collector) {
+ Assert.isNotNull(collector, "Collecto cannot be null"); //$NON-NLS-1$
+ this.collector = collector;
+ }
+
+ /**
+ * @return collector
+ */
+ public IResourceCollector<V> getCollector() {
+ return this.collector;
+ }
+
+}