Search in sources :

Example 56 with OperationResourceInfo

use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.

the class JAXRSUtilsTest method verifyParametersBean.

private void verifyParametersBean(Method m, MultivaluedMap<String, String> simpleValues, Message simpleMessageImpl, MultivaluedMap<String, String> complexValues, Message complexMessageImpl) throws Exception {
    List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), simpleValues, simpleMessageImpl);
    assertEquals("Bean should be created", 1, params.size());
    Customer.CustomerBean cb = (Customer.CustomerBean) params.get(0);
    assertNotNull(cb);
    assertEquals("aValue", cb.getA());
    assertEquals(Long.valueOf(123), cb.getB());
    params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), complexValues, complexMessageImpl);
    assertEquals("Bean should be created", 1, params.size());
    Customer.CustomerBean cb1 = (Customer.CustomerBean) params.get(0);
    assertNotNull(cb1);
    assertEquals("A", cb1.getA());
    assertEquals(Long.valueOf(123), cb1.getB());
    List<String> list1 = cb1.getC();
    assertEquals(3, list1.size());
    assertEquals("1", list1.get(0));
    assertEquals("2", list1.get(1));
    assertEquals("3", list1.get(2));
    Customer.CustomerBean cb2 = cb1.getD();
    assertNotNull(cb2);
    assertEquals("B", cb2.getA());
    assertEquals(Long.valueOf(456), cb2.getB());
    List<String> list2 = cb2.getC();
    assertEquals(3, list2.size());
    assertEquals("4", list2.get(0));
    assertEquals("5", list2.get(1));
    assertEquals("6", list2.get(2));
    List<Customer.CustomerBean> cb2List = cb1.e;
    assertEquals(2, cb2List.size());
    int idx = 1;
    for (Customer.CustomerBean cb2E : cb2List) {
        assertNotNull(cb2E);
        assertEquals("B" + idx, cb2E.getA());
        assertEquals(Long.valueOf(456 + idx), cb2E.getB());
        // ensure C was stripped properly since lists within lists are not supported
        assertNull(cb2E.getC());
        assertNull(cb2E.getD());
        assertNull(cb2E.e);
        idx++;
    }
    Customer.CustomerBean cb3 = cb2.getD();
    assertNotNull(cb3);
    assertEquals("C", cb3.getA());
    assertEquals(Long.valueOf(789), cb3.getB());
    List<String> list3 = cb3.getC();
    assertEquals(3, list3.size());
    assertEquals("7", list3.get(0));
    assertEquals("8", list3.get(1));
    assertEquals("9", list3.get(2));
    List<Customer.CustomerBean> cb3List = cb2.e;
    assertEquals(2, cb3List.size());
    idx = 1;
    for (Customer.CustomerBean cb3E : cb3List) {
        assertNotNull(cb3E);
        assertEquals("C" + idx, cb3E.getA());
        assertEquals(Long.valueOf(789 + idx), cb3E.getB());
        // ensure C was stripped properly since lists within lists are not supported
        assertNull(cb3E.getC());
        assertNull(cb3E.getD());
        assertNull(cb3E.e);
        idx++;
    }
}
Also used : Customer(org.apache.cxf.jaxrs.Customer) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Endpoint(org.apache.cxf.endpoint.Endpoint)

Example 57 with OperationResourceInfo

use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.

the class JAXRSUtilsTest method testXmlAdapterString.

@Test
public void testXmlAdapterString() throws Exception {
    Method m = Customer.class.getMethod("testXmlAdapter4", String.class);
    Message messageImpl = createMessage();
    messageImpl.put(Message.QUERY_STRING, "a=3");
    List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), null, messageImpl);
    assertEquals(1, params.size());
    String ret = (String) params.get(0);
    assertEquals("Val: 3", ret);
}
Also used : Message(org.apache.cxf.message.Message) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 58 with OperationResourceInfo

use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.

the class JAXRSUtilsTest method testParamAnnotationOnField.

