Example usage for org.springframework.http HttpHeaders setIfNoneMatch

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

Introduction

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

Prototype

public void setIfNoneMatch(List<String> ifNoneMatchList) 

Source Link

Document

Set the (new) values of the If-None-Match header.

Usage

From source file:org.springsource.restbucks.training.payment.web.PaymentProcessIntegrationTest.java

/**
 * Polls the order resource every 2 seconds and uses an {@code If-None-Match} header alongside the {@code ETag} of the
 * first response to avoid sending the representation over and over again.
 * /*from ww w  .  j  a va2  s .  c  om*/
 * @param response
 * @return
 * @throws Exception
 */
private MockHttpServletResponse pollUntilOrderHasReceiptLink(MockHttpServletResponse response)
        throws Exception {

    // Grab
    String content = response.getContentAsString();
    LinkDiscoverer discoverer = getDiscovererFor(response);
    Link orderLink = discoverer.findLinkWithRel(ORDER_REL, content);

    // Poll order until receipt link is set
    Link receiptLink = null;
    String etag = null;
    MockHttpServletResponse pollResponse;

    do {

        HttpHeaders headers = new HttpHeaders();
        if (etag != null) {
            headers.setIfNoneMatch(etag);
        }

        log.info("Poll state of order until receipt is ready");

        ResultActions action = mvc.perform(get(orderLink.getHref()).headers(headers));
        pollResponse = action.andReturn().getResponse();

        int status = pollResponse.getStatus();
        etag = pollResponse.getHeader("ETag");

        log.info(String.format("Received %s with ETag of %s", status, etag));

        if (status == HttpStatus.OK.value()) {

            action.andExpect(linkWithRelIsPresent(Link.REL_SELF)). //
                    andExpect(linkWithRelIsNotPresent(UPDATE_REL)). //
                    andExpect(linkWithRelIsNotPresent(CANCEL_REL));

            receiptLink = discoverer.findLinkWithRel(RECEIPT_REL, pollResponse.getContentAsString());

        } else if (status == HttpStatus.NO_CONTENT.value()) {
            action.andExpect(content().string(isEmptyOrNullString()));
        }

        if (receiptLink == null) {
            Thread.sleep(2000);
        }

    } while (receiptLink == null);

    return pollResponse;
}

From source file:org.springsource.restbucks.payment.web.PaymentProcessIntegrationTest.java

/**
 * Polls the order resource every 2 seconds and uses an {@code If-None-Match} header alongside the {@code ETag} of the
 * first response to avoid sending the representation over and over again.
 * //w w w  .  j a  va2s. c  o  m
 * @param response
 * @return
 * @throws Exception
 */
private MockHttpServletResponse pollUntilOrderHasReceiptLink(MockHttpServletResponse response)
        throws Exception {

    // Grab
    String content = response.getContentAsString();
    LinkDiscoverer discoverer = getDiscovererFor(response);
    Link orderLink = discoverer.findLinkWithRel(ORDER_REL, content);

    // Poll order until receipt link is set
    Link receiptLink = null;
    String etag = null;
    MockHttpServletResponse pollResponse;

    do {

        HttpHeaders headers = new HttpHeaders();
        if (etag != null) {
            headers.setIfNoneMatch(etag);
        }

        LOG.info("Poll state of order until receipt is ready");

        ResultActions action = mvc.perform(get(orderLink.expand().getHref()).headers(headers));
        pollResponse = action.andReturn().getResponse();

        int status = pollResponse.getStatus();
        etag = pollResponse.getHeader("ETag");

        LOG.info(String.format("Received %s with ETag of %s", status, etag));

        if (status == HttpStatus.OK.value()) {

            action.andExpect(linkWithRelIsPresent(Link.REL_SELF)). //
                    andExpect(linkWithRelIsNotPresent(UPDATE_REL)). //
                    andExpect(linkWithRelIsNotPresent(CANCEL_REL));

            receiptLink = discoverer.findLinkWithRel(RECEIPT_REL, pollResponse.getContentAsString());

        } else if (status == HttpStatus.NO_CONTENT.value()) {
            action.andExpect(content().string(isEmptyOrNullString()));
        }

        if (receiptLink == null) {
            Thread.sleep(2000);
        }

    } while (receiptLink == null);

    return pollResponse;
}

From source file:org.springsource.restbucks.PaymentProcessIntegrationTest.java

