use of org.apache.cxf.jaxrs.ext.MessageContextImpl in project cxf by apache.
the class XSLTJaxbProviderTest method testWrite.
@Test
public void testWrite() throws Exception {
XSLTJaxbProvider<Book> provider = new XSLTJaxbProvider<Book>();
provider.setOutTemplate(TEMPLATE_LOCATION);
provider.setMessageContext(new MessageContextImpl(createMessage()));
Book b = new Book();
b.setId(123L);
b.setName("TheBook");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
provider.writeTo(b, Book.class, Book.class, b.getClass().getAnnotations(), MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos);
Unmarshaller um = provider.getClassContext(Book.class).createUnmarshaller();
Book b2 = (Book) um.unmarshal(new StringReader(bos.toString()));
b.setName("TheBook2");
assertEquals("Transformation is bad", b, b2);
}
use of org.apache.cxf.jaxrs.ext.MessageContextImpl in project cxf by apache.
the class XSLTJaxbProviderTest method testWriteToStreamWriter.
@Test
public void testWriteToStreamWriter() throws Exception {
XSLTJaxbProvider<Book> provider = new XSLTJaxbProvider<Book>() {
@Override
protected XMLStreamWriter getStreamWriter(Object obj, OutputStream os, MediaType mt) {
return StaxUtils.createXMLStreamWriter(os);
}
};
provider.setOutTemplate(TEMPLATE_LOCATION);
provider.setMessageContext(new MessageContextImpl(createMessage()));
Book b = new Book();
b.setId(123L);
b.setName("TheBook");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
provider.writeTo(b, Book.class, Book.class, b.getClass().getAnnotations(), MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos);
Unmarshaller um = provider.getClassContext(Book.class).createUnmarshaller();
Book b2 = (Book) um.unmarshal(new StringReader(bos.toString()));
b.setName("TheBook2");
assertEquals("Transformation is bad", b, b2);
}
use of org.apache.cxf.jaxrs.ext.MessageContextImpl in project cxf by apache.
the class JAXRSUtils method processFormParam.
private static Object processFormParam(Message m, String key, Class<?> pClass, Type genericType, Annotation[] paramAnns, String defaultValue, boolean decode) {
MessageContext mc = new MessageContextImpl(m);
MediaType mt = mc.getHttpHeaders().getMediaType();
@SuppressWarnings("unchecked") MultivaluedMap<String, String> params = (MultivaluedMap<String, String>) m.get(FormUtils.FORM_PARAM_MAP);
if (params == null) {
params = new MetadataMap<String, String>();
m.put(FormUtils.FORM_PARAM_MAP, params);
if (mt == null || mt.isCompatible(MediaType.APPLICATION_FORM_URLENCODED_TYPE)) {
String enc = HttpUtils.getEncoding(mt, StandardCharsets.UTF_8.name());
String body = FormUtils.readBody(m.getContent(InputStream.class), enc);
FormUtils.populateMapFromStringOrHttpRequest(params, m, body, enc, decode);
} else {
if ("multipart".equalsIgnoreCase(mt.getType()) && MediaType.MULTIPART_FORM_DATA_TYPE.isCompatible(mt)) {
MultipartBody body = AttachmentUtils.getMultipartBody(mc);
FormUtils.populateMapFromMultipart(params, body, m, decode);
} else {
org.apache.cxf.common.i18n.Message errorMsg = new org.apache.cxf.common.i18n.Message("WRONG_FORM_MEDIA_TYPE", BUNDLE, mt.toString());
LOG.warning(errorMsg.toString());
throw ExceptionUtils.toNotSupportedException(null, null);
}
}
}
if ("".equals(key)) {
return InjectionUtils.handleBean(pClass, paramAnns, params, ParameterType.FORM, m, false);
}
List<String> results = params.get(key);
return InjectionUtils.createParameterObject(results, pClass, genericType, paramAnns, defaultValue, false, ParameterType.FORM, m);
}
use of org.apache.cxf.jaxrs.ext.MessageContextImpl in project cxf by apache.
the class OAuthRequestFilter method filter.
@Override
public void filter(ContainerRequestContext context) {
try {
Message m = JAXRSUtils.getCurrentMessage();
MessageContext mc = new MessageContextImpl(m);
OAuthInfo info = handleOAuthRequest(mc.getHttpServletRequest());
setSecurityContext(mc, m, info);
} catch (OAuthProblemException e) {
context.abortWith(Response.status(401).header("WWW-Authenticate", "OAuth").build());
} catch (Exception e) {
context.abortWith(Response.status(401).header("WWW-Authenticate", "OAuth").build());
}
}
use of org.apache.cxf.jaxrs.ext.MessageContextImpl in project cxf by apache.
the class OidcRpAuthenticationFilter method checkSecurityContext.
protected boolean checkSecurityContext(ContainerRequestContext rc) {
OidcClientTokenContext tokenContext = (OidcClientTokenContext) stateManager.getClientTokenContext(mc);
if (tokenContext == null) {
return false;
}
IdToken idToken = tokenContext.getIdToken();
try {
// If ID token has expired then the context is no longer valid
JwtUtils.validateJwtExpiry(idToken, 0, idToken.getExpiryTime() != null);
} catch (JwtException ex) {
stateManager.removeClientTokenContext(new MessageContextImpl(JAXRSUtils.getCurrentMessage()));
return false;
}
OidcClientTokenContextImpl newTokenContext = new OidcClientTokenContextImpl();
newTokenContext.setToken(tokenContext.getToken());
newTokenContext.setIdToken(idToken);
newTokenContext.setUserInfo(tokenContext.getUserInfo());
newTokenContext.setState(toRequestState(rc));
JAXRSUtils.getCurrentMessage().setContent(ClientTokenContext.class, newTokenContext);
OidcSecurityContext oidcSecCtx = new OidcSecurityContext(newTokenContext);
oidcSecCtx.setRoleClaim(roleClaim);
rc.setSecurityContext(oidcSecCtx);
return true;
}
Aggregations