View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2014 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.caravan.io.http.impl;
21  
22  import java.util.Arrays;
23  
24  import org.apache.commons.lang3.StringUtils;
25  import org.apache.http.Header;
26  import org.apache.http.HttpEntityEnclosingRequest;
27  import org.apache.http.client.methods.HttpDelete;
28  import org.apache.http.client.methods.HttpGet;
29  import org.apache.http.client.methods.HttpPost;
30  import org.apache.http.client.methods.HttpPut;
31  import org.apache.http.client.methods.HttpUriRequest;
32  import org.apache.http.entity.ByteArrayEntity;
33  import org.apache.http.entity.StringEntity;
34  
35  import com.google.common.collect.ImmutableListMultimap;
36  import com.google.common.collect.LinkedHashMultimap;
37  import com.google.common.collect.Multimap;
38  import com.netflix.loadbalancer.Server;
39  
40  import io.wcm.caravan.io.http.request.CaravanHttpRequest;
41  
42  /**
43   * Utility methods for preparing a request for execution.
44   */
45  public final class RequestUtil {
46  
47    /**
48     * Choose protocol automatically.
49     */
50    public static final String PROTOCOL_AUTO = "auto";
51    /**
52     * Non secure protocol.
53     */
54    public static final String PROTOCOL_HTTP = "http";
55    /**
56     * Secure protocol.
57     */
58    public static final String PROTOCOL_HTTPS = "https";
59  
60    private RequestUtil() {
61      // static methods only
62    }
63  
64    /**
65     * @param server Server
66     * @return URL prefix with scheme, hostname and port
67     */
68    public static String buildUrlPrefix(Server server, String protocol) {
69      StringBuilder urlPrefix = new StringBuilder();
70      if (StringUtils.equals(protocol, PROTOCOL_HTTP)) {
71        urlPrefix.append("http://");
72        if (server.getPort() == 80) {
73          urlPrefix.append(server.getHost());
74        }
75        else {
76          urlPrefix.append(server.getHost()).append(":").append(server.getPort());
77        }
78      }
79      else if (StringUtils.equals(protocol, PROTOCOL_HTTPS)) {
80        urlPrefix.append("https://");
81        if (server.getPort() == 443 || server.getPort() == 80) {
82          urlPrefix.append(server.getHost());
83        }
84        else {
85          urlPrefix.append(server.getHost()).append(":").append(server.getPort());
86        }
87      }
88      else {
89        if (server.getPort() == 443 || server.getPort() == 8443) {
90          urlPrefix.append("https");
91        }
92        else {
93          urlPrefix.append("http");
94        }
95        urlPrefix.append("://").append(server.getHost());
96        if (server.getPort() != 80 && server.getPort() != 443) {
97          urlPrefix.append(':').append(server.getPort());
98        }
99      }
100     return urlPrefix.toString();
101   }
102 
103   /**
104    * @param request Requset
105    * @return HTTP client request object
106    */
107   public static HttpUriRequest buildHttpRequest(CaravanHttpRequest request) {
108 
109     // http method
110     HttpUriRequest httpRequest;
111     String method = StringUtils.upperCase(request.getMethod());
112     switch (method) {
113       case HttpGet.METHOD_NAME:
114         httpRequest = new HttpGet(request.getUrl());
115         break;
116       case HttpPost.METHOD_NAME:
117         httpRequest = new HttpPost(request.getUrl());
118         break;
119       case HttpPut.METHOD_NAME:
120         httpRequest = new HttpPut(request.getUrl());
121         break;
122       case HttpDelete.METHOD_NAME:
123         httpRequest = new HttpDelete(request.getUrl());
124         break;
125       default:
126         throw new IllegalArgumentException("Unsupported HTTP method type: " + request.getMethod());
127     }
128 
129     // headers
130     request.getHeaders().entries().forEach(e -> httpRequest.addHeader(e.getKey(), e.getValue()));
131 
132     // body
133     if ((httpRequest instanceof HttpEntityEnclosingRequest) && request.getBody() != null) {
134       HttpEntityEnclosingRequest entityHttpRequest = (HttpEntityEnclosingRequest)httpRequest;
135       if (request.getCharset() != null) {
136         entityHttpRequest.setEntity(new StringEntity(new String(request.getBody(), request.getCharset()), request.getCharset()));
137       }
138       else {
139         entityHttpRequest.setEntity(new ByteArrayEntity(request.getBody()));
140       }
141     }
142 
143     return httpRequest;
144   }
145 
146   /**
147    * @param headerArray Http-client header array
148    * @return Map with header values
149    */
150   public static Multimap<String, String> toHeadersMap(Header... headerArray) {
151     LinkedHashMultimap<String, String> headerMap = LinkedHashMultimap.create();
152     Arrays.stream(headerArray).forEach(h -> headerMap.put(h.getName(), h.getValue()));
153     return ImmutableListMultimap.copyOf(headerMap);
154   }
155 
156 }