Search in sources :

Example 1 with JavaException

use of org.apache.cxf.tools.common.model.JavaException in project cxf by apache.

the class WSActionAnnotator method annotate.

public void annotate(JavaAnnotatable ja) {
    JavaMethod method;
    if (ja instanceof JavaMethod) {
        method = (JavaMethod) ja;
    } else {
        throw new RuntimeException("Action can only annotate JavaMethod");
    }
    boolean required = false;
    JavaInterface intf = method.getInterface();
    MessageInfo inputMessage = operation.getInput();
    MessageInfo outputMessage = operation.getOutput();
    JAnnotation actionAnnotation = new JAnnotation(Action.class);
    if (inputMessage.getExtensionAttributes() != null) {
        String inputAction = getAction(inputMessage);
        if (inputAction != null) {
            actionAnnotation.addElement(new JAnnotationElement("input", inputAction));
            required = true;
        }
    }
    if (outputMessage != null && outputMessage.getExtensionAttributes() != null) {
        String outputAction = getAction(outputMessage);
        if (outputAction != null) {
            actionAnnotation.addElement(new JAnnotationElement("output", outputAction));
            required = true;
        }
    }
    if (operation.hasFaults()) {
        List<JAnnotation> faultAnnotations = new ArrayList<>();
        for (FaultInfo faultInfo : operation.getFaults()) {
            if (faultInfo.getExtensionAttributes() != null) {
                String faultAction = getAction(faultInfo);
                if (faultAction == null) {
                    continue;
                }
                JavaException exceptionClass = getExceptionClass(method, faultInfo);
                if (!StringUtils.isEmpty(exceptionClass.getPackageName()) && !exceptionClass.getPackageName().equals(intf.getPackageName())) {
                    intf.addImport(exceptionClass.getClassName());
                }
                JAnnotation faultAnnotation = new JAnnotation(FaultAction.class);
                faultAnnotation.addElement(new JAnnotationElement("className", exceptionClass));
                faultAnnotation.addElement(new JAnnotationElement("value", faultAction));
                faultAnnotations.add(faultAnnotation);
                required = true;
            }
        }
        actionAnnotation.addElement(new JAnnotationElement("fault", faultAnnotations));
    }
    if (required) {
        method.addAnnotation("Action", actionAnnotation);
    }
}
Also used : JavaInterface(org.apache.cxf.tools.common.model.JavaInterface) FaultInfo(org.apache.cxf.service.model.FaultInfo) JavaException(org.apache.cxf.tools.common.model.JavaException) JAnnotation(org.apache.cxf.tools.common.model.JAnnotation) JAnnotationElement(org.apache.cxf.tools.common.model.JAnnotationElement) ArrayList(java.util.ArrayList) JavaMethod(org.apache.cxf.tools.common.model.JavaMethod) MessageInfo(org.apache.cxf.service.model.MessageInfo)

Example 2 with JavaException

use of org.apache.cxf.tools.common.model.JavaException in project cxf by apache.

the class JAXWSFrontEndProcessor method serviceInfo2JavaInf.

public JavaInterface serviceInfo2JavaInf(ServiceInfo service) {
    JavaInterface javaInf = new JavaInterface();
    InterfaceInfo inf = service.getInterface();
    for (OperationInfo op : inf.getOperations()) {
        JavaMethod jm = new JavaMethod();
        Method m = (Method) op.getProperty(ReflectionServiceFactoryBean.METHOD);
        jm.setName(m.getName());
        int i = 0;
        for (Type type : m.getGenericParameterTypes()) {
            JavaParameter jp = new JavaParameter();
            jp.setClassName(getClassName(type));
            jp.setStyle(Style.IN);
            jp.setName("arg" + i++);
            jm.addParameter(jp);
        }
        for (Type type : m.getGenericExceptionTypes()) {
            JavaException jex = new JavaException();
            String className = getClassName(type);
            jex.setClassName(className);
            jex.setName(className);
            jm.addException(jex);
        }
        JavaReturn jreturn = new JavaReturn();
        jreturn.setClassName(getClassName(m.getGenericReturnType()));
        jreturn.setStyle(Style.OUT);
        jm.setReturn(jreturn);
        String pkg = PackageUtils.getPackageName(m.getDeclaringClass());
        javaInf.setPackageName(pkg.length() > 0 ? pkg : ToolConstants.DEFAULT_PACKAGE_NAME);
        javaInf.addMethod(jm);
        javaInf.setName(inf.getName().getLocalPart());
        jm.getParameterList();
    }
    return javaInf;
}
Also used : OperationInfo(org.apache.cxf.service.model.OperationInfo) JavaReturn(org.apache.cxf.tools.common.model.JavaReturn) JavaInterface(org.apache.cxf.tools.common.model.JavaInterface) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JavaException(org.apache.cxf.tools.common.model.JavaException) JavaMethod(org.apache.cxf.tools.common.model.JavaMethod) JavaParameter(org.apache.cxf.tools.common.model.JavaParameter) InterfaceInfo(org.apache.cxf.service.model.InterfaceInfo) Method(java.lang.reflect.Method) JavaMethod(org.apache.cxf.tools.common.model.JavaMethod)

