类javax.ws.rs.ext.RuntimeDelegate.HeaderDelegate源码实例Demo

下面列出了怎么用javax.ws.rs.ext.RuntimeDelegate.HeaderDelegate的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: cxf   文件: HttpUtils.java
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);
                }

            }

        }
    }

}
 
源代码2 项目: cxf   文件: ResponseImpl.java
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;
}
 
源代码3 项目: cxf   文件: ResponseImplTest.java
@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);
}
 
源代码4 项目: cxf   文件: InvocationBuilderImpl.java
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);
    }
}
 
源代码5 项目: everrest   文件: AcceptMediaTypeTest.java
@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);
}
 
源代码6 项目: everrest   文件: HeaderHelperTest.java
@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"));
}
 
源代码7 项目: everrest   文件: RangesTest.java
@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);
}
 
源代码8 项目: everrest   文件: ResponseImplTest.java
@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());
}
 
源代码9 项目: everrest   文件: ResponseImplTest.java
@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"));
}
 
源代码10 项目: everrest   文件: ResponseImplTest.java
@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"));
}
 
源代码11 项目: cxf   文件: HttpUtils.java
public static HeaderDelegate<Object> getHeaderDelegate(Object o) {
    return getHeaderDelegate(RuntimeDelegate.getInstance(), o);
}
 
源代码12 项目: cxf   文件: HttpUtils.java
@SuppressWarnings("unchecked")
public static HeaderDelegate<Object> getHeaderDelegate(RuntimeDelegate rd, Object o) {
    return rd == null ? null : (HeaderDelegate<Object>)rd.createHeaderDelegate(o.getClass());
}
 
源代码13 项目: everrest   文件: HeaderHelper.java
/**
 * 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);
}
 
 类所在包
 类方法
 同包方法