Example usage for org.springframework.http HttpHeaders put

List of usage examples for org.springframework.http HttpHeaders put

Introduction

In this page you can find the example usage for org.springframework.http HttpHeaders put.

Prototype

@Override
    public List<String> put(String key, List<String> value) 

Source Link

Usage

From source file:HCNIOEngine.java

@Override
public ResponseEntity<String> submit(JCurlRequestOptions requestOptions) throws Exception {
    ResponseEntity<String> stringResponseEntity = null;
    try (CloseableHttpAsyncClient hc = createCloseableHttpAsyncClient()) {
        for (int i = 0; i < requestOptions.getCount(); i++) {
            final HttpHeaders headers = new HttpHeaders();
            for (Map.Entry<String, String> e : requestOptions.getHeaderMap().entrySet()) {
                headers.put(e.getKey(), Collections.singletonList(e.getValue()));
            }/*from   www . jav  a  2 s.  com*/

            final HttpEntity<Void> requestEntity = new HttpEntity<>(headers);

            AsyncRestTemplate template = new AsyncRestTemplate(
                    new HttpComponentsAsyncClientHttpRequestFactory(hc));
            final ListenableFuture<ResponseEntity<String>> exchange = template.exchange(requestOptions.getUrl(),
                    HttpMethod.GET, requestEntity, String.class);
            stringResponseEntity = exchange.get();
            System.out.println(stringResponseEntity.getBody());

        }
        return stringResponseEntity;
    }
}

From source file:be.solidx.hot.rest.RestController.java

protected HttpHeaders jsonResponseHeaders() {
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.put("Content-Type", Lists.newArrayList("application/json", "UTF-8"));
    return httpHeaders;
}

From source file:NNIOEngine.java

@Override
public ResponseEntity<String> submit(JCurlRequestOptions requestOptions) throws Exception {
    int ioWorkerCount = Runtime.getRuntime().availableProcessors() * 2;
    NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup(ioWorkerCount);

    try {/*from ww  w  .ja  v  a2 s.  c o m*/
        final Netty4ClientHttpRequestFactory netty4ClientHttpRequestFactory = new Netty4ClientHttpRequestFactory(
                eventLoopGroup);
        netty4ClientHttpRequestFactory.setConnectTimeout(2000);
        netty4ClientHttpRequestFactory.setReadTimeout(2000);
        /*
                    SslContext sslContext = SslContextBuilder
        .forClient()
        .sslProvider(SslProvider.JDK)
        .build()
                    ;
        */
        if (requestOptions.getUrl().toLowerCase().startsWith("https://")) {
            SslContext sslContext = new DefaultClientSslContext();
            netty4ClientHttpRequestFactory.setSslContext(sslContext);
        }
        netty4ClientHttpRequestFactory.afterPropertiesSet();

        ResponseEntity<String> stringResponseEntity = null;
        for (int i = 0; i < requestOptions.getCount(); i++) {
            final HttpHeaders headers = new HttpHeaders();
            for (Map.Entry<String, String> e : requestOptions.getHeaderMap().entrySet()) {
                headers.put(e.getKey(), Collections.singletonList(e.getValue()));
            }

            final HttpEntity<Void> requestEntity = new HttpEntity<>(headers);

            AsyncRestTemplate template = new AsyncRestTemplate(netty4ClientHttpRequestFactory);
            final ListenableFuture<ResponseEntity<String>> exchange = template.exchange(requestOptions.getUrl(),
                    HttpMethod.GET, requestEntity, String.class);
            stringResponseEntity = exchange.get();
            System.out.println(stringResponseEntity.getBody());
        }
        return stringResponseEntity;
    } finally {
        eventLoopGroup.shutdownGracefully(100, 500, TimeUnit.MILLISECONDS);
    }
}

From source file:com.github.jmnarloch.spring.cloud.feign.VndErrorDecoder.java

/**
 * Maps the response headers map to {@link HttpHeaders}.
 *
 * @param responseHeaders the response headers
 * @return the http headers//from   ww w. j ava 2s.  c  o  m
 */
private HttpHeaders mapHeaders(Map<String, Collection<String>> responseHeaders) {
    final HttpHeaders headers = new HttpHeaders();
    for (Map.Entry<String, Collection<String>> header : responseHeaders.entrySet()) {
        headers.put(header.getKey(), new ArrayList<>(header.getValue()));
    }
    return headers;
}

From source file:io.neba.core.mvc.MultipartSlingHttpServletRequest.java

@Override
@SuppressWarnings("unchecked")
public HttpHeaders getRequestHeaders() {
    HttpHeaders headers = new HttpHeaders();
    Enumeration<String> headerNames = getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String headerName = headerNames.nextElement();
        headers.put(headerName, list(getHeaders(headerName)));
    }//from   w  ww  .  jav a2  s  . com
    return headers;
}

From source file:com.wavemaker.commons.json.deserializer.HttpHeadersDeSerializer.java