Example 3 with JavaException

use of org.apache.cxf.tools.common.model.JavaException in project cxf by apache.

the class JavaFirstUtil method serviceInfo2JavaInf.

public static JavaInterface serviceInfo2JavaInf(ServiceInfo service) {
    JavaInterface javaInf = new JavaInterface();
    InterfaceInfo inf = service.getInterface();
    for (OperationInfo op : inf.getOperations()) {
        JavaMethod jm = new JavaMethod();
        Method m = (Method) op.getProperty(ReflectionServiceFactoryBean.METHOD);
        jm.setName(m.getName());
        int i = 0;
        for (Type type : m.getGenericParameterTypes()) {
            JavaParameter jp = new JavaParameter();
            jp.setClassName(getClassName(type));
            jp.setStyle(Style.IN);
            jp.setName("arg" + i++);
            jm.addParameter(jp);
        }
        for (Type type : m.getGenericExceptionTypes()) {
            JavaException jex = new JavaException();
            String className = getClassName(type);
            jex.setClassName(className);
            jex.setName(className);
            jm.addException(jex);
        }
        JavaReturn jreturn = new JavaReturn();
        jreturn.setClassName(getClassName(m.getGenericReturnType()));
        jreturn.setStyle(Style.OUT);
        jm.setReturn(jreturn);
        String pkg = PackageUtils.getPackageName(m.getDeclaringClass());
        javaInf.setPackageName(pkg.length() == 0 ? ToolConstants.DEFAULT_PACKAGE_NAME : pkg);
        javaInf.addMethod(jm);
        javaInf.setName(inf.getName().getLocalPart());
        jm.getParameterList();
    }
    return javaInf;
}
Also used : OperationInfo(org.apache.cxf.service.model.OperationInfo) JavaReturn(org.apache.cxf.tools.common.model.JavaReturn) JavaInterface(org.apache.cxf.tools.common.model.JavaInterface) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JavaException(org.apache.cxf.tools.common.model.JavaException) JavaMethod(org.apache.cxf.tools.common.model.JavaMethod) JavaParameter(org.apache.cxf.tools.common.model.JavaParameter) InterfaceInfo(org.apache.cxf.service.model.InterfaceInfo) JavaMethod(org.apache.cxf.tools.common.model.JavaMethod) Method(java.lang.reflect.Method)

Example 4 with JavaException

use of org.apache.cxf.tools.common.model.JavaException in project cxf by apache.

the class JAXWSContainerTest method testSuppressCodeGen.

