Search in sources :

Example 36 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class ClientProxyImpl method invoke.

/**
 * 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
 */
public Object invoke(Object o, Method m, Object[] params) throws Throwable {
    Class<?> declaringClass = m.getDeclaringClass();
    if (Client.class == declaringClass || InvocationHandlerAware.class == declaringClass || Object.class == declaringClass) {
        return m.invoke(this, params);
    }
    resetResponse();
    OperationResourceInfo ori = cri.getMethodDispatcher().getOperationResourceInfo(m);
    if (ori == null) {
        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<String, String>();
    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);
    }
    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();
    }
}
Also used : ParameterType(org.apache.cxf.jaxrs.model.ParameterType) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) URI(java.net.URI) Endpoint(org.apache.cxf.endpoint.Endpoint) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Parameter(org.apache.cxf.jaxrs.model.Parameter) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) UriBuilder(javax.ws.rs.core.UriBuilder)

Example 37 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class WadlGenerator method getResourcesList.

public List<ClassResourceInfo> getResourcesList(Message m, UriInfo ui) {
    final String slash = "/";
    String path = ui.getPath();
    if (!path.startsWith(slash)) {
        path = slash + path;
    }
    List<ClassResourceInfo> all = ((JAXRSServiceImpl) m.getExchange().getService()).getClassResourceInfos();
    boolean absolutePathSlashOn = checkAbsolutePathSlash && ui.getAbsolutePath().getPath().endsWith(slash);
    if (slash.equals(path) && !absolutePathSlashOn) {
        return all;
    }
    List<ClassResourceInfo> cris = new LinkedList<>();
    for (ClassResourceInfo cri : all) {
        MultivaluedMap<String, String> map = new MetadataMap<>();
        if (cri.getURITemplate().match(path, map) && slash.equals(map.getFirst(URITemplate.FINAL_MATCH_GROUP))) {
            cris.add(cri);
        }
    }
    return cris;
}
Also used : JAXRSServiceImpl(org.apache.cxf.jaxrs.JAXRSServiceImpl) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) LinkedList(java.util.LinkedList)

Example 38 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class AegisElementProviderTest method testReadWriteComplexMap.

@Test
public void testReadWriteComplexMap() throws Exception {
    Map<AegisTestBean, AegisSuperBean> testMap = new HashMap<AegisTestBean, AegisSuperBean>();
    Class<InterfaceWithMap> iwithMapClass = InterfaceWithMap.class;
    Method method = iwithMapClass.getMethod("mapFunction");
    Type mapType = method.getGenericReturnType();
    AegisTestBean bean = new AegisTestBean();
    bean.setBoolValue(Boolean.TRUE);
    bean.setStrValue("hovercraft");
    AegisSuperBean bean2 = new AegisSuperBean();
    bean2.setBoolValue(Boolean.TRUE);
    bean2.setStrValue("hovercraft2");
    testMap.put(bean, bean2);
    MessageBodyWriter<Map<AegisTestBean, AegisSuperBean>> writer = new AegisElementProvider<Map<AegisTestBean, AegisSuperBean>>();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    writer.writeTo(testMap, testMap.getClass(), mapType, new Annotation[] {}, MediaType.APPLICATION_OCTET_STREAM_TYPE, new MetadataMap<String, Object>(), os);
    byte[] bytes = os.toByteArray();
    String xml = new String(bytes, "utf-8");
    MessageBodyReader<Map<AegisTestBean, AegisSuperBean>> reader = new AegisElementProvider<Map<AegisTestBean, AegisSuperBean>>();
    byte[] simpleBytes = xml.getBytes("utf-8");
    Map<AegisTestBean, AegisSuperBean> map2 = reader.readFrom(null, mapType, new Annotation[] {}, MediaType.APPLICATION_OCTET_STREAM_TYPE, new MetadataMap<String, String>(), new ByteArrayInputStream(simpleBytes));
    assertEquals(1, map2.size());
    Map.Entry<AegisTestBean, AegisSuperBean> entry = map2.entrySet().iterator().next();
    AegisTestBean bean1 = entry.getKey();
    assertEquals("hovercraft", bean1.getStrValue());
    assertEquals(Boolean.TRUE, bean1.getBoolValue());
    AegisTestBean bean22 = entry.getValue();
    assertEquals("hovercraft2", bean22.getStrValue());
    assertEquals(Boolean.TRUE, bean22.getBoolValue());
}
Also used : HashMap(java.util.HashMap) Method(java.lang.reflect.Method) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) ByteArrayInputStream(java.io.ByteArrayInputStream) AegisTestBean(org.apache.cxf.jaxrs.resources.AegisTestBean) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 39 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class CrossOriginResourceSharingFilter method findPreflightMethod.

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<String, String>();
        newValues.putAll(values);
        return findPreflightMethod(Collections.singletonMap(subcri, newValues), values.getFirst(URITemplate.FINAL_MATCH_GROUP), httpMethod, newValues, m);
    }
    return ori;
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) MediaType(javax.ws.rs.core.MediaType) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo)

Example 40 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class CrossOriginResourceSharingFilter method getResourceMethod.

private Method getResourceMethod(Message m, String httpMethod) {
    String requestUri = HttpUtils.getPathToMatch(m, true);
    List<ClassResourceInfo> resources = JAXRSUtils.getRootResources(m);
    Map<ClassResourceInfo, MultivaluedMap<String, String>> matchedResources = JAXRSUtils.selectResourceClass(resources, requestUri, m);
    if (matchedResources == null) {
        return null;
    }
    MultivaluedMap<String, String> values = new MetadataMap<String, String>();
    OperationResourceInfo ori = findPreflightMethod(matchedResources, requestUri, httpMethod, values, m);
    return ori == null ? null : ori.getAnnotatedMethod();
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Aggregations

MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)80 Test (org.junit.Test)43 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)36 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)34 ByteArrayInputStream (java.io.ByteArrayInputStream)25 Message (org.apache.cxf.message.Message)25 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)15 List (java.util.List)13 Method (java.lang.reflect.Method)12 ArrayList (java.util.ArrayList)11 Map (java.util.Map)10 Endpoint (org.apache.cxf.endpoint.Endpoint)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 LinkedHashMap (java.util.LinkedHashMap)9 Customer (org.apache.cxf.jaxrs.Customer)9 WebClient (org.apache.cxf.jaxrs.client.WebClient)9 Annotation (java.lang.annotation.Annotation)8 HashMap (java.util.HashMap)7 WebApplicationException (javax.ws.rs.WebApplicationException)7 URITemplate (org.apache.cxf.jaxrs.model.URITemplate)7