private Request getModifyRequest(Request request) { String xid = RootContext.getXID(); if (StringUtils.isEmpty(xid)) { return request; } Map<String, Collection<String>> headers = new HashMap<>(); headers.putAll(request.headers()); List<String> fescarXid = new ArrayList<>(); fescarXid.add(xid); headers.put(RootContext.KEY_XID, fescarXid); return Request.create(request.method(), request.url(), headers, request.body(), request.charset()); }
static Request toOkHttpRequest(feign.Request input) { Request.Builder requestBuilder = new Request.Builder(); requestBuilder.url(input.url()); for (String field : input.headers().keySet()) { if (field.equalsIgnoreCase("Accept")) { hasAcceptHeader = true; for (String value : input.headers().get(field)) { requestBuilder.addHeader(field, value); if (field.equalsIgnoreCase("Content-Type")) { mediaType = MediaType.parse(value); if (input.charset() != null) { mediaType.charset(input.charset()); byte[] inputBody = input.body(); boolean isMethodWithBody = HttpMethod.POST == input.httpMethod() || HttpMethod.PUT == input.httpMethod() || HttpMethod.PATCH == input.httpMethod(); if (isMethodWithBody) { requestBuilder.removeHeader("Content-Type"); requestBuilder.method(input.httpMethod().name(), body); return requestBuilder.build();
private String getHeaderValue(Request req, String headerName) { Map<String, Collection<String>> headers = req.headers(); Collection<String> values = headers.get(headerName); String value = null; if (values != null && values.isEmpty() == false) { String[] array = new String[values.size()]; values.toArray(array); value = array[0]; } return value; }
.addContextValue("requestUrl", response.request().url()) .addContextValue("requestMethod", response.request().httpMethod()) .addContextValue("requestHeaders", response.request().headers().entrySet().toString()) .addContextValue("responseBody", responseBody); } else if (response.status() == 404) { throw new NotFoundException() .addContextValue("requestUrl", response.request().url()) .addContextValue("requestMethod", response.request().httpMethod()) .addContextValue("requestHeaders", response.request().headers().entrySet().toString()) .addContextValue("responseBody", responseBody); } else if (response.status() == 503) { .addContextValue("requestUrl", response.request().url()) .addContextValue("requestMethod", response.request().httpMethod()) .addContextValue("requestHeaders", response.request().headers().entrySet().toString()) .addContextValue("responseBody", responseBody) .addContextValue("errorType", error.getError())
@Override public String url(Request request) { return request.url(); }
/** * Creates {@link HttpClientRequest} (Vert.x) from {@link Request} (feign). * * @param request feign request * @return fully formed HttpClientRequest */ private HttpClientRequest makeHttpClientRequest(final Request request) throws MalformedURLException { final URL url = new URL(request.url()); final int port = url.getPort() > -1 ? url.getPort() : HttpClientOptions.DEFAULT_DEFAULT_PORT; final String host = url.getHost(); final String requestUri = url.getFile(); HttpClientRequest httpClientRequest = httpClient.request( HttpMethod.valueOf(request.method()), port, host, requestUri); /* Add headers to request */ for (final Map.Entry<String, Collection<String>> header : request.headers().entrySet()) { httpClientRequest = httpClientRequest.putHeader(header.getKey(), header.getValue()); } return httpClientRequest; } }
@Override public Response execute(Request request, Request.Options options) throws IOException { URI uri = URI.create(request.url()); BambooRequest.Builder builder = BambooRequest.builder() .serviceId(uri.getHost()) .uri(uri.getPath()) .ip(RequestIpKeeper.getRequestIp()) .addMultiParams(WebUtils.getQueryParams(uri.getQuery())); if(bambooProperties.getBambooRequest().isLoadBody()){ builder.requestBody(request.body()); } request.headers().entrySet().forEach(entry ->{ for (String v : entry.getValue()) { builder.addHeader(entry.getKey(), v); } }); ConnectPointContext connectPointContext = ConnectPointContext.builder().bambooRequest(builder.build()).build(); try { BambooAppContext.getBambooRibbonConnectionPoint().executeConnectPoint(connectPointContext); return delegate.execute(request, options); }finally { BambooAppContext.getBambooRibbonConnectionPoint().shutdownconnectPoint(); } } }
@Override public void apply(RequestTemplate template) { try { template.header( StarterConstants.TRACE_ID_KEY, TraceIdHelper.getTraceId() ); template.header( StarterConstants.IP_HEADER_NAME, TraceIdHelper.getRemoteIp() ); String bodyStr = ""; byte[] body = template.body(); if ( null != body && body.length > 0 ) { bodyStr = new String( body ); } LOGGER.info( "traceId:{}, request -> path:{}, headers:{}, querys:{}, body:{}", TraceIdHelper.getTraceId(), template.request().url(), JSON.toJSONString(template.request().headers()), JSON.toJSONString(template.queries()), bodyStr ); } catch (Exception e) { e.printStackTrace(); } } }
private Request toRequest(Request request) { Map<String, Collection<String>> headers = new LinkedHashMap<>( request.headers()); return Request.create(request.httpMethod(), getUri().toASCIIString(), headers, request.requestBody()); }
private ContentType getContentType(Request request) { ContentType contentType = ContentType.DEFAULT_TEXT; for (Map.Entry<String, Collection<String>> entry : request.headers().entrySet()) { if (entry.getKey().equalsIgnoreCase("Content-Type")) { Collection values = entry.getValue(); if (values != null && !values.isEmpty()) { contentType = ContentType.create(entry.getValue().iterator().next(), request.charset()); break; } } } return contentType; }
@Override public String method(Request request) { return request.method(); }
@Override public HttpMethod getMethod() { return HttpMethod .resolve(RibbonRequest.this.toRequest().httpMethod().name()); }
public Request request() { Map<String, Collection<String>> safeCopy = new LinkedHashMap<String, Collection<String>>(); safeCopy.putAll(headers); return Request.create( method, new StringBuilder(url).append(queryLine()).toString(), Collections.unmodifiableMap(safeCopy), body, charset ); }
HttpUriRequest toHttpUriRequest(Request request, Request.Options options) throws URISyntaxException { RequestBuilder requestBuilder = RequestBuilder.create(request.httpMethod().name()); requestBuilder.setConfig(requestConfig); URI uri = new URIBuilder(request.url()).build(); for (Map.Entry<String, Collection<String>> headerEntry : request.headers().entrySet()) { String headerName = headerEntry.getKey(); if (headerName.equalsIgnoreCase(ACCEPT_HEADER_NAME)) { if (request.body() != null) { HttpEntity entity = null; if (request.charset() != null) { ContentType contentType = getContentType(request); String content = new String(request.body(), request.charset()); entity = new StringEntity(content, contentType); } else { entity = new ByteArrayEntity(request.body());
private String getHeaderValue(Request req, String headerName) { Map<String, Collection<String>> headers = req.headers(); Collection<String> values = headers.get(headerName); String value = null; if (values != null && values.isEmpty() == false) { String[] array = new String[values.size()]; values.toArray(array); value = array[0]; } return value; }
.addContextValue("requestUrl", response.request().url()) .addContextValue("requestMethod", response.request().httpMethod()) .addContextValue("requestHeaders", response.request().headers().entrySet().toString()) .addContextValue("responseBody", responseBody); } else if (response.status() == 404) { throw new NotFoundException() .addContextValue("requestUrl", response.request().url()) .addContextValue("requestMethod", response.request().httpMethod()) .addContextValue("requestHeaders", response.request().headers().entrySet().toString()) .addContextValue("responseBody", responseBody); } else if (response.status() == 503) { .addContextValue("requestUrl", response.request().url()) .addContextValue("requestMethod", response.request().httpMethod()) .addContextValue("requestHeaders", response.request().headers().entrySet().toString()) .addContextValue("responseBody", responseBody) .addContextValue("errorType", error.getError())
@Override public String url(Request request) { return request.url(); }