Search in sources :

Example 1 with TaskListModel

use of org.glassfish.jersey.examples.oauth2.googleclient.model.TaskListModel in project jersey by jersey.

the class TaskResource method getTasksResponse.

/**
     * Queries task data from google.
     *
     * @param client Client configured for authentication with access token.
     * @return Google task data response or redirect to google authorize page response.
     */
private Response getTasksResponse(final Client client) {
    client.register(JacksonFeature.class);
    client.register(new LoggingFeature(Logger.getLogger("example.client.tasks"), LoggingFeature.Verbosity.PAYLOAD_ANY));
    final WebTarget baseTarget = client.target(GOOGLE_TASKS_BASE_URI);
    final Response response = baseTarget.path("users/@me/lists").request().get();
    final List<TaskListModel> listOfTaskLists;
    switch(response.getStatus()) {
        case //Response.Status.UNAUTHORIZED
        401:
            SimpleOAuthService.setAccessToken(null);
            return googleAuthRedirect();
        case //Response.Status.OK
        200:
            listOfTaskLists = processTaskLists(baseTarget, response.readEntity(TaskRootBean.class));
            break;
        default:
            listOfTaskLists = null;
    }
    final AllTaskListsModel tasks = new AllTaskListsModel(listOfTaskLists);
    return Response.ok(tasks).build();
}
Also used : Response(javax.ws.rs.core.Response) LoggingFeature(org.glassfish.jersey.logging.LoggingFeature) AllTaskListsModel(org.glassfish.jersey.examples.oauth2.googleclient.model.AllTaskListsModel) WebTarget(javax.ws.rs.client.WebTarget) TaskListModel(org.glassfish.jersey.examples.oauth2.googleclient.model.TaskListModel)

Example 2 with TaskListModel

use of org.glassfish.jersey.examples.oauth2.googleclient.model.TaskListModel in project jersey by jersey.

the class TaskResource method processTaskLists.

/**
     * Process users task lists and read task details. Collect just
     * @param baseTarget base JAX-RS client target with oauth context configured
     * @param taskRootBean root task bean to be processed
     * @return Detailed list of non-completed tasks or {@code null} if there is no task list available.
     */
private List<TaskListModel> processTaskLists(final WebTarget baseTarget, final TaskRootBean taskRootBean) {
    final List<TaskListModel> listOfTaskLists = new ArrayList<>();
    for (final TaskListBean taskListBean : taskRootBean.getItems()) {
        final List<TaskModel> taskList = new ArrayList<>();
        final WebTarget listTarget = baseTarget.path("lists/{tasklist}/tasks").resolveTemplate("tasklist", taskListBean.getId());
        final TaskListBean fullTaskListBean = listTarget.request().get(TaskListBean.class);
        for (final TaskBean taskBean : fullTaskListBean.getTasks()) {
            if (taskBean.getCompleted() == null) {
                taskList.add(new TaskModel(taskBean.getTitle()));
            }
        }
        listOfTaskLists.add(new TaskListModel(taskListBean.getTitle(), taskList.size() > 0 ? taskList : null));
    }
    return listOfTaskLists.size() > 0 ? listOfTaskLists : null;
}
Also used : TaskListBean(org.glassfish.jersey.examples.oauth2.googleclient.entity.TaskListBean) ArrayList(java.util.ArrayList) TaskBean(org.glassfish.jersey.examples.oauth2.googleclient.entity.TaskBean) WebTarget(javax.ws.rs.client.WebTarget) TaskListModel(org.glassfish.jersey.examples.oauth2.googleclient.model.TaskListModel) TaskModel(org.glassfish.jersey.examples.oauth2.googleclient.model.TaskModel)

Aggregations

WebTarget (javax.ws.rs.client.WebTarget)2 TaskListModel (org.glassfish.jersey.examples.oauth2.googleclient.model.TaskListModel)2 ArrayList (java.util.ArrayList)1 Response (javax.ws.rs.core.Response)1 TaskBean (org.glassfish.jersey.examples.oauth2.googleclient.entity.TaskBean)1 TaskListBean (org.glassfish.jersey.examples.oauth2.googleclient.entity.TaskListBean)1 AllTaskListsModel (org.glassfish.jersey.examples.oauth2.googleclient.model.AllTaskListsModel)1 TaskModel (org.glassfish.jersey.examples.oauth2.googleclient.model.TaskModel)1 LoggingFeature (org.glassfish.jersey.logging.LoggingFeature)1