org.insomne.blackbush.BlackBushContextListener.java Source code

Java tutorial

Introduction

Here is the source code for org.insomne.blackbush.BlackBushContextListener.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 java.io.InputStreamReader;
import java.io.Reader;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.gson.Gson;

/**
 * @author almo
 *
 */
public class BlackBushContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        ServletContext context = event.getServletContext();

        try {
            context.setAttribute("TRANSPORT", new NetHttpTransport());
            context.setAttribute("GSON", new Gson());

            JacksonFactory JSON_FACTORY = new JacksonFactory();
            context.setAttribute("JSON_FACTORY", JSON_FACTORY);

            Reader reader = new InputStreamReader(context.getResourceAsStream("/client_secret.json"));
            context.setAttribute("clientSecrets", GoogleClientSecrets.load(JSON_FACTORY, reader));

        } catch (IOException e) {
            throw new Error("No client_secrets.json found", e);
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {

    }

}