use of org.apache.cxf.jaxrs.provider.ProviderFactory in project cxf by apache.
the class JAXRSUtilsTest method createMessage2.
private Message createMessage2() {
ProviderFactory factory = ServerProviderFactory.getInstance();
Message m = new MessageImpl();
m.put("org.apache.cxf.http.case_insensitive_queries", false);
Exchange e = new ExchangeImpl();
m.setExchange(e);
e.setInMessage(m);
Endpoint endpoint = EasyMock.createMock(Endpoint.class);
endpoint.getEndpointInfo();
EasyMock.expectLastCall().andReturn(null).anyTimes();
endpoint.get("org.apache.cxf.jaxrs.comparator");
EasyMock.expectLastCall().andReturn(null);
endpoint.size();
EasyMock.expectLastCall().andReturn(0).anyTimes();
endpoint.isEmpty();
EasyMock.expectLastCall().andReturn(true).anyTimes();
endpoint.get(ServerProviderFactory.class.getName());
EasyMock.expectLastCall().andReturn(factory).anyTimes();
EasyMock.replay(endpoint);
e.put(Endpoint.class, endpoint);
return m;
}
use of org.apache.cxf.jaxrs.provider.ProviderFactory in project tomee by apache.
the class JAXRSUtils method readFromMessageBody.
private static Object readFromMessageBody(Class<?> targetTypeClass, Type parameterType, Annotation[] parameterAnnotations, InputStream is, MediaType contentType, OperationResourceInfo ori, Message m) throws IOException, WebApplicationException {
List<MediaType> types = JAXRSUtils.intersectMimeTypes(ori.getConsumeTypes(), contentType);
final ProviderFactory pf = ServerProviderFactory.getInstance(m);
for (MediaType type : types) {
List<ReaderInterceptor> readers = pf.createMessageBodyReaderInterceptor(targetTypeClass, parameterType, parameterAnnotations, type, m, true, ori.getNameBindings());
if (readers != null) {
try {
return readFromMessageBodyReader(readers, targetTypeClass, parameterType, parameterAnnotations, is, type, m);
} catch (IOException e) {
if (e.getClass().getName().equals(NO_CONTENT_EXCEPTION)) {
throw ExceptionUtils.toBadRequestException(e, null);
}
throw e;
} catch (WebApplicationException ex) {
throw ex;
} catch (Exception ex) {
throw new Fault(ex);
}
}
}
logMessageHandlerProblem("NO_MSG_READER", targetTypeClass, contentType);
throw new WebApplicationException(Response.Status.UNSUPPORTED_MEDIA_TYPE);
}
use of org.apache.cxf.jaxrs.provider.ProviderFactory in project tomee by apache.
the class ResponseImpl method doReadEntity.
public <T> T doReadEntity(Class<T> cls, Type t, Annotation[] anns) throws ProcessingException, IllegalStateException {
checkEntityIsClosed();
// according to javadoc, should close when is not buffered.
boolean shouldClose = !entityBufferred && !JAXRSUtils.isStreamingOutType(cls);
if (lastEntity != null && cls.isAssignableFrom(lastEntity.getClass()) && !(lastEntity instanceof InputStream)) {
return cls.cast(lastEntity);
}
MediaType mediaType = getMediaType();
if (mediaType == null) {
mediaType = MediaType.WILDCARD_TYPE;
}
// the stream is available if entity is IS or
// message contains XMLStreamReader or Reader
boolean entityStreamAvailable = entityStreamAvailable();
InputStream entityStream = null;
if (!entityStreamAvailable) {
// try create a stream if the entity is String or Number
entityStream = convertEntityToStreamIfPossible();
entityStreamAvailable = entityStream != null;
} else if (entity instanceof InputStream) {
entityStream = InputStream.class.cast(entity);
} else {
Message inMessage = getResponseMessage();
Reader reader = inMessage.getContent(Reader.class);
if (reader != null) {
entityStream = InputStream.class.cast(new ReaderInputStream(reader));
}
}
// we need to check for readers even if no IS is set - the readers may still do it
List<ReaderInterceptor> readers = outMessage == null ? null : ProviderFactory.getInstance(outMessage).createMessageBodyReaderInterceptor(cls, t, anns, mediaType, outMessage, entityStreamAvailable, null);
if (readers != null) {
try {
if (entityBufferred) {
InputStream.class.cast(entity).reset();
}
Message responseMessage = getResponseMessage();
responseMessage.put(Message.PROTOCOL_HEADERS, getHeaders());
lastEntity = JAXRSUtils.readFromMessageBodyReader(readers, cls, t, anns, entityStream, mediaType, responseMessage);
// close the entity after readEntity is called.
T tCastLastEntity = castLastEntity();
shouldClose = shouldClose && !(tCastLastEntity instanceof AutoCloseable) && !(tCastLastEntity instanceof Source);
if (shouldClose) {
close();
}
return tCastLastEntity;
} catch (NoContentException ex) {
// check if basic type. Basic type throw exception, else return null.
if (isBasicType(cls)) {
autoClose(cls, true);
reportMessageHandlerProblem("MSG_READER_PROBLEM", cls, mediaType, ex);
} else {
if (shouldClose) {
close();
}
return null;
}
} catch (Exception ex) {
autoClose(cls, true);
reportMessageHandlerProblem("MSG_READER_PROBLEM", cls, mediaType, ex);
} finally {
ProviderFactory pf = ProviderFactory.getInstance(outMessage);
if (pf != null) {
pf.clearThreadLocalProxies();
}
}
} else if (entity != null && cls.isAssignableFrom(entity.getClass())) {
lastEntity = entity;
return castLastEntity();
} else if (entityStreamAvailable) {
reportMessageHandlerProblem("NO_MSG_READER", cls, mediaType, null);
}
throw new IllegalStateException("The entity is not backed by an input stream, entity class is : " + (entity != null ? entity.getClass().getName() : cls.getName()));
}
use of org.apache.cxf.jaxrs.provider.ProviderFactory in project cxf by apache.
the class ResponseImplTest method createMessage.
private Message createMessage() {
ProviderFactory factory = ServerProviderFactory.getInstance();
Message m = new MessageImpl();
m.put("org.apache.cxf.http.case_insensitive_queries", false);
Exchange e = new ExchangeImpl();
m.setExchange(e);
e.setInMessage(m);
e.setOutMessage(new MessageImpl());
Endpoint endpoint = EasyMock.createMock(Endpoint.class);
EasyMock.expect(endpoint.getEndpointInfo()).andReturn(null).anyTimes();
EasyMock.expect(endpoint.get(Application.class.getName())).andReturn(null);
EasyMock.expect(endpoint.size()).andReturn(0).anyTimes();
EasyMock.expect(endpoint.isEmpty()).andReturn(true).anyTimes();
EasyMock.expect(endpoint.get(ServerProviderFactory.class.getName())).andReturn(factory).anyTimes();
EasyMock.replay(endpoint);
e.put(Endpoint.class, endpoint);
return m;
}
use of org.apache.cxf.jaxrs.provider.ProviderFactory in project cxf by apache.
the class DOM4JProviderTest method createMessageWithJSONProvider.
private Message createMessageWithJSONProvider() {
ProviderFactory factory = ServerProviderFactory.getInstance();
JSONProvider<Object> provider = new JSONProvider<>();
provider.setDropRootElement(true);
provider.setIgnoreNamespaces(true);
factory.registerUserProvider(provider);
return createMessage(factory);
}
Aggregations