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

The following examples show how to use org.apache.cxf.jaxrs.model.OperationResourceInfo#isSubResourceLocator() . 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: CrossOriginResourceSharingFilter.java    From cxf with Apache License 2.0 5 votes vote down vote up
private OperationResourceInfo findPreflightMethod(
    Map<ClassResourceInfo, MultivaluedMap<String, String>> matchedResources,
                                                  String requestUri,
                                                  String httpMethod,
                                                  MultivaluedMap<String, String> values,
                                                  Message m) {
    final String contentType = MediaType.WILDCARD;
    final MediaType acceptType = MediaType.WILDCARD_TYPE;
    OperationResourceInfo ori = JAXRSUtils.findTargetMethod(matchedResources,
                                m, httpMethod, values,
                                contentType,
                                Collections.singletonList(acceptType),
                                false,
                                false);
    if (ori == null) {
        return null;
    }
    if (ori.isSubResourceLocator()) {
        Class<?> cls = ori.getMethodToInvoke().getReturnType();
        ClassResourceInfo subcri = ori.getClassResourceInfo().getSubResource(cls, cls);
        if (subcri == null) {
            return null;
        }
        MultivaluedMap<String, String> newValues = new MetadataMap<>();
        newValues.putAll(values);
        return findPreflightMethod(Collections.singletonMap(subcri, newValues),
                                   values.getFirst(URITemplate.FINAL_MATCH_GROUP),
                                   httpMethod,
                                   newValues,
                                   m);
    }
    return ori;
}
 
Example 2
Source File: ClientProxyImpl.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected static int getBodyIndex(MultivaluedMap<ParameterType, Parameter> map,
                                OperationResourceInfo ori) {
    List<Parameter> list = map.get(ParameterType.REQUEST_BODY);
    int index = list == null || list.size() > 1 ? -1 : list.get(0).getIndex();
    if (ori.isSubResourceLocator() && index != -1) {
        reportInvalidResourceMethod(ori.getMethodToInvoke(), "NO_BODY_IN_SUBRESOURCE");
    }
    return index;
}
 
Example 3
Source File: ClassIntrospector.java    From aries-jax-rs-whiteboard with Apache License 2.0 4 votes vote down vote up
private static Stream<ResourceMethodInfoDTO> convert(
    Set<ClassResourceInfo> visited,
    String parentPath, String defaultHttpMethod,
    List<MediaType> defaultConsumeTypes,
    List<MediaType> defaultProduceTypes,
    Set<String> defaultNameBindings, boolean recurse,
    OperationResourceInfo operationResourceInfo) {

    List<MediaType> consumeTypes = getConsumesInfo(
        operationResourceInfo.getAnnotatedMethod());

    if (consumeTypes == null) {
        consumeTypes = defaultConsumeTypes;
    }

    List<MediaType> produceTypes = getProducesInfo(
        operationResourceInfo.getAnnotatedMethod());

    if (produceTypes == null) {
        produceTypes = defaultProduceTypes;
    }

    String httpMethod = operationResourceInfo.getHttpMethod();

    if (httpMethod == null) {
        httpMethod = defaultHttpMethod;
    }

    Set<String> nameBindings = operationResourceInfo.getNameBindings();
    if (nameBindings.isEmpty()) {
        nameBindings = defaultNameBindings;
    }

    String path = parentPath +
        operationResourceInfo.getURITemplate().getValue();

    if (operationResourceInfo.isSubResourceLocator()) {
        ClassResourceInfo classResourceInfo =
            operationResourceInfo.getClassResourceInfo();

        Class<?> returnType =
            operationResourceInfo.getAnnotatedMethod().getReturnType();

        ClassResourceInfo subResource = classResourceInfo.getSubResource(
            returnType, returnType);

        if (subResource != null) {
            if (recurse) {
                return convert(visited,
                    path, httpMethod, consumeTypes, produceTypes,
                    nameBindings, !visited.contains(subResource),
                    subResource);
            }
            else {
                return Stream.empty();
            }
        }
    }

    ResourceMethodInfoDTO resourceMethodInfoDTO =
        new ResourceMethodInfoDTO();

    resourceMethodInfoDTO.consumingMimeType = consumeTypes == null ? null :
        consumeTypes.stream().
        map(
            MediaType::toString
        ).toArray(
            String[]::new
        );

    resourceMethodInfoDTO.producingMimeType = produceTypes == null ? null :
        produceTypes.stream().
        map(
            MediaType::toString
        ).toArray(
            String[]::new
        );

    resourceMethodInfoDTO.nameBindings = nameBindings == null ? null :
        nameBindings.toArray(new String[0]);

    resourceMethodInfoDTO.path = normalize(path);

    resourceMethodInfoDTO.method = httpMethod;

    return Stream.of(resourceMethodInfoDTO);
}
 
