Skip to content

Commit dc67da7

Browse files
authored
Merge pull request #31 from RestComm/restcomm-java
Restcomm java
2 parents beee881 + b3ab8f5 commit dc67da7

32 files changed

+1365
-0
lines changed

.gitignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
lib/
2+
target/
3+
4+
### IntelliJ IDEA ###
5+
**/.idea/**
6+
*.iws
7+
*.iml
8+
*.ipr
9+
10+
### NetBeans ###
11+
nbproject/private/
12+
build/
13+
nbbuild/
14+
dist/
15+
nbdist/
16+
.nb-gradle/
17+
/bin/
18+
19+
### Eclipse ###
20+
**/.settings/**
21+
.classpath
22+
.project
23+
24+
### Mac ###
25+
.DS_Store

pom.xml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.restcomm.sdk</groupId>
8+
<artifactId>restcomm-java</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<java.version>1.8</java.version>
13+
<lombok.project>1.16.18</lombok.project>
14+
<fluent-hc.version>4.5.5</fluent-hc.version>
15+
<jackson.version>2.9.5</jackson.version>
16+
<junit.version>4.12</junit.version>
17+
<wiremock.version>2.16.0</wiremock.version>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.projectlombok</groupId>
23+
<artifactId>lombok</artifactId>
24+
<version>${lombok.project}</version>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.apache.httpcomponents</groupId>
29+
<artifactId>fluent-hc</artifactId>
30+
<version>${fluent-hc.version}</version>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>com.fasterxml.jackson.core</groupId>
35+
<artifactId>jackson-core</artifactId>
36+
<version>${jackson.version}</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>com.fasterxml.jackson.core</groupId>
40+
<artifactId>jackson-databind</artifactId>
41+
<version>${jackson.version}</version>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>junit</groupId>
46+
<artifactId>junit</artifactId>
47+
<version>${junit.version}</version>
48+
<scope>test</scope>
49+
</dependency>
50+
51+
<dependency>
52+
<groupId>com.github.tomakehurst</groupId>
53+
<artifactId>wiremock</artifactId>
54+
<version>${wiremock.version}</version>
55+
<scope>test</scope>
56+
</dependency>
57+
</dependencies>
58+
59+
<build>
60+
<plugins>
61+
<plugin>
62+
<groupId>org.apache.maven.plugins</groupId>
63+
<artifactId>maven-compiler-plugin</artifactId>
64+
<configuration>
65+
<source>${java.version}</source>
66+
<target>${java.version}</target>
67+
</configuration>
68+
</plugin>
69+
</plugins>
70+
</build>
71+
</project>
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package org.restcomm.sdk;
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.fasterxml.jackson.databind.DeserializationFeature;
5+
import com.fasterxml.jackson.databind.JavaType;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import org.apache.http.client.fluent.Content;
8+
import org.apache.http.client.fluent.Executor;
9+
import org.apache.http.client.fluent.Request;
10+
import org.apache.http.entity.ContentType;
11+
import org.apache.http.message.BasicHeader;
12+
import org.apache.http.message.BasicNameValuePair;
13+
import org.restcomm.sdk.domain.Restful;
14+
15+
import java.io.IOException;
16+
import java.util.Map;
17+
import java.util.stream.Collectors;
18+
19+
import static com.fasterxml.jackson.databind.PropertyNamingStrategy.SNAKE_CASE;
20+
import static org.apache.commons.codec.binary.Base64.encodeBase64String;
21+
22+
/**
23+
* @author [email protected] (Oleg Agafonov)
24+
*/
25+
public class HttpClient {
26+
27+
private final Executor executor;
28+
29+
private final BasicHeader authoriztion;
30+
31+
private final ObjectMapper serializer;
32+
33+
private final ObjectMapper deserializer;
34+
35+
public HttpClient(String accountSid, String accountToken) {
36+
this.executor = Executor.newInstance();
37+
38+
String authorization = accountSid + ":" + accountToken;
39+
this.authoriztion = new BasicHeader("Authorization", "Basic " + encodeBase64String(authorization.getBytes()));
40+
41+
this.serializer = new ObjectMapper();
42+
this.serializer.setSerializationInclusion(JsonInclude.Include.NON_NULL);
43+
44+
this.deserializer = new ObjectMapper();
45+
this.deserializer.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
46+
this.deserializer.setPropertyNamingStrategy(SNAKE_CASE);
47+
}
48+
49+
public <T> T post(String url, Object entity, Class<T> type) {
50+
Request request = Request.Post(url);
51+
addRequestBody(request, entity);
52+
return executeRequest(request, type);
53+
}
54+
55+
public <T> T get(String url, Object entity, JavaType type) {
56+
String response = get(addRequestParams(url, entity), String.class);
57+
try {
58+
return deserializer.readValue(response, type);
59+
} catch (IOException e) {
60+
throw new RestcommClientException(e);
61+
}
62+
}
63+
64+
public <T> T get(String url, Class<T> type) {
65+
return executeRequest(Request.Get(url), type);
66+
}
67+
68+
public <T> T put(String url, Object entity, Class<T> type) {
69+
Request request = Request.Put(url);
70+
addRequestBody(request, entity);
71+
return executeRequest(request, type);
72+
}
73+
74+
public <T> T delete(String url, Class<T> type) {
75+
return executeRequest(Request.Delete(url), type);
76+
}
77+
78+
private String addRequestParams(String url, Object entity) {
79+
if (entity == null) {
80+
return url;
81+
}
82+
try {
83+
Map<String, Object> values = serializer.convertValue(entity, Map.class);
84+
if (values.size() == 0) {
85+
return url;
86+
}
87+
StringBuilder sb = new StringBuilder(url)
88+
.append("?");
89+
values.forEach((k, v) -> {
90+
sb.append(k)
91+
.append("=")
92+
.append(v)
93+
.append("&");
94+
});
95+
sb.setLength(sb.length() - 1);
96+
return sb.toString();
97+
} catch (Exception e) {
98+
throw new RestcommClientException(e);
99+
}
100+
}
101+
102+
private void addRequestBody(Request request, Object entity) {
103+
try {
104+
if (entity instanceof Restful) {
105+
request.bodyString(serializer.writeValueAsString(entity), ContentType.APPLICATION_JSON);
106+
} else {
107+
Map<String, Object> values = serializer.convertValue(entity, Map.class);
108+
request.bodyForm(values.entrySet()
109+
.stream()
110+
.filter(entry -> entry.getValue() != null)
111+
.map(entry -> {
112+
String name = entry.getKey().toString();
113+
// Restcomm.Connect inconsistent API: isSIP instead of IsSIP ;(
114+
if (!name.equals("isSIP")) {
115+
name = name.substring(0, 1).toUpperCase() + name.substring(1);
116+
}
117+
String value = entry.getValue().toString();
118+
return new BasicNameValuePair(name, value);
119+
})
120+
.collect(Collectors.toList()));
121+
}
122+
} catch (Exception e) {
123+
throw new RestcommClientException(e);
124+
}
125+
}
126+
127+
private <T> T executeRequest(Request request, Class<T> type) {
128+
try {
129+
request.addHeader(authoriztion);
130+
Content content = executor
131+
.execute(request)
132+
.returnContent();
133+
if (content == null) {
134+
return null;
135+
}
136+
String response = content.asString();
137+
if (response.isEmpty()) {
138+
return null;
139+
}
140+
if (type.equals(String.class)) {
141+
return (T) response;
142+
}
143+
return deserializer.readValue(response, type);
144+
} catch (Exception e) {
145+
throw new RestcommClientException(e);
146+
}
147+
}
148+
}

0 commit comments

Comments
 (0)