Java Code Examples for org.apache.cxf.jaxrs.model.OperationResourceInfo#getMethodToInvoke()

The following examples show how to use org.apache.cxf.jaxrs.model.OperationResourceInfo#getMethodToInvoke() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MicroProfileClientProxyImpl.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Override
protected Message createMessage(Object body,
                                OperationResourceInfo ori,
                                MultivaluedMap<String, String> headers,
                                URI currentURI,
                                Exchange exchange,
                                Map<String, Object> invocationContext,
                                boolean proxy) {

    Method m = ori.getMethodToInvoke();

    Message msg = super.createMessage(body, ori, headers, currentURI, exchange, invocationContext, proxy);

    @SuppressWarnings("unchecked")
    Map<String, Object> filterProps = (Map<String, Object>) msg.getExchange()
                                                               .get("jaxrs.filter.properties");
    if (filterProps == null) {
        filterProps = new HashMap<>();
        msg.getExchange().put("jaxrs.filter.properties", filterProps);
    }
    filterProps.put("org.eclipse.microprofile.rest.client.invokedMethod", m);
    return msg;
}
 
Example 2
Source File: CustomJAXRSInvoker.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public Object invoke(Exchange exchange, Object requestParams, Object resourceObject) {

    OperationResourceInfo ori = exchange.get(OperationResourceInfo.class);
    Method m = ori.getMethodToInvoke();
    Class<?> realClass = ClassHelper.getRealClass(exchange.getBus(), resourceObject);

    Principal p = new SecurityContextImpl(exchange.getInMessage()).getUserPrincipal();
    if (realClass == SecureBookStore.class && "getThatBook".equals(m.getName())
        && "baddy".equals(p.getName())) {
        return new MessageContentsList(Response.status(Response.Status.FORBIDDEN).build());
    }

    return super.invoke(exchange, requestParams, resourceObject);
}
 
Example 3
Source File: JAXRSServiceImpl.java    From cxf with Apache License 2.0 5 votes vote down vote up
public List<ServiceInfo> getServiceInfos() {
    if (!createServiceModel) {
        return Collections.emptyList();
    }
    // try to convert to WSDL-centric model so that CXF DataBindings can get initialized
    // might become useful too if we support wsdl2

    // make databindings to use databinding-specific information
    // like @XmlRootElement for ex to select a namespace
    this.put("org.apache.cxf.databinding.namespace", "true");

    List<ServiceInfo> infos = new ArrayList<>();
    for (ClassResourceInfo cri : classResourceInfos) {
        ServiceInfo si = new ServiceInfo();
        infos.add(si);
        QName qname = JAXRSUtils.getClassQName(cri.getServiceClass());
        si.setName(qname);
        InterfaceInfo inf = new InterfaceInfo(si, qname);
        si.setInterface(inf);
        for (OperationResourceInfo ori : cri.getMethodDispatcher().getOperationResourceInfos()) {
            Method m = ori.getMethodToInvoke();
            QName oname = new QName(qname.getNamespaceURI(), m.getName());
            OperationInfo oi = inf.addOperation(oname);
            createMessagePartInfo(oi, m.getReturnType(), qname, m, false);
            for (Parameter pm : ori.getParameters()) {

                if (pm.getType() == ParameterType.REQUEST_BODY) {
                    createMessagePartInfo(oi,
                                          ori.getMethodToInvoke().getParameterTypes()[pm.getIndex()],
                                          qname, m, true);
                }
            }
        }

    }
    return infos;
}
 
Example 4
Source File: ResourceMapJavaDocProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public String getMethodDoc(OperationResourceInfo ori) {
    Method method = ori.getAnnotatedMethod() == null ? ori.getMethodToInvoke()
        : ori.getAnnotatedMethod();
    String methodKey = method.getDeclaringClass().getName()
        + "." + method.getName();
    return dumpedDocFile.getProperty(methodKey);
}
 
Example 5
Source File: ResourceMapJavaDocProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public String getMethodResponseDoc(OperationResourceInfo ori) {
    Method method = ori.getAnnotatedMethod() == null ? ori.getMethodToInvoke()
        : ori.getAnnotatedMethod();
    String methodResponseKey = method.getDeclaringClass().getName()
        + "." + method.getName() + "." + "returnCommentTag";
    return dumpedDocFile.getProperty(methodResponseKey);
}
 
