1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| import com.alibaba.fastjson.JSON; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.handler.codec.http.*; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.apache.http.NameValuePair; import org.apache.http.client.entity.EntityBuilder; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.client.methods.RequestBuilder; import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.ContentType; import org.apache.http.message.BasicNameValuePair;
import java.net.URI; import java.net.URISyntaxException; import java.nio.charset.Charset; import java.util.List; import java.util.Map;
@Data @AllArgsConstructor @NoArgsConstructor @Builder public class HttpDelegateRequest implements Payload {
private static final String ACCEPT_CONTENT_TYPE = "Accept"; private static final String REQUEST_CONTENT_TYPE = "Content-Type"; private static final String CONTENT_LENGTH = "Content-Length";
private String method; private String requestContentType; private String acceptContentType;
private String path; private Map<String, String> headers = Maps.newHashMap(); private Map<String, String> queryParams = Maps.newHashMap(); private Map<String, String> formParams = Maps.newHashMap(); private byte[] body;
public static HttpDelegateRequest parse(byte[] bytes) { return JSON.parseObject(new String(bytes, Charset.defaultCharset()), HttpDelegateRequest.class); }
public HttpRequestBase toApacheRequest(String schema, String host, int port) { RequestBuilder builder = RequestBuilder.create(this.method);
try { URIBuilder uriBuilder = new URIBuilder(); uriBuilder.setScheme(schema); uriBuilder.setHost(host); uriBuilder.setPort(port); uriBuilder.setPath(path); if (MapUtils.isNotEmpty(this.queryParams)) { for (Map.Entry<String, String> entry : this.queryParams.entrySet()) { uriBuilder.addParameter(entry.getKey(), entry.getValue()); } } builder.setUri(uriBuilder.build()); } catch (URISyntaxException e) { throw new RuntimeException("build http request uri failed", e); }
EntityBuilder bodyBuilder = EntityBuilder.create(); bodyBuilder.setContentType(ContentType.parse(requestContentType)); if (MapUtils.isNotEmpty(this.formParams)) { List<NameValuePair> paramList = Lists.newArrayList(); for (Map.Entry<String, String> entry : this.formParams.entrySet()) { paramList.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } bodyBuilder.setParameters(paramList); builder.setEntity(bodyBuilder.build()); } else if (ArrayUtils.isNotEmpty(this.body)) { bodyBuilder.setBinary(this.body); builder.setEntity(bodyBuilder.build()); }
for (Map.Entry<String, String> entry : this.headers.entrySet()) { builder.addHeader(entry.getKey(), entry.getValue()); }
return (HttpRequestBase) builder.build(); }
public FullHttpRequest toNettyRequest() { URI uri; try { URIBuilder uriBuilder = new URIBuilder(); uriBuilder.setPath(path); if (MapUtils.isNotEmpty(this.queryParams)) { for (Map.Entry<String, String> entry : this.queryParams.entrySet()) { uriBuilder.addParameter(entry.getKey(), entry.getValue()); } }
uri = uriBuilder.build(); } catch (URISyntaxException e) { throw new RuntimeException("build http request uri failed", e); }
EntityBuilder bodyBuilder = EntityBuilder.create(); bodyBuilder.setContentType(ContentType.parse(requestContentType)); if (MapUtils.isNotEmpty(this.formParams)) { List<NameValuePair> paramList = Lists.newArrayList(); for (Map.Entry<String, String> entry : this.formParams.entrySet()) { paramList.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } bodyBuilder.setParameters(paramList); } else if (ArrayUtils.isNotEmpty(this.body)) { bodyBuilder.setBinary(this.body); }
byte[] bodyBytes = bodyBuilder.getBinary(); ByteBuf bodyByteBuf;
if (ArrayUtils.isNotEmpty(bodyBytes)) { bodyByteBuf = Unpooled.wrappedBuffer(bodyBytes); } else { bodyByteBuf = Unpooled.buffer(0); }
DefaultHttpHeaders headers = new DefaultHttpHeaders();
if (StringUtils.isNotBlank(requestContentType)) { headers.add(REQUEST_CONTENT_TYPE, requestContentType); } if (StringUtils.isNotBlank(acceptContentType)) { headers.add(ACCEPT_CONTENT_TYPE, acceptContentType); }
for (Map.Entry<String, String> entry : this.headers.entrySet()) { headers.add(entry.getKey(), entry.getValue()); }
if (ArrayUtils.isNotEmpty(bodyBytes)) { headers.add(CONTENT_LENGTH, bodyBytes.length); } else { headers.add(CONTENT_LENGTH, 0); }
return new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.valueOf(method), uri.toASCIIString(), bodyByteBuf, headers, new DefaultHttpHeaders(true)); }
@Override public String toAbstractInfo() { Map<String, String> map = Maps.newHashMap(); map.put("method", method); map.put("path", path);
return JSON.toJSONString(map); } }
|