/**
 * Polls the order resource every 2 seconds and uses an {@code If-None-Match} header alongside the {@code ETag} of the
 * first response to avoid sending the representation over and over again.
 * /*ww  w.jav  a 2s  . co m*/
 * @param response
 * @return
 * @throws Exception
 */
private MockHttpServletResponse pollUntilOrderHasReceiptLink(MockHttpServletResponse response)
        throws Exception {

    // Grab
    String content = response.getContentAsString();
    Link orderLink = links.findLinkWithRel(ORDER_REL, content);

    // Poll order until receipt link is set
    Link receiptLink = null;
    String etag = null;
    MockHttpServletResponse pollResponse;

    do {

        HttpHeaders headers = new HttpHeaders();
        if (etag != null) {
            headers.setIfNoneMatch(etag);
        }

        log.info("Poll state of order until receipt is ready");

        ResultActions action = mvc.perform(get(orderLink.getHref()).headers(headers));
        pollResponse = action.andReturn().getResponse();

        int status = pollResponse.getStatus();
        etag = pollResponse.getHeader("ETag");

        log.info(String.format("Received %s with ETag of %s", status, etag));

        if (status == HttpStatus.OK.value()) {

            action.andExpect(linkWithRelIsPresent(Link.REL_SELF)). //
                    andExpect(linkWithRelIsNotPresent(UPDATE_REL)). //
                    andExpect(linkWithRelIsNotPresent(CANCEL_REL));

            receiptLink = links.findLinkWithRel(RECEIPT_REL, pollResponse.getContentAsString());

        } else if (status == HttpStatus.NO_CONTENT.value()) {
            action.andExpect(content().string(isEmptyOrNullString()));
        }

        if (receiptLink == null) {
            Thread.sleep(2000);
        }

    } while (receiptLink == null);

    return pollResponse;
}

From source file:net.paslavsky.springrest.HttpHeadersHelper.java

public HttpHeaders getHttpHeaders(Map<String, Integer> headerParameters, Object[] arguments) {
    HttpHeaders headers = new HttpHeaders();
    for (String headerName : headerParameters.keySet()) {
        Object headerValue = arguments[headerParameters.get(headerName)];
        if (headerValue != null) {
            if (ACCEPT.equalsIgnoreCase(headerName)) {
                headers.setAccept(toList(headerValue, MediaType.class));
            } else if (ACCEPT_CHARSET.equalsIgnoreCase(headerName)) {
                headers.setAcceptCharset(toList(headerValue, Charset.class));
            } else if (ALLOW.equalsIgnoreCase(headerName)) {
                headers.setAllow(toSet(headerValue, HttpMethod.class));
            } else if (CONNECTION.equalsIgnoreCase(headerName)) {
                headers.setConnection(toList(headerValue, String.class));
            } else if (CONTENT_DISPOSITION.equalsIgnoreCase(headerName)) {
                setContentDisposition(headers, headerName, headerValue);
            } else if (CONTENT_LENGTH.equalsIgnoreCase(headerName)) {
                headers.setContentLength(toLong(headerValue));
            } else if (CONTENT_TYPE.equalsIgnoreCase(headerName)) {
                headers.setContentType(toMediaType(headerValue));
            } else if (DATE.equalsIgnoreCase(headerName)) {
                headers.setDate(toLong(headerValue));
            } else if (ETAG.equalsIgnoreCase(headerName)) {
                headers.setETag(toString(headerValue));
            } else if (EXPIRES.equalsIgnoreCase(headerName)) {
                headers.setExpires(toLong(headerValue));
            } else if (IF_MODIFIED_SINCE.equalsIgnoreCase(headerName)) {
                headers.setIfModifiedSince(toLong(headerValue));
            } else if (IF_NONE_MATCH.equalsIgnoreCase(headerName)) {
                headers.setIfNoneMatch(toList(headerValue, String.class));
            } else if (LAST_MODIFIED.equalsIgnoreCase(headerName)) {
                headers.setLastModified(toLong(headerValue));
            } else if (LOCATION.equalsIgnoreCase(headerName)) {
                headers.setLocation(toURI(headerValue));
            } else if (ORIGIN.equalsIgnoreCase(headerName)) {
                headers.setOrigin(toString(headerValue));
            } else if (PRAGMA.equalsIgnoreCase(headerName)) {
                headers.setPragma(toString(headerValue));
            } else if (UPGRADE.equalsIgnoreCase(headerName)) {
                headers.setUpgrade(toString(headerValue));
            } else if (headerValue instanceof String) {
                headers.set(headerName, (String) headerValue);
            } else if (headerValue instanceof String[]) {
                headers.put(headerName, Arrays.asList((String[]) headerValue));
            } else if (instanceOf(headerValue, String.class)) {
                headers.put(headerName, toList(headerValue, String.class));
            } else {
                headers.set(headerName, conversionService.convert(headerValue, String.class));
            }//from w w w  .  ja va 2s  . c  o m
        }
    }
    return headers;
}

