Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #17 from racker/pagination_support

Pagination support
  • Loading branch information...
commit 12739b99142240fb4f1b9f5ca04aaa273d698e90 2 parents 2c581d5 + c2bd995
@Kami Kami authored
View
1  .gitignore
@@ -2,3 +2,4 @@ target/
out/
lib/slf4j-simple-1.7.2.jar
src/main/java/com/rackspacecloud/client/service_registry/Main.java
+*.iml
View
BIN  lib/mockito-all-1.9.5.jar
Binary file not shown
View
8 pom.xml
@@ -11,7 +11,7 @@
<name>service-registry-client</name>
<artifactId>service-registry-client</artifactId>
<groupId>com.rackspacecloud</groupId>
- <version>1.1.5-SNAPSHOT</version>
+ <version>1.2.0-SNAPSHOT</version>
<packaging>jar</packaging>
<url>https://github.com/racker/java-service-registry-client</url>
@@ -71,6 +71,12 @@
<version>4.10</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-all</artifactId>
+ <version>1.9.5</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
View
49 src/main/java/com/rackspacecloud/client/service_registry/PaginationOptions.java
@@ -0,0 +1,49 @@
+/**
+ * Copyright 2012 Rackspace
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package com.rackspacecloud.client.service_registry;
+
+public class PaginationOptions {
+ private Integer limit;
+ private String marker;
+
+ public PaginationOptions() {
+ }
+
+ public PaginationOptions(Integer limit, String marker) {
+ this.limit = limit;
+ this.marker = marker;
+ }
+
+ public PaginationOptions withLimit(Integer limit) {
+ this.limit = limit;
+ return this;
+ }
+
+ public PaginationOptions withMarker(String marker) {
+ this.marker = marker;
+ return this;
+ }
+
+ public Integer getLimit() {
+ return limit;
+ }
+
+ public String getMarker() {
+ return marker;
+ }
+}
View
17 src/main/java/com/rackspacecloud/client/service_registry/Region.java
@@ -1,3 +1,20 @@
+/**
+ * Copyright 2012 Rackspace
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
package com.rackspacecloud.client.service_registry;
public class Region {
View
20 src/main/java/com/rackspacecloud/client/service_registry/clients/BaseClient.java
@@ -31,6 +31,7 @@
import com.google.gson.GsonBuilder;
import com.rackspacecloud.client.service_registry.Client;
import com.rackspacecloud.client.service_registry.ClientResponse;
+import com.rackspacecloud.client.service_registry.PaginationOptions;
import com.rackspacecloud.client.service_registry.events.client.ClientEvent;
import com.rackspacecloud.client.service_registry.events.client.ClientEventListener;
import com.rackspacecloud.client.service_registry.events.client.ClientEventThread;
@@ -48,6 +49,7 @@
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
+import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.slf4j.Logger;
@@ -117,6 +119,24 @@ public void run() {
}
}
}
+
+ protected ClientResponse performListRequest(PaginationOptions paginationOptions, String path, List<NameValuePair> params, HttpRequestBase method, boolean parseAsJson, Type responseType) throws Exception {
+ if (params == null) {
+ params = new ArrayList<NameValuePair>();
+ }
+
+ if (paginationOptions != null) {
+ if (paginationOptions.getLimit() != null) {
+ params.add(new BasicNameValuePair("limit", paginationOptions.getLimit().toString()));
+ }
+
+ if (paginationOptions.getMarker() != null) {
+ params.add(new BasicNameValuePair("marker", paginationOptions.getMarker()));
+ }
+ }
+
+ return performRequest(path, params, method, parseAsJson, responseType, false, 0);
+ }
protected ClientResponse performRequest(String path, List<NameValuePair> params, HttpRequestBase method) throws Exception {
return performRequest(path, params, method, false, null, false, 0);
View
10 src/main/java/com/rackspacecloud/client/service_registry/clients/ConfigurationClient.java
@@ -19,6 +19,7 @@
import com.google.gson.reflect.TypeToken;
import com.rackspacecloud.client.service_registry.ClientResponse;
+import com.rackspacecloud.client.service_registry.PaginationOptions;
import com.rackspacecloud.client.service_registry.containers.ConfigurationValuesContainer;
import com.rackspacecloud.client.service_registry.objects.ConfigurationValue;
import org.apache.http.NameValuePair;
@@ -35,15 +36,14 @@ public ConfigurationClient(AuthClient authClient) {
super(authClient);
}
- public List<ConfigurationValue> list(Map<String, String> options) throws Exception {
- return list(options, null);
+ public List<ConfigurationValue> list(PaginationOptions paginationOptions) throws Exception {
+ return list(paginationOptions, null);
}
- public List<ConfigurationValue> list(Map<String, String> options, String namespace) throws Exception {
+ public List<ConfigurationValue> list(PaginationOptions paginationOptions, String namespace) throws Exception {
String url = "/configuration";
Type type = new TypeToken<ConfigurationValuesContainer>() {}.getType();
- List<NameValuePair> params = new ArrayList<NameValuePair>();
if (namespace != null) {
// Make sure leading and trailing forward slashes are present
@@ -59,7 +59,7 @@ public ConfigurationClient(AuthClient authClient) {
}
}
- ClientResponse response = this.performRequest(url, params, new HttpGet(), true, type);
+ ClientResponse response = this.performListRequest(paginationOptions, url, null, new HttpGet(), true, type);
ConfigurationValuesContainer container = (ConfigurationValuesContainer)response.getBody();
return container.getValues();
View
5 src/main/java/com/rackspacecloud/client/service_registry/clients/EventsClient.java
@@ -18,6 +18,7 @@
package com.rackspacecloud.client.service_registry.clients;
import com.google.gson.reflect.TypeToken;
+import com.rackspacecloud.client.service_registry.PaginationOptions;
import com.rackspacecloud.client.service_registry.events.server.*;
import com.rackspacecloud.client.service_registry.objects.*;
import org.apache.http.client.methods.HttpGet;
@@ -41,10 +42,10 @@ public EventsClient(AuthClient authClient) {
super(authClient);
}
- public List<BaseEvent> list(Map<String, String> options) throws Exception {
+ public List<BaseEvent> list(PaginationOptions paginationOptions) throws Exception {
String url = "/events";
Type type = new TypeToken<EventsContainer>() {}.getType();
- ClientResponse response = this.performRequest(url, null, new HttpGet(), true, type);
+ ClientResponse response = this.performListRequest(paginationOptions, url, null, new HttpGet(), true, type);
EventsContainer container = (EventsContainer)response.getBody();
List<BaseEvent> events = this.parseEvents(container.getValues());
View
9 src/main/java/com/rackspacecloud/client/service_registry/clients/ServicesClient.java
@@ -19,6 +19,7 @@
import com.google.gson.reflect.TypeToken;
import com.rackspacecloud.client.service_registry.ClientResponse;
+import com.rackspacecloud.client.service_registry.PaginationOptions;
import com.rackspacecloud.client.service_registry.containers.ServicesContainer;
import com.rackspacecloud.client.service_registry.objects.Service;
import com.rackspacecloud.client.service_registry.objects.Session;
@@ -39,11 +40,11 @@ public ServicesClient(AuthClient authClient) {
super(authClient);
}
- public List<Service> list(Map<String, String> options) throws Exception {
- return list(options, null);
+ public List<Service> list(PaginationOptions paginationOptions) throws Exception {
+ return list(paginationOptions, null);
}
- public List<Service> list(Map<String, String> options, String tag) throws Exception {
+ public List<Service> list(PaginationOptions paginationOptions, String tag) throws Exception {
Type type = new TypeToken<ServicesContainer>() {}.getType();
List<NameValuePair> params = new ArrayList<NameValuePair>();
@@ -51,7 +52,7 @@ public ServicesClient(AuthClient authClient) {
params.add(new BasicNameValuePair("tag", tag));
}
- ClientResponse response = this.performRequest("/services", params, new HttpGet(), true, type);
+ ClientResponse response = this.performListRequest(paginationOptions, "/services", params, new HttpGet(), true, type);
ServicesContainer container = (ServicesContainer)response.getBody();
return container.getValues();
View
9 src/main/java/com/rackspacecloud/client/service_registry/clients/SessionsClient.java
@@ -18,10 +18,7 @@
package com.rackspacecloud.client.service_registry.clients;
import com.google.gson.reflect.TypeToken;
-import com.rackspacecloud.client.service_registry.ClientResponse;
-import com.rackspacecloud.client.service_registry.HeartBeater;
-import com.rackspacecloud.client.service_registry.SessionCreateResponse;
-import com.rackspacecloud.client.service_registry.Utils;
+import com.rackspacecloud.client.service_registry.*;
import com.rackspacecloud.client.service_registry.containers.SessionsContainer;
import com.rackspacecloud.client.service_registry.objects.HeartbeatToken;
import com.rackspacecloud.client.service_registry.objects.Session;
@@ -41,9 +38,9 @@ public SessionsClient(AuthClient authClient) {
this.authClient = authClient;
}
- public List<Session> list(Map<String, String> options) throws Exception {
+ public List<Session> list(PaginationOptions paginationOptions) throws Exception {
Type type = new TypeToken<SessionsContainer>() {}.getType();
- ClientResponse response = this.performRequest("/sessions", null, new HttpGet(), true, type);
+ ClientResponse response = this.performListRequest(paginationOptions, "/sessions", null, new HttpGet(), true, type);
SessionsContainer container = (SessionsContainer)response.getBody();
return container.getValues();
View
5 src/main/java/com/rackspacecloud/client/service_registry/clients/ViewsClient.java
@@ -19,6 +19,7 @@
import com.google.gson.reflect.TypeToken;
import com.rackspacecloud.client.service_registry.ClientResponse;
+import com.rackspacecloud.client.service_registry.PaginationOptions;
import com.rackspacecloud.client.service_registry.containers.OverviewContainer;
import com.rackspacecloud.client.service_registry.objects.Overview;
import org.apache.http.client.methods.HttpGet;
@@ -32,9 +33,9 @@ public ViewsClient(AuthClient authClient) {
super(authClient);
}
- public List<Overview> getOverview(Map<String, String> options) throws Exception {
+ public List<Overview> getOverview(PaginationOptions paginationOptions) throws Exception {
Type type = new TypeToken<OverviewContainer>() {}.getType();
- ClientResponse response = this.performRequest("/views/overview", null, new HttpGet(), true, type);
+ ClientResponse response = this.performListRequest(paginationOptions, "/views/overview", null, new HttpGet(), true, type);
OverviewContainer container = (OverviewContainer)response.getBody();
return container.getValues();
View
7 src/main/java/com/rackspacecloud/client/service_registry/exceptions/ValidationException.java
@@ -46,4 +46,11 @@ public String getDetails() {
public String getTxnId() {
return txnId;
}
+
+ @Override
+ public String toString() {
+ return String.format("[ValidationException code=%s, type=%s, " +
+ "details=\"%s\" txnId=%s]", this.code, this.type,
+ this.details, this.txnId);
+ }
}
View
29 src/test/java/com/rackspacecloud/client/service_registry/util/PaginationOptionsTest.java
@@ -0,0 +1,29 @@
+package com.rackspacecloud.client.service_registry.util;
+
+import com.rackspacecloud.client.service_registry.PaginationOptions;
+import junit.framework.Assert;
+import org.junit.Test;
+
+public class PaginationOptionsTest {
+ @Test
+ public void testPaginationOptions() {
+ int actualLimit;
+ String actualMarker;
+
+ PaginationOptions po1 = new PaginationOptions().withLimit(55)
+ .withMarker("markera");
+ PaginationOptions po2 = new PaginationOptions(100, "markerb");
+
+ actualLimit = po1.getLimit();
+ actualMarker = po1.getMarker();
+
+ Assert.assertEquals(55, actualLimit);
+ Assert.assertEquals("markera", actualMarker);
+
+ actualLimit = po2.getLimit();
+ actualMarker = po2.getMarker();
+
+ Assert.assertEquals(100, actualLimit);
+ Assert.assertEquals("markerb", actualMarker);
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.