Skip to content

Commit

Permalink
Merge pull request #26 from evanwong/release/0.4.0
Browse files Browse the repository at this point in the history
Release/0.4.0
  • Loading branch information
evanwong committed Apr 20, 2015
2 parents e953367 + 57a181b commit ef24a46
Show file tree
Hide file tree
Showing 18 changed files with 403 additions and 28 deletions.
25 changes: 20 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,35 @@ To add this client into your project:
<dependency>
<groupId>io.evanwong.oss</groupId>
<artifactId>hipchat-java</artifactId>
<version>0.3.0</version>
<version>0.4.0</version>
</dependency>
```
* gradle
```gradle
compile 'io.evanwong.oss:hipchat-java:0.3.0'
compile 'io.evanwong.oss:hipchat-java:0.4.0'
```

If you are using Java 7, there is a temporary workaround using [retrolambda](https://github.com/orfjackal/retrolambda).

* maven
```xml
<dependency>
<groupId>io.evanwong.oss</groupId>
<artifactId>hipchat-java7</artifactId>
<version>0.4.0</version>
</dependency>
```
* gradle
```gradle
compile 'io.evanwong.oss:hipchat-java7:0.4.0'
```

To send a notification
```java
String defaultAccessToken = "abcd1234";
HipChatClient client = new HipChatClient(defaultAccessToken);
SendRoomNotificationRequestBuilder builder = client.prepareSendRoomNotificationRequestBuilder("myRoom", "hello world!");
Future<NoContent> future = builder.setColor(Color.YELLOW).setNotify(true).build().execute();
Future<NoContent> future = builder.setColor(MessageColor.YELLOW).setNotify(true).build().execute();
//optional... if you want/need to inspect the result:
NoContent noContent = future.get();
```
Expand All @@ -52,7 +67,7 @@ NoContent noContent = future.get();
- [x] Get all rooms
- [x] Create room
- [x] Get room
- [ ] Update room
- [x] Update room
- [x] Delete room
- [ ] View room history
- [ ] Get room message
Expand All @@ -68,7 +83,7 @@ NoContent noContent = future.get();
- [ ] Share file with room
- [ ] Share link with room
- [ ] Get room statistics
- [ ] Set topic
- [x] Set topic
- [ ] Get all webhooks
- [ ] Create webhook
- [ ] Get webhook
Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ repositories {
sourceCompatibility = 1.8

group = 'io.evanwong.oss'
version = "0.3.0"
version = "0.4.0"

dependencies {
compile 'org.apache.httpcomponents:httpclient:4.3.5'
compile 'org.slf4j:slf4j-api:1.7.7'
compile 'com.fasterxml.jackson.core:jackson-databind:2.4.2'
testCompile "org.codehaus.groovy:groovy-all:2.3.7"
testCompile "org.spockframework:spock-core:1.0-groovy-2.3-SNAPSHOT"
testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
testRuntime 'org.slf4j:slf4j-simple:1.7.7'
}

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

task wrapper(type: Wrapper) {
gradleVersion = '2.2.1'
gradleVersion = '2.3'
}
16 changes: 16 additions & 0 deletions src/main/java/io/evanwong/oss/hipchat/v2/HipChatClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,22 @@ public AddRoomMemberRequestBuilder prepareAddRoomMemberRequestBuilder(String use
return new AddRoomMemberRequestBuilder(userIdOrEmail, roomIdOrName, accessToken, httpClient, executorService);
}

public SetTopicRequestBuilder prepareSetTopicRequestBuilder(String roomIdOrName, String topic) {
return prepareSetTopicRequestBuilder(roomIdOrName, topic, defaultAccessToken);
}

public SetTopicRequestBuilder prepareSetTopicRequestBuilder(String roomIdOrName, String topic, String accessToken) {
return new SetTopicRequestBuilder(roomIdOrName, topic, accessToken, httpClient, executorService);
}

public UpdateRoomRequestBuilder prepareUpdateRoomRequestBuilder(String roomIdOrName) {
return prepareUpdateRoomRequestBuilder(roomIdOrName, defaultAccessToken);
}

public UpdateRoomRequestBuilder prepareUpdateRoomRequestBuilder(String roomIdOrName, String accessToken) {
return new UpdateRoomRequestBuilder(roomIdOrName, accessToken, httpClient, executorService);
}

public void close() {
log.info("Shutting down...");
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ protected DeleteRequest(String accessToken, HttpClient httpClient, ExecutorServi
@Override
protected HttpResponse request() throws IOException {
Map<String, Object> params = toQueryMap();
log.info("POST - path: {}, params: {}", getPath(), params);
String encodedPath = getEncodedPath();
log.info("POST - path: {}, params: {}", encodedPath, params);

HttpDelete httpDelete = new HttpDelete(BASE_URL + getPath());
HttpDelete httpDelete = new HttpDelete(BASE_URL + encodedPath);
httpDelete.addHeader(new BasicHeader("Authorization", "Bearer " + accessToken));
httpDelete.addHeader(new BasicHeader("Content-Type", "application/json"));
return httpClient.execute(httpDelete, HttpClientContext.create());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ protected HttpResponse request() throws IOException {
if (!expansions.isEmpty()) {
params.put("expand", expansions.stream().collect(Collectors.joining(",")));
}
log.info("GET - path: {}, params: {}", getPath(), params);
String encodedPath = getEncodedPath();
log.info("GET - path: {}, params: {}", encodedPath, params);
String query = params != null && params.size() > 0 ? "?" : "";
if (params != null) {
for (String key : params.keySet()) {
query += key + "=" + params.get(key) + "&";
}
}

HttpGet httpGet = new HttpGet(BASE_URL + getPath() + query);
HttpGet httpGet = new HttpGet(BASE_URL + encodedPath + query);
httpGet.addHeader(new BasicHeader("Authorization", "Bearer " + accessToken));
return httpClient.execute(httpGet, HttpClientContext.create());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ protected PostRequest(String accessToken, HttpClient httpClient, ExecutorService
@Override
protected HttpResponse request() throws IOException {
Map<String, Object> params = toQueryMap();
log.info("POST - path: {}, params: {}", getPath(), params);
String encodedPath = getEncodedPath();
log.info("POST - path: {}, params: {}", encodedPath, params);

HttpPost httpPost = new HttpPost(BASE_URL + getPath());
HttpPost httpPost = new HttpPost(BASE_URL + encodedPath);
httpPost.addHeader(new BasicHeader("Authorization", "Bearer " + accessToken));
httpPost.addHeader(new BasicHeader("Content-Type", "application/json"));
httpPost.setEntity(new StringEntity(objectWriter.writeValueAsString(params)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ protected PutRequest(String accessToken, HttpClient httpClient, ExecutorService
@Override
protected HttpResponse request() throws IOException {
Map<String, Object> params = toQueryMap();
log.info("PUT - path: {}, params: {}", getPath(), params);
String encodedPath = getEncodedPath();
log.info("PUT - path: {}, params: {}", encodedPath, params);

HttpPut httpPut = new HttpPut(BASE_URL + getPath());
HttpPut httpPut = new HttpPut(BASE_URL + encodedPath);
httpPut.addHeader(new BasicHeader("Authorization", "Bearer " + accessToken));
httpPut.addHeader(new BasicHeader("Content-Type", "application/json"));
httpPut.setEntity(new StringEntity(objectWriter.writeValueAsString(params)));
Expand Down
30 changes: 28 additions & 2 deletions src/main/java/io/evanwong/oss/hipchat/v2/commons/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.net.URLCodec;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
Expand All @@ -18,23 +21,46 @@

public abstract class Request<T> {

private static final Logger log = LoggerFactory.getLogger(PostRequest.class);
private static final Logger log = LoggerFactory.getLogger(Request.class);

protected static final String BASE_URL = "https://api.hipchat.com/v2";
protected ExecutorService executorService;
protected String accessToken;
protected HttpClient httpClient;
private final ObjectMapper objectMapper = new ObjectMapper();
protected final ObjectWriter objectWriter = objectMapper.writer();
protected final ObjectReader objectReader = objectMapper.reader(getParameterClass());
protected final ObjectReader objectReader;

protected abstract Map<String, Object> toQueryMap();

protected abstract HttpResponse request() throws IOException;

protected abstract String getPath();

protected String getEncodedPath() {
String path = getPath();
String[] tokens = path.split("/");
String encodedPath = "";
URLCodec urlCodec = new URLCodec();
try {
for (String token : tokens) {
if (!token.isEmpty()) {
//replace + to %20
encodedPath += "/" + urlCodec.encode(token).replace("+", "%20");
}
}
} catch (EncoderException e) {
log.error("Failed to encode the path properly.", e);
}
return encodedPath;
}

protected Request(String accessToken, HttpClient httpClient, ExecutorService executorService) {
this.executorService = executorService;
this.accessToken = accessToken;
this.httpClient = httpClient;
this.objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
this.objectReader = objectMapper.reader(getParameterClass());
}

public Future<T> execute() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.evanwong.oss.hipchat.v2.emoticons;

import com.fasterxml.jackson.annotation.JsonSetter;
import io.evanwong.oss.hipchat.v2.commons.PagingLinks;

import java.util.List;
Expand Down Expand Up @@ -30,6 +31,7 @@ public Integer getStartIndex() {
return startIndex;
}

@JsonSetter("startIndex")
public void setStartIndex(Integer startIndex) {
this.startIndex = startIndex;
}
Expand All @@ -38,6 +40,7 @@ public Integer getMaxResults() {
return maxResults;
}

@JsonSetter("maxResults")
public void setMaxResults(Integer maxResults) {
this.maxResults = maxResults;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/io/evanwong/oss/hipchat/v2/rooms/Privacy.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package io.evanwong.oss.hipchat.v2.rooms;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

public enum Privacy {

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

private String value;

@JsonCreator
Privacy(String value) {
this.value = value;
}

@JsonValue
public String getValue() {
return value;
}
Expand Down
10 changes: 1 addition & 9 deletions src/main/java/io/evanwong/oss/hipchat/v2/rooms/Room.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package io.evanwong.oss.hipchat.v2.rooms;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonSetter;
import io.evanwong.oss.hipchat.v2.commons.Links;

import java.util.Date;

@JsonIgnoreProperties(ignoreUnknown=true)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Room {
private String xmppJid;
private Links statistics;
Expand Down Expand Up @@ -76,13 +75,6 @@ public void setPrivacy(Privacy privacy) {
this.privacy = privacy;
}

@JsonSetter("privacy")
public void setPrivacyStr(String privacyStr) {
if (privacyStr != null) {
this.privacy = Privacy.valueOf(privacyStr.toUpperCase());
}
}

public Boolean getIsGuestAccessible() {
return isGuestAccessible;
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/io/evanwong/oss/hipchat/v2/rooms/Rooms.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.evanwong.oss.hipchat.v2.rooms;

import com.fasterxml.jackson.annotation.JsonSetter;
import io.evanwong.oss.hipchat.v2.commons.PagingLinks;

import java.util.List;
Expand Down Expand Up @@ -33,6 +34,7 @@ public Integer getStartIndex() {
return startIndex;
}

@JsonSetter("startIndex")
public void setStartIndex(Integer startIndex) {
this.startIndex = startIndex;
}
Expand All @@ -41,6 +43,7 @@ public Integer getMaxResults() {
return maxResults;
}

@JsonSetter("maxResults")
public void setMaxResults(Integer maxResults) {
this.maxResults = maxResults;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.evanwong.oss.hipchat.v2.rooms;

import io.evanwong.oss.hipchat.v2.commons.NoContent;
import io.evanwong.oss.hipchat.v2.commons.PutRequest;
import org.apache.http.client.HttpClient;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ExecutorService;

public class SetTopicRequest extends PutRequest<NoContent> {

private String roomIdOrName;
private String topic;

public SetTopicRequest(String idOrName, String topic, String accessToken, HttpClient httpClient, ExecutorService executorService) {
super(accessToken, httpClient, executorService);
this.roomIdOrName = idOrName;
this.topic = topic;
}

public String getRoomIdOrName() {
return roomIdOrName;
}

public String getTopic() {
return topic;
}

@Override
protected String getPath() {
return "/room/" + this.roomIdOrName + "/topic";
}

@Override
protected Map<String, Object> toQueryMap() {
return Collections.singletonMap("topic", this.topic);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.evanwong.oss.hipchat.v2.rooms;

import io.evanwong.oss.hipchat.v2.commons.RequestBuilder;
import org.apache.http.client.HttpClient;

import java.util.concurrent.ExecutorService;

public class SetTopicRequestBuilder extends RequestBuilder<SetTopicRequest> {

private String roomIdOrName;
private String topic;

public SetTopicRequestBuilder(String idOrName, String topic, String accessToken, HttpClient httpClient, ExecutorService executorService) {
super(accessToken, httpClient, executorService);
this.roomIdOrName = idOrName;
this.topic = topic;
}

@Override
public SetTopicRequest build() {
if (topic == null) {
throw new IllegalArgumentException("topic is required.");
}
if (roomIdOrName == null) {
throw new IllegalArgumentException("idOrName is required.");
}
return new SetTopicRequest(roomIdOrName, topic, accessToken, httpClient, executorService);
}
}
Loading

0 comments on commit ef24a46

Please sign in to comment.