javax.ws.rs.ext.RuntimeDelegate.HeaderDelegate Java Examples

The following examples show how to use javax.ws.rs.ext.RuntimeDelegate.HeaderDelegate. 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: ResponseImplTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> HeaderDelegate<T> createHeaderDelegate(Class<T> arg0)
    throws IllegalArgumentException {
    if (arg0 == StringBean.class) {
        return (HeaderDelegate<T>) new StringBeanHeaderDelegate();
    }
    return original.createHeaderDelegate(arg0);
}
 
Example #4
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 #5
Source File: AcceptMediaTypeTest.java    From everrest with Eclipse Public License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
    headerDelegate = mock(HeaderDelegate.class);

    RuntimeDelegate runtimeDelegate = mock(RuntimeDelegate.class);
    when(runtimeDelegate.createHeaderDelegate(AcceptMediaType.class)).thenReturn(headerDelegate);

    RuntimeDelegate.setInstance(runtimeDelegate);
}
 
Example #6
Source File: HeaderHelperTest.java    From everrest with Eclipse Public License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void transformsObjectToStringWithHeaderDelegate() throws Exception {
    RuntimeDelegate runtimeDelegate = mock(RuntimeDelegate.class);
    RuntimeDelegate.setInstance(runtimeDelegate);

    HeaderDelegate<String> headerDelegate = mock(HeaderDelegate.class);
    when(runtimeDelegate.createHeaderDelegate(String.class)).thenReturn(headerDelegate);
    when(headerDelegate.toString("foo")).thenReturn("<foo>");

    assertEquals("<foo>", HeaderHelper.getHeaderAsString("foo"));
}
 
Example #7
Source File: RangesTest.java    From everrest with Eclipse Public License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
    headerDelegate = mock(HeaderDelegate.class);

    RuntimeDelegate runtimeDelegate = mock(RuntimeDelegate.class);
    when(runtimeDelegate.createHeaderDelegate(Ranges.class)).thenReturn(headerDelegate);

    RuntimeDelegate.setInstance(runtimeDelegate);
}
 
Example #8
Source File: ResponseImplTest.java    From everrest with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void getsHeadersAsStringToStringMapAndUsesRuntimeDelegateForConvertValuesToString() throws Exception {
    HeaderDelegate<HeaderValue> headerDelegate = mock(HeaderDelegate.class);
    when(headerDelegate.toString(isA(HeaderValue.class))).thenReturn("bar");
    RuntimeDelegate runtimeDelegate = mock(RuntimeDelegate.class);
    when(runtimeDelegate.createHeaderDelegate(HeaderValue.class)).thenReturn(headerDelegate);
    RuntimeDelegate.setInstance(runtimeDelegate);

    MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
    headers.put("foo", newArrayList(new HeaderValue()));
    ResponseImpl response = new ResponseImpl(200, "foo", null, headers);

    assertEquals(ImmutableMap.of("foo", newArrayList("bar")), response.getStringHeaders());
}
 
Example #9
Source File: ResponseImplTest.java    From everrest with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void getSingleHeaderAsStringAndUsesRuntimeDelegateForConvertValueToString() throws Exception {
    HeaderDelegate<HeaderValue> headerDelegate = mock(HeaderDelegate.class);
    when(headerDelegate.toString(isA(HeaderValue.class))).thenReturn("bar");
    RuntimeDelegate runtimeDelegate = mock(RuntimeDelegate.class);
    when(runtimeDelegate.createHeaderDelegate(HeaderValue.class)).thenReturn(headerDelegate);
    RuntimeDelegate.setInstance(runtimeDelegate);

    MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
    headers.put("foo", newArrayList(new HeaderValue()));
    ResponseImpl response = new ResponseImpl(200, "foo", null, headers);

    assertEquals("bar", response.getHeaderString("foo"));
}
 
Example #10
Source File: ResponseImplTest.java    From everrest with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void getMultipleHeaderAsStringAndUsesRuntimeDelegateForConvertValuesToString() throws Exception {
    HeaderDelegate<HeaderValue> headerDelegate = mock(HeaderDelegate.class);
    when(headerDelegate.toString(isA(HeaderValue.class))).thenReturn("bar1", "bar2");
    RuntimeDelegate runtimeDelegate = mock(RuntimeDelegate.class);
    when(runtimeDelegate.createHeaderDelegate(HeaderValue.class)).thenReturn(headerDelegate);
    RuntimeDelegate.setInstance(runtimeDelegate);

    MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
    headers.put("foo", newArrayList(new HeaderValue(), new HeaderValue()));
    ResponseImpl response = new ResponseImpl(200, "foo", null, headers);

    assertEquals("bar1,bar2", response.getHeaderString("foo"));
}
 
Example #11
Source File: HttpUtils.java    From cxf with Apache License 2.0 4 votes vote down vote up
public static HeaderDelegate<Object> getHeaderDelegate(Object o) {
    return getHeaderDelegate(RuntimeDelegate.getInstance(), o);
}
 
Example #12
Source File: HttpUtils.java    From cxf with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public static HeaderDelegate<Object> getHeaderDelegate(RuntimeDelegate rd, Object o) {
    return rd == null ? null : (HeaderDelegate<Object>)rd.createHeaderDelegate(o.getClass());
}
 
Example #13
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);
}