use of org.apache.cxf.service.model.MessagePartInfo 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 (retType.getName().equals("java.util.concurrent.Future") || retType.getName().equals("javax.xml.ws.Response")) {
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 != 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);
}
}
use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.
the class WrapperClassOutInterceptor method createWrapperHelper.
private WrapperHelper createWrapperHelper(WrapperCapableDatabinding dataBinding, MessageInfo messageInfo, MessageInfo wrappedMessageInfo, Class<?> wrapperClass) {
List<String> partNames = new ArrayList<>();
List<String> elTypeNames = new ArrayList<>();
List<Class<?>> partClasses = new ArrayList<Class<?>>();
QName wrapperName = null;
for (MessagePartInfo p : wrappedMessageInfo.getMessageParts()) {
if (p.getTypeClass() == wrapperClass) {
wrapperName = p.getElementQName();
}
}
for (MessagePartInfo p : messageInfo.getMessageParts()) {
if (p.getTypeClass() == null) {
// WSDL part wasn't mapped to a parameter
continue;
}
ensureSize(partNames, p.getIndex());
ensureSize(elTypeNames, p.getIndex());
ensureSize(partClasses, p.getIndex());
partNames.set(p.getIndex(), p.getName().getLocalPart());
String elementType = null;
if (p.getTypeQName() == null) {
// handling anonymous complex type
elementType = null;
} else {
elementType = p.getTypeQName().getLocalPart();
}
elTypeNames.set(p.getIndex(), elementType);
partClasses.set(p.getIndex(), p.getTypeClass());
}
return dataBinding.createWrapperHelper(wrapperClass, wrapperName, partNames, elTypeNames, partClasses);
}
use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.
the class WrapperClassOutInterceptor method handleMessage.
public void handleMessage(Message message) throws Fault {
Exchange ex = message.getExchange();
BindingOperationInfo bop = ex.getBindingOperationInfo();
MessageInfo messageInfo = message.get(MessageInfo.class);
if (messageInfo == null || bop == null || !bop.isUnwrapped()) {
return;
}
BindingOperationInfo newbop = bop.getWrappedOperation();
MessageInfo wrappedMsgInfo;
if (Boolean.TRUE.equals(message.get(Message.REQUESTOR_ROLE))) {
wrappedMsgInfo = newbop.getInput().getMessageInfo();
} else {
wrappedMsgInfo = newbop.getOutput().getMessageInfo();
}
Class<?> wrapped = null;
if (wrappedMsgInfo.getMessagePartsNumber() > 0) {
wrapped = wrappedMsgInfo.getFirstMessagePart().getTypeClass();
}
if (wrapped != null) {
MessagePartInfo firstMessagePart = wrappedMsgInfo.getFirstMessagePart();
MessageContentsList objs = MessageContentsList.getContentsList(message);
WrapperHelper helper = firstMessagePart.getProperty("WRAPPER_CLASS", WrapperHelper.class);
if (helper == null) {
helper = getWrapperHelper(message, messageInfo, wrappedMsgInfo, wrapped, firstMessagePart);
}
if (helper == null) {
return;
}
try {
MessageContentsList newObjs = new MessageContentsList();
if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.OUT, message) && helper instanceof AbstractWrapperHelper) {
((AbstractWrapperHelper) helper).setValidate(true);
}
Object o2 = helper.createWrapperObject(objs);
newObjs.put(firstMessagePart, o2);
for (MessagePartInfo p : messageInfo.getMessageParts()) {
if (Boolean.TRUE.equals(p.getProperty(ReflectionServiceFactoryBean.HEADER))) {
MessagePartInfo mpi = wrappedMsgInfo.getMessagePart(p.getName());
if (objs.hasValue(p)) {
newObjs.put(mpi, objs.get(p));
}
}
}
message.setContent(List.class, newObjs);
} catch (Fault f) {
throw f;
} catch (Exception e) {
throw new Fault(e);
}
// we've now wrapped the object, so use the wrapped binding op
ex.put(BindingOperationInfo.class, newbop);
if (messageInfo == bop.getOperationInfo().getInput()) {
message.put(MessageInfo.class, newbop.getOperationInfo().getInput());
message.put(BindingMessageInfo.class, newbop.getInput());
} else if (messageInfo == bop.getOperationInfo().getOutput()) {
message.put(MessageInfo.class, newbop.getOperationInfo().getOutput());
message.put(BindingMessageInfo.class, newbop.getOutput());
}
}
}
use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.
the class JaxWsServiceFactoryBean method initializeWSDLOperationsForProvider.
protected void initializeWSDLOperationsForProvider() {
Class<?> c = getProviderParameterType(getServiceClass());
if (c == null) {
throw new ServiceConstructionException(new Message("INVALID_PROVIDER_EXC", LOG));
}
if (getEndpointInfo() == null && isFromWsdl()) {
// most likely, they specified a WSDL, but for some reason
// did not give a valid ServiceName/PortName. For provider,
// we'll allow this since everything is bound directly to
// the invoke method, however, this CAN cause other problems
// such as addresses in the wsdl not getting updated and such
// so we'll WARN about it.....
List<QName> enames = new ArrayList<>();
for (ServiceInfo si : getService().getServiceInfos()) {
for (EndpointInfo ep : si.getEndpoints()) {
enames.add(ep.getName());
}
}
LOG.log(Level.WARNING, "COULD_NOT_FIND_ENDPOINT", new Object[] { getEndpointName(), enames });
}
try {
Method invoke = getServiceClass().getMethod("invoke", c);
QName catchAll = new QName("http://cxf.apache.org/jaxws/provider", "invoke");
// Bind every operation to the invoke method.
for (ServiceInfo si : getService().getServiceInfos()) {
si.setProperty("soap.force.doclit.bare", Boolean.TRUE);
if (!isFromWsdl()) {
for (EndpointInfo ei : si.getEndpoints()) {
ei.setProperty("soap.no.validate.parts", Boolean.TRUE);
}
}
for (BindingInfo bind : si.getBindings()) {
for (BindingOperationInfo bop : bind.getOperations()) {
OperationInfo o = bop.getOperationInfo();
if (bop.isUnwrappedCapable()) {
// force to bare, no unwrapping
bop.getOperationInfo().setUnwrappedOperation(null);
bop.setUnwrappedOperation(null);
}
if (o.getInput() != null) {
final List<MessagePartInfo> messageParts;
if (o.getInput().getMessagePartsNumber() == 0) {
MessagePartInfo inf = o.getInput().addMessagePart(o.getName());
inf.setConcreteName(o.getName());
messageParts = o.getInput().getMessageParts();
bop.getInput().setMessageParts(messageParts);
} else {
messageParts = o.getInput().getMessageParts();
}
for (MessagePartInfo inf : messageParts) {
inf.setTypeClass(c);
break;
}
}
if (o.getOutput() != null) {
final List<MessagePartInfo> messageParts;
if (o.getOutput().getMessagePartsNumber() == 0) {
MessagePartInfo inf = o.getOutput().addMessagePart(o.getName());
inf.setConcreteName(new QName(o.getName().getNamespaceURI(), o.getName().getLocalPart() + "Response"));
messageParts = o.getOutput().getMessageParts();
bop.getOutput().setMessageParts(messageParts);
} else {
messageParts = o.getOutput().getMessageParts();
}
for (MessagePartInfo inf : messageParts) {
inf.setTypeClass(c);
break;
}
}
getMethodDispatcher().bind(o, invoke);
}
BindingOperationInfo bop = bind.getOperation(catchAll);
if (bop == null) {
OperationInfo op = bind.getInterface().getOperation(catchAll);
if (op == null) {
op = bind.getInterface().addOperation(catchAll);
String name = catchAll.getLocalPart();
MessageInfo mInfo = op.createMessage(new QName(catchAll.getNamespaceURI(), name + "Request"), MessageInfo.Type.INPUT);
op.setInput(catchAll.getLocalPart() + "Request", mInfo);
MessagePartInfo mpi = mInfo.addMessagePart("parameters");
mpi.setElement(true);
mpi.setTypeClass(c);
mpi.setTypeQName(Constants.XSD_ANYTYPE);
mInfo = op.createMessage(new QName(catchAll.getNamespaceURI(), name + "Response"), MessageInfo.Type.OUTPUT);
op.setOutput(name + "Response", mInfo);
mpi = mInfo.addMessagePart("parameters");
mpi.setElement(true);
mpi.setTypeClass(c);
mpi.setTypeQName(Constants.XSD_ANYTYPE);
BindingOperationInfo bo = new BindingOperationInfo(bind, op);
op.setProperty("operation.is.synthetic", Boolean.TRUE);
bo.setProperty("operation.is.synthetic", Boolean.TRUE);
bind.addOperation(bo);
}
}
}
}
} catch (SecurityException e) {
throw new ServiceConstructionException(e);
} catch (NoSuchMethodException e) {
throw new ServiceConstructionException(e);
}
}
use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.
the class WrapperClassGenerator method createWrapperClass.
private void createWrapperClass(MessagePartInfo wrapperPart, MessageInfo messageInfo, OperationInfo op, Method method, boolean isRequest) {
ClassWriter cw = createClassWriter();
if (cw == null) {
LOG.warning(op.getName() + "requires a wrapper bean but problems with" + " ASM has prevented creating one. Operation may not work correctly.");
return;
}
QName wrapperElement = messageInfo.getName();
boolean anonymous = factory.getAnonymousWrapperTypes();
String pkg = getPackageName(method) + ".jaxws_asm" + (anonymous ? "_an" : "");
String className = pkg + "." + StringUtils.capitalize(op.getName().getLocalPart());
if (!isRequest) {
className = className + "Response";
}
String pname = pkg + ".package-info";
Class<?> def = findClass(pname, method.getDeclaringClass());
if (def == null) {
generatePackageInfo(pname, wrapperElement.getNamespaceURI(), method.getDeclaringClass());
}
def = findClass(className, method.getDeclaringClass());
String origClassName = className;
int count = 0;
while (def != null) {
Boolean b = messageInfo.getProperty("parameterized", Boolean.class);
if (b != null && b) {
className = origClassName + (++count);
def = findClass(className, method.getDeclaringClass());
} else {
wrapperPart.setTypeClass(def);
wrapperBeans.add(def);
return;
}
}
String classFileName = periodToSlashes(className);
cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, classFileName, null, "java/lang/Object", null);
AnnotationVisitor av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/XmlRootElement;", true);
av0.visit("name", wrapperElement.getLocalPart());
av0.visit("namespace", wrapperElement.getNamespaceURI());
av0.visitEnd();
av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/XmlAccessorType;", true);
av0.visitEnum("value", "Ljavax/xml/bind/annotation/XmlAccessType;", "FIELD");
av0.visitEnd();
av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/XmlType;", true);
if (!anonymous) {
av0.visit("name", wrapperElement.getLocalPart());
av0.visit("namespace", wrapperElement.getNamespaceURI());
} else {
av0.visit("name", "");
}
av0.visitEnd();
// add constructor
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
Label lbegin = createLabel();
mv.visitLabel(lbegin);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitInsn(Opcodes.RETURN);
Label lend = createLabel();
mv.visitLabel(lend);
mv.visitLocalVariable("this", "L" + classFileName + ";", null, lbegin, lend, 0);
mv.visitMaxs(1, 1);
mv.visitEnd();
for (MessagePartInfo mpi : messageInfo.getMessageParts()) {
generateMessagePart(cw, mpi, method, classFileName);
}
cw.visitEnd();
Class<?> clz = loadClass(className, method.getDeclaringClass(), cw.toByteArray());
wrapperPart.setTypeClass(clz);
wrapperBeans.add(clz);
}
Aggregations