Example 6
Source File: ResourceMapJavaDocProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public String getMethodParameterDoc(OperationResourceInfo ori, int paramIndex) {
    Method method = ori.getAnnotatedMethod() == null ? ori.getMethodToInvoke()
        : ori.getAnnotatedMethod();
    String methodParamKey = method.getDeclaringClass().getName()
        + "." + method.getName()
        + ".paramCommentTag." + paramIndex;
    return dumpedDocFile.getProperty(methodParamKey);
}
 
Example 7
Source File: ResourceUtils.java    From cxf with Apache License 2.0 4 votes vote down vote up
private static void getAllTypesForResource(ClassResourceInfo resource,
                                           ResourceTypes types,
                                           boolean jaxbOnly,
                                           MessageBodyWriter<?> jaxbWriter) {
    Class<?> jaxbElement = null;
    try {
        jaxbElement = ClassLoaderUtils.loadClass("javax.xml.bind.JAXBElement", ResourceUtils.class);
    } catch (final ClassNotFoundException e) {
        // no-op
    }

    for (OperationResourceInfo ori : resource.getMethodDispatcher().getOperationResourceInfos()) {
        Method method = ori.getAnnotatedMethod() == null ? ori.getMethodToInvoke() : ori.getAnnotatedMethod();
        Class<?> realReturnType = method.getReturnType();
        Class<?> cls = realReturnType;
        if (cls == Response.class || ori.isAsync()) {
            cls = getActualJaxbType(cls, method, false);
        }
        Type type = method.getGenericReturnType();
        if (jaxbOnly) {
            checkJaxbType(resource.getServiceClass(), cls, realReturnType == Response.class || ori.isAsync()
                ? cls : type, types, method.getAnnotations(), jaxbWriter, jaxbElement);
        } else {
            types.getAllTypes().put(cls, type);
        }

        for (Parameter pm : ori.getParameters()) {
            if (pm.getType() == ParameterType.REQUEST_BODY) {
                Class<?> inType = method.getParameterTypes()[pm.getIndex()];
                if (inType != AsyncResponse.class) {
                    Type paramType = method.getGenericParameterTypes()[pm.getIndex()];
                    if (jaxbOnly) {
                        checkJaxbType(resource.getServiceClass(), inType, paramType, types,
                                      method.getParameterAnnotations()[pm.getIndex()], jaxbWriter, jaxbElement);
                    } else {
                        types.getAllTypes().put(inType, paramType);
                    }
                }
            }
        }

    }

    for (ClassResourceInfo sub : resource.getSubResources()) {
        if (!isRecursiveSubResource(resource, sub)) {
            getAllTypesForResource(sub, types, jaxbOnly, jaxbWriter);
        }
    }
}
 
