Refine search
@Override public void apply(final RequestTemplate requestTemplate) { requestTemplate.header(CommonConstant.TX_TRANSACTION_GROUP, TxTransactionLocal.getInstance().getTxGroupId()); }
public void apply(feign.RequestTemplate template) { final SpringCloudBeanRegistry beanRegistry = SpringCloudBeanRegistry.getInstance(); CompensableBeanFactory beanFactory = beanRegistry.getBeanFactory(); CompensableManager compensableManager = beanFactory.getCompensableManager(); CompensableTransaction compensable = compensableManager.getCompensableTransactionQuietly(); if (compensable == null) { return; } try { TransactionContext transactionContext = compensable.getTransactionContext(); byte[] byteArray = SerializeUtils.serializeObject(transactionContext); String transactionText = Base64.getEncoder().encodeToString(byteArray); Map<String, Collection<String>> headers = template.headers(); if (headers.containsKey(HEADER_TRANCACTION_KEY) == false) { template.header(HEADER_TRANCACTION_KEY, transactionText); } if (headers.containsKey(HEADER_PROPAGATION_KEY) == false) { template.header(HEADER_PROPAGATION_KEY, identifier); } } catch (IOException ex) { throw new RuntimeException("Error occurred while preparing the transaction context!", ex); } }
private void parseProduces(MethodMetadata md, Method method, RequestMapping annotation) { String[] serverProduces = annotation.produces(); String clientAccepts = serverProduces.length == 0 ? null : emptyToNull(serverProduces[0]); if (clientAccepts != null) { md.template().header(ACCEPT, clientAccepts); } }
@Override protected void processMetadata(Class<?> targetType, Method method, MethodMetadata metadata) { metadata.template().header(OkhttpTraceInterceptor.PATH_TEMPLATE_HEADER, metadata.template().method() + " " + metadata.template().url() // escape from feign string interpolation // See RequestTemplate.expand .replace("{", "{{")); } }
@Override public void apply(RequestTemplate template) { Collection<String> apiSources = template.headers().get(HeaderUtils.API_SOURCE_HEADER); if (apiSources == null || apiSources.isEmpty()) { return; } if (apiSources.iterator().next().equals(HeaderUtils.MARATHON_API_SOURCE)) { template.insert(0, "/marathon"); } template.header(HeaderUtils.API_SOURCE_HEADER); } }
@Override public boolean processArgument(AnnotatedParameterContext context, Annotation annotation, Method method) { int parameterIndex = context.getParameterIndex(); Class<?> parameterType = method.getParameterTypes()[parameterIndex]; MethodMetadata data = context.getMethodMetadata(); if (Map.class.isAssignableFrom(parameterType)) { checkState(data.headerMapIndex() == null, "Header map can only be present once."); data.headerMapIndex(parameterIndex); return true; } String name = ANNOTATION.cast(annotation).value(); checkState(emptyToNull(name) != null, "RequestHeader.value() was empty on parameter %s", parameterIndex); context.setParameterName(name); Collection<String> header = context.setTemplateParameter(name, data.template().headers().get(name)); data.template().header(name, header); return true; } }
@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(); } } }
@Override public void apply(RequestTemplate template) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder .getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); Enumeration<String> headerNames = request.getHeaderNames(); if (headerNames != null) { while (headerNames.hasMoreElements()) { String name = headerNames.nextElement(); String values = request.getHeader(name); template.header(name, values); } } template.method(request.getHeader(ShiroConstants.REDIRECT_METHOD)); template.insert(0, request.getHeader(ShiroConstants.REDIRECT_URI)); } };
@Override public boolean processArgument(AnnotatedParameterContext context, Annotation annotation, Method method) { String name = ANNOTATION.cast(annotation).value(); checkState(emptyToNull(name) != null, "PathVariable annotation was empty on param %s.", context.getParameterIndex()); context.setParameterName(name); MethodMetadata data = context.getMethodMetadata(); String varName = '{' + name + '}'; if (!data.template().url().contains(varName) && !searchMapValues(data.template().queries(), varName) && !searchMapValues(data.template().headers(), varName)) { data.formParams().add(name); } return true; }
private void handleProducesAnnotation(MethodMetadata data, Produces produces, String name) { String[] serverProduces = produces.value(); String clientAccepts = serverProduces.length == 0 ? null : emptyToNull(serverProduces[0]); checkState(clientAccepts != null, "Produces.value() was empty on %s", name); data.template().header(ACCEPT, (String) null); // remove any previous produces data.template().header(ACCEPT, clientAccepts); }
@Override public void process (RequestTemplate template, Charset charset, Map<String, Object> data) throws EncodeException { val bodyData = new StringBuilder(); for (Entry<String, Object> entry : data.entrySet()) { if (bodyData.length() > 0) { bodyData.append('&'); } bodyData.append(createKeyValuePair(entry, charset)); } val contentTypeValue = new StringBuilder() .append(getSupportedContentType().getHeader()) .append("; charset=").append(charset.name()) .toString(); val bytes = bodyData.toString().getBytes(charset); val body = Request.Body.encoded(bytes, charset); template.header(CONTENT_TYPE_HEADER, Collections.<String>emptyList()); // reset header template.header(CONTENT_TYPE_HEADER, contentTypeValue); template.body(body); }
if (annotationType == RequestLine.class) { String requestLine = RequestLine.class.cast(methodAnnotation).value(); checkState(emptyToNull(requestLine) != null, "RequestLine annotation was empty on method %s.", method.getName()); if (requestLine.indexOf(' ') == -1) { checkState(requestLine.indexOf('/') == -1, "RequestLine annotation didn't start with an HTTP verb on method %s.", method.getName()); data.template().method(requestLine); return; data.template().method(requestLine.substring(0, requestLine.indexOf(' '))); if (requestLine.indexOf(' ') == requestLine.lastIndexOf(' ')) { data.template().append(requestLine.substring(requestLine.indexOf(' ') + 1)); } else { data.template().append( requestLine.substring(requestLine.indexOf(' ') + 1, requestLine.lastIndexOf(' '))); data.template().decodeSlash(RequestLine.class.cast(methodAnnotation).decodeSlash()); method.getName()); if (body.indexOf('{') == -1) { data.template().body(body); } else { data.template().bodyTemplate(body); checkState(headersOnMethod.length > 0, "Headers annotation was empty on method %s.",
@Override public void encode(Object object, Type bodyType, RequestTemplate template) { template.body(gson.toJson(object, bodyType)); } }
@Override public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException { Collection<String> contentTypes = HeaderAccessUtils.caseInsensitiveGet(template.headers(), HttpHeaders.CONTENT_TYPE); if (contentTypes == null) { contentTypes = ImmutableSet.of(); } if (!contentTypes.contains(MIME_TYPE)) { delegate.encode(object, bodyType, template); return; } try { JavaType javaType = cborObjectMapper.getTypeFactory().constructType(bodyType); template.body(cborObjectMapper.writerFor(javaType).writeValueAsBytes(object), StandardCharsets.UTF_8); } catch (JsonProcessingException e) { throw new RuntimeException(e); } }
@Override public void encode(Object requestBody, Type bodyType, RequestTemplate request) throws EncodeException { Class<?> requestType = requestBody.getClass(); if (AnnotationUtils.findAnnotation(requestType, RaptorMessage.class) != null) { if ("GET".equalsIgnoreCase(request.method())) { try { Map<String, String> map = RaptorMessageUtils.transferMessageToMap(requestBody); for (Map.Entry<String, String> entry : map.entrySet()) { request.query(entry.getKey(), entry.getValue()); } } catch (Exception e) { throw new RuntimeException("Transfer requestBody to query string error.", e); } } else { FeignRequestOutputMessage outputMessage = new FeignRequestOutputMessage(request); try { raptorMessageConverter.write(requestBody, MediaType.APPLICATION_JSON, outputMessage); request.body(outputMessage.body(), StandardCharsets.UTF_8); } catch (IOException ex) { throw new EncodeException("Error converting request body", ex); } request.headers(HttpHeadersUtils.getHeaders(outputMessage.getHeaders())); } } else { throw new RuntimeException("Can't encode requestBody, bodyType must be RaptorMessage."); } } }
public PublisherClientMethodHandler(Target target, MethodMetadata methodMetadata, PublisherHttpClient publisherClient) { this.target = checkNotNull(target, "target must be not null"); this.methodMetadata = checkNotNull(methodMetadata, "methodMetadata must be not null"); this.publisherClient = checkNotNull(publisherClient, "client must be not null"); this.pathExpander = buildExpandFunction(methodMetadata.template().url()); this.headerExpanders = buildExpanders(methodMetadata.template().headers()); this.queriesAll = new HashMap<>(methodMetadata.template().queries()); if (methodMetadata.formParams() != null) { methodMetadata.formParams() .forEach(param -> add(queriesAll, param, "{" + param + "}")); } this.queryExpanders = buildExpanders(queriesAll); }