@Override
public HttpHeaders deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    ObjectCodec codec = jp.getCodec();/*from   ww  w  .  j  a  v a2 s.c  o  m*/
    ObjectNode root = codec.readTree(jp);
    Map<String, Object> map = ((ObjectMapper) codec).readValue(root.toString(),
            new TypeReference<LinkedHashMap<String, Object>>() {
            });
    HttpHeaders httpHeaders = new HttpHeaders();
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        Object value = entry.getValue();
        if (value instanceof String) {
            httpHeaders.add(entry.getKey(), (String) entry.getValue());
        } else if (value instanceof List) {
            httpHeaders.put(entry.getKey(), (List<String>) value);
        }
    }
    return httpHeaders;
}

From source file:org.jrb.commons.web.ResponseUtils.java

public <R extends Response> ResponseEntity<R> finalize(final R response, final HttpStatus status,
        final HttpHeaders headers) {
    final long startTime = response.getStartTime().getTime();
    response.setElapsedTime(new Date().getTime() - startTime);
    for (final Map.Entry<String, ?> header : response.getHeaders().entrySet()) {
        final String value = header.getValue().toString();
        headers.put(header.getKey(), Collections.singletonList(value));
    }/*from  w w  w.j  av  a 2 s.  c o  m*/
    response.setStatus(status);
    return new ResponseEntity<R>(response, headers, response.getStatus());
}

From source file:com.castlemock.web.mock.rest.web.rest.controller.AbstractRestServiceController.java

/**
 * The process method provides the functionality to process an incoming request. The request will be identified
 * and a corresponding action will be applied for the request. The following actions are support:
 * Forward, record, mock or disable.//from w  w w  .  j  av  a  2 s .c  om
 * @param restRequest The incoming request
 * @param projectId The id of the project that the incoming request belongs to
 * @param applicationId The id of the application that the incoming request belongs to
 * @param resourceId The id of the resource that the incoming request belongs to
 * @param restMethod The REST method which the incoming request belongs to
 * @param httpServletResponse The HTTP servlet response
 * @return A response in String format
 */
protected ResponseEntity<String> process(final RestRequestDto restRequest, final String projectId,
        final String applicationId, final String resourceId, final RestMethodDto restMethod,
        final HttpServletResponse httpServletResponse) {
    Preconditions.checkNotNull(restRequest, "Rest request cannot be null");
    RestEventDto event = null;
    RestResponseDto response = null;
    try {
        event = new RestEventDto(restMethod.getName(), restRequest, projectId, applicationId, resourceId,
                restMethod.getId());
        if (RestMethodStatus.DISABLED.equals(restMethod.getStatus())) {
            throw new RestException("The requested REST method, " + restMethod.getName() + ", is disabled");
        } else if (RestMethodStatus.FORWARDED.equals(restMethod.getStatus())) {
            response = forwardRequest(restRequest, projectId, applicationId, resourceId, restMethod);
        } else if (RestMethodStatus.RECORDING.equals(restMethod.getStatus())) {
            response = forwardRequestAndRecordResponse(restRequest, projectId, applicationId, resourceId,
                    restMethod);
        } else if (RestMethodStatus.RECORD_ONCE.equals(restMethod.getStatus())) {
            response = forwardRequestAndRecordResponseOnce(restRequest, projectId, applicationId, resourceId,
                    restMethod);
        } else if (RestMethodStatus.ECHO.equals(restMethod.getStatus())) {
            response = echoResponse(restRequest);
        } else { // Status.MOCKED
            response = mockResponse(restRequest, projectId, applicationId, resourceId, restMethod);
        }

        HttpHeaders responseHeaders = new HttpHeaders();
        for (HttpHeaderDto httpHeader : response.getHttpHeaders()) {
            List<String> headerValues = new LinkedList<String>();
            headerValues.add(httpHeader.getValue());
            responseHeaders.put(httpHeader.getName(), headerValues);
        }

        if (restMethod.getSimulateNetworkDelay() && restMethod.getNetworkDelay() >= 0) {
            try {
                Thread.sleep(restMethod.getNetworkDelay());
            } catch (InterruptedException e) {
                LOGGER.error("Unable to simulate network delay", e);
            }
        }

        return new ResponseEntity<String>(response.getBody(), responseHeaders,
                HttpStatus.valueOf(response.getHttpStatusCode()));
    } finally {
        if (event != null) {
            event.finish(response);
            serviceProcessor.processAsync(new CreateRestEventInput(event));
        }
    }
}

From source file:com.orange.ngsi2.client.Ngsi2Client.java

/**
 * @return return a clone HttpHeader from default HttpHeader
 */// w  ww.  j  a  v  a  2s . c  om
private HttpHeaders cloneHttpHeaders() {
    HttpHeaders httpHeaders = getHttpHeaders();
    HttpHeaders clone = new HttpHeaders();
    for (String entry : httpHeaders.keySet()) {
        clone.put(entry, httpHeaders.get(entry));
    }
    return clone;
}

From source file:edu.mayo.cts2.framework.webapp.rest.controller.AbstractMessageWrappingController.java

@SuppressWarnings("unchecked")
protected HttpHeaders getHeaders(HttpServletRequest request) {
    HttpHeaders httpHeaders = new HttpHeaders();

    Enumeration<String> headers = request.getHeaderNames();
    while (headers.hasMoreElements()) {
        String headerName = headers.nextElement();
        httpHeaders.put(headerName, Collections.list(request.getHeaders(headerName)));
    }/*from www.ja  v a 2s  . com*/

    return httpHeaders;
}