From source file:org.springframework.cloud.stream.app.http.source.DefaultMixedCaseContentTypeHttpHeaderMapper.java

private void setHttpHeader(HttpHeaders target, String name, Object value) {
    if (ACCEPT.equalsIgnoreCase(name)) {
        if (value instanceof Collection<?>) {
            Collection<?> values = (Collection<?>) value;
            if (!CollectionUtils.isEmpty(values)) {
                List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
                for (Object type : values) {
                    if (type instanceof MediaType) {
                        acceptableMediaTypes.add((MediaType) type);
                    } else if (type instanceof String) {
                        acceptableMediaTypes.addAll(MediaType.parseMediaTypes((String) type));
                    } else {
                        Class<?> clazz = (type != null) ? type.getClass() : null;
                        throw new IllegalArgumentException(
                                "Expected MediaType or String value for 'Accept' header value, but received: "
                                        + clazz);
                    }/*  w  w w.j ava2 s. com*/
                }
                target.setAccept(acceptableMediaTypes);
            }
        } else if (value instanceof MediaType) {
            target.setAccept(Collections.singletonList((MediaType) value));
        } else if (value instanceof String[]) {
            List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
            for (String next : (String[]) value) {
                acceptableMediaTypes.add(MediaType.parseMediaType(next));
            }
            target.setAccept(acceptableMediaTypes);
        } else if (value instanceof String) {
            target.setAccept(MediaType.parseMediaTypes((String) value));
        } else {
            Class<?> clazz = (value != null) ? value.getClass() : null;
            throw new IllegalArgumentException(
                    "Expected MediaType or String value for 'Accept' header value, but received: " + clazz);
        }
    } else if (ACCEPT_CHARSET.equalsIgnoreCase(name)) {
        if (value instanceof Collection<?>) {
            Collection<?> values = (Collection<?>) value;
            if (!CollectionUtils.isEmpty(values)) {
                List<Charset> acceptableCharsets = new ArrayList<Charset>();
                for (Object charset : values) {
                    if (charset instanceof Charset) {
                        acceptableCharsets.add((Charset) charset);
                    } else if (charset instanceof String) {
                        acceptableCharsets.add(Charset.forName((String) charset));
                    } else {
                        Class<?> clazz = (charset != null) ? charset.getClass() : null;
                        throw new IllegalArgumentException(
                                "Expected Charset or String value for 'Accept-Charset' header value, but received: "
                                        + clazz);
                    }
                }
                target.setAcceptCharset(acceptableCharsets);
            }
        } else if (value instanceof Charset[] || value instanceof String[]) {
            List<Charset> acceptableCharsets = new ArrayList<Charset>();
            Object[] values = ObjectUtils.toObjectArray(value);
            for (Object charset : values) {
                if (charset instanceof Charset) {
                    acceptableCharsets.add((Charset) charset);
                } else if (charset instanceof String) {
                    acceptableCharsets.add(Charset.forName((String) charset));
                }
            }
            target.setAcceptCharset(acceptableCharsets);
        } else if (value instanceof Charset) {
            target.setAcceptCharset(Collections.singletonList((Charset) value));
        } else if (value instanceof String) {
            String[] charsets = StringUtils.commaDelimitedListToStringArray((String) value);
            List<Charset> acceptableCharsets = new ArrayList<Charset>();
            for (String charset : charsets) {
                acceptableCharsets.add(Charset.forName(charset.trim()));
            }
            target.setAcceptCharset(acceptableCharsets);
        } else {
            Class<?> clazz = (value != null) ? value.getClass() : null;
            throw new IllegalArgumentException(
                    "Expected Charset or String value for 'Accept-Charset' header value, but received: "
                            + clazz);
        }
    } else if (ALLOW.equalsIgnoreCase(name)) {
        if (value instanceof Collection<?>) {
            Collection<?> values = (Collection<?>) value;
            if (!CollectionUtils.isEmpty(values)) {
                Set<HttpMethod> allowedMethods = new HashSet<HttpMethod>();
                for (Object method : values) {
                    if (method instanceof HttpMethod) {
                        allowedMethods.add((HttpMethod) method);
                    } else if (method instanceof String) {
                        allowedMethods.add(HttpMethod.valueOf((String) method));
                    } else {
                        Class<?> clazz = (method != null) ? method.getClass() : null;
                        throw new IllegalArgumentException(
                                "Expected HttpMethod or String value for 'Allow' header value, but received: "
                                        + clazz);
                    }
                }
                target.setAllow(allowedMethods);
            }
        } else {
            if (value instanceof HttpMethod) {
                target.setAllow(Collections.singleton((HttpMethod) value));
            } else if (value instanceof HttpMethod[]) {
                Set<HttpMethod> allowedMethods = new HashSet<HttpMethod>();
                Collections.addAll(allowedMethods, (HttpMethod[]) value);
                target.setAllow(allowedMethods);
            } else if (value instanceof String || value instanceof String[]) {
                String[] values = (value instanceof String[]) ? (String[]) value
                        : StringUtils.commaDelimitedListToStringArray((String) value);
                Set<HttpMethod> allowedMethods = new HashSet<HttpMethod>();
                for (String next : values) {
                    allowedMethods.add(HttpMethod.valueOf(next.trim()));
                }
                target.setAllow(allowedMethods);
            } else {
                Class<?> clazz = (value != null) ? value.getClass() : null;
                throw new IllegalArgumentException(
                        "Expected HttpMethod or String value for 'Allow' header value, but received: " + clazz);
            }
        }
    } else if (CACHE_CONTROL.equalsIgnoreCase(name)) {
        if (value instanceof String) {
            target.setCacheControl((String) value);
        } else {
            Class<?> clazz = (value != null) ? value.getClass() : null;
            throw new IllegalArgumentException(
                    "Expected String value for 'Cache-Control' header value, but received: " + clazz);
        }
    } else if (CONTENT_LENGTH.equalsIgnoreCase(name)) {
        if (value instanceof Number) {
            target.setContentLength(((Number) value).longValue());
        } else if (value instanceof String) {
            target.setContentLength(Long.parseLong((String) value));
        } else {
            Class<?> clazz = (value != null) ? value.getClass() : null;
            throw new IllegalArgumentException(
                    "Expected Number or String value for 'Content-Length' header value, but received: "
                            + clazz);
        }
    } else if (MessageHeaders.CONTENT_TYPE.equalsIgnoreCase(name)) {
        if (value instanceof MediaType) {
            target.setContentType((MediaType) value);
        } else if (value instanceof String) {
            target.setContentType(MediaType.parseMediaType((String) value));
        } else {
            Class<?> clazz = (value != null) ? value.getClass() : null;
            throw new IllegalArgumentException(
                    "Expected MediaType or String value for 'Content-Type' header value, but received: "
                            + clazz);
        }
    } else if (DATE.equalsIgnoreCase(name)) {
        if (value instanceof Date) {
            target.setDate(((Date) value).getTime());
        } else if (value instanceof Number) {
            target.setDate(((Number) value).longValue());
        } else if (value instanceof String) {
            try {
                target.setDate(Long.parseLong((String) value));
            } catch (NumberFormatException e) {
                target.setDate(this.getFirstDate((String) value, DATE));
            }
        } else {
            Class<?> clazz = (value != null) ? value.getClass() : null;
            throw new IllegalArgumentException(
                    "Expected Date, Number, or String value for 'Date' header value, but received: " + clazz);
        }
    } else if (ETAG.equalsIgnoreCase(name)) {
        if (value instanceof String) {
            target.setETag((String) value);
        } else {
            Class<?> clazz = (value != null) ? value.getClass() : null;
            throw new IllegalArgumentException(
                    "Expected String value for 'ETag' header value, but received: " + clazz);
        }
    } else if (EXPIRES.equalsIgnoreCase(name)) {
        if (value instanceof Date) {
            target.setExpires(((Date) value).getTime());
        } else if (value instanceof Number) {
            target.setExpires(((Number) value).longValue());
        } else if (value instanceof String) {
            try {
                target.setExpires(Long.parseLong((String) value));
            } catch (NumberFormatException e) {
                target.setExpires(this.getFirstDate((String) value, EXPIRES));
            }
        } else {
            Class<?> clazz = (value != null) ? value.getClass() : null;
            throw new IllegalArgumentException(
                    "Expected Date, Number, or String value for 'Expires' header value, but received: "
                            + clazz);
        }
    } else if (IF_MODIFIED_SINCE.equalsIgnoreCase(name)) {
        if (value instanceof Date) {
            target.setIfModifiedSince(((Date) value).getTime());
        } else if (value instanceof Number) {
            target.setIfModifiedSince(((Number) value).longValue());
        } else if (value instanceof String) {
            try {
                target.setIfModifiedSince(Long.parseLong((String) value));
            } catch (NumberFormatException e) {
                target.setIfModifiedSince(this.getFirstDate((String) value, IF_MODIFIED_SINCE));
            }
        } else {
            Class<?> clazz = (value != null) ? value.getClass() : null;
            throw new IllegalArgumentException(
                    "Expected Date, Number, or String value for 'If-Modified-Since' header value, but received: "
                            + clazz);
        }
    } else if (IF_UNMODIFIED_SINCE.equalsIgnoreCase(name)) {
        String ifUnmodifiedSinceValue = null;
        if (value instanceof Date) {
            ifUnmodifiedSinceValue = this.formatDate(((Date) value).getTime());
        } else if (value instanceof Number) {
            ifUnmodifiedSinceValue = this.formatDate(((Number) value).longValue());
        } else if (value instanceof String) {
            try {
                ifUnmodifiedSinceValue = this.formatDate(Long.parseLong((String) value));
            } catch (NumberFormatException e) {
                long longValue = this.getFirstDate((String) value, IF_UNMODIFIED_SINCE);
                ifUnmodifiedSinceValue = this.formatDate(longValue);
            }
        } else {
            Class<?> clazz = (value != null) ? value.getClass() : null;
            throw new IllegalArgumentException(
                    "Expected Date, Number, or String value for 'If-Unmodified-Since' header value, but received: "
                            + clazz);
        }
        target.set(IF_UNMODIFIED_SINCE, ifUnmodifiedSinceValue);
    } else if (IF_NONE_MATCH.equalsIgnoreCase(name)) {
        if (value instanceof String) {
            target.setIfNoneMatch((String) value);
        } else if (value instanceof String[]) {
            String delmitedString = StringUtils.arrayToCommaDelimitedString((String[]) value);
            target.setIfNoneMatch(delmitedString);
        } else if (value instanceof Collection) {
            Collection<?> values = (Collection<?>) value;
            if (!CollectionUtils.isEmpty(values)) {
                List<String> ifNoneMatchList = new ArrayList<String>();
                for (Object next : values) {
                    if (next instanceof String) {
                        ifNoneMatchList.add((String) next);
                    } else {
                        Class<?> clazz = (next != null) ? next.getClass() : null;
                        throw new IllegalArgumentException(
                                "Expected String value for 'If-None-Match' header value, but received: "
                                        + clazz);
                    }
                }
                target.setIfNoneMatch(ifNoneMatchList);
            }
        }
    } else if (LAST_MODIFIED.equalsIgnoreCase(name)) {
        if (value instanceof Date) {
            target.setLastModified(((Date) value).getTime());
        } else if (value instanceof Number) {
            target.setLastModified(((Number) value).longValue());
        } else if (value instanceof String) {
            try {
                target.setLastModified(Long.parseLong((String) value));
            } catch (NumberFormatException e) {
                target.setLastModified(this.getFirstDate((String) value, LAST_MODIFIED));
            }
        } else {
            Class<?> clazz = (value != null) ? value.getClass() : null;
            throw new IllegalArgumentException(
                    "Expected Date, Number, or String value for 'Last-Modified' header value, but received: "
                            + clazz);
        }
    } else if (LOCATION.equalsIgnoreCase(name)) {
        if (value instanceof URI) {
            target.setLocation((URI) value);
        } else if (value instanceof String) {
            try {
                target.setLocation(new URI((String) value));
            } catch (URISyntaxException e) {
                throw new IllegalArgumentException(e);
            }
        } else {
            Class<?> clazz = (value != null) ? value.getClass() : null;
            throw new IllegalArgumentException(
                    "Expected URI or String value for 'Location' header value, but received: " + clazz);
        }
    } else if (PRAGMA.equalsIgnoreCase(name)) {
        if (value instanceof String) {
            target.setPragma((String) value);
        } else {
            Class<?> clazz = (value != null) ? value.getClass() : null;
            throw new IllegalArgumentException(
                    "Expected String value for 'Pragma' header value, but received: " + clazz);
        }
    } else if (value instanceof String) {
        target.set(name, (String) value);
    } else if (value instanceof String[]) {
        for (String next : (String[]) value) {
            target.add(name, next);
        }
    } else if (value instanceof Iterable<?>) {
        for (Object next : (Iterable<?>) value) {
            String convertedValue = null;
            if (next instanceof String) {
                convertedValue = (String) next;
            } else {
                convertedValue = this.convertToString(value);
            }
            if (StringUtils.hasText(convertedValue)) {
                target.add(name, convertedValue);
            } else {
                logger.warn("Element of the header '" + name + "' with value '" + value
                        + "' will not be set since it is not a String and no Converter is available. "
                        + "Consider registering a Converter with ConversionService (e.g., <int:converter>)");
            }
        }
    } else {
        String convertedValue = this.convertToString(value);
        if (StringUtils.hasText(convertedValue)) {
            target.set(name, convertedValue);
        } else {
            logger.warn("Header '" + name + "' with value '" + value
                    + "' will not be set since it is not a String and no Converter is available. "
                    + "Consider registering a Converter with ConversionService (e.g., <int:converter>)");
        }
    }
}

