net.navasoft.madcoin.backend.services.push.AndroidPushNotificationConfiguration.java Source code

Java tutorial

Introduction

Here is the source code for net.navasoft.madcoin.backend.services.push.AndroidPushNotificationConfiguration.java

Source

/*******************************************************************************
 * Copyright 2014 Juan Diego Navarre Gonzalez
 * 
 * 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 net.navasoft.madcoin.backend.services.push;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;

/**
 * net.navasoft.madcoin.backend.services.push Class class
 * AndroidPushNotificationConfiguration. Description:
 * 
 * @author Juan Diego Navarre Gonzalez - (<a
 *         href="mailto:jdnavarreg@outlook.com">{@literal jdnavarreg@outlook.com}
 *         </a>)
 * @version 1.0
 * @since 27/07/2014 06:48:42 PM
 */
public class AndroidPushNotificationConfiguration implements PushNotificationConfiguration {

    /**
     * username.
     * 
     * @since 27/07/2014, 06:48:42 PM
     */
    private String username;

    /**
     * password.
     * 
     * @since 27/07/2014, 06:48:42 PM
     */
    private String password;

    /**
     * token.
     * 
     * @since 27/07/2014, 06:48:42 PM
     */
    private String token;

    /**
     * service.
     * 
     * @since 27/07/2014, 06:48:42 PM
     */
    private HttpClient service;

    /**
     * Gets the password.
     * 
     * @return the password
     * @since 27/07/2014, 06:48:42 PM
     */
    @Override
    public String getPassword() {
        return password;
    }

    /**
     * Gets the username.
     * 
     * @return the username
     * @since 27/07/2014, 06:48:42 PM
     */
    public String getUsername() {
        return username;
    }

    /**
     * Sets the username.
     * 
     * @param username
     *            the new username
     * @since 27/07/2014, 06:48:42 PM
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * Creates the service.
     * 
     * @return the object
     * @throws PushNotificationException
     *             the push notification exception
     * @since 27/07/2014, 06:48:42 PM
     */
    @Override
    public Object createService() throws PushNotificationException {
        try {
            service = new HttpClient();
            PostMethod method = new PostMethod("https://www.google.com/accounts/ClientLogin");
            method.addParameter("Email", getUsername());
            method.addParameter("Passwd", getPassword());
            method.addParameter("accountType", "HOSTED_OR_GOOGLE");
            method.addParameter("source", "unit-test");
            method.addParameter("service", "ac2dm");
            service.executeMethod(method);
            byte[] responseBody = method.getResponseBody();
            String response = new String(responseBody);
            String auth = response.split("\n")[2];
            token = auth.split("=")[1];
            return service;
        } catch (HttpException e) {
            e.printStackTrace();
            throw new PushNotificationException("HTTP ", e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new PushNotificationException("IO", e);
        }

    }

    /**
     * Gets the token.
     * 
     * @return the token
     * @since 27/07/2014, 06:48:42 PM
     */
    public String getToken() {
        return token;
    }

}