Java Code Examples for javax.ws.rs.ext.RuntimeDelegate.HeaderDelegate#toString()

The following examples show how to use javax.ws.rs.ext.RuntimeDelegate.HeaderDelegate#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static void convertHeaderValuesToString(Map<String, List<Object>> headers, boolean delegateOnly) {
    if (headers == null) {
        return;
    }
    RuntimeDelegate rd = getOtherRuntimeDelegate();
    if (rd == null && delegateOnly) {
        return;
    }
    for (Map.Entry<String, List<Object>> entry : headers.entrySet()) {
        List<Object> values = entry.getValue();
        for (int i = 0; i < values.size(); i++) {
            Object value = values.get(i);

            if (value != null && !(value instanceof String)) {

                HeaderDelegate<Object> hd = getHeaderDelegate(rd, value);

                if (hd != null) {
                    value = hd.toString(value);
                } else if (!delegateOnly) {
                    value = value.toString();
                }

                try {
                    values.set(i, value);
                } catch (UnsupportedOperationException ex) {
                    // this may happen if an unmodifiable List was set via Map put
                    List<Object> newList = new ArrayList<>(values);
                    newList.set(i, value);
                    // Won't help if the map is unmodifiable in which case it is a bug anyway
                    headers.put(entry.getKey(), newList);
                }

            }

        }
    }

}
 
Example 2
Source File: ResponseImpl.java    From cxf with Apache License 2.0 5 votes vote down vote up
private List<String> toListOfStrings(List<Object> values) {
    if (values == null) {
        return null;
    }
    List<String> stringValues = new ArrayList<>(values.size());
    HeaderDelegate<Object> hd = HttpUtils.getHeaderDelegate(values.get(0));
    for (Object value : values) {
        String actualValue = hd == null ? value.toString() : hd.toString(value);
        stringValues.add(actualValue);
    }
    return stringValues;
}
 
Example 3
Source File: InvocationBuilderImpl.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void doSetHeader(RuntimeDelegate rd, String name, Object value) {
    HeaderDelegate<Object> hd = HttpUtils.getHeaderDelegate(rd, value);
    if (hd != null) {
        value = hd.toString(value);
    }
    
    // If value is null then all current headers of the same name should be removed
    if (value == null) {
        webClient.replaceHeader(name, value);
    } else {
        webClient.header(name, value);
    }
}
 
Example 4
Source File: HeaderHelper.java    From everrest with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Creates string representation of given object for adding to response header. Method uses {@link HeaderDelegate#toString()}.
 * If required implementation of HeaderDelegate is not accessible via {@link RuntimeDelegate#createHeaderDelegate(java.lang.Class)}
 * then method {@code toString} of given object is used.
 *
 * @param o
 *         object
 * @return string representation of supplied type
 */
@SuppressWarnings({"unchecked"})
public static String getHeaderAsString(Object o) {
    HeaderDelegate headerDelegate = RuntimeDelegate.getInstance().createHeaderDelegate(o.getClass());
    if (headerDelegate == null) {
        return o.toString();
    }
    return headerDelegate.toString(o);
}