Search in sources :

Example 71 with Request

use of org.restlet.Request 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 72 with Request

use of org.restlet.Request 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 73 with Request

use of org.restlet.Request in project lucene-solr by apache.

the class TestRestManager method testResolveResourceIdDecodeUrlEntities.

@Test
public void testResolveResourceIdDecodeUrlEntities() throws Exception {
    Request testRequest = new Request();
    Reference rootRef = new Reference("http://solr.apache.org/");
    testRequest.setRootRef(rootRef);
    Reference resourceRef = new Reference("http://solr.apache.org/schema/analysis/synonyms/de/%C3%84ndern");
    testRequest.setResourceRef(resourceRef);
    String resourceId = RestManager.ManagedEndpoint.resolveResourceId(testRequest);
    assertEquals(resourceId, "/schema/analysis/synonyms/de/Ă„ndern");
}
Also used : Reference(org.restlet.data.Reference) Request(org.restlet.Request) Test(org.junit.Test)

Example 74 with Request

use of org.restlet.Request 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 75 with Request

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

the class ContextResource method resource.

private void resource() {
    Request request = Request.getCurrent();
    Response response = Response.getCurrent();
    if (!request.getMethod().equals(org.restlet.data.Method.GET)) {
        response.setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
        return;
    }
    ObjectSelection objectSelection = current();
    // Check for interaction->method mappings
    if (ResourceDelete.class.isAssignableFrom(getClass())) {
        response.getAllowedMethods().add(org.restlet.data.Method.DELETE);
    }
    if (ResourceUpdate.class.isAssignableFrom(getClass())) {
        response.getAllowedMethods().add(org.restlet.data.Method.PUT);
    }
    // Construct resource
    ValueBuilder<Resource> builder = module.newValueBuilder(Resource.class);
    List<Link> queriesProperty = builder.prototype().queries().get();
    for (Method query : resourceQueries) {
        if (constraints.isValid(query, objectSelection, module)) {
            ValueBuilder<Link> linkBuilder = module.newValueBuilder(Link.class);
            Link prototype = linkBuilder.prototype();
            prototype.classes().set("query");
            prototype.text().set(humanReadable(query.getName()));
            prototype.href().set(query.getName().toLowerCase());
            prototype.rel().set(query.getName().toLowerCase());
            prototype.id().set(query.getName().toLowerCase());
            queriesProperty.add(linkBuilder.newInstance());
        }
    }
    List<Link> commandsProperty = builder.prototype().commands().get();
    for (Method command : resourceCommands) {
        if (constraints.isValid(command, objectSelection, module)) {
            ValueBuilder<Link> linkBuilder = module.newValueBuilder(Link.class);
            Link prototype = linkBuilder.prototype();
            prototype.classes().set("command");
            prototype.text().set(humanReadable(command.getName()));
            prototype.href().set(command.getName().toLowerCase());
            prototype.rel().set(command.getName().toLowerCase());
            prototype.id().set(command.getName().toLowerCase());
            commandsProperty.add(linkBuilder.newInstance());
        }
    }
    List<Link> resourcesProperty = builder.prototype().resources().get();
    for (Method subResource : subResources.values()) {
        if (constraints.isValid(subResource, objectSelection, module)) {
            ValueBuilder<Link> linkBuilder = module.newValueBuilder(Link.class);
            Link prototype = linkBuilder.prototype();
            prototype.classes().set("resource");
            prototype.text().set(humanReadable(subResource.getName()));
            prototype.href().set(subResource.getName().toLowerCase() + "/");
            prototype.rel().set(subResource.getName().toLowerCase());
            prototype.id().set(subResource.getName().toLowerCase());
            resourcesProperty.add(linkBuilder.newInstance());
        }
    }
    try {
        Method indexMethod = resourceMethodQueries.get("index");
        if (indexMethod != null) {
            Object index = convert(indexMethod.invoke(this));
            if (index != null && index instanceof ValueComposite) {
                builder.prototype().index().set((ValueComposite) index);
            }
        }
    } catch (Throwable e) {
    // Ignore
    }
    try {
        responseWriter.writeResponse(builder.newInstance(), response);
    } catch (Throwable e) {
        handleException(response, e);
    }
}
Also used : Response(org.restlet.Response) Request(org.restlet.Request) Resource(org.qi4j.library.rest.common.Resource) Method(java.lang.reflect.Method) ValueComposite(org.qi4j.api.value.ValueComposite) Link(org.qi4j.library.rest.common.link.Link)

Aggregations

Request (org.restlet.Request)79 Response (org.restlet.Response)44 OAuth2Request (org.forgerock.oauth2.core.OAuth2Request)37 Test (org.testng.annotations.Test)36 ChallengeResponse (org.restlet.data.ChallengeResponse)18 Status (org.restlet.data.Status)15 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 AccessToken (org.forgerock.oauth2.core.AccessToken)11 AccessTokenVerifier (org.forgerock.oauth2.core.AccessTokenVerifier)11 HttpRequest (org.restlet.engine.adapter.HttpRequest)9 Representation (org.restlet.representation.Representation)9 OAuth2ProviderSettings (org.forgerock.oauth2.core.OAuth2ProviderSettings)8 Form (org.restlet.data.Form)8 Reference (org.restlet.data.Reference)8 BeforeMethod (org.testng.annotations.BeforeMethod)8 Map (java.util.Map)7 OAuth2ProviderSettingsFactory (org.forgerock.oauth2.core.OAuth2ProviderSettingsFactory)6 Test (org.junit.Test)6 Client (org.restlet.Client)6 URI (java.net.URI)4