Search in sources :

Example 91 with Request

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

the class TestHelixAdminScenariosRest method testGetResources.

@Test
public void testGetResources() throws IOException {
    final String clusterName = "TestTagAwareness_testGetResources";
    final String TAG = "tag";
    final String URL_BASE = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/resourceGroups";
    _gSetupTool.addCluster(clusterName, true);
    HelixAdmin admin = _gSetupTool.getClusterManagementTool();
    // Add a tagged resource
    IdealState taggedResource = new IdealState("taggedResource");
    taggedResource.setInstanceGroupTag(TAG);
    taggedResource.setStateModelDefRef("OnlineOffline");
    admin.addResource(clusterName, taggedResource.getId(), taggedResource);
    // Add an untagged resource
    IdealState untaggedResource = new IdealState("untaggedResource");
    untaggedResource.setStateModelDefRef("OnlineOffline");
    admin.addResource(clusterName, untaggedResource.getId(), untaggedResource);
    // Now make a REST call for all resources
    Reference resourceRef = new Reference(URL_BASE);
    Request request = new Request(Method.GET, resourceRef);
    Response response = _gClient.handle(request);
    ZNRecord responseRecord = ClusterRepresentationUtil.JsonToObject(ZNRecord.class, response.getEntityAsText());
    // Ensure that the tagged resource has information and the untagged one doesn't
    Assert.assertNotNull(responseRecord.getMapField("ResourceTags"));
    Assert.assertEquals(TAG, responseRecord.getMapField("ResourceTags").get(taggedResource.getId()));
    Assert.assertFalse(responseRecord.getMapField("ResourceTags").containsKey(untaggedResource.getId()));
}
Also used : Response(org.restlet.Response) Reference(org.restlet.data.Reference) Request(org.restlet.Request) HelixAdmin(org.apache.helix.HelixAdmin) IdealState(org.apache.helix.model.IdealState) ZNRecord(org.apache.helix.ZNRecord) Test(org.testng.annotations.Test)

Example 92 with Request

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

the class TestHelixAdminScenariosRest method getUrl.

String getUrl(String url) throws IOException {
    Reference resourceRef = new Reference(url);
    Request request = new Request(Method.GET, resourceRef);
    Response response = _gClient.handle(request);
    Representation result = response.getEntity();
    StringWriter sw = new StringWriter();
    result.write(sw);
    return sw.toString();
}
Also used : Response(org.restlet.Response) StringWriter(java.io.StringWriter) Reference(org.restlet.data.Reference) Request(org.restlet.Request) Representation(org.restlet.representation.Representation)

Example 93 with Request

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

the class TestHelixAdminScenariosRest method assertSuccessPostOperation.

static String assertSuccessPostOperation(String url, Map<String, String> jsonParameters, Map<String, String> extraForm, boolean hasException) throws IOException {
    Reference resourceRef = new Reference(url);
    int numRetries = 0;
    while (numRetries <= MAX_RETRIES) {
        Request request = new Request(Method.POST, resourceRef);
        if (extraForm != null) {
            String entity = JsonParameters.JSON_PARAMETERS + "=" + ClusterRepresentationUtil.ObjectToJson(jsonParameters);
            for (String key : extraForm.keySet()) {
                entity = entity + "&" + (key + "=" + extraForm.get(key));
            }
            request.setEntity(entity, MediaType.APPLICATION_ALL);
        } else {
            request.setEntity(JsonParameters.JSON_PARAMETERS + "=" + ClusterRepresentationUtil.ObjectToJson(jsonParameters), MediaType.APPLICATION_ALL);
        }
        Response response = _gClient.handle(request);
        Representation result = response.getEntity();
        StringWriter sw = new StringWriter();
        if (result != null) {
            result.write(sw);
        }
        int code = response.getStatus().getCode();
        boolean successCode = code == Status.SUCCESS_NO_CONTENT.getCode() || code == Status.SUCCESS_OK.getCode();
        if (successCode || numRetries == MAX_RETRIES) {
            Assert.assertTrue(successCode);
            Assert.assertTrue(hasException == sw.toString().toLowerCase().contains("exception"));
            return sw.toString();
        }
        numRetries++;
    }
    Assert.fail("Request failed after all retries");
    return null;
}
Also used : Response(org.restlet.Response) StringWriter(java.io.StringWriter) Reference(org.restlet.data.Reference) Request(org.restlet.Request) Representation(org.restlet.representation.Representation)

Example 94 with Request

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

the class RestAdminApplication method createInboundRoot.

