Skip to content

Commit ef24a46

Browse files
author
Evan Wong
committed
Merge pull request #26 from evanwong/release/0.4.0
Release/0.4.0
2 parents e953367 + 57a181b commit ef24a46

18 files changed

Lines changed: 403 additions & 28 deletions

README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,35 @@ To add this client into your project:
1616
<dependency>
1717
<groupId>io.evanwong.oss</groupId>
1818
<artifactId>hipchat-java</artifactId>
19-
<version>0.3.0</version>
19+
<version>0.4.0</version>
2020
</dependency>
2121
```
2222
* gradle
2323
```gradle
24-
compile 'io.evanwong.oss:hipchat-java:0.3.0'
24+
compile 'io.evanwong.oss:hipchat-java:0.4.0'
25+
```
26+
27+
If you are using Java 7, there is a temporary workaround using [retrolambda](https://github.com/orfjackal/retrolambda).
28+
29+
* maven
30+
```xml
31+
<dependency>
32+
<groupId>io.evanwong.oss</groupId>
33+
<artifactId>hipchat-java7</artifactId>
34+
<version>0.4.0</version>
35+
</dependency>
36+
```
37+
* gradle
38+
```gradle
39+
compile 'io.evanwong.oss:hipchat-java7:0.4.0'
2540
```
2641

2742
To send a notification
2843
```java
2944
String defaultAccessToken = "abcd1234";
3045
HipChatClient client = new HipChatClient(defaultAccessToken);
3146
SendRoomNotificationRequestBuilder builder = client.prepareSendRoomNotificationRequestBuilder("myRoom", "hello world!");
32-
Future<NoContent> future = builder.setColor(Color.YELLOW).setNotify(true).build().execute();
47+
Future<NoContent> future = builder.setColor(MessageColor.YELLOW).setNotify(true).build().execute();
3348
//optional... if you want/need to inspect the result:
3449
NoContent noContent = future.get();
3550
```
@@ -52,7 +67,7 @@ NoContent noContent = future.get();
5267
- [x] Get all rooms
5368
- [x] Create room
5469
- [x] Get room
55-
- [ ] Update room
70+
- [x] Update room
5671
- [x] Delete room
5772
- [ ] View room history
5873
- [ ] Get room message
@@ -68,7 +83,7 @@ NoContent noContent = future.get();
6883
- [ ] Share file with room
6984
- [ ] Share link with room
7085
- [ ] Get room statistics
71-
- [ ] Set topic
86+
- [x] Set topic
7287
- [ ] Get all webhooks
7388
- [ ] Create webhook
7489
- [ ] Get webhook

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ repositories {
1010
sourceCompatibility = 1.8
1111

1212
group = 'io.evanwong.oss'
13-
version = "0.3.0"
13+
version = "0.4.0"
1414

1515
dependencies {
1616
compile 'org.apache.httpcomponents:httpclient:4.3.5'
1717
compile 'org.slf4j:slf4j-api:1.7.7'
1818
compile 'com.fasterxml.jackson.core:jackson-databind:2.4.2'
1919
testCompile "org.codehaus.groovy:groovy-all:2.3.7"
20-
testCompile "org.spockframework:spock-core:1.0-groovy-2.3-SNAPSHOT"
20+
testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
2121
testRuntime 'org.slf4j:slf4j-simple:1.7.7'
2222
}
2323

@@ -44,5 +44,5 @@ if (hasProperty('ossrhUsername') && hasProperty('ossrhPassword')) {
4444
}
4545

4646
task wrapper(type: Wrapper) {
47-
gradleVersion = '2.2.1'
47+
gradleVersion = '2.3'
4848
}

src/main/java/io/evanwong/oss/hipchat/v2/HipChatClient.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,22 @@ public AddRoomMemberRequestBuilder prepareAddRoomMemberRequestBuilder(String use
123123
return new AddRoomMemberRequestBuilder(userIdOrEmail, roomIdOrName, accessToken, httpClient, executorService);
124124
}
125125

126+
public SetTopicRequestBuilder prepareSetTopicRequestBuilder(String roomIdOrName, String topic) {
127+
return prepareSetTopicRequestBuilder(roomIdOrName, topic, defaultAccessToken);
128+
}
129+
130+
public SetTopicRequestBuilder prepareSetTopicRequestBuilder(String roomIdOrName, String topic, String accessToken) {
131+
return new SetTopicRequestBuilder(roomIdOrName, topic, accessToken, httpClient, executorService);
132+
}
133+
134+
public UpdateRoomRequestBuilder prepareUpdateRoomRequestBuilder(String roomIdOrName) {
135+
return prepareUpdateRoomRequestBuilder(roomIdOrName, defaultAccessToken);
136+
}
137+
138+
public UpdateRoomRequestBuilder prepareUpdateRoomRequestBuilder(String roomIdOrName, String accessToken) {
139+
return new UpdateRoomRequestBuilder(roomIdOrName, accessToken, httpClient, executorService);
140+
}
141+
126142
public void close() {
127143
log.info("Shutting down...");
128144
try {

src/main/java/io/evanwong/oss/hipchat/v2/commons/DeleteRequest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ protected DeleteRequest(String accessToken, HttpClient httpClient, ExecutorServi
2525
@Override
2626
protected HttpResponse request() throws IOException {
2727
Map<String, Object> params = toQueryMap();
28-
log.info("POST - path: {}, params: {}", getPath(), params);
28+
String encodedPath = getEncodedPath();
29+
log.info("POST - path: {}, params: {}", encodedPath, params);
2930

30-
HttpDelete httpDelete = new HttpDelete(BASE_URL + getPath());
31+
HttpDelete httpDelete = new HttpDelete(BASE_URL + encodedPath);
3132
httpDelete.addHeader(new BasicHeader("Authorization", "Bearer " + accessToken));
3233
httpDelete.addHeader(new BasicHeader("Content-Type", "application/json"));
3334
return httpClient.execute(httpDelete, HttpClientContext.create());

src/main/java/io/evanwong/oss/hipchat/v2/commons/GetRequest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,16 @@ protected HttpResponse request() throws IOException {
3030
if (!expansions.isEmpty()) {
3131
params.put("expand", expansions.stream().collect(Collectors.joining(",")));
3232
}
33-
log.info("GET - path: {}, params: {}", getPath(), params);
33+
String encodedPath = getEncodedPath();
34+
log.info("GET - path: {}, params: {}", encodedPath, params);
3435
String query = params != null && params.size() > 0 ? "?" : "";
3536
if (params != null) {
3637
for (String key : params.keySet()) {
3738
query += key + "=" + params.get(key) + "&";
3839
}
3940
}
4041

41-
HttpGet httpGet = new HttpGet(BASE_URL + getPath() + query);
42+
HttpGet httpGet = new HttpGet(BASE_URL + encodedPath + query);
4243
httpGet.addHeader(new BasicHeader("Authorization", "Bearer " + accessToken));
4344
return httpClient.execute(httpGet, HttpClientContext.create());
4445
}

src/main/java/io/evanwong/oss/hipchat/v2/commons/PostRequest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ protected PostRequest(String accessToken, HttpClient httpClient, ExecutorService
2424
@Override
2525
protected HttpResponse request() throws IOException {
2626
Map<String, Object> params = toQueryMap();
27-
log.info("POST - path: {}, params: {}", getPath(), params);
27+
String encodedPath = getEncodedPath();
28+
log.info("POST - path: {}, params: {}", encodedPath, params);
2829

29-
HttpPost httpPost = new HttpPost(BASE_URL + getPath());
30+
HttpPost httpPost = new HttpPost(BASE_URL + encodedPath);
3031
httpPost.addHeader(new BasicHeader("Authorization", "Bearer " + accessToken));
3132
httpPost.addHeader(new BasicHeader("Content-Type", "application/json"));
3233
httpPost.setEntity(new StringEntity(objectWriter.writeValueAsString(params)));

src/main/java/io/evanwong/oss/hipchat/v2/commons/PutRequest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ protected PutRequest(String accessToken, HttpClient httpClient, ExecutorService
2424
@Override
2525
protected HttpResponse request() throws IOException {
2626
Map<String, Object> params = toQueryMap();
27-
log.info("PUT - path: {}, params: {}", getPath(), params);
27+
String encodedPath = getEncodedPath();
28+
log.info("PUT - path: {}, params: {}", encodedPath, params);
2829

29-
HttpPut httpPut = new HttpPut(BASE_URL + getPath());
30+
HttpPut httpPut = new HttpPut(BASE_URL + encodedPath);
3031
httpPut.addHeader(new BasicHeader("Authorization", "Bearer " + accessToken));
3132
httpPut.addHeader(new BasicHeader("Content-Type", "application/json"));
3233
httpPut.setEntity(new StringEntity(objectWriter.writeValueAsString(params)));

src/main/java/io/evanwong/oss/hipchat/v2/commons/Request.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import com.fasterxml.jackson.databind.ObjectReader;
55
import com.fasterxml.jackson.databind.ObjectWriter;
6+
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
7+
import org.apache.commons.codec.EncoderException;
8+
import org.apache.commons.codec.net.URLCodec;
69
import org.apache.http.HttpEntity;
710
import org.apache.http.HttpResponse;
811
import org.apache.http.client.HttpClient;
@@ -18,23 +21,46 @@
1821

1922
public abstract class Request<T> {
2023

21-
private static final Logger log = LoggerFactory.getLogger(PostRequest.class);
24+
private static final Logger log = LoggerFactory.getLogger(Request.class);
2225

2326
protected static final String BASE_URL = "https://api.hipchat.com/v2";
2427
protected ExecutorService executorService;
2528
protected String accessToken;
2629
protected HttpClient httpClient;
2730
private final ObjectMapper objectMapper = new ObjectMapper();
2831
protected final ObjectWriter objectWriter = objectMapper.writer();
29-
protected final ObjectReader objectReader = objectMapper.reader(getParameterClass());
32+
protected final ObjectReader objectReader;
33+
3034
protected abstract Map<String, Object> toQueryMap();
35+
3136
protected abstract HttpResponse request() throws IOException;
37+
3238
protected abstract String getPath();
3339

40+
protected String getEncodedPath() {
41+
String path = getPath();
42+
String[] tokens = path.split("/");
43+
String encodedPath = "";
44+
URLCodec urlCodec = new URLCodec();
45+
try {
46+
for (String token : tokens) {
47+
if (!token.isEmpty()) {
48+
//replace + to %20
49+
encodedPath += "/" + urlCodec.encode(token).replace("+", "%20");
50+
}
51+
}
52+
} catch (EncoderException e) {
53+
log.error("Failed to encode the path properly.", e);
54+
}
55+
return encodedPath;
56+
}
57+
3458
protected Request(String accessToken, HttpClient httpClient, ExecutorService executorService) {
3559
this.executorService = executorService;
3660
this.accessToken = accessToken;
3761
this.httpClient = httpClient;
62+
this.objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
63+
this.objectReader = objectMapper.reader(getParameterClass());
3864
}
3965

4066
public Future<T> execute() {

src/main/java/io/evanwong/oss/hipchat/v2/emoticons/Emoticons.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.evanwong.oss.hipchat.v2.emoticons;
22

3+
import com.fasterxml.jackson.annotation.JsonSetter;
34
import io.evanwong.oss.hipchat.v2.commons.PagingLinks;
45

56
import java.util.List;
@@ -30,6 +31,7 @@ public Integer getStartIndex() {
3031
return startIndex;
3132
}
3233

34+
@JsonSetter("startIndex")
3335
public void setStartIndex(Integer startIndex) {
3436
this.startIndex = startIndex;
3537
}
@@ -38,6 +40,7 @@ public Integer getMaxResults() {
3840
return maxResults;
3941
}
4042

43+
@JsonSetter("maxResults")
4144
public void setMaxResults(Integer maxResults) {
4245
this.maxResults = maxResults;
4346
}

src/main/java/io/evanwong/oss/hipchat/v2/rooms/Privacy.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package io.evanwong.oss.hipchat.v2.rooms;
22

3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonValue;
5+
36
public enum Privacy {
47

58
PUBLIC("public"), PRIVATE("private");
69

710
private String value;
811

12+
@JsonCreator
913
Privacy(String value) {
1014
this.value = value;
1115
}
1216

17+
@JsonValue
1318
public String getValue() {
1419
return value;
1520
}

0 commit comments

Comments
 (0)