Search in sources :

Example 36 with OperationResourceInfo

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

the class JAXRSUtilsTest method testQueryParametersIntegerArray.

@Test
public void testQueryParametersIntegerArray() throws Exception {
    Class<?>[] argType = { Integer[].class };
    Method m = Customer.class.getMethod("testQueryIntegerArray", argType);
    Message messageImpl = createMessage();
    messageImpl.put(Message.QUERY_STRING, "query=1&query=2");
    List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), null, messageImpl);
    assertEquals(1, params.size());
    Integer[] intValues = (Integer[]) params.get(0);
    assertEquals(2, intValues.length);
    assertEquals(1, (int) intValues[0]);
    assertEquals(2, (int) intValues[1]);
}
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 37 with OperationResourceInfo

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

the class JAXRSUtilsTest method testQueryParameterDefaultValue.

@Test
public void testQueryParameterDefaultValue() throws Exception {
    Message messageImpl = createMessage();
    ProviderFactory.getInstance(messageImpl).registerUserProvider(new GenericObjectParameterHandler());
    Class<?>[] argType = { String.class, String.class };
    Method m = Customer.class.getMethod("testGenericObjectParamDefaultValue", argType);
    messageImpl.put(Message.QUERY_STRING, "p1=thequery&p2");
    List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), null, messageImpl);
    assertEquals(2, params.size());
    String query = (String) params.get(0);
    assertEquals("thequery", query);
    query = (String) params.get(1);
    assertEquals("thequery", query);
}
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 38 with OperationResourceInfo

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

the class JAXRSUtilsTest method testCookieParameters.

@Test
public void testCookieParameters() throws Exception {
    Class<?>[] argType = { String.class, Set.class, String.class, Set.class };
    Method m = Customer.class.getMethod("testCookieParam", argType);
    Message messageImpl = createMessage();
    MultivaluedMap<String, String> headers = new MetadataMap<>();
    headers.add("Cookie", "c1=c1Value");
    messageImpl.put(Message.PROTOCOL_HEADERS, headers);
    List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), null, messageImpl);
    assertEquals(4, params.size());
    assertEquals("c1Value", params.get(0));
    Set<Cookie> set1 = CastUtils.cast((Set<?>) params.get(1));
    assertEquals(1, set1.size());
    assertTrue(set1.contains(Cookie.valueOf("c1=c1Value")));
    assertEquals("c2Value", params.get(2));
    Set<Cookie> set2 = CastUtils.cast((Set<?>) params.get(3));
    assertTrue(set2.contains((Object) "c2Value"));
    assertEquals(1, set2.size());
}
Also used : Cookie(javax.ws.rs.core.Cookie) Message(org.apache.cxf.message.Message) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Method(java.lang.reflect.Method) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Test(org.junit.Test)

Example 39 with OperationResourceInfo

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

the class JAXRSOutInterceptor method processResponse.

// Response shouldn't be closed here
@SuppressWarnings("resource")
private void processResponse(ServerProviderFactory providerFactory, Message message) {
    if (isResponseAlreadyHandled(message)) {
        return;
    }
    MessageContentsList objs = MessageContentsList.getContentsList(message);
    if (objs == null || objs.isEmpty()) {
        return;
    }
    Object responseObj = objs.get(0);
    final Response response;
    if (responseObj instanceof Response) {
        response = (Response) responseObj;
        if (response.getStatus() == 500 && message.getExchange().get(JAXRSUtils.EXCEPTION_FROM_MAPPER) != null) {
            message.put(Message.RESPONSE_CODE, 500);
            return;
        }
    } else {
        int status = getStatus(message, responseObj != null ? 200 : 204);
        response = JAXRSUtils.toResponseBuilder(status).entity(responseObj).build();
    }
    Exchange exchange = message.getExchange();
    OperationResourceInfo ori = (OperationResourceInfo) exchange.get(OperationResourceInfo.class.getName());
    serializeMessage(providerFactory, message, response, ori, true);
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) Exchange(org.apache.cxf.message.Exchange) MessageContentsList(org.apache.cxf.message.MessageContentsList) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo)

Example 40 with OperationResourceInfo

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

the class UriInfoImpl method getMatchedURIs.

public List<String> getMatchedURIs(boolean decode) {
    if (stack != null) {
        List<String> objects = new ArrayList<>();
        List<String> uris = new LinkedList<>();
        StringBuilder sumPath = new StringBuilder("");
        for (MethodInvocationInfo invocation : stack) {
            List<String> templateObjects = invocation.getTemplateValues();
            OperationResourceInfo ori = invocation.getMethodInfo();
            URITemplate[] paths = { ori.getClassResourceInfo().getURITemplate(), ori.getURITemplate() };
            if (paths[0] != null) {
                int count = paths[0].getVariables().size();
                List<String> rootObjects = new ArrayList<>(count);
                for (int i = 0; i < count && i < templateObjects.size(); i++) {
                    rootObjects.add(templateObjects.get(i));
                }
                uris.add(0, createMatchedPath(paths[0].getValue(), rootObjects, decode));
            }
            if (paths[1] != null && paths[1].getValue().length() > 1) {
                for (URITemplate t : paths) {
                    if (t != null) {
                        sumPath.append('/').append(t.getValue());
                    }
                }
                objects.addAll(templateObjects);
                uris.add(0, createMatchedPath(sumPath.toString(), objects, decode));
            }
        }
        return uris;
    }
    LOG.fine("No resource stack information, returning empty list");
    return Collections.emptyList();
}
Also used : ArrayList(java.util.ArrayList) URITemplate(org.apache.cxf.jaxrs.model.URITemplate) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MethodInvocationInfo(org.apache.cxf.jaxrs.model.MethodInvocationInfo) LinkedList(java.util.LinkedList)

Aggregations

OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)130 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)105 Message (org.apache.cxf.message.Message)72 Test (org.junit.Test)70 Method (java.lang.reflect.Method)53 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)40 Customer (org.apache.cxf.jaxrs.Customer)22 ArrayList (java.util.ArrayList)13 List (java.util.List)13 Endpoint (org.apache.cxf.endpoint.Endpoint)13 MessageImpl (org.apache.cxf.message.MessageImpl)13 ByteArrayInputStream (java.io.ByteArrayInputStream)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 MethodDispatcher (org.apache.cxf.jaxrs.model.MethodDispatcher)8 HttpServletResponse (javax.servlet.http.HttpServletResponse)7