@Test
public void testSuppressCodeGen() {
    try {
        JAXWSContainer container = new JAXWSContainer(null);
        ToolContext context = new ToolContext();
        // Do not generate any artifacts, we just want the code model.
        context.put(ToolConstants.CFG_SUPPRESS_GEN, "suppress");
        // Where to put the generated source code
        context.put(ToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
        context.put(ToolConstants.CFG_WSDLURL, getLocation("/wsdl2java_wsdl/hello_world.wsdl"));
        // Delegate jaxb to generate the type classes
        context.put(DataBindingProfile.class, PluginLoader.getInstance().getDataBindingProfile("jaxb"));
        context.put(FrontEndProfile.class, PluginLoader.getInstance().getFrontEndProfile("jaxws"));
        container.setContext(context);
        // Now shoot
        container.execute();
        // At this point you should be able to get the
        // SEI/Service(Client stub)/Exception classes/Types classes
        assertNotNull(output.list());
        assertEquals(0, output.list().length);
        // Now you can get the JavaModel from the context.
        Map<QName, JavaModel> map = CastUtils.cast((Map<?, ?>) context.get(WSDLToJavaProcessor.MODEL_MAP));
        JavaModel javaModel = map.get(new QName("http://cxf.apache.org/w2j/hello_world_soap_http", "SOAPService"));
        assertNotNull(javaModel);
        Map<String, JavaInterface> interfaces = javaModel.getInterfaces();
        assertEquals(1, interfaces.size());
        JavaInterface intf = interfaces.values().iterator().next();
        String interfaceName = intf.getName();
        assertEquals("Greeter", interfaceName);
        assertEquals("http://cxf.apache.org/w2j/hello_world_soap_http", intf.getNamespace());
        assertEquals("org.apache.cxf.w2j.hello_world_soap_http", intf.getPackageName());
        List<JavaMethod> methods = intf.getMethods();
        assertEquals(6, methods.size());
        Boolean methodSame = false;
        JavaMethod m1 = null;
        for (JavaMethod m2 : methods) {
            if ("testDocLitFault".equals(m2.getName())) {
                methodSame = true;
                m1 = m2;
                break;
            }
        }
        assertTrue(methodSame);
        assertNotNull(m1);
        assertEquals(2, m1.getExceptions().size());
        List<String> names = new ArrayList<>();
        for (JavaException exc : m1.getExceptions()) {
            names.add(exc.getName());
        }
        assertTrue("BadRecordLitFault", names.contains("BadRecordLitFault"));
        assertTrue("NoSuchCodeLitFault", names.contains("NoSuchCodeLitFault"));
        String address = null;
        for (JavaServiceClass service : javaModel.getServiceClasses().values()) {
            if ("SOAPService_Test1".equals(service.getName())) {
                continue;
            }
            List<JavaPort> ports = service.getPorts();
            for (JavaPort port : ports) {
                if (interfaceName.equals(port.getPortType())) {
                    address = port.getBindingAdress();
                    break;
                }
            }
            if (!"".equals(address)) {
                break;
            }
        }
        assertEquals("http://localhost:9000/SoapContext/SoapPort", address);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : JavaInterface(org.apache.cxf.tools.common.model.JavaInterface) JavaException(org.apache.cxf.tools.common.model.JavaException) JavaServiceClass(org.apache.cxf.tools.common.model.JavaServiceClass) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) JAXWSContainer(org.apache.cxf.tools.wsdlto.frontend.jaxws.JAXWSContainer) ToolContext(org.apache.cxf.tools.common.ToolContext) URISyntaxException(java.net.URISyntaxException) JavaException(org.apache.cxf.tools.common.model.JavaException) JavaPort(org.apache.cxf.tools.common.model.JavaPort) JavaModel(org.apache.cxf.tools.common.model.JavaModel) JavaMethod(org.apache.cxf.tools.common.model.JavaMethod) Test(org.junit.Test)

Example 5 with JavaException

use of org.apache.cxf.tools.common.model.JavaException in project cxf by apache.

the class WSActionAnnotator method getExceptionClass.

private JavaException getExceptionClass(JavaMethod method, FaultInfo faultInfo) {
    for (JavaException exception : method.getExceptions()) {
        QName faultName = faultInfo.getName();
        // Perform a case insensitive "startsWith" check that works for different locales
        String prefix = faultName.getLocalPart();
        if (exception.getTargetNamespace().equals(faultName.getNamespaceURI()) && exception.getName().regionMatches(true, 0, prefix, 0, prefix.length())) {
            return exception;
        }
    }
    return null;
}
Also used : JavaException(org.apache.cxf.tools.common.model.JavaException) QName(javax.xml.namespace.QName)

Aggregations

JavaException (org.apache.cxf.tools.common.model.JavaException)6 JavaInterface (org.apache.cxf.tools.common.model.JavaInterface)4 JavaMethod (org.apache.cxf.tools.common.model.JavaMethod)4 GenericArrayType (java.lang.reflect.GenericArrayType)2 Method (java.lang.reflect.Method)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 ArrayList (java.util.ArrayList)2 QName (javax.xml.namespace.QName)2 InterfaceInfo (org.apache.cxf.service.model.InterfaceInfo)2 OperationInfo (org.apache.cxf.service.model.OperationInfo)2 JavaModel (org.apache.cxf.tools.common.model.JavaModel)2 JavaParameter (org.apache.cxf.tools.common.model.JavaParameter)2 JavaReturn (org.apache.cxf.tools.common.model.JavaReturn)2 URISyntaxException (java.net.URISyntaxException)1 FaultInfo (org.apache.cxf.service.model.FaultInfo)1 MessageInfo (org.apache.cxf.service.model.MessageInfo)1 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)1 ToolContext (org.apache.cxf.tools.common.ToolContext)1 JAnnotation (org.apache.cxf.tools.common.model.JAnnotation)1