From source file:org.springframework.integration.http.support.DefaultHttpHeaderMapper.java

private void setHttpHeader(HttpHeaders target, String name, Object value) {
    if (ACCEPT.equalsIgnoreCase(name)) {
        if (value instanceof Collection<?>) {
            Collection<?> values = (Collection<?>) value;
            if (!CollectionUtils.isEmpty(values)) {
                List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
                for (Object type : values) {
                    if (type instanceof MediaType) {
                        acceptableMediaTypes.add((MediaType) type);
                    } else if (type instanceof String) {
                        acceptableMediaTypes.addAll(MediaType.parseMediaTypes((String) type));
                    } else {
                        Class<?> clazz = (type != null) ? type.getClass() : null;
                        throw new IllegalArgumentException(
                                "Expected MediaType or String value for 'Accept' header value, but received: "
                                        + clazz);
                    }/*from   w w  w .j a v  a  2  s  .c om*/
                }
                target.setAccept(acceptableMediaTypes);
            }
        } else if (value instanceof MediaType) {
            target.setAccept(Collections.singletonList((MediaType) value));
        } else if (value instanceof String[]) {
            List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
            for (String next : (String[]) value) {
                acceptableMediaTypes.add(MediaType.parseMediaType(next));
            }
            target.setAccept(acceptableMediaTypes);
        } else if (value instanceof String) {
            target.setAccept(MediaType.parseMediaTypes((String) value));
        } else {
            Class<?> clazz = (value != null) ? value.getClass() : null;
            throw new IllegalArgumentException(
                    "Expected MediaType or String value for 'Accept' header value, but received: " + clazz);
        }
    } else if (ACCEPT_CHARSET.equalsIgnoreCase(name)) {
        if (value instanceof Collection<?>) {
            Collection<?> values = (Collection<?>) value;
            if (!CollectionUtils.isEmpty(values)) {
                List<Charset> acceptableCharsets = new ArrayList<Charset>();
                for (Object charset : values) {
                    if (charset instanceof Charset) {
                        acceptableCharsets.add((Charset) charset);
                    } else if (charset instanceof String) {
                        acceptableCharsets.add(Charset.forName((String) charset));
                    } else {
                        Class<?> clazz = (charset != null) ? charset.getClass() : null;
                        throw new IllegalArgumentException(
                                "Expected Charset or String value for 'Accept-Charset' header value, but received: "
                                        + clazz);
                    }
                }
                target.setAcceptCharset(acceptableCharsets);
            }
        } else if (value instanceof Charset[] || value instanceof String[]) {
            List<Charset> acceptableCharsets = new ArrayList<Charset>();
            Object[] values = ObjectUtils.toObjectArray(value);
            for (Object charset : values) {
                if (charset instanceof Charset) {
                    acceptableCharsets.add((Charset) charset);
                } else if (charset instanceof String) {
                    acceptableCharsets.add(Charset.forName((String) charset));
                }
            }
            target.setAcceptCharset(acceptableCharsets);
        } else if (value instanceof Charset) {
            target.setAcceptCharset(Collections.singletonList((Charset) value));
        } else if (value instanceof String) {
            String[] charsets = StringUtils.commaDelimitedListToStringArray((String) value);
            List<Charset> acceptableCharsets = new ArrayList<Charset>();
            for (String charset : charsets) {
                acceptableCharsets.add(Charset.forName(charset.trim()));
            }
            target.setAcceptCharset(acceptableCharsets);
        } else {
            Class<?> clazz = (value != null) ? value.getClass() : null;
            throw new IllegalArgumentException(
                    "Expected Charset or String value for 'Accept-Charset' header value, but received: "
                            + clazz);
        }
    } else if (ALLOW.equalsIgnoreCase(name)) {
        if (value instanceof Collection<?>) {
            Collection<?> values = (Collection<?>) value;
            if (!CollectionUtils.isEmpty(values)) {
                Set<HttpMethod> allowedMethods = new HashSet<HttpMethod>();
                for (Object method : values) {
                    if (method instanceof HttpMethod) {
                        allowedMethods.add((HttpMethod) method);
                    } else if (method instanceof String) {
                        allowedMethods.add(HttpMethod.valueOf((String) method));
                    } else {
                        Class<?> clazz = (method != null) ? method.getClass() : null;
                        throw new IllegalArgumentException(
                                "Expected HttpMethod or String value for 'Allow' header value, but received: "
                                        + clazz);
                    }
                }
                target.setAllow(allowedMethods);
            }
        } else {
            if (value instanceof HttpMethod) {
                target.setAllow(Collections.singleton((HttpMethod) value));
            } else if (value instanceof HttpMethod[]) {
                Set<HttpMethod> allowedMethods = new HashSet<HttpMethod>();
                for (HttpMethod next : (HttpMethod[]) value) {
                    allowedMethods.add(next);
                }
                target.setAllow(allowedMethods);
            } else if (value instanceof String || value instanceof String[]) {
                String[] values = (value instanceof String[]) ? (String[]) value
                        : StringUtils.commaDelimitedListToStringArray((String) value);
                Set<HttpMethod> allowedMethods = new HashSet<HttpMethod>();
                for (String next : values) {
                    allowedMethods.add(HttpMethod.valueOf(next.trim()));
                }
                target.setAllow(allowedMethods);
            } else {
                Class<?> clazz = (value != null) ? value.getClass() : null;
                throw new IllegalArgumentException(
                        "Expected HttpMethod or String value for 'Allow' header value, but received: " + clazz);
            }
        }
    } else if (CACHE_CONTROL.equalsIgnoreCase(name)) {
        if (value instanceof String) {
            target.setCacheControl((String) value);
        } else {
            Class<?> clazz = (value != null) ? value.getClass() : null;
            throw new IllegalArgumentException(
                    "Expected String value for 'Cache-Control' header value, but received: " + clazz);
        }
    } else if (CONTENT_LENGTH.equalsIgnoreCase(name)) {
        if (value instanceof Number) {
            target.setContentLength(((Number) value).longValue());
        } else if (value instanceof String) {
            target.setContentLength(Long.parseLong((String) value));
        } else {
            Class<?> clazz = (value != null) ? value.getClass() : null;
            throw new IllegalArgumentException(
                    "Expected Number or String value for 'Content-Length' header value, but received: "
                            + clazz);
        }
    } else if (CONTENT_TYPE.equalsIgnoreCase(name)) {
        if (value instanceof MediaType) {
            target.setContentType((MediaType) value);
        } else if (value instanceof String) {
            target.setContentType(MediaType.parseMediaType((String) value));
        } else {
            Class<?> clazz = (value != null) ? value.getClass() : null;
            throw new IllegalArgumentException(
                    "Expected MediaType or String value for 'Content-Type' header value, but received: "
                            + clazz);
        }
    } else if (DATE.equalsIgnoreCase(name)) {
        if (value instanceof Date) {
            target.setDate(((Date) value).getTime());
        } else if (value instanceof Number) {
            target.setDate(((Number) value).longValue());
        } else if (value instanceof String) {
            target.setDate(Long.parseLong((String) value));
        } else {
            Class<?> clazz = (value != null) ? value.getClass() : null;
            throw new IllegalArgumentException(
                    "Expected Date, Number, or String value for 'Date' header value, but received: " + clazz);
        }
    } else if (ETAG.equalsIgnoreCase(name)) {
        if (value instanceof String) {
            target.setETag((String) value);
        } else {
            Class<?> clazz = (value != null) ? value.getClass() : null;
            throw new IllegalArgumentException(
                    "Expected String value for 'ETag' header value, but received: " + clazz);
        }
    } else if (EXPIRES.equalsIgnoreCase(name)) {
        if (value instanceof Date) {
            target.setExpires(((Date) value).getTime());
        } else if (value instanceof Number) {
            target.setExpires(((Number) value).longValue());
        } else if (value instanceof String) {
            target.setExpires(Long.parseLong((String) value));
        } else {
            Class<?> clazz = (value != null) ? value.getClass() : null;
            throw new IllegalArgumentException(
                    "Expected Date, Number, or String value for 'Expires' header value, but received: "
                            + clazz);
        }
    } else if (IF_MODIFIED_SINCE.equalsIgnoreCase(name)) {
        if (value instanceof Date) {
            target.setIfModifiedSince(((Date) value).getTime());
        } else if (value instanceof Number) {
            target.setIfModifiedSince(((Number) value).longValue());
        } else if (value instanceof String) {
            target.setIfModifiedSince(Long.parseLong((String) value));
        } else {
            Class<?> clazz = (value != null) ? value.getClass() : null;
            throw new IllegalArgumentException(
                    "Expected Date, Number, or String value for 'If-Modified-Since' header value, but received: "
                            + clazz);
        }
    } else if (IF_NONE_MATCH.equalsIgnoreCase(name)) {
        if (value instanceof String) {
            target.setIfNoneMatch((String) value);
        } else if (value instanceof String[]) {
            String delmitedString = StringUtils.arrayToCommaDelimitedString((String[]) value);
            target.setIfNoneMatch(delmitedString);
        } else if (value instanceof Collection) {
            Collection<?> values = (Collection<?>) value;
            if (!CollectionUtils.isEmpty(values)) {
                List<String> ifNoneMatchList = new ArrayList<String>();
                for (Object next : values) {
                    if (next instanceof String) {
                        ifNoneMatchList.add((String) next);
                    } else {
                        Class<?> clazz = (next != null) ? next.getClass() : null;
                        throw new IllegalArgumentException(
                                "Expected String value for 'If-None-Match' header value, but received: "
                                        + clazz);
                    }
                }
                target.setIfNoneMatch(ifNoneMatchList);
            }
        }
    } else if (LAST_MODIFIED.equalsIgnoreCase(name)) {
        if (value instanceof Date) {
            target.setLastModified(((Date) value).getTime());
        } else if (value instanceof Number) {
            target.setLastModified(((Number) value).longValue());
        } else if (value instanceof String) {
            target.setLastModified(Long.parseLong((String) value));
        } else {
            Class<?> clazz = (value != null) ? value.getClass() : null;
            throw new IllegalArgumentException(
                    "Expected Date, Number, or String value for 'Last-Modified' header value, but received: "
                            + clazz);
        }
    } else if (LOCATION.equalsIgnoreCase(name)) {
        if (value instanceof URI) {
            target.setLocation((URI) value);
        } else if (value instanceof String) {
            try {
                target.setLocation(new URI((String) value));
            } catch (URISyntaxException e) {
                throw new IllegalArgumentException(e);
            }
        } else {
            Class<?> clazz = (value != null) ? value.getClass() : null;
            throw new IllegalArgumentException(
                    "Expected URI or String value for 'Location' header value, but received: " + clazz);
        }
    } else if (PRAGMA.equalsIgnoreCase(name)) {
        if (value instanceof String) {
            target.setPragma((String) value);
        } else {
            Class<?> clazz = (value != null) ? value.getClass() : null;
            throw new IllegalArgumentException(
                    "Expected String value for 'Pragma' header value, but received: " + clazz);
        }
    } else if (value instanceof String) {
        target.set(name, (String) value);
    } else if (value instanceof String[]) {
        for (String next : (String[]) value) {
            target.add(name, next);
        }
    } else if (value instanceof Iterable<?>) {
        for (Object next : (Iterable<?>) value) {
            String convertedValue = null;
            if (next instanceof String) {
                convertedValue = (String) next;
            } else {
                convertedValue = this.convertToString(value);
            }
            if (StringUtils.hasText(convertedValue)) {
                target.add(name, (String) next);
            } else {
                logger.warn("Element of the header '" + name + "' with value '" + value
                        + "' will not be set since it is not a String and no Converter "
                        + "is available. Consider registering a Converter with ConversionService (e.g., <int:converter>)");
            }
        }
    } else {
        String convertedValue = this.convertToString(value);
        if (StringUtils.hasText(convertedValue)) {
            target.set(name, convertedValue);
        } else {
            logger.warn("Header '" + name + "' with value '" + value
                    + "' will not be set since it is not a String and no Converter "
                    + "is available. Consider registering a Converter with ConversionService (e.g., <int:converter>)");
        }
    }
}