Search in sources :

Example 51 with Response

use of org.restlet.Response in project camel by apache.

the class RestletRouteBuilderTest method testNotFound.

@Test
public void testNotFound() throws IOException {
    Client client = new Client(Protocol.HTTP);
    Response response = client.handle(new Request(Method.POST, "http://localhost:" + portNum + "/unknown"));
    // expect error status as no Restlet consumer to handle POST method
    assertEquals(Status.CLIENT_ERROR_NOT_FOUND, response.getStatus());
    assertNotNull(response.getEntity().getText());
}
Also used : Response(org.restlet.Response) Request(org.restlet.Request) Client(org.restlet.Client) Test(org.junit.Test)

Example 52 with Response

use of org.restlet.Response in project camel by apache.

the class RestletRouteBuilderTest method testConsumer.

@Test
public void testConsumer() throws IOException {
    Client client = new Client(Protocol.HTTP);
    Response response = client.handle(new Request(Method.GET, "http://localhost:" + portNum + "/orders/99991/6"));
    assertEquals("received GET request with id=99991 and x=6", response.getEntity().getText());
}
Also used : Response(org.restlet.Response) Request(org.restlet.Request) Client(org.restlet.Client) Test(org.junit.Test)

Example 53 with Response

use of org.restlet.Response in project camel by apache.

the class RestletRouteBuilderTest method testConsumerWithSpaces.

@Test
public void testConsumerWithSpaces() throws IOException {
    Client client = new Client(Protocol.HTTP);
    Response response = client.handle(new Request(Method.GET, "http://localhost:" + portNum + "/orders with spaces in path/99991/6"));
    assertEquals("received GET request with id=99991 and x=6", response.getEntity().getText());
}
Also used : Response(org.restlet.Response) Request(org.restlet.Request) Client(org.restlet.Client) Test(org.junit.Test)

Example 54 with Response

use of org.restlet.Response in project qi4j-sdk by Qi4j.

the class ContextResource method handleCommand.

private void handleCommand(String segment) {
    Request request = Request.getCurrent();
    Response response = Response.getCurrent();
    // Check if this is a request to show the form for this command
    Method interactionMethod = resourceMethodCommands.get(segment);
    if (shouldShowCommandForm(interactionMethod)) {
        // Show form
        // TODO This should check if method is idempotent
        response.getAllowedMethods().add(org.restlet.data.Method.POST);
        try {
            // Check if there is a query with this name - if so invoke it
            Method queryMethod = resourceMethodQueries.get(segment);
            if (queryMethod != null) {
                result(queryMethod.invoke(this));
            } else {
                request.setMethod(org.restlet.data.Method.POST);
                response.setStatus(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY);
                result(formForMethod(interactionMethod));
            }
        } catch (Exception e) {
            handleException(response, e);
        }
    } else {
        // Check timestamps
        ResourceValidity validity = (ResourceValidity) request.getAttributes().get(RESOURCE_VALIDITY);
        if (validity != null) {
            validity.checkRequest();
        }
        // Check method constraints
        if (!constraints.isValid(interactionMethod, current(), module)) {
            throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND);
        }
        // Create argument
        Object[] arguments = requestReader.readRequest(Request.getCurrent(), interactionMethod);
        Request.getCurrent().getAttributes().put(ARGUMENTS, arguments);
        // Invoke method
        try {
            Object result = interactionMethod.invoke(this, arguments);
            if (result != null) {
                if (result instanceof Representation) {
                    response.setEntity((Representation) result);
                } else {
                    result(convert(result));
                }
            }
        } catch (Throwable e) {
            handleException(response, e);
        }
    }
}
Also used : Response(org.restlet.Response) Request(org.restlet.Request) ResourceException(org.restlet.resource.ResourceException) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) StringRepresentation(org.restlet.representation.StringRepresentation) Representation(org.restlet.representation.Representation) Method(java.lang.reflect.Method) EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) ResourceException(org.restlet.resource.ResourceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchEntityException(org.qi4j.api.unitofwork.NoSuchEntityException) ConstraintViolationException(org.qi4j.api.constraint.ConstraintViolationException)

Example 55 with Response

use of org.restlet.Response in project qi4j-sdk by Qi4j.

the class ContextResourceClientFactoryTest method testQueryListAndCommandProgressive.

@Test
public void testQueryListAndCommandProgressive() {
    //START SNIPPET: query-list-and-command-progressive
    crc.onResource(new ResultHandler<Resource>() {

        @Override
        public HandlerCommand handleResult(Resource result, ContextResourceClient client) {
            return query("commandwithvalue").onSuccess(new ResultHandler<Links>() {

                @Override
                public HandlerCommand handleResult(Links result, ContextResourceClient client) {
                    Link link = LinksUtil.withId("right", result);
                    return command(link).onSuccess(new ResponseHandler() {

                        @Override
                        public HandlerCommand handleResponse(Response response, ContextResourceClient client) {
                            System.out.println("Done");
                            return null;
                        }
                    });
                }
            });
        }
    });
    crc.start();
//END SNIPPET: query-list-and-command-progressive
}
Also used : HandlerCommand(org.qi4j.library.rest.client.api.HandlerCommand) Response(org.restlet.Response) ResponseHandler(org.qi4j.library.rest.client.spi.ResponseHandler) SubResource(org.qi4j.library.rest.server.api.SubResource) ContextResource(org.qi4j.library.rest.server.api.ContextResource) Resource(org.qi4j.library.rest.common.Resource) Links(org.qi4j.library.rest.common.link.Links) ResultHandler(org.qi4j.library.rest.client.spi.ResultHandler) ContextResourceClient(org.qi4j.library.rest.client.api.ContextResourceClient) Link(org.qi4j.library.rest.common.link.Link) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Aggregations

Response (org.restlet.Response)62 Request (org.restlet.Request)44 Test (org.testng.annotations.Test)26 Status (org.restlet.data.Status)17 ChallengeResponse (org.restlet.data.ChallengeResponse)15 OAuth2Request (org.forgerock.oauth2.core.OAuth2Request)12 ResponseHandler (org.qi4j.library.rest.client.spi.ResponseHandler)12 Map (java.util.Map)10 Test (org.junit.Test)10 Client (org.restlet.Client)9 Representation (org.restlet.representation.Representation)9 Reference (org.restlet.data.Reference)8 ContextResourceClient (org.qi4j.library.rest.client.api.ContextResourceClient)7 HandlerCommand (org.qi4j.library.rest.client.api.HandlerCommand)7 JacksonRepresentation (org.restlet.ext.jackson.JacksonRepresentation)7 AccessToken (org.forgerock.oauth2.core.AccessToken)6 ResourceException (org.restlet.resource.ResourceException)6 Resource (org.qi4j.library.rest.common.Resource)5 Link (org.qi4j.library.rest.common.link.Link)5 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)5