Example 8
Source File: JavaDocProvider.java    From cxf with Apache License 2.0 4 votes vote down vote up
private MethodDocs getOperationDocInternal(OperationResourceInfo ori) throws Exception {
    Method method = ori.getAnnotatedMethod() == null
            ? ori.getMethodToInvoke()
            : ori.getAnnotatedMethod();
    ClassDocs classDoc = getClassDocInternal(method.getDeclaringClass());
    if (classDoc == null) {
        return null;
    }
    MethodDocs mDocs = classDoc.getMethodDocs(method);
    if (mDocs == null) {
        String operLink = getOperLink();
        String operMarker = operLink + method.getName() + getOperationMarkerOpen();

        int operMarkerIndex = classDoc.getClassDoc().indexOf(operMarker);
        while (operMarkerIndex != -1) {
            int startOfOpSigIndex = operMarkerIndex + operMarker.length();
            int endOfOpSigIndex = classDoc.getClassDoc().indexOf(getOperationMarkerClose(),
                                                                 startOfOpSigIndex);
            int paramLen = method.getParameterTypes().length;
            if (endOfOpSigIndex == startOfOpSigIndex && paramLen == 0) {
                break;
            } else if (endOfOpSigIndex > startOfOpSigIndex + 1) {
                String paramSequence = classDoc.getClassDoc().substring(operMarkerIndex, endOfOpSigIndex);
                if (paramSequence.startsWith(operMarker)) {
                    paramSequence = paramSequence.substring(operMarker.length());
                    String[] opBits = paramSequence.split(getOperationParamSeparator());
                    if (opBits.length == paramLen) {
                        break;
                    }
                }
            }
            operMarkerIndex = classDoc.getClassDoc().indexOf(operMarker,
                                                             operMarkerIndex + operMarker.length());
        }

        if (operMarkerIndex == -1) {
            return null;
        }

        String operDoc = classDoc.getClassDoc().substring(operMarkerIndex + operMarker.length());
        String operInfoTag = getOperInfoTag();
        String operInfo = getJavaDocText(operDoc, operInfoTag, operLink, 0);
        String responseInfo = null;
        List<String> paramDocs = new LinkedList<>();
        if (!StringUtils.isEmpty(operInfo)) {
            int returnsIndex = operDoc.indexOf("Returns:", operLink.length());
            int nextOpIndex = operDoc.indexOf(operLink);
            if (returnsIndex != -1 && (nextOpIndex > returnsIndex || nextOpIndex == -1)) {
                responseInfo = getJavaDocText(operDoc, getResponseMarker(), operLink, returnsIndex + 8);
            }

            int paramIndex = operDoc.indexOf("Parameters:");
            if (paramIndex != -1 && (nextOpIndex == -1 || paramIndex < nextOpIndex)) {
                String paramString = returnsIndex == -1 ? operDoc.substring(paramIndex)
                    : operDoc.substring(paramIndex, returnsIndex);

                String codeTag = getCodeTag();

                int codeIndex = paramString.indexOf(codeTag);
                while (codeIndex != -1) {
                    int next = paramString.indexOf('<', codeIndex + 7);
                    if (next == -1) {
                        next = paramString.length();
                    }
                    String param = paramString.substring(codeIndex + 7, next).trim();
                    if (param.startsWith("-")) {
                        param = param.substring(1).trim();
                    }
                    paramDocs.add(param);
                    if (next == paramString.length()) {
                        break;
                    }
                    codeIndex = next + 1;
                    codeIndex = paramString.indexOf(codeTag, codeIndex);
                }

            }
        }
        mDocs = new MethodDocs(operInfo, paramDocs, responseInfo);
        classDoc.addMethodDocs(method, mDocs);
    }

    return mDocs;
}
 
Example 9
Source File: ClientProxyImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Override
protected void doWriteBody(Message outMessage,
                           Object body,
                           Type bodyType,
                           Annotation[] customAnns,
                           OutputStream os) throws Fault {


    OperationResourceInfo ori = outMessage.getContent(OperationResourceInfo.class);
    if (ori == null) {
        return;
    }

    Method method = ori.getMethodToInvoke();
    int bodyIndex = (Integer)outMessage.get(PROXY_METHOD_PARAM_BODY_INDEX);

    Annotation[] anns = customAnns != null ? customAnns
        : getMethodAnnotations(ori.getAnnotatedMethod(), bodyIndex);
    try {
        if (bodyIndex != -1) {
            Class<?> paramClass = method.getParameterTypes()[bodyIndex];
            Class<?> bodyClass =
                paramClass.isAssignableFrom(body.getClass()) ? paramClass : body.getClass();
            Type genericType = method.getGenericParameterTypes()[bodyIndex];
            if (bodyType != null) {
                genericType = bodyType;
            }
            genericType = InjectionUtils.processGenericTypeIfNeeded(
                ori.getClassResourceInfo().getServiceClass(), bodyClass, genericType);
            bodyClass = InjectionUtils.updateParamClassToTypeIfNeeded(bodyClass, genericType);
            writeBody(body, outMessage, bodyClass, genericType, anns, os);
        } else {
            Type paramType = body.getClass();
            if (bodyType != null) {
                paramType = bodyType;
            }
            writeBody(body, outMessage, body.getClass(), paramType,
                      anns, os);
        }
    } catch (Exception ex) {
        throw new Fault(ex);
    }

}
 
Example 10
Source File: WadlGenerator.java    From cxf with Apache License 2.0 4 votes vote down vote up
private Method getMethod(OperationResourceInfo ori) {
    Method annMethod = ori.getAnnotatedMethod();
    return annMethod != null ? annMethod : ori.getMethodToInvoke();
}