Example 4
Source File: ClientProxyImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
/**
 * Updates the current state if Client method is invoked, otherwise
 * does the remote invocation or returns a new proxy if subresource
 * method is invoked. Can throw an expected exception if ResponseExceptionMapper
 * is registered
 */
@Override
public Object invoke(Object o, Method m, Object[] params) throws Throwable {
    checkClosed();
    Class<?> declaringClass = m.getDeclaringClass();
    if (Client.class == declaringClass || InvocationHandlerAware.class == declaringClass
        || Object.class == declaringClass || Closeable.class == declaringClass
        || AutoCloseable.class == declaringClass) {
        return m.invoke(this, params);
    }
    resetResponse();
    OperationResourceInfo ori = cri.getMethodDispatcher().getOperationResourceInfo(m);
    if (ori == null) {
        if (m.isDefault()) {
            return invokeDefaultMethod(declaringClass, o, m, params);
        }
        reportInvalidResourceMethod(m, "INVALID_RESOURCE_METHOD");
    }

    MultivaluedMap<ParameterType, Parameter> types = getParametersInfo(m, params, ori);
    List<Parameter> beanParamsList = getParameters(types, ParameterType.BEAN);

    int bodyIndex = getBodyIndex(types, ori);

    List<Object> pathParams = getPathParamValues(m, params, types, beanParamsList, ori, bodyIndex);

    UriBuilder builder = getCurrentBuilder().clone();
    if (isRoot) {
        addNonEmptyPath(builder, ori.getClassResourceInfo().getURITemplate().getValue());
    }
    addNonEmptyPath(builder, ori.getURITemplate().getValue());

    handleMatrixes(m, params, types, beanParamsList, builder);
    handleQueries(m, params, types, beanParamsList, builder);

    URI uri = builder.buildFromEncoded(pathParams.toArray()).normalize();

    MultivaluedMap<String, String> headers = getHeaders();
    MultivaluedMap<String, String> paramHeaders = new MetadataMap<>();
    handleHeaders(m, params, paramHeaders, beanParamsList, types);
    handleCookies(m, params, paramHeaders, beanParamsList, types);

    if (ori.isSubResourceLocator()) {
        ClassResourceInfo subCri = cri.getSubResource(m.getReturnType(), m.getReturnType());
        if (subCri == null) {
            reportInvalidResourceMethod(m, "INVALID_SUBRESOURCE");
        }

        MultivaluedMap<String, String> subHeaders = paramHeaders;
        if (inheritHeaders) {
            subHeaders.putAll(headers);
        }

        ClientState newState = getState().newState(uri, subHeaders,
             getTemplateParametersMap(ori.getURITemplate(), pathParams));
        ClientProxyImpl proxyImpl =
            new ClientProxyImpl(newState, proxyLoader, subCri, false, inheritHeaders);
        proxyImpl.setConfiguration(getConfiguration());
        return JAXRSClientFactory.createProxy(m.getReturnType(), proxyLoader, proxyImpl);
    }
    headers.putAll(paramHeaders);

    getState().setTemplates(getTemplateParametersMap(ori.getURITemplate(), pathParams));

    Object body = null;
    if (bodyIndex != -1) {
        body = params[bodyIndex];
        if (body == null) {
            bodyIndex = -1;
        }
    } else if (types.containsKey(ParameterType.FORM))  {
        body = handleForm(m, params, types, beanParamsList);
    } else if (types.containsKey(ParameterType.REQUEST_BODY))  {
        body = handleMultipart(types, ori, params);
    } else if (hasFormParams(params, beanParamsList)) {
        body = handleForm(m, params, types, beanParamsList);
    }
    
    setRequestHeaders(headers, ori, types.containsKey(ParameterType.FORM),
        body == null ? null : body.getClass(), m.getReturnType());

    try {
        return doChainedInvocation(uri, headers, ori, params, body, bodyIndex, null, null);
    } finally {
        resetResponseStateImmediatelyIfNeeded();
    }

}