下面列出了怎么用javax.ws.rs.ext.RuntimeDelegate.HeaderDelegate的API类实例代码及写法,或者点击链接到github查看源代码。
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);
}
}
}
}
}
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;
}
@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);
}
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);
}
}
@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);
}
@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"));
}
@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);
}
@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());
}
@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"));
}
@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"));
}
public static HeaderDelegate<Object> getHeaderDelegate(Object o) {
return getHeaderDelegate(RuntimeDelegate.getInstance(), o);
}
@SuppressWarnings("unchecked")
public static HeaderDelegate<Object> getHeaderDelegate(RuntimeDelegate rd, Object o) {
return rd == null ? null : (HeaderDelegate<Object>)rd.createHeaderDelegate(o.getClass());
}
/**
* 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);
}