org.insomne.blackbush.DisconnectServlet.java Source code

Java tutorial

Introduction

Here is the source code for org.insomne.blackbush.DisconnectServlet.java

Source

/*
 *
 * Copyright 2014 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.insomne.blackbush;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;

/**
 * @author Andres Leonardo Martinez Ortiz a.k.a almo
 *
 */
@SuppressWarnings("serial")
public class DisconnectServlet extends HttpServlet {

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

        GoogleClientSecrets clientSecrets = (GoogleClientSecrets) getServletContext().getAttribute("clientSecrets");
        String CLIENT_ID = clientSecrets.getWeb().getClientId();
        String CLIENT_SECRET = clientSecrets.getWeb().getClientSecret();

        HttpTransport TRANSPORT = (HttpTransport) getServletContext().getAttribute("TRANSPORT");
        JacksonFactory JSON_FACTORY = (JacksonFactory) getServletContext().getAttribute("JSON_FACTORY");

        String tokenData = (String) req.getSession().getAttribute("token");
        if (tokenData == null) {
            resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            resp.getWriter().print("Current user not connected.");
            return;
        }

        try {
            GoogleCredential credential = new GoogleCredential.Builder().setJsonFactory(JSON_FACTORY)
                    .setTransport(TRANSPORT).setClientSecrets(CLIENT_ID, CLIENT_SECRET).build()
                    .setFromTokenResponse(JSON_FACTORY.fromString(tokenData, GoogleTokenResponse.class));

            GenericUrl genericURL = new GenericUrl(String
                    .format("https://accounts.google.com/o/oauth2/revoke?token=%s", credential.getAccessToken()));
            HttpRequest revokeResponse = TRANSPORT.createRequestFactory().buildDeleteRequest(genericURL);
            revokeResponse.execute();

            req.getSession().removeAttribute("token");
            resp.setStatus(HttpServletResponse.SC_OK);

        } catch (IOException e) {
            resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }
    }
}