|
| 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