Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6d9bbabd460ae91eaad813d3c138a034890e63ab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*******************************************************************************
 *  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.client;

import static org.eclipse.egit.github.core.client.IGitHubConstants.HEADER_LAST;
import static org.eclipse.egit.github.core.client.IGitHubConstants.HEADER_LINK;
import static org.eclipse.egit.github.core.client.IGitHubConstants.HEADER_NEXT;
import static org.eclipse.egit.github.core.client.IGitHubConstants.META_FIRST;
import static org.eclipse.egit.github.core.client.IGitHubConstants.META_LAST;
import static org.eclipse.egit.github.core.client.IGitHubConstants.META_NEXT;
import static org.eclipse.egit.github.core.client.IGitHubConstants.META_PREV;
import static org.eclipse.egit.github.core.client.IGitHubConstants.META_REL;

import org.apache.http.Header;
import org.apache.http.HttpResponse;

/**
 * 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 response
	 */
	public PageLinks(HttpResponse response) {
		Header[] linkHeaders = response.getHeaders(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 || !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 (META_FIRST.equals(relValue))
						first = linkPart;
					else if (META_LAST.equals(relValue))
						last = linkPart;
					else if (META_NEXT.equals(relValue))
						next = linkPart;
					else if (META_PREV.equals(relValue))
						prev = linkPart;
				}
			}
		} else {
			Header[] nextHeaders = response.getHeaders(HEADER_NEXT);
			if (nextHeaders.length > 0)
				next = nextHeaders[0].getValue();

			Header[] lastHeaders = response.getHeaders(HEADER_LAST);
			if (lastHeaders.length > 0)
				last = lastHeaders[0].getValue();
		}
	}

	/**
	 * @return first
	 */
	public String getFirst() {
		return first;
	}

	/**
	 * @return last
	 */
	public String getLast() {
		return last;
	}

	/**
	 * @return next
	 */
	public String getNext() {
		return next;
	}

	/**
	 * @return prev
	 */
	public String getPrev() {
		return prev;
	}
}

Back to the top