use of org.apache.cxf.service.Service in project cxf by apache.
the class AbstractJAXRSFactoryBean method createEndpoint.
protected Endpoint createEndpoint() throws BusException, EndpointException {
Service service = serviceFactory.getService();
if (service == null) {
service = serviceFactory.create();
}
EndpointInfo ei = createEndpointInfo(service);
Endpoint ep = new EndpointImpl(getBus(), service, ei);
if (properties != null) {
ep.putAll(properties);
}
if (getInInterceptors() != null) {
ep.getInInterceptors().addAll(getInInterceptors());
}
if (getOutInterceptors() != null) {
ep.getOutInterceptors().addAll(getOutInterceptors());
}
if (getInFaultInterceptors() != null) {
ep.getInFaultInterceptors().addAll(getInFaultInterceptors());
}
if (getOutFaultInterceptors() != null) {
ep.getOutFaultInterceptors().addAll(getOutFaultInterceptors());
}
List<ClassResourceInfo> list = serviceFactory.getRealClassResourceInfo();
for (ClassResourceInfo cri : list) {
initializeAnnotationInterceptors(ep, cri.getServiceClass());
serviceFactory.sendEvent(FactoryBeanListener.Event.ENDPOINT_SELECTED, ei, ep, cri.getServiceClass(), null);
}
ep.put(JAXRSServiceFactoryBean.class.getName(), serviceFactory);
return ep;
}
use of org.apache.cxf.service.Service in project cxf by apache.
the class TestUtils method setupServiceInfo.
public EndpointInfo setupServiceInfo(String ns, String wsdl, String serviceName, String portName) throws Exception {
URL wsdlUrl = getClass().getResource(wsdl);
WSDLServiceFactory f = new WSDLServiceFactory(bus, wsdlUrl.toString(), new QName(ns, serviceName));
Service service = f.create();
return service.getEndpointInfo(new QName(ns, portName));
}
use of org.apache.cxf.service.Service 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());
Schema schema = null;
final boolean schemaValidationEnabled = ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.IN, message);
if (schemaValidationEnabled) {
schema = EndpointReferenceUtils.getSchema(service.getServiceInfos().get(0), message.getExchange().getBus());
}
for (SoapHeaderInfo header : headers) {
MessagePartInfo mpi = header.getPart();
try {
if (schemaValidationEnabled && schema != null) {
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.service.Service in project cxf by apache.
the class WebFaultOutInterceptor method handleMessage.
public void handleMessage(Message message) throws Fault {
Fault f = (Fault) message.getContent(Exception.class);
if (f == null) {
return;
}
try {
Throwable thr = f.getCause();
SOAPFaultException sf = null;
if (thr instanceof SOAPFaultException) {
sf = (SOAPFaultException) thr;
} else if (thr.getCause() instanceof SOAPFaultException) {
sf = (SOAPFaultException) thr.getCause();
}
if (sf != null) {
SoapVersion soapVersion = (SoapVersion) message.get(SoapVersion.class.getName());
if (soapVersion != null && soapVersion.getVersion() != 1.1) {
if (f instanceof SoapFault) {
for (Iterator<QName> it = CastUtils.cast(sf.getFault().getFaultSubcodes()); it.hasNext(); ) {
((SoapFault) f).addSubCode(it.next());
}
}
if (sf.getFault().getFaultReasonLocales().hasNext()) {
Locale lang = (Locale) sf.getFault().getFaultReasonLocales().next();
String convertedLang = lang.getLanguage();
String country = lang.getCountry();
if (country.length() > 0) {
convertedLang = convertedLang + '-' + country;
}
f.setLang(convertedLang);
}
}
message.setContent(Exception.class, f);
}
} catch (Exception e) {
// do nothing;
}
Throwable cause = f.getCause();
WebFault fault = null;
if (cause != null) {
fault = getWebFaultAnnotation(cause.getClass());
if (fault == null && cause.getCause() != null) {
fault = getWebFaultAnnotation(cause.getCause().getClass());
if (fault != null || cause instanceof RuntimeException) {
cause = cause.getCause();
}
}
}
if (cause instanceof Exception && fault != null) {
Exception ex = (Exception) cause;
Object faultInfo;
try {
Method method = cause.getClass().getMethod("getFaultInfo", new Class[0]);
faultInfo = method.invoke(cause, new Object[0]);
} catch (NoSuchMethodException e) {
faultInfo = createFaultInfoBean(fault, cause);
} catch (InvocationTargetException e) {
throw new Fault(new org.apache.cxf.common.i18n.Message("INVOCATION_TARGET_EXC", BUNDLE), e);
} catch (IllegalAccessException | IllegalArgumentException e) {
throw new Fault(new org.apache.cxf.common.i18n.Message("COULD_NOT_INVOKE", BUNDLE), e);
}
Service service = message.getExchange().getService();
try {
DataWriter<XMLStreamWriter> writer = service.getDataBinding().createWriter(XMLStreamWriter.class);
if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.OUT, message)) {
Schema schema = EndpointReferenceUtils.getSchema(service.getServiceInfos().get(0), message.getExchange().getBus());
writer.setSchema(schema);
}
OperationInfo op = null;
// Prevent a NPE if we can't match the operation
if (message.getExchange().getBindingOperationInfo() != null) {
op = message.getExchange().getBindingOperationInfo().getOperationInfo();
}
QName faultName = getFaultName(fault, cause.getClass(), op);
MessagePartInfo part = op != null ? getFaultMessagePart(faultName, op) : null;
if (f.hasDetails()) {
writer.write(faultInfo, part, new W3CDOMStreamWriter(f.getDetail()));
} else {
writer.write(faultInfo, part, new W3CDOMStreamWriter(f.getOrCreateDetail()));
if (!f.getDetail().hasChildNodes()) {
f.setDetail(null);
}
}
f.setMessage(ex.getMessage());
} catch (Fault f2) {
message.setContent(Exception.class, f2);
super.handleMessage(message);
} catch (Exception nex) {
// if exception occurs while writing a fault, we'll just let things continue
// and let the rest of the chain try handling it as is.
LOG.log(Level.WARNING, "EXCEPTION_WHILE_WRITING_FAULT", nex);
}
} else if (cause instanceof SOAPFaultException && ((SOAPFaultException) cause).getFault().hasDetail()) {
return;
} else if (f instanceof SoapFault && f.getCause() instanceof SOAPFaultException && ((SOAPFaultException) f.getCause()).getFault().hasDetail()) {
return;
} else {
FaultMode mode = message.get(FaultMode.class);
if (mode == FaultMode.CHECKED_APPLICATION_FAULT) {
// only convert checked exceptions with this
// otherwise delegate down to the normal protocol specific stuff
super.handleMessage(message);
}
}
}
use of org.apache.cxf.service.Service in project cxf by apache.
the class WrapperClassInInterceptor method handleMessage.
public void handleMessage(Message message) throws Fault {
Exchange ex = message.getExchange();
BindingOperationInfo boi = ex.getBindingOperationInfo();
if (Boolean.TRUE.equals(message.get(Message.PARTIAL_RESPONSE_MESSAGE)) || boi == null) {
return;
}
Method method = ex.get(Method.class);
if (method != null && method.getName().endsWith("Async")) {
Class<?> retType = method.getReturnType();
if ("java.util.concurrent.Future".equals(retType.getName()) || "javax.xml.ws.Response".equals(retType.getName())) {
return;
}
}
if (boi.isUnwrappedCapable()) {
BindingOperationInfo boi2 = boi.getUnwrappedOperation();
OperationInfo op = boi2.getOperationInfo();
BindingMessageInfo bmi;
MessageInfo wrappedMessageInfo = message.get(MessageInfo.class);
MessageInfo messageInfo;
if (wrappedMessageInfo == boi.getOperationInfo().getInput()) {
messageInfo = op.getInput();
bmi = boi2.getInput();
} else {
messageInfo = op.getOutput();
bmi = boi2.getOutput();
}
// Sometimes, an operation can be unwrapped according to WSDLServiceFactory,
// but not according to JAX-WS. We should unify these at some point, but
// for now check for the wrapper class.
MessageContentsList lst = MessageContentsList.getContentsList(message);
if (lst == null) {
return;
}
message.put(MessageInfo.class, messageInfo);
message.put(BindingMessageInfo.class, bmi);
ex.put(BindingOperationInfo.class, boi2);
if (isGET(message)) {
LOG.fine("WrapperClassInInterceptor skipped in HTTP GET method");
return;
}
MessagePartInfo wrapperPart = wrappedMessageInfo.getFirstMessagePart();
Class<?> wrapperClass = wrapperPart.getTypeClass();
Object wrappedObject = lst.get(wrapperPart.getIndex());
if (wrapperClass == null || wrappedObject == null || !wrapperClass.isInstance(wrappedObject)) {
return;
}
WrapperHelper helper = wrapperPart.getProperty("WRAPPER_CLASS", WrapperHelper.class);
if (helper == null) {
Service service = ServiceModelUtil.getService(message.getExchange());
DataBinding dataBinding = service.getDataBinding();
if (dataBinding instanceof WrapperCapableDatabinding) {
helper = createWrapperHelper((WrapperCapableDatabinding) dataBinding, messageInfo, wrappedMessageInfo, wrapperClass);
wrapperPart.setProperty("WRAPPER_CLASS", helper);
} else {
return;
}
}
MessageContentsList newParams;
try {
newParams = new MessageContentsList(helper.getWrapperParts(wrappedObject));
List<Integer> removes = null;
int count = 0;
for (MessagePartInfo part : messageInfo.getMessageParts()) {
if (Boolean.TRUE.equals(part.getProperty(ReflectionServiceFactoryBean.HEADER))) {
MessagePartInfo mpi = null;
for (MessagePartInfo mpi2 : wrappedMessageInfo.getMessageParts()) {
if (mpi2.getConcreteName().equals(part.getConcreteName())) {
mpi = mpi2;
}
}
if (mpi != null && lst.hasValue(mpi)) {
count++;
newParams.put(part, lst.get(mpi));
} else if (mpi == null || mpi.getTypeClass() == null) {
// header, but not mapped to a param on the method
if (removes == null) {
removes = new ArrayList<>();
}
removes.add(part.getIndex());
}
} else {
++count;
}
}
if (count == 0) {
newParams.clear();
} else if (removes != null) {
Collections.sort(removes, Collections.reverseOrder());
for (Integer i : removes) {
if (i < newParams.size()) {
newParams.remove(i.intValue());
}
}
}
} catch (Exception e) {
throw new Fault(e);
}
message.setContent(List.class, newParams);
}
}
Aggregations