Search in sources :

Example 36 with Request

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

the class RestletRouteBuilderTest method testUnhandledConsumer.

@Test
public void testUnhandledConsumer() throws IOException {
    Client client = new Client(Protocol.HTTP);
    Response response = client.handle(new Request(Method.POST, "http://localhost:" + portNum + "/orders/99991/6"));
    // expect error status as no Restlet consumer to handle POST method
    assertEquals(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED, 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 37 with Request

use of org.restlet.Request in project xwiki-platform by xwiki.

the class AnnotationsRESTResource method doGetAnnotatedContent.

/**
 * @param wiki the wiki of the document to get annotations for
 * @param space the space of the document to get annotations for
 * @param page the name of the document to get annotation for
 * @return annotations of a given XWiki page. Note that we're returning a response holding the AnnotatedContent
 *         instead of an AnnotatedContent object because we need to be able to set custom expire fields to prevent
 *         IE from caching this resource.
 * @throws XWikiRestException when failing to parse space
 */
@GET
public Response doGetAnnotatedContent(@PathParam("spaceName") String space, @PathParam("pageName") String page, @PathParam("wikiName") String wiki) throws XWikiRestException {
    try {
        DocumentReference documentReference = new DocumentReference(wiki, parseSpaceSegments(space), page);
        // Initialize the context with the correct value.
        updateContext(documentReference);
        String documentName = referenceSerializer.serialize(documentReference);
        // check access to this function
        if (!annotationRightService.canViewAnnotatedTarget(documentName, getXWikiUser())) {
            throw new WebApplicationException(Status.UNAUTHORIZED);
        }
        // Manually construct the Annotation request entity
        // Historically it used to be passed as method argument, but this only worked because of a bug in
        // earlier version of Restlet (1.1.x) ; the JAX-RS specification explicitly states that such entity
        // parameters "[are] mapped from the request entity body.", and thus cannot be passed to a GET resource.
        // See paragraph 3.3.2.1 "Entity Parameters" of JAX-RS 1.1 specification.
        Form form = Request.getCurrent().getResourceRef().getQueryAsForm();
        AnnotationRequest request = new AnnotationRequest();
        AnnotationFieldCollection fields = new AnnotationFieldCollection();
        List<AnnotationField> annotationFields = new ArrayList<AnnotationField>();
        AnnotationRequest.Request requestedFields = new AnnotationRequest.Request();
        for (String name : form.getNames()) {
            if (StringUtils.startsWith(name, ANNOTATION_REQUEST_FILTER_PARAMETER_PREFIX)) {
                for (String value : form.getValuesArray(name)) {
                    AnnotationField field = new AnnotationField();
                    field.setName(StringUtils.substringAfter(name, ANNOTATION_REQUEST_FILTER_PARAMETER_PREFIX));
                    field.setValue(value);
                    annotationFields.add(field);
                }
            } else if (StringUtils.equals(name, ANNOTATION_REQUEST_REQUESTED_FIELD_PARAMETER)) {
                requestedFields.getFields().addAll(Arrays.asList(form.getValuesArray(name)));
            }
        }
        request.setRequest(requestedFields);
        fields.getFields().addAll(annotationFields);
        request.setFilter(fields);
        AnnotationResponse response = getSuccessResponseWithAnnotatedContent(documentName, request);
        // make this content expire now because cacheControl is not implemented in this version of restlet
        return Response.ok(response).expires(new Date()).build();
    } catch (AnnotationServiceException e) {
        getLogger().error(e.getMessage(), e);
        return Response.ok(getErrorResponse(e)).build();
    } catch (XWikiException e) {
        getLogger().error(e.getMessage(), e);
        return Response.ok(getErrorResponse(e)).build();
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) AnnotationRequest(org.xwiki.annotation.rest.model.jaxb.AnnotationRequest) AnnotationServiceException(org.xwiki.annotation.AnnotationServiceException) Form(org.restlet.data.Form) ArrayList(java.util.ArrayList) AnnotationAddRequest(org.xwiki.annotation.rest.model.jaxb.AnnotationAddRequest) AnnotationRequest(org.xwiki.annotation.rest.model.jaxb.AnnotationRequest) Request(org.restlet.Request) AnnotationResponse(org.xwiki.annotation.rest.model.jaxb.AnnotationResponse) Date(java.util.Date) AnnotationField(org.xwiki.annotation.rest.model.jaxb.AnnotationField) AnnotationFieldCollection(org.xwiki.annotation.rest.model.jaxb.AnnotationFieldCollection) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) GET(javax.ws.rs.GET)

Example 38 with Request

use of org.restlet.Request in project helix by apache.

the class AdminTestHelper method delete.

public static void delete(Client client, String url) throws IOException {
    Reference resourceRef = new Reference(url);
    Request request = new Request(Method.DELETE, resourceRef);
    Response response = client.handle(request);
    Assert.assertEquals(response.getStatus(), Status.SUCCESS_NO_CONTENT);
}
Also used : Response(org.restlet.Response) Reference(org.restlet.data.Reference) Request(org.restlet.Request)

Example 39 with Request

use of org.restlet.Request in project helix by apache.

the class TestClusterManagementWebapp method postConfig.

private void postConfig(Client client, String url, ObjectMapper mapper, String command, String configs) throws Exception {
    Map<String, String> params = new HashMap<String, String>();
    params.put(JsonParameters.MANAGEMENT_COMMAND, command);
    params.put(JsonParameters.CONFIGS, configs);
    Request request = new Request(Method.POST, new Reference(url));
    request.setEntity(JsonParameters.JSON_PARAMETERS + "=" + ClusterRepresentationUtil.ObjectToJson(params), MediaType.APPLICATION_ALL);
    Response response = client.handle(request);
    Representation result = response.getEntity();
    StringWriter sw = new StringWriter();
    result.write(sw);
    String responseStr = sw.toString();
    Assert.assertTrue(responseStr.toLowerCase().indexOf("error") == -1);
    Assert.assertTrue(responseStr.toLowerCase().indexOf("exception") == -1);
}
Also used : Response(org.restlet.Response) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) Reference(org.restlet.data.Reference) TypeReference(org.codehaus.jackson.type.TypeReference) Request(org.restlet.Request) Representation(org.restlet.representation.Representation)

Example 40 with Request

use of org.restlet.Request in project helix by apache.

the class TestClusterManagementWebapp method verifyEnableInstance.

void verifyEnableInstance() throws JsonGenerationException, JsonMappingException, IOException {
    String httpUrlBase = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/instances/" + instance1 + "_" + instancePort;
    Map<String, String> paraMap = new HashMap<String, String>();
    // Add 1 instance
    paraMap.put(JsonParameters.ENABLED, "" + false);
    paraMap.put(JsonParameters.MANAGEMENT_COMMAND, ClusterSetup.enableInstance);
    Reference resourceRef = new Reference(httpUrlBase);
    Request request = new Request(Method.POST, resourceRef);
    request.setEntity(JsonParameters.JSON_PARAMETERS + "=" + ClusterRepresentationUtil.ObjectToJson(paraMap), MediaType.APPLICATION_ALL);
    Response response = _gClient.handle(request);
    Representation result = response.getEntity();
    StringWriter sw = new StringWriter();
    result.write(sw);
    // System.out.println(sw.toString());
    ObjectMapper mapper = new ObjectMapper();
    ZNRecord r = mapper.readValue(new StringReader(sw.toString()), ZNRecord.class);
    AssertJUnit.assertTrue(r.getSimpleField(InstanceConfigProperty.HELIX_ENABLED.toString()).equals("" + false));
    // Then enable it
    paraMap.put(JsonParameters.ENABLED, "" + true);
    request = new Request(Method.POST, resourceRef);
    request.setEntity(JsonParameters.JSON_PARAMETERS + "=" + ClusterRepresentationUtil.ObjectToJson(paraMap), MediaType.APPLICATION_ALL);
    response = _gClient.handle(request);
    result = response.getEntity();
    sw = new StringWriter();
    result.write(sw);
    // System.out.println(sw.toString());
    mapper = new ObjectMapper();
    r = mapper.readValue(new StringReader(sw.toString()), ZNRecord.class);
    AssertJUnit.assertTrue(r.getSimpleField(InstanceConfigProperty.HELIX_ENABLED.toString()).equals("" + true));
}
Also used : Response(org.restlet.Response) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) Reference(org.restlet.data.Reference) TypeReference(org.codehaus.jackson.type.TypeReference) Request(org.restlet.Request) StringReader(java.io.StringReader) Representation(org.restlet.representation.Representation) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) ZNRecord(org.apache.helix.ZNRecord)

Aggregations

Request (org.restlet.Request)100 Response (org.restlet.Response)64 Test (org.testng.annotations.Test)38 OAuth2Request (org.forgerock.oauth2.core.OAuth2Request)37 Reference (org.restlet.data.Reference)26 Representation (org.restlet.representation.Representation)24 ChallengeResponse (org.restlet.data.ChallengeResponse)18 StringWriter (java.io.StringWriter)15 Status (org.restlet.data.Status)15 HttpServletRequest (javax.servlet.http.HttpServletRequest)13 HashMap (java.util.HashMap)12 ZNRecord (org.apache.helix.ZNRecord)11 AccessToken (org.forgerock.oauth2.core.AccessToken)11 AccessTokenVerifier (org.forgerock.oauth2.core.AccessTokenVerifier)11 StringReader (java.io.StringReader)10 TypeReference (org.codehaus.jackson.type.TypeReference)10 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)9 Form (org.restlet.data.Form)9 OAuth2ProviderSettings (org.forgerock.oauth2.core.OAuth2ProviderSettings)8 BeforeMethod (org.testng.annotations.BeforeMethod)8