@Test
public void testParamAnnotationOnField() throws Exception {
    ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
    Customer c = new Customer();
    OperationResourceInfo ori = new OperationResourceInfo(Customer.class.getMethods()[0], cri);
    Message m = createMessage();
    MultivaluedMap<String, String> headers = new MetadataMap<>();
    headers.add("AHeader", "theAHeader");
    m.put(Message.PROTOCOL_HEADERS, headers);
    m.put(Message.QUERY_STRING, "b=bValue");
    JAXRSUtils.injectParameters(ori, c, m);
    assertEquals("bValue", c.getB());
    assertEquals("theAHeader", c.getAHeader());
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Message(org.apache.cxf.message.Message) Customer(org.apache.cxf.jaxrs.Customer) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Test(org.junit.Test)

Example 59 with OperationResourceInfo

use of org.apache.cxf.jaxrs.model.OperationResourceInfo 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
 */
@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();
    }
}
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 60 with OperationResourceInfo

use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.

the class ClientProxyImpl method doChainedInvocation.

// CHECKSTYLE:OFF
protected Object doChainedInvocation(URI uri, MultivaluedMap<String, String> headers, OperationResourceInfo ori, Object[] methodParams, Object body, int bodyIndex, Exchange exchange, Map<String, Object> invocationContext) throws Throwable {
    // CHECKSTYLE:ON
    Bus configuredBus = getConfiguration().getBus();
    Bus origBus = BusFactory.getAndSetThreadDefaultBus(configuredBus);
    ClassLoaderHolder origLoader = null;
    try {
        ClassLoader loader = configuredBus.getExtension(ClassLoader.class);
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        Message outMessage = createMessage(body, ori, headers, uri, exchange, invocationContext, true);
        if (bodyIndex != -1) {
            outMessage.put(Type.class, ori.getMethodToInvoke().getGenericParameterTypes()[bodyIndex]);
        }
        outMessage.getExchange().setOneWay(ori.isOneway());
        setSupportOnewayResponseProperty(outMessage);
        outMessage.setContent(OperationResourceInfo.class, ori);
        setPlainOperationNameProperty(outMessage, ori.getMethodToInvoke().getName());
        outMessage.getExchange().put(Method.class, ori.getMethodToInvoke());
        outMessage.put(Annotation.class.getName(), getMethodAnnotations(ori.getAnnotatedMethod(), bodyIndex));
        outMessage.getExchange().put(Message.SERVICE_OBJECT, proxy);
        if (methodParams != null) {
            outMessage.put(List.class, Arrays.asList(methodParams));
        }
        if (body != null) {
            outMessage.put(PROXY_METHOD_PARAM_BODY_INDEX, bodyIndex);
        }
        outMessage.getInterceptorChain().add(bodyWriter);
        Map<String, Object> reqContext = getRequestContext(outMessage);
        reqContext.put(OperationResourceInfo.class.getName(), ori);
        reqContext.put(PROXY_METHOD_PARAM_BODY_INDEX, bodyIndex);
        // execute chain
        InvocationCallback<Object> asyncCallback = checkAsyncCallback(ori, reqContext, outMessage);
        if (asyncCallback != null) {
            return doInvokeAsync(ori, outMessage, asyncCallback);
        }
        doRunInterceptorChain(outMessage);
        Object[] results = preProcessResult(outMessage);
        if (results != null && results.length == 1) {
            return results[0];
        }
        try {
            return handleResponse(outMessage, ori.getClassResourceInfo().getServiceClass());
        } finally {
            completeExchange(outMessage.getExchange(), true);
        }
    } finally {
        if (origLoader != null) {
            origLoader.reset();
        }
        if (origBus != configuredBus) {
            BusFactory.setThreadDefaultBus(origBus);
        }
    }
}
Also used : Bus(org.apache.cxf.Bus) Message(org.apache.cxf.message.Message) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Annotation(java.lang.annotation.Annotation)

Aggregations

OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)129 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)104 Message (org.apache.cxf.message.Message)72 Test (org.junit.Test)70 Method (java.lang.reflect.Method)52 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)40 Customer (org.apache.cxf.jaxrs.Customer)22 Endpoint (org.apache.cxf.endpoint.Endpoint)13 MessageImpl (org.apache.cxf.message.MessageImpl)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ArrayList (java.util.ArrayList)12 List (java.util.List)12 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)11 Response (javax.ws.rs.core.Response)11 URITemplate (org.apache.cxf.jaxrs.model.URITemplate)11 Exchange (org.apache.cxf.message.Exchange)11 MediaType (javax.ws.rs.core.MediaType)10 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)10 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 MethodDispatcher (org.apache.cxf.jaxrs.model.MethodDispatcher)7