/*
* Copyright (c) 2014 Jacob D. Parr
*
* This software may not be used without permission.
*/
package com.cosmicpush.app.resources.api;
import com.cosmicpush.app.domain.accounts.Account;
import com.cosmicpush.app.domain.clients.ApiClient;
import com.cosmicpush.app.domain.clients.config.GoogleTalkConfig;
import com.cosmicpush.app.domain.requests.*;
import com.cosmicpush.app.resources.api.jabber.JabberFactory;
import com.cosmicpush.app.system.CpServerObjectMapper;
import com.cosmicpush.pub.common.RequestStatus;
import com.cosmicpush.pub.push.ImPush;
import org.crazyyak.dev.common.StringUtils;
import org.crazyyak.dev.common.exceptions.ExceptionUtils;
public class GoolgeTalkDelegate extends AbstractDelegate {
private final Account account;
private final ApiClient apiClient;
private final ImPush action;
public GoolgeTalkDelegate(CpServerObjectMapper objectMapper, ApiRequestStore apiRequestStore, Account account, ApiClient apiClient, ApiRequest apiRequest, ImPush action) {
super(objectMapper, apiRequest, apiRequestStore);
this.action = ExceptionUtils.assertNotNull(action, "action");
this.account = ExceptionUtils.assertNotNull(account, "account");
this.apiClient = ExceptionUtils.assertNotNull(apiClient, "apiClient");
}
@Override
public synchronized RequestStatus processRequest() throws Exception {
String reasonNotPermitted = account.getReasonNotPermitted(action);
if (StringUtils.isNotBlank(reasonNotPermitted)) {
return apiRequest.denyRequest(reasonNotPermitted);
}
String apiMessage = null;
GoogleTalkConfig config = apiClient.getGoogleTalkConfig();
if (config == null) {
return apiRequest.failed("Google Talk configuration was not specified.");
}
JabberFactory factory = new JabberFactory(config.getUserName(), config.getPassword());
if (StringUtils.isNotBlank(config.getRecipientOverride())) {
// This is NOT a "production" request.
factory.sendTo(config.getRecipientOverride(), action.getMessage());
apiMessage = String.format("Request sent to recipient override, %s.", config.getRecipientOverride());
} else {
// This IS a "production" request.
factory.sendTo(action.getRecipient(), action.getMessage());
}
return apiRequest.processed(apiMessage);
}
}