Java tutorial
/** * Copyright (C) 2016-2016 52North Initiative for Geospatial Open Source * Software GmbH * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License version 2 as published by the Free * Software Foundation. * * If the program is linked with libraries which are licensed under one of the * following licenses, the combination of the program with the linked library is * not considered a "derivative work" of the program: * * - Apache License, version 2.0 * - Apache Software License, version 1.0 * - GNU Lesser General Public License, version 3 * - Mozilla Public License, versions 1.0, 1.1 and 2.0 * - Common Development and Distribution License (CDDL), version 1.0 * * Therefore the distribution of the program linked with libraries licensed under * the aforementioned licenses, is permitted by the copyright holders if the * distribution is compliant with both the GNU General Public License version 2 * and the aforementioned licenses. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * PARTICULAR PURPOSE. See the GNU General Public License for more details. */ package org.n52.v3d.terraintools.auth; import com.google.api.client.auth.oauth2.TokenResponseException; import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken; import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import static org.n52.v3d.terraintools.auth.Signin.CLIENT_ID; import static org.n52.v3d.terraintools.auth.Signin.CLIENT_SECRET; import static org.n52.v3d.terraintools.auth.Signin.GSON; import static org.n52.v3d.terraintools.auth.Signin.JSON_FACTORY; import static org.n52.v3d.terraintools.auth.Signin.TRANSPORT; import org.n52.v3d.terraintools.drive.DriveSample; /** * Upgrade given auth code to token, and store it in the session. POST body of * request should be the authorization code. Example URI: * /connect?state=...&gplus_id=... */ public class ConnectServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/json"); // Only connect a user that is not already connected. String tokenData = (String) request.getSession().getAttribute("token"); if (tokenData != null) { response.setStatus(HttpServletResponse.SC_OK); System.out.println("Current user is already connected"); response.getWriter().print(GSON.toJson("Current user is already connected.")); DriveSample.init(tokenData); return; } // Normally the state would be a one-time use token, however in our // simple case, we want a user to be able to connect and disconnect // without reloading the page. Thus, for demonstration, we don't // implement this best practice. //request.getSession().removeAttribute("state"); ByteArrayOutputStream resultStream = new ByteArrayOutputStream(); getContent(request.getInputStream(), resultStream); String code = new String(resultStream.toByteArray(), "UTF-8"); try { // Upgrade the authorization code into an access and refresh token. GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(TRANSPORT, JSON_FACTORY, CLIENT_ID, CLIENT_SECRET, code, "postmessage").execute(); // You can read the Google user ID in the ID token. // This sample does not use the user ID. GoogleIdToken idToken = tokenResponse.parseIdToken(); String gplusId = idToken.getPayload().getSubject(); // Store the token in the session for later use. request.getSession().setAttribute("token", tokenResponse.toString()); response.setStatus(HttpServletResponse.SC_OK); response.getWriter().print(GSON.toJson("Successfully connected user.")); tokenData = (String) request.getSession().getAttribute("token"); DriveSample.init(tokenData); } catch (TokenResponseException e) { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.getWriter().print(GSON.toJson("Failed to upgrade the authorization code.")); } catch (IOException e) { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.getWriter().print(GSON.toJson("Failed to read token data from Google. " + e.getMessage())); } } /* * Read the content of an InputStream. * * @param inputStream the InputStream to be read. * @return the content of the InputStream as a ByteArrayOutputStream. * @throws IOException */ static void getContent(InputStream inputStream, ByteArrayOutputStream outputStream) throws IOException { // Read the response into a buffered stream BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); int readChar; while ((readChar = reader.read()) != -1) { outputStream.write(readChar); } reader.close(); } }