Search in sources :

Example 1 with ApiTask

use of org.opengrok.web.api.ApiTask in project OpenGrok by OpenGrok.

the class ProjectsController method deleteProject.

@DELETE
@Path("/{project}")
public Response deleteProject(@Context HttpServletRequest request, @PathParam("project") String projectNameParam) {
    // Avoid classification as a taint bug.
    final String projectName = Laundromat.launderInput(projectNameParam);
    Project project = disableProject(projectName);
    LOGGER.log(Level.INFO, "deleting configuration for project {0}", projectName);
    return ApiTaskManager.getInstance().submitApiTask(PROJECTS_PATH, new ApiTask(request.getRequestURI(), () -> {
        deleteProjectWorkHorse(projectName, project);
        return null;
    }, Response.Status.NO_CONTENT));
}
Also used : Project(org.opengrok.indexer.configuration.Project) ApiTask(org.opengrok.web.api.ApiTask) Path(jakarta.ws.rs.Path) DELETE(jakarta.ws.rs.DELETE)

Example 2 with ApiTask

use of org.opengrok.web.api.ApiTask in project OpenGrok by OpenGrok.

the class ProjectsController method deleteProjectData.

@DELETE
@Path("/{project}/data")
public Response deleteProjectData(@Context HttpServletRequest request, @PathParam("project") String projectNameParam) {
    // Avoid classification as a taint bug.
    final String projectName = Laundromat.launderInput(projectNameParam);
    disableProject(projectName);
    return ApiTaskManager.getInstance().submitApiTask(PROJECTS_PATH, new ApiTask(request.getRequestURI(), () -> {
        deleteProjectDataWorkHorse(projectName, false);
        return null;
    }, Response.Status.NO_CONTENT));
}
Also used : ApiTask(org.opengrok.web.api.ApiTask) Path(jakarta.ws.rs.Path) DELETE(jakarta.ws.rs.DELETE)

Example 3 with ApiTask

use of org.opengrok.web.api.ApiTask in project OpenGrok by OpenGrok.

the class ApiUtils method waitForTask.

/**
 * Busy-waits the status of asynchronous API call, mimicking
 * {@link org.opengrok.indexer.web.ApiUtils#waitForAsyncApi(Response)},
 * however side-steps status API check by going to the {@link ApiTaskManager} directly in order to avoid
 * going through the {@link StatusController} as it might not be deployed in the unit tests.
 * The method will return right away if the status of the response object parameter is not
 * {@code Response.Status.ACCEPTED}.
 * @param response API Response object
 * @return the response object
 */
protected static Response waitForTask(Response response) {
    if (response.getStatus() != Response.Status.ACCEPTED.getStatusCode()) {
        return response;
    }
    String location = response.getHeaderString(HttpHeaders.LOCATION);
    String locationUri = URI.create(location).getPath();
    final String apiPrefix = "api/v1/status/";
    assertTrue(locationUri.contains(apiPrefix));
    int idx = locationUri.indexOf(apiPrefix);
    assertTrue(idx > 0);
    String uuid = locationUri.substring(idx + apiPrefix.length());
    ApiTask apiTask = ApiTaskManager.getInstance().getApiTask(uuid);
    assertNotNull(apiTask);
    await().atMost(16, TimeUnit.SECONDS).until(apiTask::isDone);
    if (!apiTask.isDone()) {
        return response;
    } else {
        return apiTask.getResponse();
    }
}
Also used : ApiTask(org.opengrok.web.api.ApiTask)

Example 4 with ApiTask

use of org.opengrok.web.api.ApiTask in project OpenGrok by OpenGrok.

the class StatusControllerTest method testGet.

@Test
void testGet() throws InterruptedException {
    int sleepTime = 3000;
    ApiTask apiTask = new ApiTask("foo", () -> {
        try {
            Thread.sleep(sleepTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }, Response.Status.CREATED);
    String uuidString = apiTask.getUuid().toString();
    ApiTaskManager apiTaskManager = ApiTaskManager.getInstance();
    String poolName = "foo";
    apiTaskManager.addPool(poolName, 1);
    apiTaskManager.submitApiTask(poolName, apiTask);
    Response response = target(StatusController.PATH).path(uuidString).request().get();
    assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatus());
    Thread.sleep(sleepTime);
    response = target(StatusController.PATH).path(uuidString).request().get();
    assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus());
}
Also used : Response(jakarta.ws.rs.core.Response) ApiTaskManager(org.opengrok.web.api.ApiTaskManager) ApiTask(org.opengrok.web.api.ApiTask) Test(org.junit.jupiter.api.Test)

Example 5 with ApiTask

use of org.opengrok.web.api.ApiTask in project OpenGrok by OpenGrok.

the class StatusControllerTest method testDelete.

@Test
void testDelete() throws InterruptedException {
    ApiTask apiTask = new ApiTask("foo", this::doNothing);
    String uuidString = apiTask.getUuid().toString();
    ApiTaskManager apiTaskManager = ApiTaskManager.getInstance();
    String poolName = "deleteCompleted";
    apiTaskManager.addPool(poolName, 1);
    apiTaskManager.submitApiTask(poolName, apiTask);
    Thread.sleep(1000);
    Response response = target(StatusController.PATH).path(uuidString).request().delete();
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
}
Also used : Response(jakarta.ws.rs.core.Response) ApiTaskManager(org.opengrok.web.api.ApiTaskManager) ApiTask(org.opengrok.web.api.ApiTask) Test(org.junit.jupiter.api.Test)

Aggregations

ApiTask (org.opengrok.web.api.ApiTask)9 Path (jakarta.ws.rs.Path)4 DELETE (jakarta.ws.rs.DELETE)3 Response (jakarta.ws.rs.core.Response)3 Test (org.junit.jupiter.api.Test)3 ApiTaskManager (org.opengrok.web.api.ApiTaskManager)3 Consumes (jakarta.ws.rs.Consumes)2 Project (org.opengrok.indexer.configuration.Project)2 NotFoundException (jakarta.ws.rs.NotFoundException)1 POST (jakarta.ws.rs.POST)1 PUT (jakarta.ws.rs.PUT)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Repository (org.opengrok.indexer.history.Repository)1 RepositoryFactory.getRepository (org.opengrok.indexer.history.RepositoryFactory.getRepository)1 RepositoryInfo (org.opengrok.indexer.history.RepositoryInfo)1