|
3 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; |
4 | 4 | import com.fasterxml.jackson.databind.ObjectReader; |
5 | 5 | 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; |
6 | 9 | import org.apache.http.HttpEntity; |
7 | 10 | import org.apache.http.HttpResponse; |
8 | 11 | import org.apache.http.client.HttpClient; |
|
18 | 21 |
|
19 | 22 | public abstract class Request<T> { |
20 | 23 |
|
21 | | - private static final Logger log = LoggerFactory.getLogger(PostRequest.class); |
| 24 | + private static final Logger log = LoggerFactory.getLogger(Request.class); |
22 | 25 |
|
23 | 26 | protected static final String BASE_URL = "https://api.hipchat.com/v2"; |
24 | 27 | protected ExecutorService executorService; |
25 | 28 | protected String accessToken; |
26 | 29 | protected HttpClient httpClient; |
27 | 30 | private final ObjectMapper objectMapper = new ObjectMapper(); |
28 | 31 | protected final ObjectWriter objectWriter = objectMapper.writer(); |
29 | | - protected final ObjectReader objectReader = objectMapper.reader(getParameterClass()); |
| 32 | + protected final ObjectReader objectReader; |
| 33 | + |
30 | 34 | protected abstract Map<String, Object> toQueryMap(); |
| 35 | + |
31 | 36 | protected abstract HttpResponse request() throws IOException; |
| 37 | + |
32 | 38 | protected abstract String getPath(); |
33 | 39 |
|
| 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 | + |
34 | 58 | protected Request(String accessToken, HttpClient httpClient, ExecutorService executorService) { |
35 | 59 | this.executorService = executorService; |
36 | 60 | this.accessToken = accessToken; |
37 | 61 | this.httpClient = httpClient; |
| 62 | + this.objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); |
| 63 | + this.objectReader = objectMapper.reader(getParameterClass()); |
38 | 64 | } |
39 | 65 |
|
40 | 66 | public Future<T> execute() { |
|
0 commit comments