@Override
public Restlet createInboundRoot() {
    Router router = new Router(getContext());
    router.setDefaultMatchingMode(Template.MODE_EQUALS);
    router.attach("/clusters", ClustersResource.class);
    router.attach("/clusters/{clusterName}", ClusterResource.class);
    router.attach("/clusters/{clusterName}/resourceGroups", ResourceGroupsResource.class);
    router.attach("/clusters/{clusterName}/resourceGroups/{resourceName}", ResourceGroupResource.class);
    router.attach("/clusters/{clusterName}/workflows", WorkflowsResource.class);
    router.attach("/clusters/{clusterName}/jobQueues", JobQueuesResource.class);
    router.attach("/clusters/{clusterName}/jobQueues/{jobQueue}", JobQueueResource.class);
    router.attach("/clusters/{clusterName}/jobQueues/{jobQueue}/{job}", JobResource.class);
    router.attach("/clusters/{clusterName}/instances", InstancesResource.class);
    router.attach("/clusters/{clusterName}/instances/{instanceName}", InstanceResource.class);
    router.attach("/clusters/{clusterName}/instances/{instanceName}/currentState/{resourceName}", CurrentStateResource.class);
    router.attach("/clusters/{clusterName}/instances/{instanceName}/statusUpdate/{resourceName}", StatusUpdateResource.class);
    router.attach("/clusters/{clusterName}/instances/{instanceName}/errors/{resourceName}", ErrorResource.class);
    router.attach("/clusters/{clusterName}/instances/{instanceName}/currentState", CurrentStatesResource.class);
    router.attach("/clusters/{clusterName}/instances/{instanceName}/statusUpdate", StatusUpdatesResource.class);
    router.attach("/clusters/{clusterName}/instances/{instanceName}/errors", ErrorsResource.class);
    router.attach("/clusters/{clusterName}/resourceGroups/{resourceName}/idealState", IdealStateResource.class);
    router.attach("/clusters/{clusterName}/resourceGroups/{resourceName}/externalView", ExternalViewResource.class);
    router.attach("/clusters/{clusterName}/StateModelDefs/{modelName}", StateModelResource.class);
    router.attach("/clusters/{clusterName}/StateModelDefs", StateModelsResource.class);
    router.attach("/clusters/{clusterName}/SchedulerTasks", SchedulerTasksResource.class);
    router.attach("/clusters/{clusterName}/Controller", ControllerResource.class);
    router.attach("/clusters/{clusterName}/Controller/statusUpdates/{MessageType}/{MessageId}", ControllerStatusUpdateResource.class);
    router.attach("/clusters/{clusterName}/configs", ConfigResource.class);
    router.attach("/clusters/{clusterName}/configs/{scope}", ConfigResource.class);
    router.attach("/clusters/{clusterName}/configs/{scope}/{scopeKey1}", ConfigResource.class);
    router.attach("/clusters/{clusterName}/configs/{scope}/{scopeKey1}/{scopeKey2}", ConfigResource.class);
    router.attach("/clusters/{clusterName}/constraints/{constraintType}", ConstraintResource.class);
    router.attach("/clusters/{clusterName}/constraints/{constraintType}/{constraintId}", ConstraintResource.class);
    router.attach("/zkPath", ZkPathResource.class).setMatchingMode(Template.MODE_STARTS_WITH);
    router.attach("/zkChild", ZkChildResource.class).setMatchingMode(Template.MODE_STARTS_WITH);
    Restlet mainpage = new Restlet() {

        @Override
        public void handle(Request request, Response response) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("<html>");
            stringBuilder.append("<head><title>Restlet Cluster Management page</title></head>");
            stringBuilder.append("<body bgcolor=white>");
            stringBuilder.append("<table border=\"0\">");
            stringBuilder.append("<tr>");
            stringBuilder.append("<td>");
            stringBuilder.append("<h1>Rest cluster management interface V1</h1>");
            stringBuilder.append("</td>");
            stringBuilder.append("</tr>");
            stringBuilder.append("</table>");
            stringBuilder.append("</body>");
            stringBuilder.append("</html>");
            response.setEntity(new StringRepresentation(stringBuilder.toString(), MediaType.TEXT_HTML));
        }
    };
    router.attach("", mainpage);
    return router;
}
Also used : Response(org.restlet.Response) Restlet(org.restlet.Restlet) StringRepresentation(org.restlet.representation.StringRepresentation) ZkPathResource(org.apache.helix.webapp.resources.ZkPathResource) Request(org.restlet.Request) Router(org.restlet.routing.Router) ZkChildResource(org.apache.helix.webapp.resources.ZkChildResource)

Example 95 with Request

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

the class ContextResourceClient method invokeCommand.

private HandlerCommand invokeCommand(Reference ref, Method method, Object requestObject, ResponseHandler responseHandler, ResponseHandler processingErrorHandler) {
    Request request = new Request(method, ref);
    if (requestObject == null)
        requestObject = new EmptyRepresentation();
    contextResourceFactory.writeRequest(request, requestObject);
    contextResourceFactory.updateCommandRequest(request);
    User user = request.getClientInfo().getUser();
    if (user != null)
        request.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_BASIC, user.getName(), user.getSecret()));
    Response response = new Response(request);
    contextResourceFactory.getClient().handle(request, response);
    try {
        if (response.getStatus().isSuccess()) {
            contextResourceFactory.updateCache(response);
            if (responseHandler != null)
                return responseHandler.handleResponse(response, this);
        } else {
            if (response.getStatus().equals(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY) && processingErrorHandler != null) {
                return processingErrorHandler.handleResponse(response, this);
            } else {
                // TODO This needs to be expanded to allow custom handling of all the various cases
                return errorHandler.handleResponse(response, this);
            }
        }
        // No handler found
        return null;
    } finally {
        try {
            response.getEntity().exhaust();
        } catch (Throwable e) {
        // Ignore
        }
    }
}
Also used : ChallengeResponse(org.restlet.data.ChallengeResponse) Response(org.restlet.Response) User(org.restlet.security.User) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) Request(org.restlet.Request) ChallengeResponse(org.restlet.data.ChallengeResponse)

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