use of org.apache.cxf.message.MessageContentsList in project cxf by apache.
the class SoapHeaderInterceptor method handleMessage.
public void handleMessage(Message m) throws Fault {
SoapMessage message = (SoapMessage) m;
SoapVersion soapVersion = message.getVersion();
Exchange exchange = message.getExchange();
MessageContentsList parameters = MessageContentsList.getContentsList(message);
if (null == parameters) {
parameters = new MessageContentsList();
}
BindingOperationInfo bop = exchange.getBindingOperationInfo();
if (null == bop) {
return;
}
if (bop.isUnwrapped()) {
bop = bop.getWrappedOperation();
}
boolean client = isRequestor(message);
BindingMessageInfo bmi = client ? bop.getOutput() : bop.getInput();
if (bmi == null) {
// one way operation.
return;
}
List<SoapHeaderInfo> headers = bmi.getExtensors(SoapHeaderInfo.class);
if (headers == null || headers.isEmpty()) {
return;
}
boolean supportsNode = this.supportsDataReader(message, Node.class);
Service service = ServiceModelUtil.getService(message.getExchange());
for (SoapHeaderInfo header : headers) {
MessagePartInfo mpi = header.getPart();
try {
if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.IN, message)) {
Schema schema = EndpointReferenceUtils.getSchema(service.getServiceInfos().get(0), message.getExchange().getBus());
validateHeader(message, mpi, schema);
}
} catch (Fault f) {
if (!isRequestor(message)) {
f.setFaultCode(Fault.FAULT_CODE_CLIENT);
}
throw f;
}
if (mpi.getTypeClass() != null) {
Header param = findHeader(message, mpi);
Object object = null;
if (param != null) {
message.getHeaders().remove(param);
if (param.getDataBinding() == null) {
Node source = (Node) param.getObject();
if (source instanceof Element) {
// need to remove these attributes as they
// would cause validation failures
Element el = (Element) source;
el.removeAttributeNS(soapVersion.getNamespace(), soapVersion.getAttrNameMustUnderstand());
el.removeAttributeNS(soapVersion.getNamespace(), soapVersion.getAttrNameRole());
}
if (supportsNode) {
object = getNodeDataReader(message).read(mpi, source);
} else {
W3CDOMStreamReader reader = new W3CDOMStreamReader((Element) source);
try {
// advance into the first tag
reader.nextTag();
} catch (XMLStreamException e) {
// ignore
}
object = getDataReader(message, XMLStreamReader.class).read(mpi, reader);
}
} else {
object = param.getObject();
}
}
parameters.put(mpi, object);
}
}
if (!parameters.isEmpty()) {
message.setContent(List.class, parameters);
}
}
use of org.apache.cxf.message.MessageContentsList in project cxf by apache.
the class RPCOutInterceptorTest method setUp.
@Before
public void setUp() throws Exception {
super.setUp();
ServiceInfo si = getMockedServiceModel(this.getClass().getResource("/wsdl_soap/hello_world_rpc_lit.wsdl").toString());
BindingInfo bi = si.getBinding(new QName(TNS, "Greeter_SOAPBinding_RPCLit"));
BindingOperationInfo boi = bi.getOperation(new QName(TNS, OPNAME));
boi.getOperationInfo().getOutput().getMessagePartByIndex(0).setIndex(0);
soapMessage.getExchange().put(BindingOperationInfo.class, boi);
control.reset();
Service service = control.createMock(Service.class);
EasyMock.expect(service.isEmpty()).andReturn(true).anyTimes();
JAXBDataBinding dataBinding = new JAXBDataBinding(MyComplexStruct.class);
service.getDataBinding();
EasyMock.expectLastCall().andReturn(dataBinding).anyTimes();
service.getServiceInfos();
List<ServiceInfo> list = Arrays.asList(si);
EasyMock.expectLastCall().andReturn(list).anyTimes();
soapMessage.getExchange().put(Service.class, service);
soapMessage.getExchange().put(Message.SCHEMA_VALIDATION_ENABLED, Boolean.FALSE);
control.replay();
MyComplexStruct mcs = new MyComplexStruct();
mcs.setElem1("elem1");
mcs.setElem2("elem2");
mcs.setElem3(45);
MessageContentsList param = new MessageContentsList();
param.add(mcs);
soapMessage.setContent(List.class, param);
}
use of org.apache.cxf.message.MessageContentsList in project cxf by apache.
the class JAXRSInvoker method invoke.
@SuppressWarnings("unchecked")
public Object invoke(Exchange exchange, Object request, Object resourceObject) {
final OperationResourceInfo ori = exchange.get(OperationResourceInfo.class);
final ClassResourceInfo cri = ori.getClassResourceInfo();
final Message inMessage = exchange.getInMessage();
final ServerProviderFactory providerFactory = ServerProviderFactory.getInstance(inMessage);
if (cri.isRoot()) {
cri.injectContexts(resourceObject, ori, inMessage);
ProviderInfo<Application> appProvider = providerFactory.getApplicationProvider();
if (appProvider != null) {
InjectionUtils.injectContexts(appProvider.getProvider(), appProvider, inMessage);
}
}
Method methodToInvoke = getMethodToInvoke(cri, ori, resourceObject);
List<Object> params = null;
if (request instanceof List) {
params = CastUtils.cast((List<?>) request);
} else if (request != null) {
params = new MessageContentsList(request);
}
Object result = null;
ClassLoaderHolder contextLoader = null;
AsyncResponseImpl asyncResponse = null;
try {
if (setServiceLoaderAsContextLoader(inMessage)) {
contextLoader = ClassLoaderUtils.setThreadContextClassloader(resourceObject.getClass().getClassLoader());
}
if (!ori.isSubResourceLocator()) {
asyncResponse = (AsyncResponseImpl) inMessage.get(AsyncResponse.class);
}
result = invoke(exchange, resourceObject, methodToInvoke, params);
if (asyncResponse == null && !ori.isSubResourceLocator()) {
asyncResponse = checkFutureResponse(inMessage, checkResultObject(result));
}
if (asyncResponse != null) {
if (!asyncResponse.suspendContinuationIfNeeded()) {
result = handleAsyncResponse(exchange, asyncResponse);
} else {
providerFactory.clearThreadLocalProxies();
}
}
} catch (Fault ex) {
Object faultResponse;
if (asyncResponse != null) {
faultResponse = handleAsyncFault(exchange, asyncResponse, ex.getCause() == null ? ex : ex.getCause());
} else {
faultResponse = handleFault(ex, inMessage, cri, methodToInvoke);
}
return faultResponse;
} finally {
exchange.put(LAST_SERVICE_OBJECT, resourceObject);
if (contextLoader != null) {
contextLoader.reset();
}
}
ClassResourceInfo subCri = null;
if (ori.isSubResourceLocator()) {
try {
MultivaluedMap<String, String> values = getTemplateValues(inMessage);
String subResourcePath = values.getFirst(URITemplate.FINAL_MATCH_GROUP);
String httpMethod = (String) inMessage.get(Message.HTTP_REQUEST_METHOD);
String contentType = (String) inMessage.get(Message.CONTENT_TYPE);
if (contentType == null) {
contentType = "*/*";
}
List<MediaType> acceptContentType = (List<MediaType>) exchange.get(Message.ACCEPT_CONTENT_TYPE);
result = checkSubResultObject(result, subResourcePath);
Class<?> subResponseType = null;
if (result.getClass() == Class.class) {
ResourceContext rc = new ResourceContextImpl(inMessage, ori);
result = rc.getResource((Class<?>) result);
subResponseType = InjectionUtils.getActualType(methodToInvoke.getGenericReturnType());
} else {
subResponseType = methodToInvoke.getReturnType();
}
subCri = cri.getSubResource(subResponseType, ClassHelper.getRealClass(exchange.getBus(), result), result);
if (subCri == null) {
org.apache.cxf.common.i18n.Message errorM = new org.apache.cxf.common.i18n.Message("NO_SUBRESOURCE_FOUND", BUNDLE, subResourcePath);
LOG.severe(errorM.toString());
throw ExceptionUtils.toNotFoundException(null, null);
}
OperationResourceInfo subOri = JAXRSUtils.findTargetMethod(Collections.singletonMap(subCri, values), inMessage, httpMethod, values, contentType, acceptContentType);
exchange.put(OperationResourceInfo.class, subOri);
inMessage.put(URITemplate.TEMPLATE_PARAMETERS, values);
if (!subOri.isSubResourceLocator() && JAXRSUtils.runContainerRequestFilters(providerFactory, inMessage, false, subOri.getNameBindings())) {
return new MessageContentsList(exchange.get(Response.class));
}
// work out request parameters for the sub-resource class. Here we
// presume InputStream has not been consumed yet by the root resource class.
List<Object> newParams = JAXRSUtils.processParameters(subOri, values, inMessage);
inMessage.setContent(List.class, newParams);
return this.invoke(exchange, newParams, result);
} catch (IOException ex) {
Response resp = JAXRSUtils.convertFaultToResponse(ex, inMessage);
if (resp == null) {
resp = JAXRSUtils.convertFaultToResponse(ex, inMessage);
}
return new MessageContentsList(resp);
} catch (WebApplicationException ex) {
Response excResponse;
if (JAXRSUtils.noResourceMethodForOptions(ex.getResponse(), (String) inMessage.get(Message.HTTP_REQUEST_METHOD))) {
excResponse = JAXRSUtils.createResponse(Collections.singletonList(subCri), null, null, 200, true);
} else {
excResponse = JAXRSUtils.convertFaultToResponse(ex, inMessage);
}
return new MessageContentsList(excResponse);
}
}
setResponseContentTypeIfNeeded(inMessage, result);
return result;
}
use of org.apache.cxf.message.MessageContentsList in project cxf by apache.
the class JAXRSDefaultFaultOutInterceptor method handleMessage.
public void handleMessage(Message message) throws Fault {
if (PropertyUtils.isTrue(message.getExchange().get(JAXRSUtils.SECOND_JAXRS_EXCEPTION))) {
return;
}
final Fault f = (Fault) message.getContent(Exception.class);
Response r = JAXRSUtils.convertFaultToResponse(f.getCause(), message);
if (r != null) {
JAXRSUtils.setMessageContentType(message, r);
message.setContent(List.class, new MessageContentsList(r));
if (message.getExchange().getOutMessage() == null && message.getExchange().getOutFaultMessage() != null) {
message.getExchange().setOutMessage(message.getExchange().getOutFaultMessage());
}
new JAXRSOutInterceptor().handleMessage(message);
return;
}
ServerProviderFactory.releaseRequestState(message);
if (mustPropogateException(message)) {
throw f;
}
new StaxOutInterceptor().handleMessage(message);
message.put(org.apache.cxf.message.Message.RESPONSE_CODE, f.getStatusCode());
NSStack nsStack = new NSStack();
nsStack.push();
XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
try {
nsStack.add("http://cxf.apache.org/bindings/xformat");
String prefix = nsStack.getPrefix("http://cxf.apache.org/bindings/xformat");
StaxUtils.writeStartElement(writer, prefix, "XMLFault", "http://cxf.apache.org/bindings/xformat");
StaxUtils.writeStartElement(writer, prefix, "faultstring", "http://cxf.apache.org/bindings/xformat");
Throwable t = f.getCause();
writer.writeCharacters(t == null ? f.getMessage() : t.toString());
// fault string
writer.writeEndElement();
if (f.getDetail() != null) {
StaxUtils.writeStartElement(writer, prefix, "detail", "http://cxf.apache.org/bindings/xformat");
StaxUtils.writeNode(DOMUtils.getChild(f.getDetail(), Node.ELEMENT_NODE), writer, false);
writer.writeEndElement();
}
// fault root
writer.writeEndElement();
writer.flush();
} catch (XMLStreamException xe) {
throw new Fault(new org.apache.cxf.common.i18n.Message("XML_WRITE_EXC", BUNDLE), xe);
}
}
use of org.apache.cxf.message.MessageContentsList in project cxf by apache.
the class JAXRSInInterceptor method createOutMessage.
private Message createOutMessage(Message inMessage, Response r) {
Endpoint e = inMessage.getExchange().getEndpoint();
Message mout = e.getBinding().createMessage();
mout.setContent(List.class, new MessageContentsList(r));
mout.setExchange(inMessage.getExchange());
mout.setInterceptorChain(OutgoingChainInterceptor.getOutInterceptorChain(inMessage.getExchange()));
inMessage.getExchange().setOutMessage(mout);
if (r.getStatus() >= Response.Status.BAD_REQUEST.getStatusCode()) {
inMessage.getExchange().put("cxf.io.cacheinput", Boolean.FALSE);
}
return mout;
}
Aggregations