Skip to content

Commit ca1ffde

Browse files
committed
* implemented simple repo browser
0 parents  commit ca1ffde

8 files changed

Lines changed: 469 additions & 0 deletions

File tree

pom.xml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>de.linsin.sample</groupId>
6+
<artifactId>github-resttemplate</artifactId>
7+
<version>0.0.1</version>
8+
9+
<dependencies>
10+
<dependency>
11+
<groupId>org.springframework</groupId>
12+
<artifactId>org.springframework.web</artifactId>
13+
<version>3.0.0.M4</version>
14+
</dependency>
15+
<dependency>
16+
<groupId>org.springframework.ws</groupId>
17+
<artifactId>org.springframework.xml</artifactId>
18+
<version>1.5.5.A</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.apache.log4j</groupId>
22+
<artifactId>com.springsource.org.apache.log4j</artifactId>
23+
<version>1.2.15</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.json</groupId>
27+
<artifactId>json</artifactId>
28+
<version>20090211</version>
29+
<scope>compile</scope>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.codehaus.jackson</groupId>
33+
<artifactId>jackson-mapper-lgpl</artifactId>
34+
<version>0.9.9-6</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>junit</groupId>
38+
<artifactId>junit</artifactId>
39+
<version>4.6</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.easymock</groupId>
43+
<artifactId>easymockclassextension</artifactId>
44+
<version>2.4</version>
45+
</dependency>
46+
</dependencies>
47+
48+
<build>
49+
<plugins>
50+
<plugin>
51+
<groupId>org.apache.maven.plugins</groupId>
52+
<artifactId>maven-compiler-plugin</artifactId>
53+
<configuration>
54+
<source>1.5</source>
55+
<target>1.5</target>
56+
</configuration>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
61+
<repositories>
62+
<repository>
63+
<id>s2-snapshot</id>
64+
<url>http://repository.springsource.com/maven/bundles/snapshot</url>
65+
<snapshots>
66+
<enabled>true</enabled>
67+
</snapshots>
68+
</repository>
69+
<repository>
70+
<id>s2-release</id>
71+
<url>http://repository.springsource.com/maven/bundles/release</url>
72+
</repository>
73+
<repository>
74+
<id>s2-external</id>
75+
<url>http://repository.springsource.com/maven/bundles/external</url>
76+
</repository>
77+
</repositories>
78+
79+
</project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package de.linsin.sample.github;
2+
3+
import de.linsin.sample.github.rest.domain.RepositoriesResponse;
4+
import org.springframework.http.converter.HttpMessageConverter;
5+
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
6+
import org.springframework.web.client.RestTemplate;
7+
8+
/**
9+
* TODO document
10+
*
11+
* @author David Linsin - [email protected]
12+
*/
13+
public class StartUp {
14+
public static void main(String[] args) {
15+
RestTemplate template = new RestTemplate();
16+
// template.setMessageConverters(new HttpMessageConverter[] {new MappingJacksonHttpMessageConverter<RepositoryResponse>()});
17+
// RepositoryResponse object = template.getForObject("http://github.com/api/v2/json/repos/show/{username}/{repo}", RepositoryResponse.class, "dlinsin", "area51");
18+
template.setMessageConverters(new HttpMessageConverter[] {new MappingJacksonHttpMessageConverter<RepositoriesResponse>()});
19+
String test = null;
20+
RepositoriesResponse object = template.getForObject("http://github.com/api/v2/json/repos/show/{username}", RepositoriesResponse.class, test);
21+
// template.setMessageConverters(new HttpMessageConverter[]{new MappingJacksonHttpMessageConverter()});
22+
// Map object = template.getForObject("http://github.com/api/v2/json/repos/show/{username}", HashMap.class, "dlinsin");
23+
System.out.println("object = " + object);
24+
}
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package de.linsin.sample.github.rest.domain;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Represents a set of {@link Repository} instances
7+
*
8+
* @author David Linsin - [email protected]
9+
*/
10+
public class RepositoriesResponse {
11+
private Repository[] repositories;
12+
13+
public Repository[] getRepositories() {
14+
return repositories;
15+
}
16+
17+
public void setRepositories(Repository[] argRepositories) {
18+
repositories = argRepositories;
19+
}
20+
21+
@Override
22+
public String toString() {
23+
return "RepositoriesResponse{" +
24+
"repositories=" + (repositories == null ? null : Arrays.asList(repositories)) +
25+
'}';
26+
}
27+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package de.linsin.sample.github.rest.domain;
2+
3+
/**
4+
* Represents a GitHub repository
5+
*
6+
* @author David Linsin - [email protected]
7+
*/
8+
public class Repository {
9+
private String description;
10+
private int forks;
11+
private int open_issues;
12+
private int watchers;
13+
private String url;
14+
private String homepage;
15+
private String fork;
16+
private String is_private;
17+
private String name;
18+
private String owner;
19+
20+
public String getDescription() {
21+
return description;
22+
}
23+
24+
public void setDescription(String argDescription) {
25+
description = argDescription;
26+
}
27+
28+
public int getForks() {
29+
return forks;
30+
}
31+
32+
public void setForks(int argForks) {
33+
forks = argForks;
34+
}
35+
36+
public int getOpen_issues() {
37+
return open_issues;
38+
}
39+
40+
public void setOpen_issues(int argOpen_issues) {
41+
open_issues = argOpen_issues;
42+
}
43+
44+
public int getWatchers() {
45+
return watchers;
46+
}
47+
48+
public void setWatchers(int argWatchers) {
49+
watchers = argWatchers;
50+
}
51+
52+
public String getUrl() {
53+
return url;
54+
}
55+
56+
public void setUrl(String argUrl) {
57+
url = argUrl;
58+
}
59+
60+
public String getHomepage() {
61+
return homepage;
62+
}
63+
64+
public void setHomepage(String argHomepage) {
65+
homepage = argHomepage;
66+
}
67+
68+
public String getFork() {
69+
return fork;
70+
}
71+
72+
public void setFork(String argFork) {
73+
fork = argFork;
74+
}
75+
76+
public String getPrivate() {
77+
return is_private;
78+
}
79+
80+
public void setPrivate(String argIs_private) {
81+
is_private = argIs_private;
82+
}
83+
84+
public String getName() {
85+
return name;
86+
}
87+
88+
public void setName(String argName) {
89+
name = argName;
90+
}
91+
92+
public String getOwner() {
93+
return owner;
94+
}
95+
96+
public void setOwner(String argOwner) {
97+
owner = argOwner;
98+
}
99+
100+
@Override
101+
public boolean equals(Object o) {
102+
if (this == o) return true;
103+
if (o == null || getClass() != o.getClass()) return false;
104+
105+
Repository that = (Repository) o;
106+
107+
if (url != null ? !url.equals(that.url) : that.url != null) return false;
108+
109+
return true;
110+
}
111+
112+
@Override
113+
public int hashCode() {
114+
int result = description != null ? description.hashCode() : 0;
115+
result = 31 * result + forks;
116+
result = 31 * result + open_issues;
117+
result = 31 * result + watchers;
118+
result = 31 * result + (url != null ? url.hashCode() : 0);
119+
result = 31 * result + (homepage != null ? homepage.hashCode() : 0);
120+
result = 31 * result + (fork != null ? fork.hashCode() : 0);
121+
result = 31 * result + (is_private != null ? is_private.hashCode() : 0);
122+
result = 31 * result + (name != null ? name.hashCode() : 0);
123+
result = 31 * result + (owner != null ? owner.hashCode() : 0);
124+
return result;
125+
}
126+
127+
@Override
128+
public String toString() {
129+
return "Repository{" +
130+
"description='" + description + '\'' +
131+
", forks=" + forks +
132+
", open_issues=" + open_issues +
133+
", watchers=" + watchers +
134+
", url='" + url + '\'' +
135+
", homepage='" + homepage + '\'' +
136+
", fork='" + fork + '\'' +
137+
", is_private='" + is_private + '\'' +
138+
", name='" + name + '\'' +
139+
", owner='" + owner + '\'' +
140+
'}';
141+
}
142+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package de.linsin.sample.github.rest.domain;
2+
3+
/**
4+
* Represents a response containing a {@link Repository}
5+
*
6+
* @author David Linsin - [email protected]
7+
*/
8+
public class RepositoryResponse {
9+
private Repository repository;
10+
11+
public Repository getRepository() {
12+
return repository;
13+
}
14+
15+
public void setRepository(Repository argRepository) {
16+
repository = argRepository;
17+
}
18+
19+
@Override
20+
public String toString() {
21+
return "RepositoryResponse{" +
22+
"repository=" + repository +
23+
'}';
24+
}
25+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package de.linsin.sample.github.rest.service;
2+
3+
/**
4+
* TODO document
5+
*
6+
* @author David Linsin - [email protected]
7+
*/
8+
public class IssueBrowser {
9+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package de.linsin.sample.github.rest.service;
2+
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
import de.linsin.sample.github.rest.domain.RepositoriesResponse;
8+
import de.linsin.sample.github.rest.domain.Repository;
9+
import de.linsin.sample.github.rest.domain.RepositoryResponse;
10+
import org.springframework.http.converter.HttpMessageConverter;
11+
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
12+
import org.springframework.web.client.RestTemplate;
13+
14+
/**
15+
* Provides methods to browser through a GitHub repository
16+
*
17+
* @author David Linsin - [email protected]
18+
*/
19+
public class RepositoryBrowser {
20+
public static final String BASE_URL = "http://github.com/api/v2/json/";
21+
public static final String REPOSITORIES_URL = BASE_URL.concat("repos/show/{username}");
22+
public static final String REPOSITORY_URL = REPOSITORIES_URL.concat("/{repo}");
23+
24+
/**
25+
* Browse repositories for a user
26+
*
27+
* @param argUsername {@link String} username
28+
* @return {@link List} containing {@link Repository} instances, empty List if no repositories are found
29+
* @throws {@link NullPointerException} in case passed username is null
30+
*/
31+
public List<Repository> browse(String argUsername) {
32+
RestTemplate template = initTemplate();
33+
RepositoriesResponse resp = template.getForObject(REPOSITORIES_URL, RepositoriesResponse.class, argUsername);
34+
if (resp == null || resp.getRepositories() == null || resp.getRepositories().length == 0) {
35+
return Collections.emptyList();
36+
} else {
37+
return Arrays.asList(resp.getRepositories());
38+
}
39+
}
40+
41+
/**
42+
* Browse a single respository for a user
43+
*
44+
* @param argUsername {@link String} username
45+
* @param argRepositoryname {@link String} name of repository
46+
* @return {@link Repository} instance denoted by passed user and repository name, null if no repository was found
47+
* @throws {@link NullPointerException} in case passed username or repository name is null
48+
*/
49+
public Repository browse(String argUsername, String argRepositoryname) {
50+
RestTemplate template = initTemplate();
51+
RepositoryResponse resp = template.getForObject(REPOSITORY_URL, RepositoryResponse.class, argUsername, argRepositoryname);
52+
if (resp != null) {
53+
return resp.getRepository();
54+
}
55+
return null;
56+
}
57+
58+
protected RestTemplate initTemplate() {
59+
RestTemplate template = new RestTemplate();
60+
template.setMessageConverters(new HttpMessageConverter[] {new MappingJacksonHttpMessageConverter()});
61+
return template;
62+
}
63+
64+
}

0 commit comments

Comments
 (0)