use of org.apache.cxf.jaxrs.impl.ProvidersImpl in project cxf by apache.
the class JAXRSUtils method createContextValue.
public static <T> T createContextValue(Message m, Type genericType, Class<T> clazz) {
Message contextMessage = getContextMessage(m);
Object o = null;
if (UriInfo.class.isAssignableFrom(clazz)) {
o = createUriInfo(contextMessage);
} else if (HttpHeaders.class.isAssignableFrom(clazz) || ProtocolHeaders.class.isAssignableFrom(clazz)) {
o = createHttpHeaders(contextMessage, clazz);
} else if (SecurityContext.class.isAssignableFrom(clazz)) {
SecurityContext customContext = contextMessage.get(SecurityContext.class);
o = customContext == null ? new SecurityContextImpl(contextMessage) : customContext;
} else if (MessageContext.class.isAssignableFrom(clazz)) {
o = new MessageContextImpl(m);
} else if (ResourceInfo.class.isAssignableFrom(clazz)) {
o = new ResourceInfoImpl(contextMessage);
} else if (ResourceContext.class.isAssignableFrom(clazz)) {
o = new ResourceContextImpl(contextMessage, contextMessage.getExchange().get(OperationResourceInfo.class));
} else if (Request.class.isAssignableFrom(clazz)) {
o = new RequestImpl(contextMessage);
} else if (Providers.class.isAssignableFrom(clazz)) {
o = new ProvidersImpl(contextMessage);
} else if (ContextResolver.class.isAssignableFrom(clazz)) {
o = createContextResolver(genericType, contextMessage);
} else if (Configuration.class.isAssignableFrom(clazz)) {
o = ProviderFactory.getInstance(contextMessage).getConfiguration(contextMessage);
} else if (Application.class.isAssignableFrom(clazz)) {
ProviderInfo<?> providerInfo = (ProviderInfo<?>) contextMessage.getExchange().getEndpoint().get(Application.class.getName());
o = providerInfo == null ? null : providerInfo.getProvider();
} else if (contextMessage != null) {
ContextProvider<?> provider = ProviderFactory.getInstance(contextMessage).createContextProvider(clazz, contextMessage);
if (provider != null) {
o = provider.createContext(contextMessage);
}
}
if (o == null && contextMessage != null && !MessageUtils.isRequestor(contextMessage)) {
o = HttpUtils.createServletResourceValue(contextMessage, clazz);
}
return clazz.cast(o);
}
use of org.apache.cxf.jaxrs.impl.ProvidersImpl in project cxf by apache.
the class DOM4JProviderTest method testWriteJSONDropRoot.
@Test
public void testWriteJSONDropRoot() throws Exception {
org.dom4j.Document dom = readXML(MediaType.APPLICATION_XML_TYPE, "<root><a/></root>");
DOM4JProvider p = new DOM4JProvider();
p.setProviders(new ProvidersImpl(createMessageWithJSONProvider()));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
p.writeTo(dom, org.dom4j.Document.class, org.dom4j.Document.class, new Annotation[] {}, MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, Object>(), bos);
String str = bos.toString();
assertEquals("{\"a\":\"\"}", str);
}
use of org.apache.cxf.jaxrs.impl.ProvidersImpl in project cxf by apache.
the class DOM4JProviderTest method readXMLBom.
private org.dom4j.Document readXMLBom() throws Exception {
byte[] bom = new byte[] { (byte) 239, (byte) 187, (byte) 191 };
assertEquals("efbbbf", StringUtils.toHexString(bom));
byte[] strBytes = "<a/>".getBytes(StandardCharsets.UTF_8);
InputStream is = new SequenceInputStream(new ByteArrayInputStream(bom), new ByteArrayInputStream(strBytes));
DOM4JProvider p = new DOM4JProvider();
p.setProviders(new ProvidersImpl(createMessage(false)));
return p.readFrom(org.dom4j.Document.class, org.dom4j.Document.class, new Annotation[] {}, MediaType.valueOf("text/xml;a=b"), new MetadataMap<String, String>(), is);
}
use of org.apache.cxf.jaxrs.impl.ProvidersImpl in project cxf by apache.
the class DOM4JProviderTest method testReadJSONConvertToXML.
@Test
public void testReadJSONConvertToXML() throws Exception {
final String xml = "{\"a\":{\"b\":2}}";
DOM4JProvider p = new DOM4JProvider();
p.setProviders(new ProvidersImpl(createMessage(false)));
org.dom4j.Document dom = p.readFrom(org.dom4j.Document.class, org.dom4j.Document.class, new Annotation[] {}, MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, String>(), new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
String str = dom.asXML();
// starts with the xml PI
assertTrue(str.contains("<a><b>2</b></a>"));
}
use of org.apache.cxf.jaxrs.impl.ProvidersImpl in project cxf by apache.
the class DOM4JProviderTest method testWriteXMLSuppressDeclaration.
@Test
public void testWriteXMLSuppressDeclaration() throws Exception {
org.dom4j.Document dom = readXML(MediaType.APPLICATION_XML_TYPE, "<a/>");
final Message message = createMessage(true);
Providers providers = new ProvidersImpl(message);
DOM4JProvider p = new DOM4JProvider() {
protected Message getCurrentMessage() {
return message;
}
};
p.setProviders(providers);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
p.writeTo(dom, org.dom4j.Document.class, org.dom4j.Document.class, new Annotation[] {}, MediaType.APPLICATION_XML_TYPE, new MetadataMap<String, Object>(), bos);
String str = bos.toString();
assertFalse(str.startsWith("<?xml"));
assertTrue(str.contains("<a/>") || str.contains("<a></a>"));
}
Aggregations