Java tutorial
/* * MIT License * * Copyright (c) 2016 Nhan Nguyen * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package imbrobits.loosh.handler; import com.google.inject.Inject; import imbrobits.loosh.core.Config; import imbrobits.loosh.models.Project; import io.vertx.core.Handler; import io.vertx.core.Vertx; import io.vertx.core.http.HttpMethod; import io.vertx.core.http.HttpServerResponse; import io.vertx.core.json.JsonObject; import io.vertx.core.logging.Logger; import io.vertx.core.logging.LoggerFactory; import io.vertx.ext.auth.oauth2.AccessToken; import io.vertx.ext.auth.oauth2.OAuth2Auth; import io.vertx.ext.auth.oauth2.OAuth2ClientOptions; import io.vertx.ext.auth.oauth2.OAuth2FlowType; import io.vertx.ext.web.RoutingContext; import rx.functions.Action1; import java.util.ArrayList; import java.util.List; public final class TodoistApiService implements OAuth2Handler, TaskService { private static final Logger LOGGER = LoggerFactory.getLogger(TodoistApiService.class); private final OAuth2Auth auth; private AccessToken accessToken; private final JsonObject appConfig; @Inject public TodoistApiService(Vertx vertx) { appConfig = vertx.getOrCreateContext().config(); auth = OAuth2Auth.create(vertx, OAuth2FlowType.AUTH_CODE, new OAuth2ClientOptions().setClientID(appConfig.getString(Config.TODOIST_CLIENT_ID)) .setClientSecret(appConfig.getString(Config.TODOIST_CLIENT_SECRET)) .setSite("https://todoist.com").setTokenPath("/oauth/access_token") .setAuthorizationPath("/oauth/authorize")); } @Override public Handler<RoutingContext> authenticate() { // This handler will be called for every request return routingContext -> { String authorizeUrl = auth .authorizeURL(new JsonObject().put("redirect_uri", "http://localhost:8080/callback/todoist") .put("scope", "data:read_write,data:delete").put("state", "3(#0/!~")); HttpServerResponse response = routingContext.request().response(); response.putHeader("Location", authorizeUrl).setStatusCode(302).end(); }; } @Override public Handler<RoutingContext> callback() { return routingContext -> { String code = routingContext.request().getParam("code"); auth.getToken(new JsonObject().put("client_id", appConfig.getString(Config.TODOIST_CLIENT_ID)) .put("client_secret", appConfig.getString(Config.TODOIST_CLIENT_SECRET)) .put("redirect_uri", "http://localhost:8080").put("code", code), res -> { if (res.failed()) { LOGGER.error("[OAuth2] Fail to get token:" + res.cause().getMessage()); } else { accessToken = res.result(); getProjects(null); } }); routingContext.response().write("Hello").end(); }; } @Override public String callbackUri() { return "/callback/todoist"; } @Override public String authenticationUri() { return "/oauth/todoist"; } @Override public void getProjects(Action1<List<Project>> callback) { auth.api(HttpMethod.POST, "/API/v7/sync", new JsonObject().put("token", accessToken.principal().getString("access_token")) .put("sync_token", "*").put("resource_types", "[\"all\"]"), event -> { event.result(); // TODO mapping result to List<Project> callback.call(new ArrayList<Project>()); }); } }