Package com.cosmicpush.app.resources.manage.client

Source Code of com.cosmicpush.app.resources.manage.client.ManageGoogleTalkApi

/*
* Copyright (c) 2014 Jacob D. Parr
*
* This software may not be used without permission.
*/

package com.cosmicpush.app.resources.manage.client;

import com.cosmicpush.app.domain.accounts.Account;
import com.cosmicpush.app.domain.clients.ApiClient;
import com.cosmicpush.app.domain.clients.actions.CreateGoogleTalkConfigAction;
import com.cosmicpush.app.domain.requests.ApiRequest;
import com.cosmicpush.app.resources.api.GoolgeTalkDelegate;
import com.cosmicpush.app.resources.manage.UserRequestConfig;
import com.cosmicpush.pub.push.ImPush;
import java.net.*;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import org.crazyyak.dev.common.*;
import org.crazyyak.dev.common.exceptions.ExceptionUtils;

public class ManageGoogleTalkApi {

  private final Account account;
  private final ApiClient apiClient;
  private final UserRequestConfig config;

  public ManageGoogleTalkApi(UserRequestConfig config, Account account, ApiClient apiClient) {
    this.config = ExceptionUtils.assertNotNull(config, "config");
    this.account = ExceptionUtils.assertNotNull(account, "account");
    this.apiClient = ExceptionUtils.assertNotNull(apiClient, "apiClient");
  }

  public Response redirect() throws Exception {
    String path = String.format("manage/api-client/%s", apiClient.getClientName());
    return Response.seeOther(new URI(path)).build();
  }

  @POST
  public Response updateGoogleTalkConfig(@FormParam("userName") String userName,
                                         @FormParam("password") String password,
                                         @FormParam("testAddress") String testAddress,
                                         @FormParam("recipientOverride") String recipientOverride) throws Exception {

    CreateGoogleTalkConfigAction action = new CreateGoogleTalkConfigAction(userName, password, testAddress, recipientOverride);

    apiClient.createGoogleTalkConfig(action);
    config.getAccountStore().update(account);

    return redirect();
  }

  @POST
  @Path("/delete")
  public Response deleteGoogleTalkConfig() throws Exception {

    apiClient.deleteGoogleTalkConfig();
    config.getAccountStore().update(account);

    return redirect();
  }

  @POST
  @Path("/test")
  public Response testGoogleTalkConfig() throws Exception {
    String recipient = apiClient.getGoogleTalkConfig().getTestAddress();

    if (StringUtils.isBlank((recipient))) {
      apiClient.setLastMessage("Test message cannot be set with out the Account's \"Google Talk Test\" setting.");
      config.getAccountStore().update(account);
      return redirect();
    }

    String override = apiClient.getGoogleTalkConfig().getRecipientOverride();
    if (StringUtils.isNotBlank(override)) {
      recipient = override;
    }

    String when = Formats.defaultStamp(new java.util.Date());
    String msg = String.format("This is a test message from Cosmic Push sent at %s.", when);
    ImPush action = ImPush.googleTalk(recipient, msg, null);

    String remoteAddress = config.getRequest().getRemoteAddr();
    InetAddress inetAddress = InetAddress.getByName(remoteAddress);

    ApiRequest apiRequest = new ApiRequest(apiClient, action, inetAddress);
    config.getApiRequestStore().create(apiRequest);

    new GoolgeTalkDelegate(config.getObjectMapper(), config.getApiRequestStore(), account, apiClient, apiRequest, action).run();

    msg = String.format("Test message sent to %s:\n%s", recipient, msg);
    apiClient.setLastMessage(msg);
    config.getAccountStore().update(account);

    return redirect();
  }
}
TOP

Related Classes of com.cosmicpush.app.resources.manage.client.ManageGoogleTalkApi

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.