use of jakarta.ws.rs.ext.Providers in project resteasy by resteasy.
the class ConfigurationInheritanceTest method testContextResolverInheritance.
@Test
public void testContextResolverInheritance() {
Client client = ClientBuilder.newClient();
try {
fakeHttpServer.start();
WebTarget parentWebTarget = client.target("http://" + fakeHttpServer.getHostAndPort());
WebTarget childWebTarget = parentWebTarget.path("path");
List<String> result = new ArrayList<>();
childWebTarget.register(new ClientRequestFilter() {
@Context
Providers providers;
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
ContextResolver<String> contextResolver = providers.getContextResolver(String.class, MediaType.WILDCARD_TYPE);
if (contextResolver != null) {
String context = contextResolver.getContext(getClass());
result.add(context);
}
}
});
// Registration on parent MUST not affect the child
ContextResolver<String> parentContextResolver = new ContextResolver<String>() {
@Override
public String getContext(Class<?> type) {
return "ParentContext";
}
};
parentWebTarget.register(parentContextResolver);
childWebTarget.request().get().close();
Assert.assertTrue(result.isEmpty());
// Child MUST only use the snapshot configuration of the parent
// taken at child creation time.
ContextResolver<String> childContextResolver = new ContextResolver<String>() {
@Override
public String getContext(Class<?> type) {
return null;
}
};
childWebTarget.register(childContextResolver);
childWebTarget.request().get().close();
Assert.assertEquals(1, result.size());
Assert.assertEquals(null, result.get(0));
} finally {
client.close();
}
}
use of jakarta.ws.rs.ext.Providers in project resteasy by resteasy.
the class MultipartSignedReader method readFrom.
public SignedInput readFrom(Class<SignedInput> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> headers, InputStream entityStream) throws IOException, WebApplicationException {
Class<?> baseType = null;
Type baseGenericType = null;
if (genericType != null && genericType instanceof ParameterizedType) {
ParameterizedType param = (ParameterizedType) genericType;
baseGenericType = param.getActualTypeArguments()[0];
baseType = Types.getRawType(baseGenericType);
}
try {
ByteArrayDataSource ds = new ByteArrayDataSource(entityStream, mediaType.toString());
MimeMultipart mm = new MimeMultipart(ds);
MultipartSignedInputImpl input = new MultipartSignedInputImpl();
input.setType(baseType);
input.setGenericType(baseGenericType);
input.setAnnotations(annotations);
input.setBody(mm);
Providers providers = ResteasyContext.getContextData(Providers.class);
input.setProviders(providers);
return input;
} catch (MessagingException e) {
throw new ReaderException(e);
}
}
use of jakarta.ws.rs.ext.Providers in project resteasy by resteasy.
the class JsonSerialization method toByteArray.
@SuppressWarnings({ "rawtypes", "unchecked" })
public static byte[] toByteArray(Object token, ResteasyProviderFactory factory) throws IOException {
MessageBodyWriter writer = factory.getMessageBodyWriter(token.getClass(), null, null, MediaType.APPLICATION_JSON_TYPE);
if (writer == null)
throw new NullPointerException(Messages.MESSAGES.couldNotFindMessageBodyWriterForJSON());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Providers old = ResteasyContext.getContextData(Providers.class);
ResteasyContext.pushContext(Providers.class, factory);
try {
writer.writeTo(token, token.getClass(), null, null, MediaType.APPLICATION_JSON_TYPE, new MultivaluedHashMap<String, Object>(), baos);
return baos.toByteArray();
} finally {
ResteasyContext.popContextData(Providers.class);
if (old != null)
ResteasyContext.pushContext(Providers.class, old);
}
}
use of jakarta.ws.rs.ext.Providers in project resteasy by resteasy.
the class ClientResponse method readFrom.
// this is synchronized in conjunction with finalize to protect against premature finalize called by the GC
@Override
protected synchronized <T> Object readFrom(Class<T> type, Type genericType, MediaType media, Annotation[] annotations) {
Type useGeneric = genericType == null ? type : genericType;
Class<?> useType = type;
media = media == null ? MediaType.WILDCARD_TYPE : media;
annotations = annotations == null ? this.annotations : annotations;
boolean isMarshalledEntity = false;
if (type.equals(MarshalledEntity.class)) {
isMarshalledEntity = true;
ParameterizedType param = (ParameterizedType) useGeneric;
useGeneric = param.getActualTypeArguments()[0];
useType = Types.getRawType(useGeneric);
}
Providers current = ResteasyContext.getContextData(Providers.class);
ResteasyContext.pushContext(Providers.class, configuration);
Object obj = null;
try {
InputStream is = getEntityStream();
if (is == null) {
throw new IllegalStateException(Messages.MESSAGES.inputStreamWasEmpty());
}
if (isMarshalledEntity) {
is = new InputStreamToByteArray(is);
}
ReaderInterceptor[] readerInterceptors = configuration.getReaderInterceptors(null, null);
final Object finalObj;
final long timestamp = tracingLogger.timestamp("RI_SUMMARY");
AbstractReaderInterceptorContext context = new ClientReaderInterceptorContext(readerInterceptors, configuration.getProviderFactory(), useType, useGeneric, annotations, media, getStringHeaders(), is, properties, tracingLogger);
try {
finalObj = context.proceed();
obj = finalObj;
} finally {
tracingLogger.logDuration("RI_SUMMARY", timestamp, context.getProcessedInterceptorCount());
}
if (isMarshalledEntity) {
InputStreamToByteArray isba = (InputStreamToByteArray) is;
final byte[] bytes = isba.toByteArray();
return new MarshalledEntity<Object>() {
@Override
public byte[] getMarshalledBytes() {
return bytes;
}
@Override
public Object getEntity() {
return finalObj;
}
};
} else {
return finalObj;
}
} catch (ProcessingException pe) {
throw pe;
} catch (Exception ex) {
throw new ProcessingException(ex);
} finally {
if (!Publisher.class.isAssignableFrom(type) && !EventInput.class.isAssignableFrom(type)) {
ResteasyContext.popContextData(Providers.class);
if (current != null)
ResteasyContext.pushContext(Providers.class, current);
if (obj instanceof ProvidersContextRetainer)
((ProvidersContextRetainer) obj).setProviders(configuration);
}
}
}
use of jakarta.ws.rs.ext.Providers in project resteasy by resteasy.
the class PriorityTest method testContextResolverPriorityOverride_2.
@Test
public void testContextResolverPriorityOverride_2() {
Client client = ClientBuilder.newClient();
try {
fakeHttpServer.start();
WebTarget webTarget = client.target("http://" + fakeHttpServer.getHostAndPort());
webTarget.register(new ClientRequestFilter() {
@Context
Providers providers;
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
providers.getContextResolver(String.class, MediaType.WILDCARD_TYPE).getContext(getClass());
}
});
StringBuilder result = new StringBuilder();
webTarget.register(new ContextResolver<String>() {
@Override
public String getContext(Class<?> type) {
result.append("K");
return null;
}
}, 1);
webTarget.register(new ContextResolver<String>() {
@Override
public String getContext(Class<?> type) {
result.append("O");
return null;
}
}, 0);
webTarget.request().get().close();
Assert.assertEquals("OK", result.toString());
} finally {
client.close();
}
}
Aggregations